2 * Reverb for the OpenAL cross platform audio library
3 * Copyright (C) 2008-2009 by Christopher Fitzgerald.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
29 #include "alAuxEffectSlot.h"
35 /* This is the maximum number of samples processed for each inner loop
37 #define MAX_UPDATE_SAMPLES 256
39 typedef struct DelayLine
41 // The delay lines use sample lengths that are powers of 2 to allow the
42 // use of bit-masking instead of a modulus for wrapping.
47 typedef struct ALreverbState
{
48 DERIVE_FROM_TYPE(ALeffectState
);
53 ALfloat (*ExtraOut
)[BUFFERSIZE
];
56 // All delay lines are allocated as a single buffer to reduce memory
57 // fragmentation and management code.
58 ALfloat
*SampleBuffer
;
61 // Master effect filters
62 ALfilterState LpFilter
;
63 ALfilterState HpFilter
; // EAX only
66 // Modulator delay line.
69 // The vibrato time is tracked with an index over a modulus-wrapped
70 // range (in samples).
74 // The depth of frequency change (also in samples) and its filter.
80 // Initial effect delay.
82 // The tap points for the initial delay. First tap goes to early
83 // reflections, the last to late reverb.
87 // Early reflections are done with 4 delay lines.
92 // The gain for each output channel based on 3D panning.
93 // NOTE: With certain output modes, we may be rendering to the dry
94 // buffer and the "real" buffer. The two combined may be using more
95 // than the max output channels, so we need some extra for the real
97 ALfloat PanGain
[4][MAX_OUTPUT_CHANNELS
*2];
100 // Decorrelator delay line.
101 DelayLine Decorrelator
;
102 // There are actually 4 decorrelator taps, but the first occurs at the
107 // Output gain for late reverb.
110 // Attenuation to compensate for the modal density and decay rate of
114 // The feed-back and feed-forward all-pass coefficient.
117 // Mixing matrix coefficient.
120 // Late reverb has 4 parallel all-pass filters.
122 DelayLine ApDelay
[4];
125 // In addition to 4 cyclical delay lines.
130 // The cyclical delay lines are 1-pole low-pass filtered.
134 // The gain for each output channel based on 3D panning.
135 // NOTE: Add some extra in case (see note about early pan).
136 ALfloat PanGain
[4][MAX_OUTPUT_CHANNELS
*2];
140 // Attenuation to compensate for the modal density and decay rate of
144 // Echo delay and all-pass lines.
155 // The echo line is 1-pole low-pass filtered.
159 // Echo mixing coefficient.
163 // The current read offset for all delay lines.
166 /* Temporary storage used when processing. */
167 ALfloat ReverbSamples
[MAX_UPDATE_SAMPLES
][4];
168 ALfloat EarlySamples
[MAX_UPDATE_SAMPLES
][4];
171 static ALvoid
ALreverbState_Destruct(ALreverbState
*State
)
173 al_free(State
->SampleBuffer
);
174 State
->SampleBuffer
= NULL
;
175 ALeffectState_Destruct(STATIC_CAST(ALeffectState
,State
));
178 static ALboolean
ALreverbState_deviceUpdate(ALreverbState
*State
, ALCdevice
*Device
);
179 static ALvoid
ALreverbState_update(ALreverbState
*State
, const ALCdevice
*Device
, const ALeffectslot
*Slot
, const ALeffectProps
*props
);
180 static ALvoid
ALreverbState_processStandard(ALreverbState
*State
, ALuint SamplesToDo
, const ALfloat
*restrict SamplesIn
, ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
);
181 static ALvoid
ALreverbState_processEax(ALreverbState
*State
, ALuint SamplesToDo
, const ALfloat
*restrict SamplesIn
, ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
);
182 static ALvoid
ALreverbState_process(ALreverbState
*State
, ALuint SamplesToDo
, const ALfloat (*restrict SamplesIn
)[BUFFERSIZE
], ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
);
183 DECLARE_DEFAULT_ALLOCATORS(ALreverbState
)
185 DEFINE_ALEFFECTSTATE_VTABLE(ALreverbState
);
187 /* This is a user config option for modifying the overall output of the reverb
190 ALfloat ReverbBoost
= 1.0f
;
192 /* Specifies whether to use a standard reverb effect in place of EAX reverb (no
193 * high-pass, modulation, or echo).
195 ALboolean EmulateEAXReverb
= AL_FALSE
;
197 /* This coefficient is used to define the maximum frequency range controlled
198 * by the modulation depth. The current value of 0.1 will allow it to swing
199 * from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the
200 * sampler to stall on the downswing, and above 1 it will cause it to sample
203 static const ALfloat MODULATION_DEPTH_COEFF
= 0.1f
;
205 /* A filter is used to avoid the terrible distortion caused by changing
206 * modulation time and/or depth. To be consistent across different sample
207 * rates, the coefficient must be raised to a constant divided by the sample
208 * rate: coeff^(constant / rate).
210 static const ALfloat MODULATION_FILTER_COEFF
= 0.048f
;
211 static const ALfloat MODULATION_FILTER_CONST
= 100000.0f
;
213 // When diffusion is above 0, an all-pass filter is used to take the edge off
214 // the echo effect. It uses the following line length (in seconds).
215 static const ALfloat ECHO_ALLPASS_LENGTH
= 0.0133f
;
217 // Input into the late reverb is decorrelated between four channels. Their
218 // timings are dependent on a fraction and multiplier. See the
219 // UpdateDecorrelator() routine for the calculations involved.
220 static const ALfloat DECO_FRACTION
= 0.15f
;
221 static const ALfloat DECO_MULTIPLIER
= 2.0f
;
223 // All delay line lengths are specified in seconds.
225 // The lengths of the early delay lines.
226 static const ALfloat EARLY_LINE_LENGTH
[4] =
228 0.0015f
, 0.0045f
, 0.0135f
, 0.0405f
231 // The lengths of the late all-pass delay lines.
232 static const ALfloat ALLPASS_LINE_LENGTH
[4] =
234 0.0151f
, 0.0167f
, 0.0183f
, 0.0200f
,
237 // The lengths of the late cyclical delay lines.
238 static const ALfloat LATE_LINE_LENGTH
[4] =
240 0.0211f
, 0.0311f
, 0.0461f
, 0.0680f
243 // The late cyclical delay lines have a variable length dependent on the
244 // effect's density parameter (inverted for some reason) and this multiplier.
245 static const ALfloat LATE_LINE_MULTIPLIER
= 4.0f
;
248 #if defined(_WIN32) && !defined (_M_X64) && !defined(_M_ARM)
249 /* HACK: Workaround for a modff bug in 32-bit Windows, which attempts to write
250 * a 64-bit double to the 32-bit float parameter.
252 static inline float hack_modff(float x
, float *y
)
255 double df
= modf((double)x
, &di
);
259 #define modff hack_modff
263 /**************************************
265 **************************************/
267 // Given the allocated sample buffer, this function updates each delay line
269 static inline ALvoid
RealizeLineOffset(ALfloat
*sampleBuffer
, DelayLine
*Delay
)
271 Delay
->Line
= &sampleBuffer
[(ptrdiff_t)Delay
->Line
];
274 // Calculate the length of a delay line and store its mask and offset.
275 static ALuint
CalcLineLength(ALfloat length
, ptrdiff_t offset
, ALuint frequency
, ALuint extra
, DelayLine
*Delay
)
279 // All line lengths are powers of 2, calculated from their lengths, with
280 // an additional sample in case of rounding errors.
281 samples
= fastf2u(length
*frequency
) + extra
;
282 samples
= NextPowerOf2(samples
+ 1);
283 // All lines share a single sample buffer.
284 Delay
->Mask
= samples
- 1;
285 Delay
->Line
= (ALfloat
*)offset
;
286 // Return the sample count for accumulation.
290 /* Calculates the delay line metrics and allocates the shared sample buffer
291 * for all lines given the sample rate (frequency). If an allocation failure
292 * occurs, it returns AL_FALSE.
294 static ALboolean
AllocLines(ALuint frequency
, ALreverbState
*State
)
296 ALuint totalSamples
, index
;
299 // All delay line lengths are calculated to accomodate the full range of
300 // lengths given their respective paramters.
303 /* The modulator's line length is calculated from the maximum modulation
304 * time and depth coefficient, and halfed for the low-to-high frequency
305 * swing. An additional sample is added to keep it stable when there is no
308 length
= (AL_EAXREVERB_MAX_MODULATION_TIME
*MODULATION_DEPTH_COEFF
/2.0f
);
309 totalSamples
+= CalcLineLength(length
, totalSamples
, frequency
, 1,
312 // The initial delay is the sum of the reflections and late reverb
313 // delays. This must include space for storing a loop update to feed the
314 // early reflections, decorrelator, and echo.
315 length
= AL_EAXREVERB_MAX_REFLECTIONS_DELAY
+
316 AL_EAXREVERB_MAX_LATE_REVERB_DELAY
;
317 totalSamples
+= CalcLineLength(length
, totalSamples
, frequency
,
318 MAX_UPDATE_SAMPLES
, &State
->Delay
);
320 // The early reflection lines.
321 for(index
= 0;index
< 4;index
++)
322 totalSamples
+= CalcLineLength(EARLY_LINE_LENGTH
[index
], totalSamples
,
323 frequency
, 0, &State
->Early
.Delay
[index
]);
325 // The decorrelator line is calculated from the lowest reverb density (a
326 // parameter value of 1). This must include space for storing a loop update
327 // to feed the late reverb.
328 length
= (DECO_FRACTION
* DECO_MULTIPLIER
* DECO_MULTIPLIER
) *
329 LATE_LINE_LENGTH
[0] * (1.0f
+ LATE_LINE_MULTIPLIER
);
330 totalSamples
+= CalcLineLength(length
, totalSamples
, frequency
, MAX_UPDATE_SAMPLES
,
331 &State
->Decorrelator
);
333 // The late all-pass lines.
334 for(index
= 0;index
< 4;index
++)
335 totalSamples
+= CalcLineLength(ALLPASS_LINE_LENGTH
[index
], totalSamples
,
336 frequency
, 0, &State
->Late
.ApDelay
[index
]);
338 // The late delay lines are calculated from the lowest reverb density.
339 for(index
= 0;index
< 4;index
++)
341 length
= LATE_LINE_LENGTH
[index
] * (1.0f
+ LATE_LINE_MULTIPLIER
);
342 totalSamples
+= CalcLineLength(length
, totalSamples
, frequency
, 0,
343 &State
->Late
.Delay
[index
]);
346 // The echo all-pass and delay lines.
347 totalSamples
+= CalcLineLength(ECHO_ALLPASS_LENGTH
, totalSamples
,
348 frequency
, 0, &State
->Echo
.ApDelay
);
349 totalSamples
+= CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME
, totalSamples
,
350 frequency
, 0, &State
->Echo
.Delay
);
352 if(totalSamples
!= State
->TotalSamples
)
356 TRACE("New reverb buffer length: %u samples (%f sec)\n", totalSamples
, totalSamples
/(float)frequency
);
357 newBuffer
= al_calloc(16, sizeof(ALfloat
) * totalSamples
);
358 if(!newBuffer
) return AL_FALSE
;
360 al_free(State
->SampleBuffer
);
361 State
->SampleBuffer
= newBuffer
;
362 State
->TotalSamples
= totalSamples
;
365 // Update all delays to reflect the new sample buffer.
366 RealizeLineOffset(State
->SampleBuffer
, &State
->Delay
);
367 RealizeLineOffset(State
->SampleBuffer
, &State
->Decorrelator
);
368 for(index
= 0;index
< 4;index
++)
370 RealizeLineOffset(State
->SampleBuffer
, &State
->Early
.Delay
[index
]);
371 RealizeLineOffset(State
->SampleBuffer
, &State
->Late
.ApDelay
[index
]);
372 RealizeLineOffset(State
->SampleBuffer
, &State
->Late
.Delay
[index
]);
374 RealizeLineOffset(State
->SampleBuffer
, &State
->Mod
.Delay
);
375 RealizeLineOffset(State
->SampleBuffer
, &State
->Echo
.ApDelay
);
376 RealizeLineOffset(State
->SampleBuffer
, &State
->Echo
.Delay
);
378 // Clear the sample buffer.
379 for(index
= 0;index
< State
->TotalSamples
;index
++)
380 State
->SampleBuffer
[index
] = 0.0f
;
385 static ALboolean
ALreverbState_deviceUpdate(ALreverbState
*State
, ALCdevice
*Device
)
387 ALuint frequency
= Device
->Frequency
, index
;
389 // Allocate the delay lines.
390 if(!AllocLines(frequency
, State
))
393 /* HRTF and UHJ will mix to the real output for ambient output. */
394 if(Device
->Hrtf
|| Device
->Uhj_Encoder
)
396 State
->ExtraOut
= Device
->RealOut
.Buffer
;
397 State
->ExtraChannels
= Device
->RealOut
.NumChannels
;
401 State
->ExtraOut
= NULL
;
402 State
->ExtraChannels
= 0;
405 // Calculate the modulation filter coefficient. Notice that the exponent
406 // is calculated given the current sample rate. This ensures that the
407 // resulting filter response over time is consistent across all sample
409 State
->Mod
.Coeff
= powf(MODULATION_FILTER_COEFF
,
410 MODULATION_FILTER_CONST
/ frequency
);
412 // The early reflection and late all-pass filter line lengths are static,
413 // so their offsets only need to be calculated once.
414 for(index
= 0;index
< 4;index
++)
416 State
->Early
.Offset
[index
] = fastf2u(EARLY_LINE_LENGTH
[index
] * frequency
);
417 State
->Late
.ApOffset
[index
] = fastf2u(ALLPASS_LINE_LENGTH
[index
] * frequency
);
420 // The echo all-pass filter line length is static, so its offset only
421 // needs to be calculated once.
422 State
->Echo
.ApOffset
= fastf2u(ECHO_ALLPASS_LENGTH
* frequency
);
427 /**************************************
429 **************************************/
431 // Calculate a decay coefficient given the length of each cycle and the time
432 // until the decay reaches -60 dB.
433 static inline ALfloat
CalcDecayCoeff(ALfloat length
, ALfloat decayTime
)
435 return powf(0.001f
/*-60 dB*/, length
/decayTime
);
438 // Calculate a decay length from a coefficient and the time until the decay
440 static inline ALfloat
CalcDecayLength(ALfloat coeff
, ALfloat decayTime
)
442 return log10f(coeff
) * decayTime
/ log10f(0.001f
)/*-60 dB*/;
445 // Calculate an attenuation to be applied to the input of any echo models to
446 // compensate for modal density and decay time.
447 static inline ALfloat
CalcDensityGain(ALfloat a
)
449 /* The energy of a signal can be obtained by finding the area under the
450 * squared signal. This takes the form of Sum(x_n^2), where x is the
451 * amplitude for the sample n.
453 * Decaying feedback matches exponential decay of the form Sum(a^n),
454 * where a is the attenuation coefficient, and n is the sample. The area
455 * under this decay curve can be calculated as: 1 / (1 - a).
457 * Modifying the above equation to find the squared area under the curve
458 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
459 * calculated by inverting the square root of this approximation,
460 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
462 return sqrtf(1.0f
- (a
* a
));
465 // Calculate the mixing matrix coefficients given a diffusion factor.
466 static inline ALvoid
CalcMatrixCoeffs(ALfloat diffusion
, ALfloat
*x
, ALfloat
*y
)
470 // The matrix is of order 4, so n is sqrt (4 - 1).
472 t
= diffusion
* atanf(n
);
474 // Calculate the first mixing matrix coefficient.
476 // Calculate the second mixing matrix coefficient.
480 // Calculate the limited HF ratio for use with the late reverb low-pass
482 static ALfloat
CalcLimitedHfRatio(ALfloat hfRatio
, ALfloat airAbsorptionGainHF
, ALfloat decayTime
)
486 /* Find the attenuation due to air absorption in dB (converting delay
487 * time to meters using the speed of sound). Then reversing the decay
488 * equation, solve for HF ratio. The delay length is cancelled out of
489 * the equation, so it can be calculated once for all lines.
491 limitRatio
= 1.0f
/ (CalcDecayLength(airAbsorptionGainHF
, decayTime
) *
492 SPEEDOFSOUNDMETRESPERSEC
);
493 /* Using the limit calculated above, apply the upper bound to the HF
494 * ratio. Also need to limit the result to a minimum of 0.1, just like the
495 * HF ratio parameter. */
496 return clampf(limitRatio
, 0.1f
, hfRatio
);
499 // Calculate the coefficient for a HF (and eventually LF) decay damping
501 static inline ALfloat
CalcDampingCoeff(ALfloat hfRatio
, ALfloat length
, ALfloat decayTime
, ALfloat decayCoeff
, ALfloat cw
)
505 // Eventually this should boost the high frequencies when the ratio
510 // Calculate the low-pass coefficient by dividing the HF decay
511 // coefficient by the full decay coefficient.
512 g
= CalcDecayCoeff(length
, decayTime
* hfRatio
) / decayCoeff
;
514 // Damping is done with a 1-pole filter, so g needs to be squared.
516 if(g
< 0.9999f
) /* 1-epsilon */
518 /* Be careful with gains < 0.001, as that causes the coefficient
519 * head towards 1, which will flatten the signal. */
521 coeff
= (1 - g
*cw
- sqrtf(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) /
525 // Very low decay times will produce minimal output, so apply an
526 // upper bound to the coefficient.
527 coeff
= minf(coeff
, 0.98f
);
532 // Update the EAX modulation index, range, and depth. Keep in mind that this
533 // kind of vibrato is additive and not multiplicative as one may expect. The
534 // downswing will sound stronger than the upswing.
535 static ALvoid
UpdateModulator(ALfloat modTime
, ALfloat modDepth
, ALuint frequency
, ALreverbState
*State
)
539 /* Modulation is calculated in two parts.
541 * The modulation time effects the sinus applied to the change in
542 * frequency. An index out of the current time range (both in samples)
543 * is incremented each sample. The range is bound to a reasonable
544 * minimum (1 sample) and when the timing changes, the index is rescaled
545 * to the new range (to keep the sinus consistent).
547 range
= maxu(fastf2u(modTime
*frequency
), 1);
548 State
->Mod
.Index
= (ALuint
)(State
->Mod
.Index
* (ALuint64
)range
/
550 State
->Mod
.Range
= range
;
552 /* The modulation depth effects the amount of frequency change over the
553 * range of the sinus. It needs to be scaled by the modulation time so
554 * that a given depth produces a consistent change in frequency over all
555 * ranges of time. Since the depth is applied to a sinus value, it needs
556 * to be halfed once for the sinus range and again for the sinus swing
557 * in time (half of it is spent decreasing the frequency, half is spent
560 State
->Mod
.Depth
= modDepth
* MODULATION_DEPTH_COEFF
* modTime
/ 2.0f
/
564 // Update the offsets for the initial effect delay line.
565 static ALvoid
UpdateDelayLine(ALfloat earlyDelay
, ALfloat lateDelay
, ALuint frequency
, ALreverbState
*State
)
567 // Calculate the initial delay taps.
568 State
->DelayTap
[0] = fastf2u(earlyDelay
* frequency
);
569 State
->DelayTap
[1] = fastf2u((earlyDelay
+ lateDelay
) * frequency
);
572 // Update the early reflections mix and line coefficients.
573 static ALvoid
UpdateEarlyLines(ALfloat lateDelay
, ALreverbState
*State
)
577 // Calculate the gain (coefficient) for each early delay line using the
578 // late delay time. This expands the early reflections to the start of
580 for(index
= 0;index
< 4;index
++)
581 State
->Early
.Coeff
[index
] = CalcDecayCoeff(EARLY_LINE_LENGTH
[index
],
585 // Update the offsets for the decorrelator line.
586 static ALvoid
UpdateDecorrelator(ALfloat density
, ALuint frequency
, ALreverbState
*State
)
591 /* The late reverb inputs are decorrelated to smooth the reverb tail and
592 * reduce harsh echos. The first tap occurs immediately, while the
593 * remaining taps are delayed by multiples of a fraction of the smallest
594 * cyclical delay time.
596 * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
598 for(index
= 0;index
< 3;index
++)
600 length
= (DECO_FRACTION
* powf(DECO_MULTIPLIER
, (ALfloat
)index
)) *
601 LATE_LINE_LENGTH
[0] * (1.0f
+ (density
* LATE_LINE_MULTIPLIER
));
602 State
->DecoTap
[index
] = fastf2u(length
* frequency
);
606 // Update the late reverb mix, line lengths, and line coefficients.
607 static ALvoid
UpdateLateLines(ALfloat xMix
, ALfloat density
, ALfloat decayTime
, ALfloat diffusion
, ALfloat echoDepth
, ALfloat hfRatio
, ALfloat cw
, ALuint frequency
, ALreverbState
*State
)
612 /* Calculate the late reverb gain. Since the output is tapped prior to the
613 * application of the next delay line coefficients, this gain needs to be
614 * attenuated by the 'x' mixing matrix coefficient as well. Also attenuate
615 * the late reverb when echo depth is high and diffusion is low, so the
616 * echo is slightly stronger than the decorrelated echos in the reverb
619 State
->Late
.Gain
= xMix
* (1.0f
- (echoDepth
*0.5f
*(1.0f
- diffusion
)));
621 /* To compensate for changes in modal density and decay time of the late
622 * reverb signal, the input is attenuated based on the maximal energy of
623 * the outgoing signal. This approximation is used to keep the apparent
624 * energy of the signal equal for all ranges of density and decay time.
626 * The average length of the cyclcical delay lines is used to calculate
627 * the attenuation coefficient.
629 length
= (LATE_LINE_LENGTH
[0] + LATE_LINE_LENGTH
[1] +
630 LATE_LINE_LENGTH
[2] + LATE_LINE_LENGTH
[3]) / 4.0f
;
631 length
*= 1.0f
+ (density
* LATE_LINE_MULTIPLIER
);
632 State
->Late
.DensityGain
= CalcDensityGain(
633 CalcDecayCoeff(length
, decayTime
)
636 // Calculate the all-pass feed-back and feed-forward coefficient.
637 State
->Late
.ApFeedCoeff
= 0.5f
* powf(diffusion
, 2.0f
);
639 for(index
= 0;index
< 4;index
++)
641 // Calculate the gain (coefficient) for each all-pass line.
642 State
->Late
.ApCoeff
[index
] = CalcDecayCoeff(
643 ALLPASS_LINE_LENGTH
[index
], decayTime
646 // Calculate the length (in seconds) of each cyclical delay line.
647 length
= LATE_LINE_LENGTH
[index
] *
648 (1.0f
+ (density
* LATE_LINE_MULTIPLIER
));
650 // Calculate the delay offset for each cyclical delay line.
651 State
->Late
.Offset
[index
] = fastf2u(length
* frequency
);
653 // Calculate the gain (coefficient) for each cyclical line.
654 State
->Late
.Coeff
[index
] = CalcDecayCoeff(length
, decayTime
);
656 // Calculate the damping coefficient for each low-pass filter.
657 State
->Late
.LpCoeff
[index
] = CalcDampingCoeff(
658 hfRatio
, length
, decayTime
, State
->Late
.Coeff
[index
], cw
661 // Attenuate the cyclical line coefficients by the mixing coefficient
663 State
->Late
.Coeff
[index
] *= xMix
;
667 // Update the echo gain, line offset, line coefficients, and mixing
669 static ALvoid
UpdateEchoLine(ALfloat echoTime
, ALfloat decayTime
, ALfloat diffusion
, ALfloat echoDepth
, ALfloat hfRatio
, ALfloat cw
, ALuint frequency
, ALreverbState
*State
)
671 // Update the offset and coefficient for the echo delay line.
672 State
->Echo
.Offset
= fastf2u(echoTime
* frequency
);
674 // Calculate the decay coefficient for the echo line.
675 State
->Echo
.Coeff
= CalcDecayCoeff(echoTime
, decayTime
);
677 // Calculate the energy-based attenuation coefficient for the echo delay
679 State
->Echo
.DensityGain
= CalcDensityGain(State
->Echo
.Coeff
);
681 // Calculate the echo all-pass feed coefficient.
682 State
->Echo
.ApFeedCoeff
= 0.5f
* powf(diffusion
, 2.0f
);
684 // Calculate the echo all-pass attenuation coefficient.
685 State
->Echo
.ApCoeff
= CalcDecayCoeff(ECHO_ALLPASS_LENGTH
, decayTime
);
687 // Calculate the damping coefficient for each low-pass filter.
688 State
->Echo
.LpCoeff
= CalcDampingCoeff(hfRatio
, echoTime
, decayTime
,
689 State
->Echo
.Coeff
, cw
);
691 /* Calculate the echo mixing coefficient. This is applied to the output mix
692 * only, not the feedback.
694 State
->Echo
.MixCoeff
= echoDepth
;
697 // Update the early and late 3D panning gains.
698 static ALvoid
UpdateMixedPanning(const ALCdevice
*Device
, const ALfloat
*ReflectionsPan
, const ALfloat
*LateReverbPan
, ALfloat Gain
, ALfloat EarlyGain
, ALfloat LateGain
, ALreverbState
*State
)
700 ALfloat DirGains
[MAX_OUTPUT_CHANNELS
];
701 ALfloat coeffs
[MAX_AMBI_COEFFS
];
705 /* With HRTF or UHJ, the normal output provides a panned reverb channel
706 * when a non-0-length vector is specified, while the real stereo output
707 * provides two other "direct" non-panned reverb channels.
709 memset(State
->Early
.PanGain
, 0, sizeof(State
->Early
.PanGain
));
710 length
= sqrtf(ReflectionsPan
[0]*ReflectionsPan
[0] + ReflectionsPan
[1]*ReflectionsPan
[1] + ReflectionsPan
[2]*ReflectionsPan
[2]);
711 if(!(length
> FLT_EPSILON
))
713 for(i
= 0;i
< Device
->RealOut
.NumChannels
;i
++)
714 State
->Early
.PanGain
[i
&3][Device
->Dry
.NumChannels
+i
] = Gain
* EarlyGain
;
718 /* Note that EAX Reverb's panning vectors are using right-handed
719 * coordinates, rather than OpenAL's left-handed coordinates. Negate Z
723 ReflectionsPan
[0] / length
,
724 ReflectionsPan
[1] / length
,
725 -ReflectionsPan
[2] / length
,
727 length
= minf(length
, 1.0f
);
729 CalcDirectionCoeffs(pan
, 0.0f
, coeffs
);
730 ComputePanningGains(Device
->Dry
, coeffs
, Gain
, DirGains
);
731 for(i
= 0;i
< Device
->Dry
.NumChannels
;i
++)
732 State
->Early
.PanGain
[3][i
] = DirGains
[i
] * EarlyGain
* length
;
733 for(i
= 0;i
< Device
->RealOut
.NumChannels
;i
++)
734 State
->Early
.PanGain
[i
&3][Device
->Dry
.NumChannels
+i
] = Gain
* EarlyGain
* (1.0f
-length
);
737 memset(State
->Late
.PanGain
, 0, sizeof(State
->Late
.PanGain
));
738 length
= sqrtf(LateReverbPan
[0]*LateReverbPan
[0] + LateReverbPan
[1]*LateReverbPan
[1] + LateReverbPan
[2]*LateReverbPan
[2]);
739 if(!(length
> FLT_EPSILON
))
741 for(i
= 0;i
< Device
->RealOut
.NumChannels
;i
++)
742 State
->Late
.PanGain
[i
&3][Device
->Dry
.NumChannels
+i
] = Gain
* LateGain
;
747 LateReverbPan
[0] / length
,
748 LateReverbPan
[1] / length
,
749 -LateReverbPan
[2] / length
,
751 length
= minf(length
, 1.0f
);
753 CalcDirectionCoeffs(pan
, 0.0f
, coeffs
);
754 ComputePanningGains(Device
->Dry
, coeffs
, Gain
, DirGains
);
755 for(i
= 0;i
< Device
->Dry
.NumChannels
;i
++)
756 State
->Late
.PanGain
[3][i
] = DirGains
[i
] * LateGain
* length
;
757 for(i
= 0;i
< Device
->RealOut
.NumChannels
;i
++)
758 State
->Late
.PanGain
[i
&3][Device
->Dry
.NumChannels
+i
] = Gain
* LateGain
* (1.0f
-length
);
762 static ALvoid
UpdateDirectPanning(const ALCdevice
*Device
, const ALfloat
*ReflectionsPan
, const ALfloat
*LateReverbPan
, ALfloat Gain
, ALfloat EarlyGain
, ALfloat LateGain
, ALreverbState
*State
)
764 ALfloat AmbientGains
[MAX_OUTPUT_CHANNELS
];
765 ALfloat DirGains
[MAX_OUTPUT_CHANNELS
];
766 ALfloat coeffs
[MAX_AMBI_COEFFS
];
770 /* Apply a boost of about 3dB to better match the expected stereo output volume. */
771 ComputeAmbientGains(Device
->Dry
, Gain
*1.414213562f
, AmbientGains
);
773 memset(State
->Early
.PanGain
, 0, sizeof(State
->Early
.PanGain
));
774 length
= sqrtf(ReflectionsPan
[0]*ReflectionsPan
[0] + ReflectionsPan
[1]*ReflectionsPan
[1] + ReflectionsPan
[2]*ReflectionsPan
[2]);
775 if(!(length
> FLT_EPSILON
))
777 for(i
= 0;i
< Device
->Dry
.NumChannels
;i
++)
778 State
->Early
.PanGain
[i
&3][i
] = AmbientGains
[i
] * EarlyGain
;
783 ReflectionsPan
[0] / length
,
784 ReflectionsPan
[1] / length
,
785 -ReflectionsPan
[2] / length
,
787 length
= minf(length
, 1.0f
);
789 CalcDirectionCoeffs(pan
, 0.0f
, coeffs
);
790 ComputePanningGains(Device
->Dry
, coeffs
, Gain
, DirGains
);
791 for(i
= 0;i
< Device
->Dry
.NumChannels
;i
++)
792 State
->Early
.PanGain
[i
&3][i
] = lerp(AmbientGains
[i
], DirGains
[i
], length
) * EarlyGain
;
795 memset(State
->Late
.PanGain
, 0, sizeof(State
->Late
.PanGain
));
796 length
= sqrtf(LateReverbPan
[0]*LateReverbPan
[0] + LateReverbPan
[1]*LateReverbPan
[1] + LateReverbPan
[2]*LateReverbPan
[2]);
797 if(!(length
> FLT_EPSILON
))
799 for(i
= 0;i
< Device
->Dry
.NumChannels
;i
++)
800 State
->Late
.PanGain
[i
&3][i
] = AmbientGains
[i
] * LateGain
;
805 LateReverbPan
[0] / length
,
806 LateReverbPan
[1] / length
,
807 -LateReverbPan
[2] / length
,
809 length
= minf(length
, 1.0f
);
811 CalcDirectionCoeffs(pan
, 0.0f
, coeffs
);
812 ComputePanningGains(Device
->Dry
, coeffs
, Gain
, DirGains
);
813 for(i
= 0;i
< Device
->Dry
.NumChannels
;i
++)
814 State
->Late
.PanGain
[i
&3][i
] = lerp(AmbientGains
[i
], DirGains
[i
], length
) * LateGain
;
818 static ALvoid
Update3DPanning(const ALCdevice
*Device
, const ALfloat
*ReflectionsPan
, const ALfloat
*LateReverbPan
, ALfloat Gain
, ALfloat EarlyGain
, ALfloat LateGain
, ALreverbState
*State
)
820 static const ALfloat PanDirs
[4][3] = {
821 { -0.707106781f
, 0.0f
, -0.707106781f
}, /* Front left */
822 { 0.707106781f
, 0.0f
, -0.707106781f
}, /* Front right */
823 { 0.707106781f
, 0.0f
, 0.707106781f
}, /* Back right */
824 { -0.707106781f
, 0.0f
, 0.707106781f
} /* Back left */
826 ALfloat coeffs
[MAX_AMBI_COEFFS
];
831 /* sqrt(0.5) would be the gain scaling when the panning vector is 0. This
832 * also equals sqrt(2/4), a nice gain scaling for the four virtual points
833 * producing an "ambient" response.
835 gain
[0] = gain
[1] = gain
[2] = gain
[3] = 0.707106781f
;
836 length
= sqrtf(ReflectionsPan
[0]*ReflectionsPan
[0] + ReflectionsPan
[1]*ReflectionsPan
[1] + ReflectionsPan
[2]*ReflectionsPan
[2]);
840 ReflectionsPan
[0] / length
,
841 ReflectionsPan
[1] / length
,
842 -ReflectionsPan
[2] / length
,
846 ALfloat dotp
= pan
[0]*PanDirs
[i
][0] + pan
[1]*PanDirs
[i
][1] + pan
[2]*PanDirs
[i
][2];
847 gain
[i
] = sqrtf(clampf(dotp
*0.5f
+ 0.5f
, 0.0f
, 1.0f
));
850 else if(length
> FLT_EPSILON
)
854 ALfloat dotp
= ReflectionsPan
[0]*PanDirs
[i
][0] + ReflectionsPan
[1]*PanDirs
[i
][1] +
855 -ReflectionsPan
[2]*PanDirs
[i
][2];
856 gain
[i
] = sqrtf(clampf(dotp
*0.5f
+ 0.5f
, 0.0f
, 1.0f
));
861 CalcDirectionCoeffs(PanDirs
[i
], 0.0f
, coeffs
);
862 ComputePanningGains(Device
->Dry
, coeffs
, Gain
*EarlyGain
*gain
[i
],
863 State
->Early
.PanGain
[i
]);
866 gain
[0] = gain
[1] = gain
[2] = gain
[3] = 0.707106781f
;
867 length
= sqrtf(LateReverbPan
[0]*LateReverbPan
[0] + LateReverbPan
[1]*LateReverbPan
[1] + LateReverbPan
[2]*LateReverbPan
[2]);
871 LateReverbPan
[0] / length
,
872 LateReverbPan
[1] / length
,
873 -LateReverbPan
[2] / length
,
877 ALfloat dotp
= pan
[0]*PanDirs
[i
][0] + pan
[1]*PanDirs
[i
][1] + pan
[2]*PanDirs
[i
][2];
878 gain
[i
] = sqrtf(clampf(dotp
*0.5f
+ 0.5f
, 0.0f
, 1.0f
));
881 else if(length
> FLT_EPSILON
)
885 ALfloat dotp
= LateReverbPan
[0]*PanDirs
[i
][0] + LateReverbPan
[1]*PanDirs
[i
][1] +
886 -LateReverbPan
[2]*PanDirs
[i
][2];
887 gain
[i
] = sqrtf(clampf(dotp
*0.5f
+ 0.5f
, 0.0f
, 1.0f
));
892 CalcDirectionCoeffs(PanDirs
[i
], 0.0f
, coeffs
);
893 ComputePanningGains(Device
->Dry
, coeffs
, Gain
*LateGain
*gain
[i
],
894 State
->Late
.PanGain
[i
]);
898 static ALvoid
ALreverbState_update(ALreverbState
*State
, const ALCdevice
*Device
, const ALeffectslot
*Slot
, const ALeffectProps
*props
)
900 ALuint frequency
= Device
->Frequency
;
901 ALfloat lfscale
, hfscale
, hfRatio
;
902 ALfloat gain
, gainlf
, gainhf
;
905 if(Slot
->Params
.EffectType
== AL_EFFECT_EAXREVERB
&& !EmulateEAXReverb
)
906 State
->IsEax
= AL_TRUE
;
907 else if(Slot
->Params
.EffectType
== AL_EFFECT_REVERB
|| EmulateEAXReverb
)
908 State
->IsEax
= AL_FALSE
;
910 // Calculate the master filters
911 hfscale
= props
->Reverb
.HFReference
/ frequency
;
912 gainhf
= maxf(props
->Reverb
.GainHF
, 0.0001f
);
913 ALfilterState_setParams(&State
->LpFilter
, ALfilterType_HighShelf
,
914 gainhf
, hfscale
, calc_rcpQ_from_slope(gainhf
, 0.75f
));
915 lfscale
= props
->Reverb
.LFReference
/ frequency
;
916 gainlf
= maxf(props
->Reverb
.GainLF
, 0.0001f
);
917 ALfilterState_setParams(&State
->HpFilter
, ALfilterType_LowShelf
,
918 gainlf
, lfscale
, calc_rcpQ_from_slope(gainlf
, 0.75f
));
920 // Update the modulator line.
921 UpdateModulator(props
->Reverb
.ModulationTime
, props
->Reverb
.ModulationDepth
,
924 // Update the initial effect delay.
925 UpdateDelayLine(props
->Reverb
.ReflectionsDelay
, props
->Reverb
.LateReverbDelay
,
928 // Update the early lines.
929 UpdateEarlyLines(props
->Reverb
.LateReverbDelay
, State
);
931 // Update the decorrelator.
932 UpdateDecorrelator(props
->Reverb
.Density
, frequency
, State
);
934 // Get the mixing matrix coefficients (x and y).
935 CalcMatrixCoeffs(props
->Reverb
.Diffusion
, &x
, &y
);
936 // Then divide x into y to simplify the matrix calculation.
937 State
->Late
.MixCoeff
= y
/ x
;
939 // If the HF limit parameter is flagged, calculate an appropriate limit
940 // based on the air absorption parameter.
941 hfRatio
= props
->Reverb
.DecayHFRatio
;
942 if(props
->Reverb
.DecayHFLimit
&& props
->Reverb
.AirAbsorptionGainHF
< 1.0f
)
943 hfRatio
= CalcLimitedHfRatio(hfRatio
, props
->Reverb
.AirAbsorptionGainHF
,
944 props
->Reverb
.DecayTime
);
946 cw
= cosf(F_TAU
* hfscale
);
947 // Update the late lines.
948 UpdateLateLines(x
, props
->Reverb
.Density
, props
->Reverb
.DecayTime
,
949 props
->Reverb
.Diffusion
, props
->Reverb
.EchoDepth
,
950 hfRatio
, cw
, frequency
, State
);
952 // Update the echo line.
953 UpdateEchoLine(props
->Reverb
.EchoTime
, props
->Reverb
.DecayTime
,
954 props
->Reverb
.Diffusion
, props
->Reverb
.EchoDepth
,
955 hfRatio
, cw
, frequency
, State
);
957 gain
= props
->Reverb
.Gain
* Slot
->Params
.Gain
* ReverbBoost
;
958 // Update early and late 3D panning.
959 if(Device
->Hrtf
|| Device
->Uhj_Encoder
)
960 UpdateMixedPanning(Device
, props
->Reverb
.ReflectionsPan
,
961 props
->Reverb
.LateReverbPan
, gain
,
962 props
->Reverb
.ReflectionsGain
,
963 props
->Reverb
.LateReverbGain
, State
);
964 else if(Device
->AmbiDecoder
|| (Device
->FmtChans
>= DevFmtAmbi1
&&
965 Device
->FmtChans
<= DevFmtAmbi3
))
966 Update3DPanning(Device
, props
->Reverb
.ReflectionsPan
,
967 props
->Reverb
.LateReverbPan
, gain
,
968 props
->Reverb
.ReflectionsGain
,
969 props
->Reverb
.LateReverbGain
, State
);
971 UpdateDirectPanning(Device
, props
->Reverb
.ReflectionsPan
,
972 props
->Reverb
.LateReverbPan
, gain
,
973 props
->Reverb
.ReflectionsGain
,
974 props
->Reverb
.LateReverbGain
, State
);
978 /**************************************
979 * Effect Processing *
980 **************************************/
982 // Basic delay line input/output routines.
983 static inline ALfloat
DelayLineOut(DelayLine
*Delay
, ALuint offset
)
985 return Delay
->Line
[offset
&Delay
->Mask
];
988 static inline ALvoid
DelayLineIn(DelayLine
*Delay
, ALuint offset
, ALfloat in
)
990 Delay
->Line
[offset
&Delay
->Mask
] = in
;
993 // Given an input sample, this function produces modulation for the late
995 static inline ALfloat
EAXModulation(ALreverbState
*State
, ALuint offset
, ALfloat in
)
997 ALfloat sinus
, frac
, fdelay
;
1001 // Calculate the sinus rythm (dependent on modulation time and the
1002 // sampling rate). The center of the sinus is moved to reduce the delay
1003 // of the effect when the time or depth are low.
1004 sinus
= 1.0f
- cosf(F_TAU
* State
->Mod
.Index
/ State
->Mod
.Range
);
1006 // Step the modulation index forward, keeping it bound to its range.
1007 State
->Mod
.Index
= (State
->Mod
.Index
+ 1) % State
->Mod
.Range
;
1009 // The depth determines the range over which to read the input samples
1010 // from, so it must be filtered to reduce the distortion caused by even
1011 // small parameter changes.
1012 State
->Mod
.Filter
= lerp(State
->Mod
.Filter
, State
->Mod
.Depth
,
1015 // Calculate the read offset and fraction between it and the next sample.
1016 frac
= modff(State
->Mod
.Filter
*sinus
, &fdelay
);
1017 delay
= fastf2u(fdelay
);
1019 /* Add the incoming sample to the delay line first, so a 0 delay gets the
1022 DelayLineIn(&State
->Mod
.Delay
, offset
, in
);
1023 /* Get the two samples crossed by the offset delay */
1024 out0
= DelayLineOut(&State
->Mod
.Delay
, offset
- delay
);
1025 out1
= DelayLineOut(&State
->Mod
.Delay
, offset
- delay
- 1);
1027 // The output is obtained by linearly interpolating the two samples that
1028 // were acquired above.
1029 return lerp(out0
, out1
, frac
);
1032 // Given some input sample, this function produces four-channel outputs for the
1033 // early reflections.
1034 static inline ALvoid
EarlyReflection(ALreverbState
*State
, ALuint todo
, ALfloat (*restrict out
)[4])
1036 ALfloat d
[4], v
, f
[4];
1039 for(i
= 0;i
< todo
;i
++)
1041 ALuint offset
= State
->Offset
+i
;
1043 // Obtain the decayed results of each early delay line.
1044 d
[0] = DelayLineOut(&State
->Early
.Delay
[0], offset
-State
->Early
.Offset
[0]) * State
->Early
.Coeff
[0];
1045 d
[1] = DelayLineOut(&State
->Early
.Delay
[1], offset
-State
->Early
.Offset
[1]) * State
->Early
.Coeff
[1];
1046 d
[2] = DelayLineOut(&State
->Early
.Delay
[2], offset
-State
->Early
.Offset
[2]) * State
->Early
.Coeff
[2];
1047 d
[3] = DelayLineOut(&State
->Early
.Delay
[3], offset
-State
->Early
.Offset
[3]) * State
->Early
.Coeff
[3];
1049 /* The following uses a lossless scattering junction from waveguide
1050 * theory. It actually amounts to a householder mixing matrix, which
1051 * will produce a maximally diffuse response, and means this can
1052 * probably be considered a simple feed-back delay network (FDN).
1060 v
= (d
[0] + d
[1] + d
[2] + d
[3]) * 0.5f
;
1061 // The junction is loaded with the input here.
1062 v
+= DelayLineOut(&State
->Delay
, offset
-State
->DelayTap
[0]);
1064 // Calculate the feed values for the delay lines.
1070 // Re-feed the delay lines.
1071 DelayLineIn(&State
->Early
.Delay
[0], offset
, f
[0]);
1072 DelayLineIn(&State
->Early
.Delay
[1], offset
, f
[1]);
1073 DelayLineIn(&State
->Early
.Delay
[2], offset
, f
[2]);
1074 DelayLineIn(&State
->Early
.Delay
[3], offset
, f
[3]);
1076 /* Output the results of the junction for all four channels with a
1077 * constant attenuation of 0.5.
1079 out
[i
][0] = f
[0] * 0.5f
;
1080 out
[i
][1] = f
[1] * 0.5f
;
1081 out
[i
][2] = f
[2] * 0.5f
;
1082 out
[i
][3] = f
[3] * 0.5f
;
1086 // Basic attenuated all-pass input/output routine.
1087 static inline ALfloat
AllpassInOut(DelayLine
*Delay
, ALuint outOffset
, ALuint inOffset
, ALfloat in
, ALfloat feedCoeff
, ALfloat coeff
)
1091 out
= DelayLineOut(Delay
, outOffset
);
1092 feed
= feedCoeff
* in
;
1093 DelayLineIn(Delay
, inOffset
, (feedCoeff
* (out
- feed
)) + in
);
1095 // The time-based attenuation is only applied to the delay output to
1096 // keep it from affecting the feed-back path (which is already controlled
1097 // by the all-pass feed coefficient).
1098 return (coeff
* out
) - feed
;
1101 // All-pass input/output routine for late reverb.
1102 static inline ALfloat
LateAllPassInOut(ALreverbState
*State
, ALuint offset
, ALuint index
, ALfloat in
)
1104 return AllpassInOut(&State
->Late
.ApDelay
[index
],
1105 offset
- State
->Late
.ApOffset
[index
],
1106 offset
, in
, State
->Late
.ApFeedCoeff
,
1107 State
->Late
.ApCoeff
[index
]);
1110 // Low-pass filter input/output routine for late reverb.
1111 static inline ALfloat
LateLowPassInOut(ALreverbState
*State
, ALuint index
, ALfloat in
)
1113 in
= lerp(in
, State
->Late
.LpSample
[index
], State
->Late
.LpCoeff
[index
]);
1114 State
->Late
.LpSample
[index
] = in
;
1118 // Given four decorrelated input samples, this function produces four-channel
1119 // output for the late reverb.
1120 static inline ALvoid
LateReverb(ALreverbState
*State
, ALuint todo
, ALfloat (*restrict out
)[4])
1125 // Feed the decorrelator from the energy-attenuated output of the second
1127 for(i
= 0;i
< todo
;i
++)
1129 ALuint offset
= State
->Offset
+i
;
1130 ALfloat sample
= DelayLineOut(&State
->Delay
, offset
- State
->DelayTap
[1]) *
1131 State
->Late
.DensityGain
;
1132 DelayLineIn(&State
->Decorrelator
, offset
, sample
);
1135 for(i
= 0;i
< todo
;i
++)
1137 ALuint offset
= State
->Offset
+i
;
1139 /* Obtain four decorrelated input samples. */
1140 f
[0] = DelayLineOut(&State
->Decorrelator
, offset
);
1141 f
[1] = DelayLineOut(&State
->Decorrelator
, offset
-State
->DecoTap
[0]);
1142 f
[2] = DelayLineOut(&State
->Decorrelator
, offset
-State
->DecoTap
[1]);
1143 f
[3] = DelayLineOut(&State
->Decorrelator
, offset
-State
->DecoTap
[2]);
1145 /* Add the decayed results of the cyclical delay lines, then pass the
1146 * results through the low-pass filters.
1148 f
[0] += DelayLineOut(&State
->Late
.Delay
[0], offset
-State
->Late
.Offset
[0]) * State
->Late
.Coeff
[0];
1149 f
[1] += DelayLineOut(&State
->Late
.Delay
[1], offset
-State
->Late
.Offset
[1]) * State
->Late
.Coeff
[1];
1150 f
[2] += DelayLineOut(&State
->Late
.Delay
[2], offset
-State
->Late
.Offset
[2]) * State
->Late
.Coeff
[2];
1151 f
[3] += DelayLineOut(&State
->Late
.Delay
[3], offset
-State
->Late
.Offset
[3]) * State
->Late
.Coeff
[3];
1153 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and
1155 d
[0] = LateLowPassInOut(State
, 2, f
[2]);
1156 d
[1] = LateLowPassInOut(State
, 0, f
[0]);
1157 d
[2] = LateLowPassInOut(State
, 3, f
[3]);
1158 d
[3] = LateLowPassInOut(State
, 1, f
[1]);
1160 // To help increase diffusion, run each line through an all-pass filter.
1161 // When there is no diffusion, the shortest all-pass filter will feed
1162 // the shortest delay line.
1163 d
[0] = LateAllPassInOut(State
, offset
, 0, d
[0]);
1164 d
[1] = LateAllPassInOut(State
, offset
, 1, d
[1]);
1165 d
[2] = LateAllPassInOut(State
, offset
, 2, d
[2]);
1166 d
[3] = LateAllPassInOut(State
, offset
, 3, d
[3]);
1168 /* Late reverb is done with a modified feed-back delay network (FDN)
1169 * topology. Four input lines are each fed through their own all-pass
1170 * filter and then into the mixing matrix. The four outputs of the
1171 * mixing matrix are then cycled back to the inputs. Each output feeds
1172 * a different input to form a circlular feed cycle.
1174 * The mixing matrix used is a 4D skew-symmetric rotation matrix
1175 * derived using a single unitary rotational parameter:
1177 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
1182 * The rotation is constructed from the effect's diffusion parameter,
1183 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
1184 * with differing signs, and d is the coefficient x. The matrix is
1187 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
1188 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
1189 * [ y, -y, x, y ] x = cos(t)
1190 * [ -y, -y, -y, x ] y = sin(t) / n
1192 * To reduce the number of multiplies, the x coefficient is applied
1193 * with the cyclical delay line coefficients. Thus only the y
1194 * coefficient is applied when mixing, and is modified to be: y / x.
1196 f
[0] = d
[0] + (State
->Late
.MixCoeff
* ( d
[1] + -d
[2] + d
[3]));
1197 f
[1] = d
[1] + (State
->Late
.MixCoeff
* (-d
[0] + d
[2] + d
[3]));
1198 f
[2] = d
[2] + (State
->Late
.MixCoeff
* ( d
[0] + -d
[1] + d
[3]));
1199 f
[3] = d
[3] + (State
->Late
.MixCoeff
* (-d
[0] + -d
[1] + -d
[2] ));
1201 // Output the results of the matrix for all four channels, attenuated by
1202 // the late reverb gain (which is attenuated by the 'x' mix coefficient).
1203 out
[i
][0] = State
->Late
.Gain
* f
[0];
1204 out
[i
][1] = State
->Late
.Gain
* f
[1];
1205 out
[i
][2] = State
->Late
.Gain
* f
[2];
1206 out
[i
][3] = State
->Late
.Gain
* f
[3];
1208 // Re-feed the cyclical delay lines.
1209 DelayLineIn(&State
->Late
.Delay
[0], offset
, f
[0]);
1210 DelayLineIn(&State
->Late
.Delay
[1], offset
, f
[1]);
1211 DelayLineIn(&State
->Late
.Delay
[2], offset
, f
[2]);
1212 DelayLineIn(&State
->Late
.Delay
[3], offset
, f
[3]);
1216 // Given an input sample, this function mixes echo into the four-channel late
1218 static inline ALvoid
EAXEcho(ALreverbState
*State
, ALuint todo
, ALfloat (*restrict late
)[4])
1223 for(i
= 0;i
< todo
;i
++)
1225 ALuint offset
= State
->Offset
+i
;
1227 // Get the latest attenuated echo sample for output.
1228 feed
= DelayLineOut(&State
->Echo
.Delay
, offset
-State
->Echo
.Offset
) *
1231 // Mix the output into the late reverb channels.
1232 out
= State
->Echo
.MixCoeff
* feed
;
1238 // Mix the energy-attenuated input with the output and pass it through
1239 // the echo low-pass filter.
1240 feed
+= DelayLineOut(&State
->Delay
, offset
-State
->DelayTap
[1]) *
1241 State
->Echo
.DensityGain
;
1242 feed
= lerp(feed
, State
->Echo
.LpSample
, State
->Echo
.LpCoeff
);
1243 State
->Echo
.LpSample
= feed
;
1245 // Then the echo all-pass filter.
1246 feed
= AllpassInOut(&State
->Echo
.ApDelay
, offset
-State
->Echo
.ApOffset
,
1247 offset
, feed
, State
->Echo
.ApFeedCoeff
,
1248 State
->Echo
.ApCoeff
);
1250 // Feed the delay with the mixed and filtered sample.
1251 DelayLineIn(&State
->Echo
.Delay
, offset
, feed
);
1255 // Perform the non-EAX reverb pass on a given input sample, resulting in
1256 // four-channel output.
1257 static inline ALvoid
VerbPass(ALreverbState
*State
, ALuint todo
, const ALfloat
*input
, ALfloat (*restrict early
)[4], ALfloat (*restrict late
)[4])
1261 // Low-pass filter the incoming samples (use the early buffer as temp storage).
1262 ALfilterState_process(&State
->LpFilter
, &early
[0][0], input
, todo
);
1263 for(i
= 0;i
< todo
;i
++)
1264 DelayLineIn(&State
->Delay
, State
->Offset
+i
, early
[i
>>2][i
&3]);
1266 // Calculate the early reflection from the first delay tap.
1267 EarlyReflection(State
, todo
, early
);
1269 // Calculate the late reverb from the decorrelator taps.
1270 LateReverb(State
, todo
, late
);
1272 // Step all delays forward one sample.
1273 State
->Offset
+= todo
;
1276 // Perform the EAX reverb pass on a given input sample, resulting in four-
1278 static inline ALvoid
EAXVerbPass(ALreverbState
*State
, ALuint todo
, const ALfloat
*input
, ALfloat (*restrict early
)[4], ALfloat (*restrict late
)[4])
1282 // Band-pass and modulate the incoming samples (use the early buffer as temp storage).
1283 ALfilterState_process(&State
->LpFilter
, &early
[0][0], input
, todo
);
1284 ALfilterState_process(&State
->HpFilter
, &early
[MAX_UPDATE_SAMPLES
/4][0], &early
[0][0], todo
);
1286 for(i
= 0;i
< todo
;i
++)
1288 ALfloat sample
= early
[(MAX_UPDATE_SAMPLES
/4)+(i
>>2)][i
&3];
1290 // Perform any modulation on the input.
1291 sample
= EAXModulation(State
, State
->Offset
+i
, sample
);
1293 // Feed the initial delay line.
1294 DelayLineIn(&State
->Delay
, State
->Offset
+i
, sample
);
1297 // Calculate the early reflection from the first delay tap.
1298 EarlyReflection(State
, todo
, early
);
1300 // Calculate the late reverb from the decorrelator taps.
1301 LateReverb(State
, todo
, late
);
1303 // Calculate and mix in any echo.
1304 EAXEcho(State
, todo
, late
);
1306 // Step all delays forward.
1307 State
->Offset
+= todo
;
1310 static ALvoid
ALreverbState_processStandard(ALreverbState
*State
, ALuint SamplesToDo
, const ALfloat
*restrict SamplesIn
, ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
)
1312 ALfloat (*restrict early
)[4] = State
->EarlySamples
;
1313 ALfloat (*restrict late
)[4] = State
->ReverbSamples
;
1314 ALuint index
, c
, i
, l
;
1317 /* Process reverb for these samples. */
1318 for(index
= 0;index
< SamplesToDo
;)
1320 ALuint todo
= minu(SamplesToDo
-index
, MAX_UPDATE_SAMPLES
);
1322 VerbPass(State
, todo
, &SamplesIn
[index
], early
, late
);
1324 for(l
= 0;l
< 4;l
++)
1326 for(c
= 0;c
< NumChannels
;c
++)
1328 gain
= State
->Early
.PanGain
[l
][c
];
1329 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1331 for(i
= 0;i
< todo
;i
++)
1332 SamplesOut
[c
][index
+i
] += gain
*early
[i
][l
];
1334 gain
= State
->Late
.PanGain
[l
][c
];
1335 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1337 for(i
= 0;i
< todo
;i
++)
1338 SamplesOut
[c
][index
+i
] += gain
*late
[i
][l
];
1341 for(c
= 0;c
< State
->ExtraChannels
;c
++)
1343 gain
= State
->Early
.PanGain
[l
][NumChannels
+c
];
1344 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1346 for(i
= 0;i
< todo
;i
++)
1347 State
->ExtraOut
[c
][index
+i
] += gain
*early
[i
][l
];
1349 gain
= State
->Late
.PanGain
[l
][NumChannels
+c
];
1350 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1352 for(i
= 0;i
< todo
;i
++)
1353 State
->ExtraOut
[c
][index
+i
] += gain
*late
[i
][l
];
1362 static ALvoid
ALreverbState_processEax(ALreverbState
*State
, ALuint SamplesToDo
, const ALfloat
*restrict SamplesIn
, ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
)
1364 ALfloat (*restrict early
)[4] = State
->EarlySamples
;
1365 ALfloat (*restrict late
)[4] = State
->ReverbSamples
;
1366 ALuint index
, c
, i
, l
;
1369 /* Process reverb for these samples. */
1370 for(index
= 0;index
< SamplesToDo
;)
1372 ALuint todo
= minu(SamplesToDo
-index
, MAX_UPDATE_SAMPLES
);
1374 EAXVerbPass(State
, todo
, &SamplesIn
[index
], early
, late
);
1376 for(l
= 0;l
< 4;l
++)
1378 for(c
= 0;c
< NumChannels
;c
++)
1380 gain
= State
->Early
.PanGain
[l
][c
];
1381 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1383 for(i
= 0;i
< todo
;i
++)
1384 SamplesOut
[c
][index
+i
] += gain
*early
[i
][l
];
1386 gain
= State
->Late
.PanGain
[l
][c
];
1387 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1389 for(i
= 0;i
< todo
;i
++)
1390 SamplesOut
[c
][index
+i
] += gain
*late
[i
][l
];
1393 for(c
= 0;c
< State
->ExtraChannels
;c
++)
1395 gain
= State
->Early
.PanGain
[l
][NumChannels
+c
];
1396 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1398 for(i
= 0;i
< todo
;i
++)
1399 State
->ExtraOut
[c
][index
+i
] += gain
*early
[i
][l
];
1401 gain
= State
->Late
.PanGain
[l
][NumChannels
+c
];
1402 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1404 for(i
= 0;i
< todo
;i
++)
1405 State
->ExtraOut
[c
][index
+i
] += gain
*late
[i
][l
];
1414 static ALvoid
ALreverbState_process(ALreverbState
*State
, ALuint SamplesToDo
, const ALfloat (*restrict SamplesIn
)[BUFFERSIZE
], ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
)
1417 ALreverbState_processEax(State
, SamplesToDo
, SamplesIn
[0], SamplesOut
, NumChannels
);
1419 ALreverbState_processStandard(State
, SamplesToDo
, SamplesIn
[0], SamplesOut
, NumChannels
);
1423 typedef struct ALreverbStateFactory
{
1424 DERIVE_FROM_TYPE(ALeffectStateFactory
);
1425 } ALreverbStateFactory
;
1427 static ALeffectState
*ALreverbStateFactory_create(ALreverbStateFactory
* UNUSED(factory
))
1429 ALreverbState
*state
;
1432 state
= ALreverbState_New(sizeof(*state
));
1433 if(!state
) return NULL
;
1434 SET_VTABLE2(ALreverbState
, ALeffectState
, state
);
1436 state
->IsEax
= AL_FALSE
;
1437 state
->ExtraChannels
= 0;
1439 state
->TotalSamples
= 0;
1440 state
->SampleBuffer
= NULL
;
1442 ALfilterState_clear(&state
->LpFilter
);
1443 ALfilterState_clear(&state
->HpFilter
);
1445 state
->Mod
.Delay
.Mask
= 0;
1446 state
->Mod
.Delay
.Line
= NULL
;
1447 state
->Mod
.Index
= 0;
1448 state
->Mod
.Range
= 1;
1449 state
->Mod
.Depth
= 0.0f
;
1450 state
->Mod
.Coeff
= 0.0f
;
1451 state
->Mod
.Filter
= 0.0f
;
1453 state
->Delay
.Mask
= 0;
1454 state
->Delay
.Line
= NULL
;
1455 state
->DelayTap
[0] = 0;
1456 state
->DelayTap
[1] = 0;
1458 for(index
= 0;index
< 4;index
++)
1460 state
->Early
.Coeff
[index
] = 0.0f
;
1461 state
->Early
.Delay
[index
].Mask
= 0;
1462 state
->Early
.Delay
[index
].Line
= NULL
;
1463 state
->Early
.Offset
[index
] = 0;
1466 state
->Decorrelator
.Mask
= 0;
1467 state
->Decorrelator
.Line
= NULL
;
1468 state
->DecoTap
[0] = 0;
1469 state
->DecoTap
[1] = 0;
1470 state
->DecoTap
[2] = 0;
1472 state
->Late
.Gain
= 0.0f
;
1473 state
->Late
.DensityGain
= 0.0f
;
1474 state
->Late
.ApFeedCoeff
= 0.0f
;
1475 state
->Late
.MixCoeff
= 0.0f
;
1476 for(index
= 0;index
< 4;index
++)
1478 state
->Late
.ApCoeff
[index
] = 0.0f
;
1479 state
->Late
.ApDelay
[index
].Mask
= 0;
1480 state
->Late
.ApDelay
[index
].Line
= NULL
;
1481 state
->Late
.ApOffset
[index
] = 0;
1483 state
->Late
.Coeff
[index
] = 0.0f
;
1484 state
->Late
.Delay
[index
].Mask
= 0;
1485 state
->Late
.Delay
[index
].Line
= NULL
;
1486 state
->Late
.Offset
[index
] = 0;
1488 state
->Late
.LpCoeff
[index
] = 0.0f
;
1489 state
->Late
.LpSample
[index
] = 0.0f
;
1492 for(l
= 0;l
< 4;l
++)
1494 for(index
= 0;index
< MAX_OUTPUT_CHANNELS
;index
++)
1496 state
->Early
.PanGain
[l
][index
] = 0.0f
;
1497 state
->Late
.PanGain
[l
][index
] = 0.0f
;
1501 state
->Echo
.DensityGain
= 0.0f
;
1502 state
->Echo
.Delay
.Mask
= 0;
1503 state
->Echo
.Delay
.Line
= NULL
;
1504 state
->Echo
.ApDelay
.Mask
= 0;
1505 state
->Echo
.ApDelay
.Line
= NULL
;
1506 state
->Echo
.Coeff
= 0.0f
;
1507 state
->Echo
.ApFeedCoeff
= 0.0f
;
1508 state
->Echo
.ApCoeff
= 0.0f
;
1509 state
->Echo
.Offset
= 0;
1510 state
->Echo
.ApOffset
= 0;
1511 state
->Echo
.LpCoeff
= 0.0f
;
1512 state
->Echo
.LpSample
= 0.0f
;
1513 state
->Echo
.MixCoeff
= 0.0f
;
1517 return STATIC_CAST(ALeffectState
, state
);
1520 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALreverbStateFactory
);
1522 ALeffectStateFactory
*ALreverbStateFactory_getFactory(void)
1524 static ALreverbStateFactory ReverbFactory
= { { GET_VTABLE2(ALreverbStateFactory
, ALeffectStateFactory
) } };
1526 return STATIC_CAST(ALeffectStateFactory
, &ReverbFactory
);
1530 void ALeaxreverb_setParami(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint val
)
1532 ALeffectProps
*props
= &effect
->Props
;
1535 case AL_EAXREVERB_DECAY_HFLIMIT
:
1536 if(!(val
>= AL_EAXREVERB_MIN_DECAY_HFLIMIT
&& val
<= AL_EAXREVERB_MAX_DECAY_HFLIMIT
))
1537 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1538 props
->Reverb
.DecayHFLimit
= val
;
1542 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1545 void ALeaxreverb_setParamiv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALint
*vals
)
1547 ALeaxreverb_setParami(effect
, context
, param
, vals
[0]);
1549 void ALeaxreverb_setParamf(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat val
)
1551 ALeffectProps
*props
= &effect
->Props
;
1554 case AL_EAXREVERB_DENSITY
:
1555 if(!(val
>= AL_EAXREVERB_MIN_DENSITY
&& val
<= AL_EAXREVERB_MAX_DENSITY
))
1556 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1557 props
->Reverb
.Density
= val
;
1560 case AL_EAXREVERB_DIFFUSION
:
1561 if(!(val
>= AL_EAXREVERB_MIN_DIFFUSION
&& val
<= AL_EAXREVERB_MAX_DIFFUSION
))
1562 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1563 props
->Reverb
.Diffusion
= val
;
1566 case AL_EAXREVERB_GAIN
:
1567 if(!(val
>= AL_EAXREVERB_MIN_GAIN
&& val
<= AL_EAXREVERB_MAX_GAIN
))
1568 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1569 props
->Reverb
.Gain
= val
;
1572 case AL_EAXREVERB_GAINHF
:
1573 if(!(val
>= AL_EAXREVERB_MIN_GAINHF
&& val
<= AL_EAXREVERB_MAX_GAINHF
))
1574 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1575 props
->Reverb
.GainHF
= val
;
1578 case AL_EAXREVERB_GAINLF
:
1579 if(!(val
>= AL_EAXREVERB_MIN_GAINLF
&& val
<= AL_EAXREVERB_MAX_GAINLF
))
1580 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1581 props
->Reverb
.GainLF
= val
;
1584 case AL_EAXREVERB_DECAY_TIME
:
1585 if(!(val
>= AL_EAXREVERB_MIN_DECAY_TIME
&& val
<= AL_EAXREVERB_MAX_DECAY_TIME
))
1586 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1587 props
->Reverb
.DecayTime
= val
;
1590 case AL_EAXREVERB_DECAY_HFRATIO
:
1591 if(!(val
>= AL_EAXREVERB_MIN_DECAY_HFRATIO
&& val
<= AL_EAXREVERB_MAX_DECAY_HFRATIO
))
1592 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1593 props
->Reverb
.DecayHFRatio
= val
;
1596 case AL_EAXREVERB_DECAY_LFRATIO
:
1597 if(!(val
>= AL_EAXREVERB_MIN_DECAY_LFRATIO
&& val
<= AL_EAXREVERB_MAX_DECAY_LFRATIO
))
1598 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1599 props
->Reverb
.DecayLFRatio
= val
;
1602 case AL_EAXREVERB_REFLECTIONS_GAIN
:
1603 if(!(val
>= AL_EAXREVERB_MIN_REFLECTIONS_GAIN
&& val
<= AL_EAXREVERB_MAX_REFLECTIONS_GAIN
))
1604 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1605 props
->Reverb
.ReflectionsGain
= val
;
1608 case AL_EAXREVERB_REFLECTIONS_DELAY
:
1609 if(!(val
>= AL_EAXREVERB_MIN_REFLECTIONS_DELAY
&& val
<= AL_EAXREVERB_MAX_REFLECTIONS_DELAY
))
1610 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1611 props
->Reverb
.ReflectionsDelay
= val
;
1614 case AL_EAXREVERB_LATE_REVERB_GAIN
:
1615 if(!(val
>= AL_EAXREVERB_MIN_LATE_REVERB_GAIN
&& val
<= AL_EAXREVERB_MAX_LATE_REVERB_GAIN
))
1616 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1617 props
->Reverb
.LateReverbGain
= val
;
1620 case AL_EAXREVERB_LATE_REVERB_DELAY
:
1621 if(!(val
>= AL_EAXREVERB_MIN_LATE_REVERB_DELAY
&& val
<= AL_EAXREVERB_MAX_LATE_REVERB_DELAY
))
1622 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1623 props
->Reverb
.LateReverbDelay
= val
;
1626 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF
:
1627 if(!(val
>= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF
&& val
<= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF
))
1628 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1629 props
->Reverb
.AirAbsorptionGainHF
= val
;
1632 case AL_EAXREVERB_ECHO_TIME
:
1633 if(!(val
>= AL_EAXREVERB_MIN_ECHO_TIME
&& val
<= AL_EAXREVERB_MAX_ECHO_TIME
))
1634 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1635 props
->Reverb
.EchoTime
= val
;
1638 case AL_EAXREVERB_ECHO_DEPTH
:
1639 if(!(val
>= AL_EAXREVERB_MIN_ECHO_DEPTH
&& val
<= AL_EAXREVERB_MAX_ECHO_DEPTH
))
1640 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1641 props
->Reverb
.EchoDepth
= val
;
1644 case AL_EAXREVERB_MODULATION_TIME
:
1645 if(!(val
>= AL_EAXREVERB_MIN_MODULATION_TIME
&& val
<= AL_EAXREVERB_MAX_MODULATION_TIME
))
1646 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1647 props
->Reverb
.ModulationTime
= val
;
1650 case AL_EAXREVERB_MODULATION_DEPTH
:
1651 if(!(val
>= AL_EAXREVERB_MIN_MODULATION_DEPTH
&& val
<= AL_EAXREVERB_MAX_MODULATION_DEPTH
))
1652 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1653 props
->Reverb
.ModulationDepth
= val
;
1656 case AL_EAXREVERB_HFREFERENCE
:
1657 if(!(val
>= AL_EAXREVERB_MIN_HFREFERENCE
&& val
<= AL_EAXREVERB_MAX_HFREFERENCE
))
1658 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1659 props
->Reverb
.HFReference
= val
;
1662 case AL_EAXREVERB_LFREFERENCE
:
1663 if(!(val
>= AL_EAXREVERB_MIN_LFREFERENCE
&& val
<= AL_EAXREVERB_MAX_LFREFERENCE
))
1664 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1665 props
->Reverb
.LFReference
= val
;
1668 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR
:
1669 if(!(val
>= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR
&& val
<= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR
))
1670 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1671 props
->Reverb
.RoomRolloffFactor
= val
;
1675 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1678 void ALeaxreverb_setParamfv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALfloat
*vals
)
1680 ALeffectProps
*props
= &effect
->Props
;
1683 case AL_EAXREVERB_REFLECTIONS_PAN
:
1684 if(!(isfinite(vals
[0]) && isfinite(vals
[1]) && isfinite(vals
[2])))
1685 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1686 props
->Reverb
.ReflectionsPan
[0] = vals
[0];
1687 props
->Reverb
.ReflectionsPan
[1] = vals
[1];
1688 props
->Reverb
.ReflectionsPan
[2] = vals
[2];
1690 case AL_EAXREVERB_LATE_REVERB_PAN
:
1691 if(!(isfinite(vals
[0]) && isfinite(vals
[1]) && isfinite(vals
[2])))
1692 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1693 props
->Reverb
.LateReverbPan
[0] = vals
[0];
1694 props
->Reverb
.LateReverbPan
[1] = vals
[1];
1695 props
->Reverb
.LateReverbPan
[2] = vals
[2];
1699 ALeaxreverb_setParamf(effect
, context
, param
, vals
[0]);
1704 void ALeaxreverb_getParami(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint
*val
)
1706 const ALeffectProps
*props
= &effect
->Props
;
1709 case AL_EAXREVERB_DECAY_HFLIMIT
:
1710 *val
= props
->Reverb
.DecayHFLimit
;
1714 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1717 void ALeaxreverb_getParamiv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint
*vals
)
1719 ALeaxreverb_getParami(effect
, context
, param
, vals
);
1721 void ALeaxreverb_getParamf(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*val
)
1723 const ALeffectProps
*props
= &effect
->Props
;
1726 case AL_EAXREVERB_DENSITY
:
1727 *val
= props
->Reverb
.Density
;
1730 case AL_EAXREVERB_DIFFUSION
:
1731 *val
= props
->Reverb
.Diffusion
;
1734 case AL_EAXREVERB_GAIN
:
1735 *val
= props
->Reverb
.Gain
;
1738 case AL_EAXREVERB_GAINHF
:
1739 *val
= props
->Reverb
.GainHF
;
1742 case AL_EAXREVERB_GAINLF
:
1743 *val
= props
->Reverb
.GainLF
;
1746 case AL_EAXREVERB_DECAY_TIME
:
1747 *val
= props
->Reverb
.DecayTime
;
1750 case AL_EAXREVERB_DECAY_HFRATIO
:
1751 *val
= props
->Reverb
.DecayHFRatio
;
1754 case AL_EAXREVERB_DECAY_LFRATIO
:
1755 *val
= props
->Reverb
.DecayLFRatio
;
1758 case AL_EAXREVERB_REFLECTIONS_GAIN
:
1759 *val
= props
->Reverb
.ReflectionsGain
;
1762 case AL_EAXREVERB_REFLECTIONS_DELAY
:
1763 *val
= props
->Reverb
.ReflectionsDelay
;
1766 case AL_EAXREVERB_LATE_REVERB_GAIN
:
1767 *val
= props
->Reverb
.LateReverbGain
;
1770 case AL_EAXREVERB_LATE_REVERB_DELAY
:
1771 *val
= props
->Reverb
.LateReverbDelay
;
1774 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF
:
1775 *val
= props
->Reverb
.AirAbsorptionGainHF
;
1778 case AL_EAXREVERB_ECHO_TIME
:
1779 *val
= props
->Reverb
.EchoTime
;
1782 case AL_EAXREVERB_ECHO_DEPTH
:
1783 *val
= props
->Reverb
.EchoDepth
;
1786 case AL_EAXREVERB_MODULATION_TIME
:
1787 *val
= props
->Reverb
.ModulationTime
;
1790 case AL_EAXREVERB_MODULATION_DEPTH
:
1791 *val
= props
->Reverb
.ModulationDepth
;
1794 case AL_EAXREVERB_HFREFERENCE
:
1795 *val
= props
->Reverb
.HFReference
;
1798 case AL_EAXREVERB_LFREFERENCE
:
1799 *val
= props
->Reverb
.LFReference
;
1802 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR
:
1803 *val
= props
->Reverb
.RoomRolloffFactor
;
1807 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1810 void ALeaxreverb_getParamfv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*vals
)
1812 const ALeffectProps
*props
= &effect
->Props
;
1815 case AL_EAXREVERB_REFLECTIONS_PAN
:
1816 vals
[0] = props
->Reverb
.ReflectionsPan
[0];
1817 vals
[1] = props
->Reverb
.ReflectionsPan
[1];
1818 vals
[2] = props
->Reverb
.ReflectionsPan
[2];
1820 case AL_EAXREVERB_LATE_REVERB_PAN
:
1821 vals
[0] = props
->Reverb
.LateReverbPan
[0];
1822 vals
[1] = props
->Reverb
.LateReverbPan
[1];
1823 vals
[2] = props
->Reverb
.LateReverbPan
[2];
1827 ALeaxreverb_getParamf(effect
, context
, param
, vals
);
1832 DEFINE_ALEFFECT_VTABLE(ALeaxreverb
);
1834 void ALreverb_setParami(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint val
)
1836 ALeffectProps
*props
= &effect
->Props
;
1839 case AL_REVERB_DECAY_HFLIMIT
:
1840 if(!(val
>= AL_REVERB_MIN_DECAY_HFLIMIT
&& val
<= AL_REVERB_MAX_DECAY_HFLIMIT
))
1841 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1842 props
->Reverb
.DecayHFLimit
= val
;
1846 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1849 void ALreverb_setParamiv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALint
*vals
)
1851 ALreverb_setParami(effect
, context
, param
, vals
[0]);
1853 void ALreverb_setParamf(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat val
)
1855 ALeffectProps
*props
= &effect
->Props
;
1858 case AL_REVERB_DENSITY
:
1859 if(!(val
>= AL_REVERB_MIN_DENSITY
&& val
<= AL_REVERB_MAX_DENSITY
))
1860 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1861 props
->Reverb
.Density
= val
;
1864 case AL_REVERB_DIFFUSION
:
1865 if(!(val
>= AL_REVERB_MIN_DIFFUSION
&& val
<= AL_REVERB_MAX_DIFFUSION
))
1866 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1867 props
->Reverb
.Diffusion
= val
;
1870 case AL_REVERB_GAIN
:
1871 if(!(val
>= AL_REVERB_MIN_GAIN
&& val
<= AL_REVERB_MAX_GAIN
))
1872 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1873 props
->Reverb
.Gain
= val
;
1876 case AL_REVERB_GAINHF
:
1877 if(!(val
>= AL_REVERB_MIN_GAINHF
&& val
<= AL_REVERB_MAX_GAINHF
))
1878 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1879 props
->Reverb
.GainHF
= val
;
1882 case AL_REVERB_DECAY_TIME
:
1883 if(!(val
>= AL_REVERB_MIN_DECAY_TIME
&& val
<= AL_REVERB_MAX_DECAY_TIME
))
1884 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1885 props
->Reverb
.DecayTime
= val
;
1888 case AL_REVERB_DECAY_HFRATIO
:
1889 if(!(val
>= AL_REVERB_MIN_DECAY_HFRATIO
&& val
<= AL_REVERB_MAX_DECAY_HFRATIO
))
1890 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1891 props
->Reverb
.DecayHFRatio
= val
;
1894 case AL_REVERB_REFLECTIONS_GAIN
:
1895 if(!(val
>= AL_REVERB_MIN_REFLECTIONS_GAIN
&& val
<= AL_REVERB_MAX_REFLECTIONS_GAIN
))
1896 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1897 props
->Reverb
.ReflectionsGain
= val
;
1900 case AL_REVERB_REFLECTIONS_DELAY
:
1901 if(!(val
>= AL_REVERB_MIN_REFLECTIONS_DELAY
&& val
<= AL_REVERB_MAX_REFLECTIONS_DELAY
))
1902 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1903 props
->Reverb
.ReflectionsDelay
= val
;
1906 case AL_REVERB_LATE_REVERB_GAIN
:
1907 if(!(val
>= AL_REVERB_MIN_LATE_REVERB_GAIN
&& val
<= AL_REVERB_MAX_LATE_REVERB_GAIN
))
1908 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1909 props
->Reverb
.LateReverbGain
= val
;
1912 case AL_REVERB_LATE_REVERB_DELAY
:
1913 if(!(val
>= AL_REVERB_MIN_LATE_REVERB_DELAY
&& val
<= AL_REVERB_MAX_LATE_REVERB_DELAY
))
1914 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1915 props
->Reverb
.LateReverbDelay
= val
;
1918 case AL_REVERB_AIR_ABSORPTION_GAINHF
:
1919 if(!(val
>= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF
&& val
<= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF
))
1920 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1921 props
->Reverb
.AirAbsorptionGainHF
= val
;
1924 case AL_REVERB_ROOM_ROLLOFF_FACTOR
:
1925 if(!(val
>= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR
&& val
<= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR
))
1926 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1927 props
->Reverb
.RoomRolloffFactor
= val
;
1931 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1934 void ALreverb_setParamfv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALfloat
*vals
)
1936 ALreverb_setParamf(effect
, context
, param
, vals
[0]);
1939 void ALreverb_getParami(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint
*val
)
1941 const ALeffectProps
*props
= &effect
->Props
;
1944 case AL_REVERB_DECAY_HFLIMIT
:
1945 *val
= props
->Reverb
.DecayHFLimit
;
1949 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1952 void ALreverb_getParamiv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint
*vals
)
1954 ALreverb_getParami(effect
, context
, param
, vals
);
1956 void ALreverb_getParamf(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*val
)
1958 const ALeffectProps
*props
= &effect
->Props
;
1961 case AL_REVERB_DENSITY
:
1962 *val
= props
->Reverb
.Density
;
1965 case AL_REVERB_DIFFUSION
:
1966 *val
= props
->Reverb
.Diffusion
;
1969 case AL_REVERB_GAIN
:
1970 *val
= props
->Reverb
.Gain
;
1973 case AL_REVERB_GAINHF
:
1974 *val
= props
->Reverb
.GainHF
;
1977 case AL_REVERB_DECAY_TIME
:
1978 *val
= props
->Reverb
.DecayTime
;
1981 case AL_REVERB_DECAY_HFRATIO
:
1982 *val
= props
->Reverb
.DecayHFRatio
;
1985 case AL_REVERB_REFLECTIONS_GAIN
:
1986 *val
= props
->Reverb
.ReflectionsGain
;
1989 case AL_REVERB_REFLECTIONS_DELAY
:
1990 *val
= props
->Reverb
.ReflectionsDelay
;
1993 case AL_REVERB_LATE_REVERB_GAIN
:
1994 *val
= props
->Reverb
.LateReverbGain
;
1997 case AL_REVERB_LATE_REVERB_DELAY
:
1998 *val
= props
->Reverb
.LateReverbDelay
;
2001 case AL_REVERB_AIR_ABSORPTION_GAINHF
:
2002 *val
= props
->Reverb
.AirAbsorptionGainHF
;
2005 case AL_REVERB_ROOM_ROLLOFF_FACTOR
:
2006 *val
= props
->Reverb
.RoomRolloffFactor
;
2010 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
2013 void ALreverb_getParamfv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*vals
)
2015 ALreverb_getParamf(effect
, context
, param
, vals
);
2018 DEFINE_ALEFFECT_VTABLE(ALreverb
);