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
->FmtChans
== DevFmtBFormat3D
|| Device
->AmbiDecoder
)
965 Update3DPanning(Device
, props
->Reverb
.ReflectionsPan
,
966 props
->Reverb
.LateReverbPan
, gain
,
967 props
->Reverb
.ReflectionsGain
,
968 props
->Reverb
.LateReverbGain
, State
);
970 UpdateDirectPanning(Device
, props
->Reverb
.ReflectionsPan
,
971 props
->Reverb
.LateReverbPan
, gain
,
972 props
->Reverb
.ReflectionsGain
,
973 props
->Reverb
.LateReverbGain
, State
);
977 /**************************************
978 * Effect Processing *
979 **************************************/
981 // Basic delay line input/output routines.
982 static inline ALfloat
DelayLineOut(DelayLine
*Delay
, ALuint offset
)
984 return Delay
->Line
[offset
&Delay
->Mask
];
987 static inline ALvoid
DelayLineIn(DelayLine
*Delay
, ALuint offset
, ALfloat in
)
989 Delay
->Line
[offset
&Delay
->Mask
] = in
;
992 // Given an input sample, this function produces modulation for the late
994 static inline ALfloat
EAXModulation(ALreverbState
*State
, ALuint offset
, ALfloat in
)
996 ALfloat sinus
, frac
, fdelay
;
1000 // Calculate the sinus rythm (dependent on modulation time and the
1001 // sampling rate). The center of the sinus is moved to reduce the delay
1002 // of the effect when the time or depth are low.
1003 sinus
= 1.0f
- cosf(F_TAU
* State
->Mod
.Index
/ State
->Mod
.Range
);
1005 // Step the modulation index forward, keeping it bound to its range.
1006 State
->Mod
.Index
= (State
->Mod
.Index
+ 1) % State
->Mod
.Range
;
1008 // The depth determines the range over which to read the input samples
1009 // from, so it must be filtered to reduce the distortion caused by even
1010 // small parameter changes.
1011 State
->Mod
.Filter
= lerp(State
->Mod
.Filter
, State
->Mod
.Depth
,
1014 // Calculate the read offset and fraction between it and the next sample.
1015 frac
= modff(State
->Mod
.Filter
*sinus
, &fdelay
);
1016 delay
= fastf2u(fdelay
);
1018 /* Add the incoming sample to the delay line first, so a 0 delay gets the
1021 DelayLineIn(&State
->Mod
.Delay
, offset
, in
);
1022 /* Get the two samples crossed by the offset delay */
1023 out0
= DelayLineOut(&State
->Mod
.Delay
, offset
- delay
);
1024 out1
= DelayLineOut(&State
->Mod
.Delay
, offset
- delay
- 1);
1026 // The output is obtained by linearly interpolating the two samples that
1027 // were acquired above.
1028 return lerp(out0
, out1
, frac
);
1031 // Given some input sample, this function produces four-channel outputs for the
1032 // early reflections.
1033 static inline ALvoid
EarlyReflection(ALreverbState
*State
, ALuint todo
, ALfloat (*restrict out
)[4])
1035 ALfloat d
[4], v
, f
[4];
1038 for(i
= 0;i
< todo
;i
++)
1040 ALuint offset
= State
->Offset
+i
;
1042 // Obtain the decayed results of each early delay line.
1043 d
[0] = DelayLineOut(&State
->Early
.Delay
[0], offset
-State
->Early
.Offset
[0]) * State
->Early
.Coeff
[0];
1044 d
[1] = DelayLineOut(&State
->Early
.Delay
[1], offset
-State
->Early
.Offset
[1]) * State
->Early
.Coeff
[1];
1045 d
[2] = DelayLineOut(&State
->Early
.Delay
[2], offset
-State
->Early
.Offset
[2]) * State
->Early
.Coeff
[2];
1046 d
[3] = DelayLineOut(&State
->Early
.Delay
[3], offset
-State
->Early
.Offset
[3]) * State
->Early
.Coeff
[3];
1048 /* The following uses a lossless scattering junction from waveguide
1049 * theory. It actually amounts to a householder mixing matrix, which
1050 * will produce a maximally diffuse response, and means this can
1051 * probably be considered a simple feed-back delay network (FDN).
1059 v
= (d
[0] + d
[1] + d
[2] + d
[3]) * 0.5f
;
1060 // The junction is loaded with the input here.
1061 v
+= DelayLineOut(&State
->Delay
, offset
-State
->DelayTap
[0]);
1063 // Calculate the feed values for the delay lines.
1069 // Re-feed the delay lines.
1070 DelayLineIn(&State
->Early
.Delay
[0], offset
, f
[0]);
1071 DelayLineIn(&State
->Early
.Delay
[1], offset
, f
[1]);
1072 DelayLineIn(&State
->Early
.Delay
[2], offset
, f
[2]);
1073 DelayLineIn(&State
->Early
.Delay
[3], offset
, f
[3]);
1075 /* Output the results of the junction for all four channels with a
1076 * constant attenuation of 0.5.
1078 out
[i
][0] = f
[0] * 0.5f
;
1079 out
[i
][1] = f
[1] * 0.5f
;
1080 out
[i
][2] = f
[2] * 0.5f
;
1081 out
[i
][3] = f
[3] * 0.5f
;
1085 // Basic attenuated all-pass input/output routine.
1086 static inline ALfloat
AllpassInOut(DelayLine
*Delay
, ALuint outOffset
, ALuint inOffset
, ALfloat in
, ALfloat feedCoeff
, ALfloat coeff
)
1090 out
= DelayLineOut(Delay
, outOffset
);
1091 feed
= feedCoeff
* in
;
1092 DelayLineIn(Delay
, inOffset
, (feedCoeff
* (out
- feed
)) + in
);
1094 // The time-based attenuation is only applied to the delay output to
1095 // keep it from affecting the feed-back path (which is already controlled
1096 // by the all-pass feed coefficient).
1097 return (coeff
* out
) - feed
;
1100 // All-pass input/output routine for late reverb.
1101 static inline ALfloat
LateAllPassInOut(ALreverbState
*State
, ALuint offset
, ALuint index
, ALfloat in
)
1103 return AllpassInOut(&State
->Late
.ApDelay
[index
],
1104 offset
- State
->Late
.ApOffset
[index
],
1105 offset
, in
, State
->Late
.ApFeedCoeff
,
1106 State
->Late
.ApCoeff
[index
]);
1109 // Low-pass filter input/output routine for late reverb.
1110 static inline ALfloat
LateLowPassInOut(ALreverbState
*State
, ALuint index
, ALfloat in
)
1112 in
= lerp(in
, State
->Late
.LpSample
[index
], State
->Late
.LpCoeff
[index
]);
1113 State
->Late
.LpSample
[index
] = in
;
1117 // Given four decorrelated input samples, this function produces four-channel
1118 // output for the late reverb.
1119 static inline ALvoid
LateReverb(ALreverbState
*State
, ALuint todo
, ALfloat (*restrict out
)[4])
1124 // Feed the decorrelator from the energy-attenuated output of the second
1126 for(i
= 0;i
< todo
;i
++)
1128 ALuint offset
= State
->Offset
+i
;
1129 ALfloat sample
= DelayLineOut(&State
->Delay
, offset
- State
->DelayTap
[1]) *
1130 State
->Late
.DensityGain
;
1131 DelayLineIn(&State
->Decorrelator
, offset
, sample
);
1134 for(i
= 0;i
< todo
;i
++)
1136 ALuint offset
= State
->Offset
+i
;
1138 /* Obtain four decorrelated input samples. */
1139 f
[0] = DelayLineOut(&State
->Decorrelator
, offset
);
1140 f
[1] = DelayLineOut(&State
->Decorrelator
, offset
-State
->DecoTap
[0]);
1141 f
[2] = DelayLineOut(&State
->Decorrelator
, offset
-State
->DecoTap
[1]);
1142 f
[3] = DelayLineOut(&State
->Decorrelator
, offset
-State
->DecoTap
[2]);
1144 /* Add the decayed results of the cyclical delay lines, then pass the
1145 * results through the low-pass filters.
1147 f
[0] += DelayLineOut(&State
->Late
.Delay
[0], offset
-State
->Late
.Offset
[0]) * State
->Late
.Coeff
[0];
1148 f
[1] += DelayLineOut(&State
->Late
.Delay
[1], offset
-State
->Late
.Offset
[1]) * State
->Late
.Coeff
[1];
1149 f
[2] += DelayLineOut(&State
->Late
.Delay
[2], offset
-State
->Late
.Offset
[2]) * State
->Late
.Coeff
[2];
1150 f
[3] += DelayLineOut(&State
->Late
.Delay
[3], offset
-State
->Late
.Offset
[3]) * State
->Late
.Coeff
[3];
1152 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and
1154 d
[0] = LateLowPassInOut(State
, 2, f
[2]);
1155 d
[1] = LateLowPassInOut(State
, 0, f
[0]);
1156 d
[2] = LateLowPassInOut(State
, 3, f
[3]);
1157 d
[3] = LateLowPassInOut(State
, 1, f
[1]);
1159 // To help increase diffusion, run each line through an all-pass filter.
1160 // When there is no diffusion, the shortest all-pass filter will feed
1161 // the shortest delay line.
1162 d
[0] = LateAllPassInOut(State
, offset
, 0, d
[0]);
1163 d
[1] = LateAllPassInOut(State
, offset
, 1, d
[1]);
1164 d
[2] = LateAllPassInOut(State
, offset
, 2, d
[2]);
1165 d
[3] = LateAllPassInOut(State
, offset
, 3, d
[3]);
1167 /* Late reverb is done with a modified feed-back delay network (FDN)
1168 * topology. Four input lines are each fed through their own all-pass
1169 * filter and then into the mixing matrix. The four outputs of the
1170 * mixing matrix are then cycled back to the inputs. Each output feeds
1171 * a different input to form a circlular feed cycle.
1173 * The mixing matrix used is a 4D skew-symmetric rotation matrix
1174 * derived using a single unitary rotational parameter:
1176 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
1181 * The rotation is constructed from the effect's diffusion parameter,
1182 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
1183 * with differing signs, and d is the coefficient x. The matrix is
1186 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
1187 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
1188 * [ y, -y, x, y ] x = cos(t)
1189 * [ -y, -y, -y, x ] y = sin(t) / n
1191 * To reduce the number of multiplies, the x coefficient is applied
1192 * with the cyclical delay line coefficients. Thus only the y
1193 * coefficient is applied when mixing, and is modified to be: y / x.
1195 f
[0] = d
[0] + (State
->Late
.MixCoeff
* ( d
[1] + -d
[2] + d
[3]));
1196 f
[1] = d
[1] + (State
->Late
.MixCoeff
* (-d
[0] + d
[2] + d
[3]));
1197 f
[2] = d
[2] + (State
->Late
.MixCoeff
* ( d
[0] + -d
[1] + d
[3]));
1198 f
[3] = d
[3] + (State
->Late
.MixCoeff
* (-d
[0] + -d
[1] + -d
[2] ));
1200 // Output the results of the matrix for all four channels, attenuated by
1201 // the late reverb gain (which is attenuated by the 'x' mix coefficient).
1202 out
[i
][0] = State
->Late
.Gain
* f
[0];
1203 out
[i
][1] = State
->Late
.Gain
* f
[1];
1204 out
[i
][2] = State
->Late
.Gain
* f
[2];
1205 out
[i
][3] = State
->Late
.Gain
* f
[3];
1207 // Re-feed the cyclical delay lines.
1208 DelayLineIn(&State
->Late
.Delay
[0], offset
, f
[0]);
1209 DelayLineIn(&State
->Late
.Delay
[1], offset
, f
[1]);
1210 DelayLineIn(&State
->Late
.Delay
[2], offset
, f
[2]);
1211 DelayLineIn(&State
->Late
.Delay
[3], offset
, f
[3]);
1215 // Given an input sample, this function mixes echo into the four-channel late
1217 static inline ALvoid
EAXEcho(ALreverbState
*State
, ALuint todo
, ALfloat (*restrict late
)[4])
1222 for(i
= 0;i
< todo
;i
++)
1224 ALuint offset
= State
->Offset
+i
;
1226 // Get the latest attenuated echo sample for output.
1227 feed
= DelayLineOut(&State
->Echo
.Delay
, offset
-State
->Echo
.Offset
) *
1230 // Mix the output into the late reverb channels.
1231 out
= State
->Echo
.MixCoeff
* feed
;
1237 // Mix the energy-attenuated input with the output and pass it through
1238 // the echo low-pass filter.
1239 feed
+= DelayLineOut(&State
->Delay
, offset
-State
->DelayTap
[1]) *
1240 State
->Echo
.DensityGain
;
1241 feed
= lerp(feed
, State
->Echo
.LpSample
, State
->Echo
.LpCoeff
);
1242 State
->Echo
.LpSample
= feed
;
1244 // Then the echo all-pass filter.
1245 feed
= AllpassInOut(&State
->Echo
.ApDelay
, offset
-State
->Echo
.ApOffset
,
1246 offset
, feed
, State
->Echo
.ApFeedCoeff
,
1247 State
->Echo
.ApCoeff
);
1249 // Feed the delay with the mixed and filtered sample.
1250 DelayLineIn(&State
->Echo
.Delay
, offset
, feed
);
1254 // Perform the non-EAX reverb pass on a given input sample, resulting in
1255 // four-channel output.
1256 static inline ALvoid
VerbPass(ALreverbState
*State
, ALuint todo
, const ALfloat
*in
, ALfloat (*restrict early
)[4], ALfloat (*restrict late
)[4])
1260 // Low-pass filter the incoming samples.
1261 for(i
= 0;i
< todo
;i
++)
1262 DelayLineIn(&State
->Delay
, State
->Offset
+i
,
1263 ALfilterState_processSingle(&State
->LpFilter
, in
[i
])
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.
1283 for(i
= 0;i
< todo
;i
++)
1285 ALfloat sample
= input
[i
];
1286 sample
= ALfilterState_processSingle(&State
->LpFilter
, sample
);
1287 sample
= ALfilterState_processSingle(&State
->HpFilter
, sample
);
1289 // Perform any modulation on the input.
1290 sample
= EAXModulation(State
, State
->Offset
+i
, sample
);
1292 // Feed the initial delay line.
1293 DelayLineIn(&State
->Delay
, State
->Offset
+i
, sample
);
1296 // Calculate the early reflection from the first delay tap.
1297 EarlyReflection(State
, todo
, early
);
1299 // Calculate the late reverb from the decorrelator taps.
1300 LateReverb(State
, todo
, late
);
1302 // Calculate and mix in any echo.
1303 EAXEcho(State
, todo
, late
);
1305 // Step all delays forward.
1306 State
->Offset
+= todo
;
1309 static ALvoid
ALreverbState_processStandard(ALreverbState
*State
, ALuint SamplesToDo
, const ALfloat
*restrict SamplesIn
, ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
)
1311 ALfloat (*restrict early
)[4] = State
->EarlySamples
;
1312 ALfloat (*restrict late
)[4] = State
->ReverbSamples
;
1313 ALuint index
, c
, i
, l
;
1316 /* Process reverb for these samples. */
1317 for(index
= 0;index
< SamplesToDo
;)
1319 ALuint todo
= minu(SamplesToDo
-index
, MAX_UPDATE_SAMPLES
);
1321 VerbPass(State
, todo
, &SamplesIn
[index
], early
, late
);
1323 for(l
= 0;l
< 4;l
++)
1325 for(c
= 0;c
< NumChannels
;c
++)
1327 gain
= State
->Early
.PanGain
[l
][c
];
1328 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1330 for(i
= 0;i
< todo
;i
++)
1331 SamplesOut
[c
][index
+i
] += gain
*early
[i
][l
];
1333 gain
= State
->Late
.PanGain
[l
][c
];
1334 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1336 for(i
= 0;i
< todo
;i
++)
1337 SamplesOut
[c
][index
+i
] += gain
*late
[i
][l
];
1340 for(c
= 0;c
< State
->ExtraChannels
;c
++)
1342 gain
= State
->Early
.PanGain
[l
][NumChannels
+c
];
1343 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1345 for(i
= 0;i
< todo
;i
++)
1346 State
->ExtraOut
[c
][index
+i
] += gain
*early
[i
][l
];
1348 gain
= State
->Late
.PanGain
[l
][NumChannels
+c
];
1349 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1351 for(i
= 0;i
< todo
;i
++)
1352 State
->ExtraOut
[c
][index
+i
] += gain
*late
[i
][l
];
1361 static ALvoid
ALreverbState_processEax(ALreverbState
*State
, ALuint SamplesToDo
, const ALfloat
*restrict SamplesIn
, ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
)
1363 ALfloat (*restrict early
)[4] = State
->EarlySamples
;
1364 ALfloat (*restrict late
)[4] = State
->ReverbSamples
;
1365 ALuint index
, c
, i
, l
;
1368 /* Process reverb for these samples. */
1369 for(index
= 0;index
< SamplesToDo
;)
1371 ALuint todo
= minu(SamplesToDo
-index
, MAX_UPDATE_SAMPLES
);
1373 EAXVerbPass(State
, todo
, &SamplesIn
[index
], early
, late
);
1375 for(l
= 0;l
< 4;l
++)
1377 for(c
= 0;c
< NumChannels
;c
++)
1379 gain
= State
->Early
.PanGain
[l
][c
];
1380 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1382 for(i
= 0;i
< todo
;i
++)
1383 SamplesOut
[c
][index
+i
] += gain
*early
[i
][l
];
1385 gain
= State
->Late
.PanGain
[l
][c
];
1386 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1388 for(i
= 0;i
< todo
;i
++)
1389 SamplesOut
[c
][index
+i
] += gain
*late
[i
][l
];
1392 for(c
= 0;c
< State
->ExtraChannels
;c
++)
1394 gain
= State
->Early
.PanGain
[l
][NumChannels
+c
];
1395 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1397 for(i
= 0;i
< todo
;i
++)
1398 State
->ExtraOut
[c
][index
+i
] += gain
*early
[i
][l
];
1400 gain
= State
->Late
.PanGain
[l
][NumChannels
+c
];
1401 if(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
)
1403 for(i
= 0;i
< todo
;i
++)
1404 State
->ExtraOut
[c
][index
+i
] += gain
*late
[i
][l
];
1413 static ALvoid
ALreverbState_process(ALreverbState
*State
, ALuint SamplesToDo
, const ALfloat (*restrict SamplesIn
)[BUFFERSIZE
], ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
)
1416 ALreverbState_processEax(State
, SamplesToDo
, SamplesIn
[0], SamplesOut
, NumChannels
);
1418 ALreverbState_processStandard(State
, SamplesToDo
, SamplesIn
[0], SamplesOut
, NumChannels
);
1422 typedef struct ALreverbStateFactory
{
1423 DERIVE_FROM_TYPE(ALeffectStateFactory
);
1424 } ALreverbStateFactory
;
1426 static ALeffectState
*ALreverbStateFactory_create(ALreverbStateFactory
* UNUSED(factory
))
1428 ALreverbState
*state
;
1431 state
= ALreverbState_New(sizeof(*state
));
1432 if(!state
) return NULL
;
1433 SET_VTABLE2(ALreverbState
, ALeffectState
, state
);
1435 state
->IsEax
= AL_FALSE
;
1436 state
->ExtraChannels
= 0;
1438 state
->TotalSamples
= 0;
1439 state
->SampleBuffer
= NULL
;
1441 ALfilterState_clear(&state
->LpFilter
);
1442 ALfilterState_clear(&state
->HpFilter
);
1444 state
->Mod
.Delay
.Mask
= 0;
1445 state
->Mod
.Delay
.Line
= NULL
;
1446 state
->Mod
.Index
= 0;
1447 state
->Mod
.Range
= 1;
1448 state
->Mod
.Depth
= 0.0f
;
1449 state
->Mod
.Coeff
= 0.0f
;
1450 state
->Mod
.Filter
= 0.0f
;
1452 state
->Delay
.Mask
= 0;
1453 state
->Delay
.Line
= NULL
;
1454 state
->DelayTap
[0] = 0;
1455 state
->DelayTap
[1] = 0;
1457 for(index
= 0;index
< 4;index
++)
1459 state
->Early
.Coeff
[index
] = 0.0f
;
1460 state
->Early
.Delay
[index
].Mask
= 0;
1461 state
->Early
.Delay
[index
].Line
= NULL
;
1462 state
->Early
.Offset
[index
] = 0;
1465 state
->Decorrelator
.Mask
= 0;
1466 state
->Decorrelator
.Line
= NULL
;
1467 state
->DecoTap
[0] = 0;
1468 state
->DecoTap
[1] = 0;
1469 state
->DecoTap
[2] = 0;
1471 state
->Late
.Gain
= 0.0f
;
1472 state
->Late
.DensityGain
= 0.0f
;
1473 state
->Late
.ApFeedCoeff
= 0.0f
;
1474 state
->Late
.MixCoeff
= 0.0f
;
1475 for(index
= 0;index
< 4;index
++)
1477 state
->Late
.ApCoeff
[index
] = 0.0f
;
1478 state
->Late
.ApDelay
[index
].Mask
= 0;
1479 state
->Late
.ApDelay
[index
].Line
= NULL
;
1480 state
->Late
.ApOffset
[index
] = 0;
1482 state
->Late
.Coeff
[index
] = 0.0f
;
1483 state
->Late
.Delay
[index
].Mask
= 0;
1484 state
->Late
.Delay
[index
].Line
= NULL
;
1485 state
->Late
.Offset
[index
] = 0;
1487 state
->Late
.LpCoeff
[index
] = 0.0f
;
1488 state
->Late
.LpSample
[index
] = 0.0f
;
1491 for(l
= 0;l
< 4;l
++)
1493 for(index
= 0;index
< MAX_OUTPUT_CHANNELS
;index
++)
1495 state
->Early
.PanGain
[l
][index
] = 0.0f
;
1496 state
->Late
.PanGain
[l
][index
] = 0.0f
;
1500 state
->Echo
.DensityGain
= 0.0f
;
1501 state
->Echo
.Delay
.Mask
= 0;
1502 state
->Echo
.Delay
.Line
= NULL
;
1503 state
->Echo
.ApDelay
.Mask
= 0;
1504 state
->Echo
.ApDelay
.Line
= NULL
;
1505 state
->Echo
.Coeff
= 0.0f
;
1506 state
->Echo
.ApFeedCoeff
= 0.0f
;
1507 state
->Echo
.ApCoeff
= 0.0f
;
1508 state
->Echo
.Offset
= 0;
1509 state
->Echo
.ApOffset
= 0;
1510 state
->Echo
.LpCoeff
= 0.0f
;
1511 state
->Echo
.LpSample
= 0.0f
;
1512 state
->Echo
.MixCoeff
= 0.0f
;
1516 return STATIC_CAST(ALeffectState
, state
);
1519 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALreverbStateFactory
);
1521 ALeffectStateFactory
*ALreverbStateFactory_getFactory(void)
1523 static ALreverbStateFactory ReverbFactory
= { { GET_VTABLE2(ALreverbStateFactory
, ALeffectStateFactory
) } };
1525 return STATIC_CAST(ALeffectStateFactory
, &ReverbFactory
);
1529 void ALeaxreverb_setParami(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint val
)
1531 ALeffectProps
*props
= &effect
->Props
;
1534 case AL_EAXREVERB_DECAY_HFLIMIT
:
1535 if(!(val
>= AL_EAXREVERB_MIN_DECAY_HFLIMIT
&& val
<= AL_EAXREVERB_MAX_DECAY_HFLIMIT
))
1536 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1537 props
->Reverb
.DecayHFLimit
= val
;
1541 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1544 void ALeaxreverb_setParamiv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALint
*vals
)
1546 ALeaxreverb_setParami(effect
, context
, param
, vals
[0]);
1548 void ALeaxreverb_setParamf(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat val
)
1550 ALeffectProps
*props
= &effect
->Props
;
1553 case AL_EAXREVERB_DENSITY
:
1554 if(!(val
>= AL_EAXREVERB_MIN_DENSITY
&& val
<= AL_EAXREVERB_MAX_DENSITY
))
1555 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1556 props
->Reverb
.Density
= val
;
1559 case AL_EAXREVERB_DIFFUSION
:
1560 if(!(val
>= AL_EAXREVERB_MIN_DIFFUSION
&& val
<= AL_EAXREVERB_MAX_DIFFUSION
))
1561 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1562 props
->Reverb
.Diffusion
= val
;
1565 case AL_EAXREVERB_GAIN
:
1566 if(!(val
>= AL_EAXREVERB_MIN_GAIN
&& val
<= AL_EAXREVERB_MAX_GAIN
))
1567 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1568 props
->Reverb
.Gain
= val
;
1571 case AL_EAXREVERB_GAINHF
:
1572 if(!(val
>= AL_EAXREVERB_MIN_GAINHF
&& val
<= AL_EAXREVERB_MAX_GAINHF
))
1573 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1574 props
->Reverb
.GainHF
= val
;
1577 case AL_EAXREVERB_GAINLF
:
1578 if(!(val
>= AL_EAXREVERB_MIN_GAINLF
&& val
<= AL_EAXREVERB_MAX_GAINLF
))
1579 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1580 props
->Reverb
.GainLF
= val
;
1583 case AL_EAXREVERB_DECAY_TIME
:
1584 if(!(val
>= AL_EAXREVERB_MIN_DECAY_TIME
&& val
<= AL_EAXREVERB_MAX_DECAY_TIME
))
1585 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1586 props
->Reverb
.DecayTime
= val
;
1589 case AL_EAXREVERB_DECAY_HFRATIO
:
1590 if(!(val
>= AL_EAXREVERB_MIN_DECAY_HFRATIO
&& val
<= AL_EAXREVERB_MAX_DECAY_HFRATIO
))
1591 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1592 props
->Reverb
.DecayHFRatio
= val
;
1595 case AL_EAXREVERB_DECAY_LFRATIO
:
1596 if(!(val
>= AL_EAXREVERB_MIN_DECAY_LFRATIO
&& val
<= AL_EAXREVERB_MAX_DECAY_LFRATIO
))
1597 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1598 props
->Reverb
.DecayLFRatio
= val
;
1601 case AL_EAXREVERB_REFLECTIONS_GAIN
:
1602 if(!(val
>= AL_EAXREVERB_MIN_REFLECTIONS_GAIN
&& val
<= AL_EAXREVERB_MAX_REFLECTIONS_GAIN
))
1603 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1604 props
->Reverb
.ReflectionsGain
= val
;
1607 case AL_EAXREVERB_REFLECTIONS_DELAY
:
1608 if(!(val
>= AL_EAXREVERB_MIN_REFLECTIONS_DELAY
&& val
<= AL_EAXREVERB_MAX_REFLECTIONS_DELAY
))
1609 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1610 props
->Reverb
.ReflectionsDelay
= val
;
1613 case AL_EAXREVERB_LATE_REVERB_GAIN
:
1614 if(!(val
>= AL_EAXREVERB_MIN_LATE_REVERB_GAIN
&& val
<= AL_EAXREVERB_MAX_LATE_REVERB_GAIN
))
1615 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1616 props
->Reverb
.LateReverbGain
= val
;
1619 case AL_EAXREVERB_LATE_REVERB_DELAY
:
1620 if(!(val
>= AL_EAXREVERB_MIN_LATE_REVERB_DELAY
&& val
<= AL_EAXREVERB_MAX_LATE_REVERB_DELAY
))
1621 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1622 props
->Reverb
.LateReverbDelay
= val
;
1625 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF
:
1626 if(!(val
>= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF
&& val
<= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF
))
1627 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1628 props
->Reverb
.AirAbsorptionGainHF
= val
;
1631 case AL_EAXREVERB_ECHO_TIME
:
1632 if(!(val
>= AL_EAXREVERB_MIN_ECHO_TIME
&& val
<= AL_EAXREVERB_MAX_ECHO_TIME
))
1633 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1634 props
->Reverb
.EchoTime
= val
;
1637 case AL_EAXREVERB_ECHO_DEPTH
:
1638 if(!(val
>= AL_EAXREVERB_MIN_ECHO_DEPTH
&& val
<= AL_EAXREVERB_MAX_ECHO_DEPTH
))
1639 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1640 props
->Reverb
.EchoDepth
= val
;
1643 case AL_EAXREVERB_MODULATION_TIME
:
1644 if(!(val
>= AL_EAXREVERB_MIN_MODULATION_TIME
&& val
<= AL_EAXREVERB_MAX_MODULATION_TIME
))
1645 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1646 props
->Reverb
.ModulationTime
= val
;
1649 case AL_EAXREVERB_MODULATION_DEPTH
:
1650 if(!(val
>= AL_EAXREVERB_MIN_MODULATION_DEPTH
&& val
<= AL_EAXREVERB_MAX_MODULATION_DEPTH
))
1651 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1652 props
->Reverb
.ModulationDepth
= val
;
1655 case AL_EAXREVERB_HFREFERENCE
:
1656 if(!(val
>= AL_EAXREVERB_MIN_HFREFERENCE
&& val
<= AL_EAXREVERB_MAX_HFREFERENCE
))
1657 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1658 props
->Reverb
.HFReference
= val
;
1661 case AL_EAXREVERB_LFREFERENCE
:
1662 if(!(val
>= AL_EAXREVERB_MIN_LFREFERENCE
&& val
<= AL_EAXREVERB_MAX_LFREFERENCE
))
1663 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1664 props
->Reverb
.LFReference
= val
;
1667 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR
:
1668 if(!(val
>= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR
&& val
<= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR
))
1669 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1670 props
->Reverb
.RoomRolloffFactor
= val
;
1674 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1677 void ALeaxreverb_setParamfv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALfloat
*vals
)
1679 ALeffectProps
*props
= &effect
->Props
;
1682 case AL_EAXREVERB_REFLECTIONS_PAN
:
1683 if(!(isfinite(vals
[0]) && isfinite(vals
[1]) && isfinite(vals
[2])))
1684 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1685 props
->Reverb
.ReflectionsPan
[0] = vals
[0];
1686 props
->Reverb
.ReflectionsPan
[1] = vals
[1];
1687 props
->Reverb
.ReflectionsPan
[2] = vals
[2];
1689 case AL_EAXREVERB_LATE_REVERB_PAN
:
1690 if(!(isfinite(vals
[0]) && isfinite(vals
[1]) && isfinite(vals
[2])))
1691 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1692 props
->Reverb
.LateReverbPan
[0] = vals
[0];
1693 props
->Reverb
.LateReverbPan
[1] = vals
[1];
1694 props
->Reverb
.LateReverbPan
[2] = vals
[2];
1698 ALeaxreverb_setParamf(effect
, context
, param
, vals
[0]);
1703 void ALeaxreverb_getParami(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint
*val
)
1705 const ALeffectProps
*props
= &effect
->Props
;
1708 case AL_EAXREVERB_DECAY_HFLIMIT
:
1709 *val
= props
->Reverb
.DecayHFLimit
;
1713 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1716 void ALeaxreverb_getParamiv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint
*vals
)
1718 ALeaxreverb_getParami(effect
, context
, param
, vals
);
1720 void ALeaxreverb_getParamf(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*val
)
1722 const ALeffectProps
*props
= &effect
->Props
;
1725 case AL_EAXREVERB_DENSITY
:
1726 *val
= props
->Reverb
.Density
;
1729 case AL_EAXREVERB_DIFFUSION
:
1730 *val
= props
->Reverb
.Diffusion
;
1733 case AL_EAXREVERB_GAIN
:
1734 *val
= props
->Reverb
.Gain
;
1737 case AL_EAXREVERB_GAINHF
:
1738 *val
= props
->Reverb
.GainHF
;
1741 case AL_EAXREVERB_GAINLF
:
1742 *val
= props
->Reverb
.GainLF
;
1745 case AL_EAXREVERB_DECAY_TIME
:
1746 *val
= props
->Reverb
.DecayTime
;
1749 case AL_EAXREVERB_DECAY_HFRATIO
:
1750 *val
= props
->Reverb
.DecayHFRatio
;
1753 case AL_EAXREVERB_DECAY_LFRATIO
:
1754 *val
= props
->Reverb
.DecayLFRatio
;
1757 case AL_EAXREVERB_REFLECTIONS_GAIN
:
1758 *val
= props
->Reverb
.ReflectionsGain
;
1761 case AL_EAXREVERB_REFLECTIONS_DELAY
:
1762 *val
= props
->Reverb
.ReflectionsDelay
;
1765 case AL_EAXREVERB_LATE_REVERB_GAIN
:
1766 *val
= props
->Reverb
.LateReverbGain
;
1769 case AL_EAXREVERB_LATE_REVERB_DELAY
:
1770 *val
= props
->Reverb
.LateReverbDelay
;
1773 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF
:
1774 *val
= props
->Reverb
.AirAbsorptionGainHF
;
1777 case AL_EAXREVERB_ECHO_TIME
:
1778 *val
= props
->Reverb
.EchoTime
;
1781 case AL_EAXREVERB_ECHO_DEPTH
:
1782 *val
= props
->Reverb
.EchoDepth
;
1785 case AL_EAXREVERB_MODULATION_TIME
:
1786 *val
= props
->Reverb
.ModulationTime
;
1789 case AL_EAXREVERB_MODULATION_DEPTH
:
1790 *val
= props
->Reverb
.ModulationDepth
;
1793 case AL_EAXREVERB_HFREFERENCE
:
1794 *val
= props
->Reverb
.HFReference
;
1797 case AL_EAXREVERB_LFREFERENCE
:
1798 *val
= props
->Reverb
.LFReference
;
1801 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR
:
1802 *val
= props
->Reverb
.RoomRolloffFactor
;
1806 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1809 void ALeaxreverb_getParamfv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*vals
)
1811 const ALeffectProps
*props
= &effect
->Props
;
1814 case AL_EAXREVERB_REFLECTIONS_PAN
:
1815 vals
[0] = props
->Reverb
.ReflectionsPan
[0];
1816 vals
[1] = props
->Reverb
.ReflectionsPan
[1];
1817 vals
[2] = props
->Reverb
.ReflectionsPan
[2];
1819 case AL_EAXREVERB_LATE_REVERB_PAN
:
1820 vals
[0] = props
->Reverb
.LateReverbPan
[0];
1821 vals
[1] = props
->Reverb
.LateReverbPan
[1];
1822 vals
[2] = props
->Reverb
.LateReverbPan
[2];
1826 ALeaxreverb_getParamf(effect
, context
, param
, vals
);
1831 DEFINE_ALEFFECT_VTABLE(ALeaxreverb
);
1833 void ALreverb_setParami(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint val
)
1835 ALeffectProps
*props
= &effect
->Props
;
1838 case AL_REVERB_DECAY_HFLIMIT
:
1839 if(!(val
>= AL_REVERB_MIN_DECAY_HFLIMIT
&& val
<= AL_REVERB_MAX_DECAY_HFLIMIT
))
1840 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1841 props
->Reverb
.DecayHFLimit
= val
;
1845 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1848 void ALreverb_setParamiv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALint
*vals
)
1850 ALreverb_setParami(effect
, context
, param
, vals
[0]);
1852 void ALreverb_setParamf(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat val
)
1854 ALeffectProps
*props
= &effect
->Props
;
1857 case AL_REVERB_DENSITY
:
1858 if(!(val
>= AL_REVERB_MIN_DENSITY
&& val
<= AL_REVERB_MAX_DENSITY
))
1859 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1860 props
->Reverb
.Density
= val
;
1863 case AL_REVERB_DIFFUSION
:
1864 if(!(val
>= AL_REVERB_MIN_DIFFUSION
&& val
<= AL_REVERB_MAX_DIFFUSION
))
1865 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1866 props
->Reverb
.Diffusion
= val
;
1869 case AL_REVERB_GAIN
:
1870 if(!(val
>= AL_REVERB_MIN_GAIN
&& val
<= AL_REVERB_MAX_GAIN
))
1871 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1872 props
->Reverb
.Gain
= val
;
1875 case AL_REVERB_GAINHF
:
1876 if(!(val
>= AL_REVERB_MIN_GAINHF
&& val
<= AL_REVERB_MAX_GAINHF
))
1877 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1878 props
->Reverb
.GainHF
= val
;
1881 case AL_REVERB_DECAY_TIME
:
1882 if(!(val
>= AL_REVERB_MIN_DECAY_TIME
&& val
<= AL_REVERB_MAX_DECAY_TIME
))
1883 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1884 props
->Reverb
.DecayTime
= val
;
1887 case AL_REVERB_DECAY_HFRATIO
:
1888 if(!(val
>= AL_REVERB_MIN_DECAY_HFRATIO
&& val
<= AL_REVERB_MAX_DECAY_HFRATIO
))
1889 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1890 props
->Reverb
.DecayHFRatio
= val
;
1893 case AL_REVERB_REFLECTIONS_GAIN
:
1894 if(!(val
>= AL_REVERB_MIN_REFLECTIONS_GAIN
&& val
<= AL_REVERB_MAX_REFLECTIONS_GAIN
))
1895 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1896 props
->Reverb
.ReflectionsGain
= val
;
1899 case AL_REVERB_REFLECTIONS_DELAY
:
1900 if(!(val
>= AL_REVERB_MIN_REFLECTIONS_DELAY
&& val
<= AL_REVERB_MAX_REFLECTIONS_DELAY
))
1901 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1902 props
->Reverb
.ReflectionsDelay
= val
;
1905 case AL_REVERB_LATE_REVERB_GAIN
:
1906 if(!(val
>= AL_REVERB_MIN_LATE_REVERB_GAIN
&& val
<= AL_REVERB_MAX_LATE_REVERB_GAIN
))
1907 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1908 props
->Reverb
.LateReverbGain
= val
;
1911 case AL_REVERB_LATE_REVERB_DELAY
:
1912 if(!(val
>= AL_REVERB_MIN_LATE_REVERB_DELAY
&& val
<= AL_REVERB_MAX_LATE_REVERB_DELAY
))
1913 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1914 props
->Reverb
.LateReverbDelay
= val
;
1917 case AL_REVERB_AIR_ABSORPTION_GAINHF
:
1918 if(!(val
>= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF
&& val
<= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF
))
1919 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1920 props
->Reverb
.AirAbsorptionGainHF
= val
;
1923 case AL_REVERB_ROOM_ROLLOFF_FACTOR
:
1924 if(!(val
>= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR
&& val
<= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR
))
1925 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
1926 props
->Reverb
.RoomRolloffFactor
= val
;
1930 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1933 void ALreverb_setParamfv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALfloat
*vals
)
1935 ALreverb_setParamf(effect
, context
, param
, vals
[0]);
1938 void ALreverb_getParami(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint
*val
)
1940 const ALeffectProps
*props
= &effect
->Props
;
1943 case AL_REVERB_DECAY_HFLIMIT
:
1944 *val
= props
->Reverb
.DecayHFLimit
;
1948 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
1951 void ALreverb_getParamiv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint
*vals
)
1953 ALreverb_getParami(effect
, context
, param
, vals
);
1955 void ALreverb_getParamf(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*val
)
1957 const ALeffectProps
*props
= &effect
->Props
;
1960 case AL_REVERB_DENSITY
:
1961 *val
= props
->Reverb
.Density
;
1964 case AL_REVERB_DIFFUSION
:
1965 *val
= props
->Reverb
.Diffusion
;
1968 case AL_REVERB_GAIN
:
1969 *val
= props
->Reverb
.Gain
;
1972 case AL_REVERB_GAINHF
:
1973 *val
= props
->Reverb
.GainHF
;
1976 case AL_REVERB_DECAY_TIME
:
1977 *val
= props
->Reverb
.DecayTime
;
1980 case AL_REVERB_DECAY_HFRATIO
:
1981 *val
= props
->Reverb
.DecayHFRatio
;
1984 case AL_REVERB_REFLECTIONS_GAIN
:
1985 *val
= props
->Reverb
.ReflectionsGain
;
1988 case AL_REVERB_REFLECTIONS_DELAY
:
1989 *val
= props
->Reverb
.ReflectionsDelay
;
1992 case AL_REVERB_LATE_REVERB_GAIN
:
1993 *val
= props
->Reverb
.LateReverbGain
;
1996 case AL_REVERB_LATE_REVERB_DELAY
:
1997 *val
= props
->Reverb
.LateReverbDelay
;
2000 case AL_REVERB_AIR_ABSORPTION_GAINHF
:
2001 *val
= props
->Reverb
.AirAbsorptionGainHF
;
2004 case AL_REVERB_ROOM_ROLLOFF_FACTOR
:
2005 *val
= props
->Reverb
.RoomRolloffFactor
;
2009 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
2012 void ALreverb_getParamfv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*vals
)
2014 ALreverb_getParamf(effect
, context
, param
, vals
);
2017 DEFINE_ALEFFECT_VTABLE(ALreverb
);