Combine the reverb decorrelator delay line with the main delay line
[openal-soft.git] / Alc / effects / reverb.c
blob7068e0772f789556c04900e9e6dc696c98c93cb5
1 /**
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
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
27 #include "alMain.h"
28 #include "alu.h"
29 #include "alAuxEffectSlot.h"
30 #include "alEffect.h"
31 #include "alFilter.h"
32 #include "alError.h"
33 #include "mixer_defs.h"
36 /* This is the maximum number of samples processed for each inner loop
37 * iteration. */
38 #define MAX_UPDATE_SAMPLES 256
41 static MixerFunc MixSamples = Mix_C;
43 static alonce_flag mixfunc_inited = AL_ONCE_FLAG_INIT;
44 static void init_mixfunc(void)
46 MixSamples = SelectMixer();
50 typedef struct DelayLine
52 // The delay lines use sample lengths that are powers of 2 to allow the
53 // use of bit-masking instead of a modulus for wrapping.
54 ALuint Mask;
55 ALfloat *Line;
56 } DelayLine;
58 typedef struct ALreverbState {
59 DERIVE_FROM_TYPE(ALeffectState);
61 ALboolean IsEax;
63 // For HRTF and UHJ
64 ALfloat (*ExtraOut)[BUFFERSIZE];
65 ALuint ExtraChannels;
67 // All delay lines are allocated as a single buffer to reduce memory
68 // fragmentation and management code.
69 ALfloat *SampleBuffer;
70 ALuint TotalSamples;
72 // Master effect filters
73 ALfilterState LpFilter;
74 ALfilterState HpFilter; // EAX only
76 struct {
77 // Modulator delay line.
78 DelayLine Delay;
80 // The vibrato time is tracked with an index over a modulus-wrapped
81 // range (in samples).
82 ALuint Index;
83 ALuint Range;
85 // The depth of frequency change (also in samples) and its filter.
86 ALfloat Depth;
87 ALfloat Coeff;
88 ALfloat Filter;
89 } Mod; // EAX only
91 /* Core delay line (early reflections and late reverb tap from this). */
92 DelayLine Delay;
93 /* The tap points for the initial delay. First tap goes to early
94 * reflections, second to late reverb.
96 ALuint DelayTap[2];
97 /* There are actually 4 decorrelator taps, but the first occurs at the late
98 * reverb tap.
100 ALuint DecoTap[3];
102 struct {
103 // Early reflections are done with 4 delay lines.
104 ALfloat Coeff[4];
105 DelayLine Delay[4];
106 ALuint Offset[4];
108 // The gain for each output channel based on 3D panning.
109 ALfloat CurrentGain[4][MAX_OUTPUT_CHANNELS+2];
110 ALfloat PanGain[4][MAX_OUTPUT_CHANNELS+2];
111 } Early;
113 struct {
114 // Output gain for late reverb.
115 ALfloat Gain;
117 // Attenuation to compensate for the modal density and decay rate of
118 // the late lines.
119 ALfloat DensityGain;
121 // The feed-back and feed-forward all-pass coefficient.
122 ALfloat ApFeedCoeff;
124 // Mixing matrix coefficient.
125 ALfloat MixCoeff;
127 // Late reverb has 4 parallel all-pass filters.
128 ALfloat ApCoeff[4];
129 DelayLine ApDelay[4];
130 ALuint ApOffset[4];
132 // In addition to 4 cyclical delay lines.
133 ALfloat Coeff[4];
134 DelayLine Delay[4];
135 ALuint Offset[4];
137 // The cyclical delay lines are 1-pole low-pass filtered.
138 ALfloat LpCoeff[4];
139 ALfloat LpSample[4];
141 // The gain for each output channel based on 3D panning.
142 ALfloat CurrentGain[4][MAX_OUTPUT_CHANNELS+2];
143 ALfloat PanGain[4][MAX_OUTPUT_CHANNELS+2];
144 } Late;
146 struct {
147 // Attenuation to compensate for the modal density and decay rate of
148 // the echo line.
149 ALfloat DensityGain;
151 // Echo delay and all-pass lines.
152 DelayLine Delay;
153 DelayLine ApDelay;
155 ALfloat Coeff;
156 ALfloat ApFeedCoeff;
157 ALfloat ApCoeff;
159 ALuint Offset;
160 ALuint ApOffset;
162 // The echo line is 1-pole low-pass filtered.
163 ALfloat LpCoeff;
164 ALfloat LpSample;
166 // Echo mixing coefficient.
167 ALfloat MixCoeff;
168 } Echo; // EAX only
170 // The current read offset for all delay lines.
171 ALuint Offset;
173 /* Temporary storage used when processing. */
174 alignas(16) ALfloat ReverbSamples[4][MAX_UPDATE_SAMPLES];
175 alignas(16) ALfloat EarlySamples[4][MAX_UPDATE_SAMPLES];
176 } ALreverbState;
178 static ALvoid ALreverbState_Destruct(ALreverbState *State);
179 static ALboolean ALreverbState_deviceUpdate(ALreverbState *State, ALCdevice *Device);
180 static ALvoid ALreverbState_update(ALreverbState *State, const ALCdevice *Device, const ALeffectslot *Slot, const ALeffectProps *props);
181 static ALvoid ALreverbState_process(ALreverbState *State, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
182 DECLARE_DEFAULT_ALLOCATORS(ALreverbState)
184 DEFINE_ALEFFECTSTATE_VTABLE(ALreverbState);
187 static void ALreverbState_Construct(ALreverbState *state)
189 ALuint index, l;
191 ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
192 SET_VTABLE2(ALreverbState, ALeffectState, state);
194 state->IsEax = AL_FALSE;
195 state->ExtraChannels = 0;
197 state->TotalSamples = 0;
198 state->SampleBuffer = NULL;
200 ALfilterState_clear(&state->LpFilter);
201 ALfilterState_clear(&state->HpFilter);
203 state->Mod.Delay.Mask = 0;
204 state->Mod.Delay.Line = NULL;
205 state->Mod.Index = 0;
206 state->Mod.Range = 1;
207 state->Mod.Depth = 0.0f;
208 state->Mod.Coeff = 0.0f;
209 state->Mod.Filter = 0.0f;
211 state->Delay.Mask = 0;
212 state->Delay.Line = NULL;
213 state->DelayTap[0] = 0;
214 state->DelayTap[1] = 0;
215 state->DecoTap[0] = 0;
216 state->DecoTap[1] = 0;
217 state->DecoTap[2] = 0;
219 for(index = 0;index < 4;index++)
221 state->Early.Coeff[index] = 0.0f;
222 state->Early.Delay[index].Mask = 0;
223 state->Early.Delay[index].Line = NULL;
224 state->Early.Offset[index] = 0;
227 state->Late.Gain = 0.0f;
228 state->Late.DensityGain = 0.0f;
229 state->Late.ApFeedCoeff = 0.0f;
230 state->Late.MixCoeff = 0.0f;
231 for(index = 0;index < 4;index++)
233 state->Late.ApCoeff[index] = 0.0f;
234 state->Late.ApDelay[index].Mask = 0;
235 state->Late.ApDelay[index].Line = NULL;
236 state->Late.ApOffset[index] = 0;
238 state->Late.Coeff[index] = 0.0f;
239 state->Late.Delay[index].Mask = 0;
240 state->Late.Delay[index].Line = NULL;
241 state->Late.Offset[index] = 0;
243 state->Late.LpCoeff[index] = 0.0f;
244 state->Late.LpSample[index] = 0.0f;
247 for(l = 0;l < 4;l++)
249 for(index = 0;index < MAX_OUTPUT_CHANNELS;index++)
251 state->Early.PanGain[l][index] = 0.0f;
252 state->Late.PanGain[l][index] = 0.0f;
256 state->Echo.DensityGain = 0.0f;
257 state->Echo.Delay.Mask = 0;
258 state->Echo.Delay.Line = NULL;
259 state->Echo.ApDelay.Mask = 0;
260 state->Echo.ApDelay.Line = NULL;
261 state->Echo.Coeff = 0.0f;
262 state->Echo.ApFeedCoeff = 0.0f;
263 state->Echo.ApCoeff = 0.0f;
264 state->Echo.Offset = 0;
265 state->Echo.ApOffset = 0;
266 state->Echo.LpCoeff = 0.0f;
267 state->Echo.LpSample = 0.0f;
268 state->Echo.MixCoeff = 0.0f;
270 state->Offset = 0;
273 static ALvoid ALreverbState_Destruct(ALreverbState *State)
275 al_free(State->SampleBuffer);
276 State->SampleBuffer = NULL;
278 ALeffectState_Destruct(STATIC_CAST(ALeffectState,State));
281 /* This is a user config option for modifying the overall output of the reverb
282 * effect.
284 ALfloat ReverbBoost = 1.0f;
286 /* Specifies whether to use a standard reverb effect in place of EAX reverb (no
287 * high-pass, modulation, or echo).
289 ALboolean EmulateEAXReverb = AL_FALSE;
291 /* This coefficient is used to define the maximum frequency range controlled
292 * by the modulation depth. The current value of 0.1 will allow it to swing
293 * from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the
294 * sampler to stall on the downswing, and above 1 it will cause it to sample
295 * backwards.
297 static const ALfloat MODULATION_DEPTH_COEFF = 0.1f;
299 /* A filter is used to avoid the terrible distortion caused by changing
300 * modulation time and/or depth. To be consistent across different sample
301 * rates, the coefficient must be raised to a constant divided by the sample
302 * rate: coeff^(constant / rate).
304 static const ALfloat MODULATION_FILTER_COEFF = 0.048f;
305 static const ALfloat MODULATION_FILTER_CONST = 100000.0f;
307 // When diffusion is above 0, an all-pass filter is used to take the edge off
308 // the echo effect. It uses the following line length (in seconds).
309 static const ALfloat ECHO_ALLPASS_LENGTH = 0.0133f;
311 // Input into the late reverb is decorrelated between four channels. Their
312 // timings are dependent on a fraction and multiplier. See the
313 // UpdateDecorrelator() routine for the calculations involved.
314 static const ALfloat DECO_FRACTION = 0.15f;
315 static const ALfloat DECO_MULTIPLIER = 2.0f;
317 // All delay line lengths are specified in seconds.
319 // The lengths of the early delay lines.
320 static const ALfloat EARLY_LINE_LENGTH[4] =
322 0.0015f, 0.0045f, 0.0135f, 0.0405f
325 // The lengths of the late all-pass delay lines.
326 static const ALfloat ALLPASS_LINE_LENGTH[4] =
328 0.0151f, 0.0167f, 0.0183f, 0.0200f,
331 // The lengths of the late cyclical delay lines.
332 static const ALfloat LATE_LINE_LENGTH[4] =
334 0.0211f, 0.0311f, 0.0461f, 0.0680f
337 // The late cyclical delay lines have a variable length dependent on the
338 // effect's density parameter (inverted for some reason) and this multiplier.
339 static const ALfloat LATE_LINE_MULTIPLIER = 4.0f;
342 #if defined(_WIN32) && !defined (_M_X64) && !defined(_M_ARM)
343 /* HACK: Workaround for a modff bug in 32-bit Windows, which attempts to write
344 * a 64-bit double to the 32-bit float parameter.
346 static inline float hack_modff(float x, float *y)
348 double di;
349 double df = modf((double)x, &di);
350 *y = (float)di;
351 return (float)df;
353 #define modff hack_modff
354 #endif
357 /**************************************
358 * Device Update *
359 **************************************/
361 // Given the allocated sample buffer, this function updates each delay line
362 // offset.
363 static inline ALvoid RealizeLineOffset(ALfloat *sampleBuffer, DelayLine *Delay)
365 Delay->Line = &sampleBuffer[(ptrdiff_t)Delay->Line];
368 // Calculate the length of a delay line and store its mask and offset.
369 static ALuint CalcLineLength(ALfloat length, ptrdiff_t offset, ALuint frequency, ALuint extra, DelayLine *Delay)
371 ALuint samples;
373 // All line lengths are powers of 2, calculated from their lengths, with
374 // an additional sample in case of rounding errors.
375 samples = fastf2u(length*frequency) + extra;
376 samples = NextPowerOf2(samples + 1);
377 // All lines share a single sample buffer.
378 Delay->Mask = samples - 1;
379 Delay->Line = (ALfloat*)offset;
380 // Return the sample count for accumulation.
381 return samples;
384 /* Calculates the delay line metrics and allocates the shared sample buffer
385 * for all lines given the sample rate (frequency). If an allocation failure
386 * occurs, it returns AL_FALSE.
388 static ALboolean AllocLines(ALuint frequency, ALreverbState *State)
390 ALuint totalSamples, index;
391 ALfloat length;
393 // All delay line lengths are calculated to accomodate the full range of
394 // lengths given their respective paramters.
395 totalSamples = 0;
397 /* The modulator's line length is calculated from the maximum modulation
398 * time and depth coefficient, and halfed for the low-to-high frequency
399 * swing. An additional sample is added to keep it stable when there is no
400 * modulation.
402 length = (AL_EAXREVERB_MAX_MODULATION_TIME*MODULATION_DEPTH_COEFF/2.0f);
403 totalSamples += CalcLineLength(length, totalSamples, frequency, 1,
404 &State->Mod.Delay);
406 /* The initial delay is the sum of the reflections and late reverb delays.
407 * The decorrelator length is calculated from the lowest reverb density (a
408 * parameter value of 1). This must include space for storing a loop
409 * update.
411 length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY +
412 AL_EAXREVERB_MAX_LATE_REVERB_DELAY;
413 length += (DECO_FRACTION * DECO_MULTIPLIER * DECO_MULTIPLIER) *
414 LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER);
415 totalSamples += CalcLineLength(length, totalSamples, frequency,
416 MAX_UPDATE_SAMPLES, &State->Delay);
418 // The early reflection lines.
419 for(index = 0;index < 4;index++)
420 totalSamples += CalcLineLength(EARLY_LINE_LENGTH[index], totalSamples,
421 frequency, 0, &State->Early.Delay[index]);
423 // The late all-pass lines.
424 for(index = 0;index < 4;index++)
425 totalSamples += CalcLineLength(ALLPASS_LINE_LENGTH[index], totalSamples,
426 frequency, 0, &State->Late.ApDelay[index]);
428 // The late delay lines are calculated from the lowest reverb density.
429 for(index = 0;index < 4;index++)
431 length = LATE_LINE_LENGTH[index] * (1.0f + LATE_LINE_MULTIPLIER);
432 totalSamples += CalcLineLength(length, totalSamples, frequency, 0,
433 &State->Late.Delay[index]);
436 // The echo all-pass and delay lines.
437 totalSamples += CalcLineLength(ECHO_ALLPASS_LENGTH, totalSamples,
438 frequency, 0, &State->Echo.ApDelay);
439 totalSamples += CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME, totalSamples,
440 frequency, 0, &State->Echo.Delay);
442 if(totalSamples != State->TotalSamples)
444 ALfloat *newBuffer;
446 TRACE("New reverb buffer length: %u samples (%f sec)\n", totalSamples, totalSamples/(float)frequency);
447 newBuffer = al_calloc(16, sizeof(ALfloat) * totalSamples);
448 if(!newBuffer) return AL_FALSE;
450 al_free(State->SampleBuffer);
451 State->SampleBuffer = newBuffer;
452 State->TotalSamples = totalSamples;
455 // Update all delays to reflect the new sample buffer.
456 RealizeLineOffset(State->SampleBuffer, &State->Delay);
457 for(index = 0;index < 4;index++)
459 RealizeLineOffset(State->SampleBuffer, &State->Early.Delay[index]);
460 RealizeLineOffset(State->SampleBuffer, &State->Late.ApDelay[index]);
461 RealizeLineOffset(State->SampleBuffer, &State->Late.Delay[index]);
463 RealizeLineOffset(State->SampleBuffer, &State->Mod.Delay);
464 RealizeLineOffset(State->SampleBuffer, &State->Echo.ApDelay);
465 RealizeLineOffset(State->SampleBuffer, &State->Echo.Delay);
467 // Clear the sample buffer.
468 for(index = 0;index < State->TotalSamples;index++)
469 State->SampleBuffer[index] = 0.0f;
471 return AL_TRUE;
474 static ALboolean ALreverbState_deviceUpdate(ALreverbState *State, ALCdevice *Device)
476 ALuint frequency = Device->Frequency, index;
478 // Allocate the delay lines.
479 if(!AllocLines(frequency, State))
480 return AL_FALSE;
482 /* HRTF and UHJ will mix to the real output for ambient output. */
483 if(Device->Hrtf.Handle || Device->Uhj_Encoder)
485 State->ExtraOut = Device->RealOut.Buffer;
486 State->ExtraChannels = Device->RealOut.NumChannels;
488 else
490 State->ExtraOut = NULL;
491 State->ExtraChannels = 0;
494 // Calculate the modulation filter coefficient. Notice that the exponent
495 // is calculated given the current sample rate. This ensures that the
496 // resulting filter response over time is consistent across all sample
497 // rates.
498 State->Mod.Coeff = powf(MODULATION_FILTER_COEFF,
499 MODULATION_FILTER_CONST / frequency);
501 // The early reflection and late all-pass filter line lengths are static,
502 // so their offsets only need to be calculated once.
503 for(index = 0;index < 4;index++)
505 State->Early.Offset[index] = fastf2u(EARLY_LINE_LENGTH[index] * frequency);
506 State->Late.ApOffset[index] = fastf2u(ALLPASS_LINE_LENGTH[index] * frequency);
509 // The echo all-pass filter line length is static, so its offset only
510 // needs to be calculated once.
511 State->Echo.ApOffset = fastf2u(ECHO_ALLPASS_LENGTH * frequency);
513 return AL_TRUE;
516 /**************************************
517 * Effect Update *
518 **************************************/
520 // Calculate a decay coefficient given the length of each cycle and the time
521 // until the decay reaches -60 dB.
522 static inline ALfloat CalcDecayCoeff(ALfloat length, ALfloat decayTime)
524 return powf(0.001f/*-60 dB*/, length/decayTime);
527 // Calculate a decay length from a coefficient and the time until the decay
528 // reaches -60 dB.
529 static inline ALfloat CalcDecayLength(ALfloat coeff, ALfloat decayTime)
531 return log10f(coeff) * decayTime / log10f(0.001f)/*-60 dB*/;
534 // Calculate an attenuation to be applied to the input of any echo models to
535 // compensate for modal density and decay time.
536 static inline ALfloat CalcDensityGain(ALfloat a)
538 /* The energy of a signal can be obtained by finding the area under the
539 * squared signal. This takes the form of Sum(x_n^2), where x is the
540 * amplitude for the sample n.
542 * Decaying feedback matches exponential decay of the form Sum(a^n),
543 * where a is the attenuation coefficient, and n is the sample. The area
544 * under this decay curve can be calculated as: 1 / (1 - a).
546 * Modifying the above equation to find the squared area under the curve
547 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
548 * calculated by inverting the square root of this approximation,
549 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
551 return sqrtf(1.0f - (a * a));
554 // Calculate the mixing matrix coefficients given a diffusion factor.
555 static inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)
557 ALfloat n, t;
559 // The matrix is of order 4, so n is sqrt (4 - 1).
560 n = sqrtf(3.0f);
561 t = diffusion * atanf(n);
563 // Calculate the first mixing matrix coefficient.
564 *x = cosf(t);
565 // Calculate the second mixing matrix coefficient.
566 *y = sinf(t) / n;
569 // Calculate the limited HF ratio for use with the late reverb low-pass
570 // filters.
571 static ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)
573 ALfloat limitRatio;
575 /* Find the attenuation due to air absorption in dB (converting delay
576 * time to meters using the speed of sound). Then reversing the decay
577 * equation, solve for HF ratio. The delay length is cancelled out of
578 * the equation, so it can be calculated once for all lines.
580 limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *
581 SPEEDOFSOUNDMETRESPERSEC);
582 /* Using the limit calculated above, apply the upper bound to the HF
583 * ratio. Also need to limit the result to a minimum of 0.1, just like the
584 * HF ratio parameter. */
585 return clampf(limitRatio, 0.1f, hfRatio);
588 // Calculate the coefficient for a HF (and eventually LF) decay damping
589 // filter.
590 static inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)
592 ALfloat coeff, g;
594 // Eventually this should boost the high frequencies when the ratio
595 // exceeds 1.
596 coeff = 0.0f;
597 if (hfRatio < 1.0f)
599 // Calculate the low-pass coefficient by dividing the HF decay
600 // coefficient by the full decay coefficient.
601 g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;
603 // Damping is done with a 1-pole filter, so g needs to be squared.
604 g *= g;
605 if(g < 0.9999f) /* 1-epsilon */
607 /* Be careful with gains < 0.001, as that causes the coefficient
608 * head towards 1, which will flatten the signal. */
609 g = maxf(g, 0.001f);
610 coeff = (1 - g*cw - sqrtf(2*g*(1-cw) - g*g*(1 - cw*cw))) /
611 (1 - g);
614 // Very low decay times will produce minimal output, so apply an
615 // upper bound to the coefficient.
616 coeff = minf(coeff, 0.98f);
618 return coeff;
621 // Update the EAX modulation index, range, and depth. Keep in mind that this
622 // kind of vibrato is additive and not multiplicative as one may expect. The
623 // downswing will sound stronger than the upswing.
624 static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALreverbState *State)
626 ALuint range;
628 /* Modulation is calculated in two parts.
630 * The modulation time effects the sinus applied to the change in
631 * frequency. An index out of the current time range (both in samples)
632 * is incremented each sample. The range is bound to a reasonable
633 * minimum (1 sample) and when the timing changes, the index is rescaled
634 * to the new range (to keep the sinus consistent).
636 range = maxu(fastf2u(modTime*frequency), 1);
637 State->Mod.Index = (ALuint)(State->Mod.Index * (ALuint64)range /
638 State->Mod.Range);
639 State->Mod.Range = range;
641 /* The modulation depth effects the amount of frequency change over the
642 * range of the sinus. It needs to be scaled by the modulation time so
643 * that a given depth produces a consistent change in frequency over all
644 * ranges of time. Since the depth is applied to a sinus value, it needs
645 * to be halfed once for the sinus range and again for the sinus swing
646 * in time (half of it is spent decreasing the frequency, half is spent
647 * increasing it).
649 State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /
650 2.0f * frequency;
653 // Update the offsets for the initial effect delay line.
654 static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALreverbState *State)
656 // Calculate the initial delay taps.
657 State->DelayTap[0] = fastf2u(earlyDelay * frequency);
658 State->DelayTap[1] = fastf2u((earlyDelay + lateDelay) * frequency);
661 // Update the early reflections mix and line coefficients.
662 static ALvoid UpdateEarlyLines(ALfloat lateDelay, ALreverbState *State)
664 ALuint index;
666 // Calculate the gain (coefficient) for each early delay line using the
667 // late delay time. This expands the early reflections to the start of
668 // the late reverb.
669 for(index = 0;index < 4;index++)
670 State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],
671 lateDelay);
674 // Update the offsets for the decorrelator line.
675 static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALreverbState *State)
677 ALuint index;
678 ALfloat length;
680 /* The late reverb inputs are decorrelated to smooth the reverb tail and
681 * reduce harsh echos. The first tap occurs immediately, while the
682 * remaining taps are delayed by multiples of a fraction of the smallest
683 * cyclical delay time.
685 * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
687 for(index = 0;index < 3;index++)
689 length = (DECO_FRACTION * powf(DECO_MULTIPLIER, (ALfloat)index)) *
690 LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));
691 State->DecoTap[index] = fastf2u(length * frequency) + State->DelayTap[1];
695 // Update the late reverb mix, line lengths, and line coefficients.
696 static ALvoid UpdateLateLines(ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALreverbState *State)
698 ALfloat length;
699 ALuint index;
701 /* Calculate the late reverb gain. Since the output is tapped prior to the
702 * application of the next delay line coefficients, this gain needs to be
703 * attenuated by the 'x' mixing matrix coefficient as well. Also attenuate
704 * the late reverb when echo depth is high and diffusion is low, so the
705 * echo is slightly stronger than the decorrelated echos in the reverb
706 * tail.
708 State->Late.Gain = xMix * (1.0f - (echoDepth*0.5f*(1.0f - diffusion)));
710 /* To compensate for changes in modal density and decay time of the late
711 * reverb signal, the input is attenuated based on the maximal energy of
712 * the outgoing signal. This approximation is used to keep the apparent
713 * energy of the signal equal for all ranges of density and decay time.
715 * The average length of the cyclcical delay lines is used to calculate
716 * the attenuation coefficient.
718 length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
719 LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;
720 length *= 1.0f + (density * LATE_LINE_MULTIPLIER);
721 State->Late.DensityGain = CalcDensityGain(
722 CalcDecayCoeff(length, decayTime)
725 // Calculate the all-pass feed-back and feed-forward coefficient.
726 State->Late.ApFeedCoeff = 0.5f * powf(diffusion, 2.0f);
728 for(index = 0;index < 4;index++)
730 // Calculate the gain (coefficient) for each all-pass line.
731 State->Late.ApCoeff[index] = CalcDecayCoeff(
732 ALLPASS_LINE_LENGTH[index], decayTime
735 // Calculate the length (in seconds) of each cyclical delay line.
736 length = LATE_LINE_LENGTH[index] *
737 (1.0f + (density * LATE_LINE_MULTIPLIER));
739 // Calculate the delay offset for each cyclical delay line.
740 State->Late.Offset[index] = fastf2u(length * frequency);
742 // Calculate the gain (coefficient) for each cyclical line.
743 State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);
745 // Calculate the damping coefficient for each low-pass filter.
746 State->Late.LpCoeff[index] = CalcDampingCoeff(
747 hfRatio, length, decayTime, State->Late.Coeff[index], cw
750 // Attenuate the cyclical line coefficients by the mixing coefficient
751 // (x).
752 State->Late.Coeff[index] *= xMix;
756 // Update the echo gain, line offset, line coefficients, and mixing
757 // coefficients.
758 static ALvoid UpdateEchoLine(ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALreverbState *State)
760 // Update the offset and coefficient for the echo delay line.
761 State->Echo.Offset = fastf2u(echoTime * frequency);
763 // Calculate the decay coefficient for the echo line.
764 State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);
766 // Calculate the energy-based attenuation coefficient for the echo delay
767 // line.
768 State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);
770 // Calculate the echo all-pass feed coefficient.
771 State->Echo.ApFeedCoeff = 0.5f * powf(diffusion, 2.0f);
773 // Calculate the echo all-pass attenuation coefficient.
774 State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);
776 // Calculate the damping coefficient for each low-pass filter.
777 State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,
778 State->Echo.Coeff, cw);
780 /* Calculate the echo mixing coefficient. This is applied to the output mix
781 * only, not the feedback.
783 State->Echo.MixCoeff = echoDepth;
786 // Update the early and late 3D panning gains.
787 static ALvoid UpdateMixedPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
789 ALfloat DirGains[MAX_OUTPUT_CHANNELS];
790 ALfloat coeffs[MAX_AMBI_COEFFS];
791 ALfloat length;
792 ALuint i;
794 /* With HRTF or UHJ, the normal output provides a panned reverb channel
795 * when a non-0-length vector is specified, while the real stereo output
796 * provides two other "direct" non-panned reverb channels.
798 memset(State->Early.PanGain, 0, sizeof(State->Early.PanGain));
799 length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
800 if(!(length > FLT_EPSILON))
802 for(i = 0;i < Device->RealOut.NumChannels;i++)
803 State->Early.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * EarlyGain;
805 else
807 /* Note that EAX Reverb's panning vectors are using right-handed
808 * coordinates, rather than OpenAL's left-handed coordinates. Negate Z
809 * to fix this.
811 ALfloat pan[3] = {
812 ReflectionsPan[0] / length,
813 ReflectionsPan[1] / length,
814 -ReflectionsPan[2] / length,
816 length = minf(length, 1.0f);
818 CalcDirectionCoeffs(pan, 0.0f, coeffs);
819 ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
820 for(i = 0;i < Device->Dry.NumChannels;i++)
821 State->Early.PanGain[3][i] = DirGains[i] * EarlyGain * length;
822 for(i = 0;i < Device->RealOut.NumChannels;i++)
823 State->Early.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * EarlyGain * (1.0f-length);
826 memset(State->Late.PanGain, 0, sizeof(State->Late.PanGain));
827 length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
828 if(!(length > FLT_EPSILON))
830 for(i = 0;i < Device->RealOut.NumChannels;i++)
831 State->Late.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * LateGain;
833 else
835 ALfloat pan[3] = {
836 LateReverbPan[0] / length,
837 LateReverbPan[1] / length,
838 -LateReverbPan[2] / length,
840 length = minf(length, 1.0f);
842 CalcDirectionCoeffs(pan, 0.0f, coeffs);
843 ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
844 for(i = 0;i < Device->Dry.NumChannels;i++)
845 State->Late.PanGain[3][i] = DirGains[i] * LateGain * length;
846 for(i = 0;i < Device->RealOut.NumChannels;i++)
847 State->Late.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * LateGain * (1.0f-length);
851 static ALvoid UpdateDirectPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
853 ALfloat AmbientGains[MAX_OUTPUT_CHANNELS];
854 ALfloat DirGains[MAX_OUTPUT_CHANNELS];
855 ALfloat coeffs[MAX_AMBI_COEFFS];
856 ALfloat length;
857 ALuint i;
859 /* Apply a boost of about 3dB to better match the expected stereo output volume. */
860 ComputeAmbientGains(Device->Dry, Gain*1.414213562f, AmbientGains);
862 memset(State->Early.PanGain, 0, sizeof(State->Early.PanGain));
863 length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
864 if(!(length > FLT_EPSILON))
866 for(i = 0;i < Device->Dry.NumChannels;i++)
867 State->Early.PanGain[i&3][i] = AmbientGains[i] * EarlyGain;
869 else
871 ALfloat pan[3] = {
872 ReflectionsPan[0] / length,
873 ReflectionsPan[1] / length,
874 -ReflectionsPan[2] / length,
876 length = minf(length, 1.0f);
878 CalcDirectionCoeffs(pan, 0.0f, coeffs);
879 ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
880 for(i = 0;i < Device->Dry.NumChannels;i++)
881 State->Early.PanGain[i&3][i] = lerp(AmbientGains[i], DirGains[i], length) * EarlyGain;
884 memset(State->Late.PanGain, 0, sizeof(State->Late.PanGain));
885 length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
886 if(!(length > FLT_EPSILON))
888 for(i = 0;i < Device->Dry.NumChannels;i++)
889 State->Late.PanGain[i&3][i] = AmbientGains[i] * LateGain;
891 else
893 ALfloat pan[3] = {
894 LateReverbPan[0] / length,
895 LateReverbPan[1] / length,
896 -LateReverbPan[2] / length,
898 length = minf(length, 1.0f);
900 CalcDirectionCoeffs(pan, 0.0f, coeffs);
901 ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
902 for(i = 0;i < Device->Dry.NumChannels;i++)
903 State->Late.PanGain[i&3][i] = lerp(AmbientGains[i], DirGains[i], length) * LateGain;
907 static ALvoid Update3DPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
909 static const ALfloat PanDirs[4][3] = {
910 { -0.707106781f, 0.0f, -0.707106781f }, /* Front left */
911 { 0.707106781f, 0.0f, -0.707106781f }, /* Front right */
912 { 0.707106781f, 0.0f, 0.707106781f }, /* Back right */
913 { -0.707106781f, 0.0f, 0.707106781f } /* Back left */
915 ALfloat coeffs[MAX_AMBI_COEFFS];
916 ALfloat gain[4];
917 ALfloat length;
918 ALuint i;
920 /* sqrt(0.5) would be the gain scaling when the panning vector is 0. This
921 * also equals sqrt(2/4), a nice gain scaling for the four virtual points
922 * producing an "ambient" response.
924 gain[0] = gain[1] = gain[2] = gain[3] = 0.707106781f;
925 length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
926 if(length > 1.0f)
928 ALfloat pan[3] = {
929 ReflectionsPan[0] / length,
930 ReflectionsPan[1] / length,
931 -ReflectionsPan[2] / length,
933 for(i = 0;i < 4;i++)
935 ALfloat dotp = pan[0]*PanDirs[i][0] + pan[1]*PanDirs[i][1] + pan[2]*PanDirs[i][2];
936 gain[i] = sqrtf(clampf(dotp*0.5f + 0.5f, 0.0f, 1.0f));
939 else if(length > FLT_EPSILON)
941 for(i = 0;i < 4;i++)
943 ALfloat dotp = ReflectionsPan[0]*PanDirs[i][0] + ReflectionsPan[1]*PanDirs[i][1] +
944 -ReflectionsPan[2]*PanDirs[i][2];
945 gain[i] = sqrtf(clampf(dotp*0.5f + 0.5f, 0.0f, 1.0f));
948 for(i = 0;i < 4;i++)
950 CalcDirectionCoeffs(PanDirs[i], 0.0f, coeffs);
951 ComputePanningGains(Device->Dry, coeffs, Gain*EarlyGain*gain[i],
952 State->Early.PanGain[i]);
955 gain[0] = gain[1] = gain[2] = gain[3] = 0.707106781f;
956 length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
957 if(length > 1.0f)
959 ALfloat pan[3] = {
960 LateReverbPan[0] / length,
961 LateReverbPan[1] / length,
962 -LateReverbPan[2] / length,
964 for(i = 0;i < 4;i++)
966 ALfloat dotp = pan[0]*PanDirs[i][0] + pan[1]*PanDirs[i][1] + pan[2]*PanDirs[i][2];
967 gain[i] = sqrtf(clampf(dotp*0.5f + 0.5f, 0.0f, 1.0f));
970 else if(length > FLT_EPSILON)
972 for(i = 0;i < 4;i++)
974 ALfloat dotp = LateReverbPan[0]*PanDirs[i][0] + LateReverbPan[1]*PanDirs[i][1] +
975 -LateReverbPan[2]*PanDirs[i][2];
976 gain[i] = sqrtf(clampf(dotp*0.5f + 0.5f, 0.0f, 1.0f));
979 for(i = 0;i < 4;i++)
981 CalcDirectionCoeffs(PanDirs[i], 0.0f, coeffs);
982 ComputePanningGains(Device->Dry, coeffs, Gain*LateGain*gain[i],
983 State->Late.PanGain[i]);
987 static ALvoid ALreverbState_update(ALreverbState *State, const ALCdevice *Device, const ALeffectslot *Slot, const ALeffectProps *props)
989 ALuint frequency = Device->Frequency;
990 ALfloat lfscale, hfscale, hfRatio;
991 ALfloat gain, gainlf, gainhf;
992 ALfloat cw, x, y;
994 if(Slot->Params.EffectType == AL_EFFECT_EAXREVERB && !EmulateEAXReverb)
995 State->IsEax = AL_TRUE;
996 else if(Slot->Params.EffectType == AL_EFFECT_REVERB || EmulateEAXReverb)
997 State->IsEax = AL_FALSE;
999 // Calculate the master filters
1000 hfscale = props->Reverb.HFReference / frequency;
1001 gainhf = maxf(props->Reverb.GainHF, 0.0001f);
1002 ALfilterState_setParams(&State->LpFilter, ALfilterType_HighShelf,
1003 gainhf, hfscale, calc_rcpQ_from_slope(gainhf, 0.75f));
1004 lfscale = props->Reverb.LFReference / frequency;
1005 gainlf = maxf(props->Reverb.GainLF, 0.0001f);
1006 ALfilterState_setParams(&State->HpFilter, ALfilterType_LowShelf,
1007 gainlf, lfscale, calc_rcpQ_from_slope(gainlf, 0.75f));
1009 // Update the modulator line.
1010 UpdateModulator(props->Reverb.ModulationTime, props->Reverb.ModulationDepth,
1011 frequency, State);
1013 // Update the initial effect delay.
1014 UpdateDelayLine(props->Reverb.ReflectionsDelay, props->Reverb.LateReverbDelay,
1015 frequency, State);
1017 // Update the decorrelator.
1018 UpdateDecorrelator(props->Reverb.Density, frequency, State);
1020 // Update the early lines.
1021 UpdateEarlyLines(props->Reverb.LateReverbDelay, State);
1023 // Get the mixing matrix coefficients (x and y).
1024 CalcMatrixCoeffs(props->Reverb.Diffusion, &x, &y);
1025 // Then divide x into y to simplify the matrix calculation.
1026 State->Late.MixCoeff = y / x;
1028 // If the HF limit parameter is flagged, calculate an appropriate limit
1029 // based on the air absorption parameter.
1030 hfRatio = props->Reverb.DecayHFRatio;
1031 if(props->Reverb.DecayHFLimit && props->Reverb.AirAbsorptionGainHF < 1.0f)
1032 hfRatio = CalcLimitedHfRatio(hfRatio, props->Reverb.AirAbsorptionGainHF,
1033 props->Reverb.DecayTime);
1035 cw = cosf(F_TAU * hfscale);
1036 // Update the late lines.
1037 UpdateLateLines(x, props->Reverb.Density, props->Reverb.DecayTime,
1038 props->Reverb.Diffusion, props->Reverb.EchoDepth,
1039 hfRatio, cw, frequency, State);
1041 // Update the echo line.
1042 UpdateEchoLine(props->Reverb.EchoTime, props->Reverb.DecayTime,
1043 props->Reverb.Diffusion, props->Reverb.EchoDepth,
1044 hfRatio, cw, frequency, State);
1046 gain = props->Reverb.Gain * Slot->Params.Gain * ReverbBoost;
1047 // Update early and late 3D panning.
1048 if(Device->Hrtf.Handle || Device->Uhj_Encoder)
1049 UpdateMixedPanning(Device, props->Reverb.ReflectionsPan,
1050 props->Reverb.LateReverbPan, gain,
1051 props->Reverb.ReflectionsGain,
1052 props->Reverb.LateReverbGain, State);
1053 else if(Device->AmbiDecoder || (Device->FmtChans >= DevFmtAmbi1 &&
1054 Device->FmtChans <= DevFmtAmbi3))
1055 Update3DPanning(Device, props->Reverb.ReflectionsPan,
1056 props->Reverb.LateReverbPan, gain,
1057 props->Reverb.ReflectionsGain,
1058 props->Reverb.LateReverbGain, State);
1059 else
1060 UpdateDirectPanning(Device, props->Reverb.ReflectionsPan,
1061 props->Reverb.LateReverbPan, gain,
1062 props->Reverb.ReflectionsGain,
1063 props->Reverb.LateReverbGain, State);
1067 /**************************************
1068 * Effect Processing *
1069 **************************************/
1071 // Basic delay line input/output routines.
1072 static inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
1074 return Delay->Line[offset&Delay->Mask];
1077 static inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
1079 Delay->Line[offset&Delay->Mask] = in;
1082 // Given some input samples, this function produces modulation for the late
1083 // reverb.
1084 static void EAXModulation(ALreverbState *State, ALuint offset, ALfloat*restrict dst, const ALfloat*restrict src, ALuint todo)
1086 ALfloat sinus, frac, fdelay;
1087 ALfloat out0, out1;
1088 ALuint delay, i;
1090 for(i = 0;i < todo;i++)
1092 /* Calculate the sinus rythm (dependent on modulation time and the
1093 * sampling rate). The center of the sinus is moved to reduce the
1094 * delay of the effect when the time or depth are low.
1096 sinus = 1.0f - cosf(F_TAU * State->Mod.Index / State->Mod.Range);
1098 /* Step the modulation index forward, keeping it bound to its range. */
1099 State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
1101 /* The depth determines the range over which to read the input samples
1102 * from, so it must be filtered to reduce the distortion caused by even
1103 * small parameter changes.
1105 State->Mod.Filter = lerp(State->Mod.Filter, State->Mod.Depth,
1106 State->Mod.Coeff);
1108 /* Calculate the read offset and fraction between it and the next
1109 * sample.
1111 frac = modff(State->Mod.Filter*sinus, &fdelay);
1112 delay = fastf2u(fdelay);
1114 /* Add the incoming sample to the delay line first, so a 0 delay gets
1115 * the incoming sample.
1117 DelayLineIn(&State->Mod.Delay, offset, src[i]);
1118 /* Get the two samples crossed by the offset delay */
1119 out0 = DelayLineOut(&State->Mod.Delay, offset - delay);
1120 out1 = DelayLineOut(&State->Mod.Delay, offset - delay - 1);
1121 offset++;
1123 /* The output is obtained by linearly interpolating the two samples
1124 * that were acquired above.
1126 dst[i] = lerp(out0, out1, frac);
1130 // Given some input sample, this function produces four-channel outputs for the
1131 // early reflections.
1132 static inline ALvoid EarlyReflection(ALreverbState *State, ALuint todo, ALfloat (*restrict out)[MAX_UPDATE_SAMPLES])
1134 ALfloat d[4], v, f[4];
1135 ALuint i;
1137 for(i = 0;i < todo;i++)
1139 ALuint offset = State->Offset+i;
1141 // Obtain the decayed results of each early delay line.
1142 d[0] = DelayLineOut(&State->Early.Delay[0], offset-State->Early.Offset[0]) * State->Early.Coeff[0];
1143 d[1] = DelayLineOut(&State->Early.Delay[1], offset-State->Early.Offset[1]) * State->Early.Coeff[1];
1144 d[2] = DelayLineOut(&State->Early.Delay[2], offset-State->Early.Offset[2]) * State->Early.Coeff[2];
1145 d[3] = DelayLineOut(&State->Early.Delay[3], offset-State->Early.Offset[3]) * State->Early.Coeff[3];
1147 /* The following uses a lossless scattering junction from waveguide
1148 * theory. It actually amounts to a householder mixing matrix, which
1149 * will produce a maximally diffuse response, and means this can
1150 * probably be considered a simple feed-back delay network (FDN).
1152 * ---
1154 * v = 2/N / d_i
1155 * ---
1156 * i=1
1158 v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
1159 // The junction is loaded with the input here.
1160 v += DelayLineOut(&State->Delay, offset-State->DelayTap[0]);
1162 // Calculate the feed values for the delay lines.
1163 f[0] = v - d[0];
1164 f[1] = v - d[1];
1165 f[2] = v - d[2];
1166 f[3] = v - d[3];
1168 // Re-feed the delay lines.
1169 DelayLineIn(&State->Early.Delay[0], offset, f[0]);
1170 DelayLineIn(&State->Early.Delay[1], offset, f[1]);
1171 DelayLineIn(&State->Early.Delay[2], offset, f[2]);
1172 DelayLineIn(&State->Early.Delay[3], offset, f[3]);
1174 /* Output the results of the junction for all four channels with a
1175 * constant attenuation of 0.5.
1177 out[0][i] = f[0] * 0.5f;
1178 out[1][i] = f[1] * 0.5f;
1179 out[2][i] = f[2] * 0.5f;
1180 out[3][i] = f[3] * 0.5f;
1184 // Basic attenuated all-pass input/output routine.
1185 static inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
1187 ALfloat out, feed;
1189 out = DelayLineOut(Delay, outOffset);
1190 feed = feedCoeff * in;
1191 DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
1193 // The time-based attenuation is only applied to the delay output to
1194 // keep it from affecting the feed-back path (which is already controlled
1195 // by the all-pass feed coefficient).
1196 return (coeff * out) - feed;
1199 // All-pass input/output routine for late reverb.
1200 static inline ALfloat LateAllPassInOut(ALreverbState *State, ALuint offset, ALuint index, ALfloat in)
1202 return AllpassInOut(&State->Late.ApDelay[index],
1203 offset - State->Late.ApOffset[index],
1204 offset, in, State->Late.ApFeedCoeff,
1205 State->Late.ApCoeff[index]);
1208 // Low-pass filter input/output routine for late reverb.
1209 static inline ALfloat LateLowPassInOut(ALreverbState *State, ALuint index, ALfloat in)
1211 in = lerp(in, State->Late.LpSample[index], State->Late.LpCoeff[index]);
1212 State->Late.LpSample[index] = in;
1213 return in;
1216 // Given four decorrelated input samples, this function produces four-channel
1217 // output for the late reverb.
1218 static inline ALvoid LateReverb(ALreverbState *State, ALuint todo, ALfloat (*restrict out)[MAX_UPDATE_SAMPLES])
1220 ALfloat d[4], f[4];
1221 ALuint offset;
1222 ALuint base, i;
1224 offset = State->Offset;
1225 for(base = 0;base < todo;)
1227 ALfloat tmp[MAX_UPDATE_SAMPLES/4][4];
1228 ALuint tmp_todo = minu(todo, MAX_UPDATE_SAMPLES/4);
1230 for(i = 0;i < tmp_todo;i++)
1232 /* Obtain four decorrelated input samples. */
1233 f[0] = DelayLineOut(&State->Delay, offset-State->DelayTap[1]) * State->Late.DensityGain;
1234 f[1] = DelayLineOut(&State->Delay, offset-State->DecoTap[0]) * State->Late.DensityGain;
1235 f[2] = DelayLineOut(&State->Delay, offset-State->DecoTap[1]) * State->Late.DensityGain;
1236 f[3] = DelayLineOut(&State->Delay, offset-State->DecoTap[2]) * State->Late.DensityGain;
1238 /* Add the decayed results of the cyclical delay lines, then pass
1239 * the results through the low-pass filters.
1241 f[0] += DelayLineOut(&State->Late.Delay[0], offset-State->Late.Offset[0]) * State->Late.Coeff[0];
1242 f[1] += DelayLineOut(&State->Late.Delay[1], offset-State->Late.Offset[1]) * State->Late.Coeff[1];
1243 f[2] += DelayLineOut(&State->Late.Delay[2], offset-State->Late.Offset[2]) * State->Late.Coeff[2];
1244 f[3] += DelayLineOut(&State->Late.Delay[3], offset-State->Late.Offset[3]) * State->Late.Coeff[3];
1246 /* This is where the feed-back cycles from line 0 to 1 to 3 to 2
1247 * and back to 0.
1249 d[0] = LateLowPassInOut(State, 2, f[2]);
1250 d[1] = LateLowPassInOut(State, 0, f[0]);
1251 d[2] = LateLowPassInOut(State, 3, f[3]);
1252 d[3] = LateLowPassInOut(State, 1, f[1]);
1254 /* To help increase diffusion, run each line through an all-pass
1255 * filter. When there is no diffusion, the shortest all-pass filter
1256 * will feed the shortest delay line.
1258 d[0] = LateAllPassInOut(State, offset, 0, d[0]);
1259 d[1] = LateAllPassInOut(State, offset, 1, d[1]);
1260 d[2] = LateAllPassInOut(State, offset, 2, d[2]);
1261 d[3] = LateAllPassInOut(State, offset, 3, d[3]);
1263 /* Late reverb is done with a modified feed-back delay network (FDN)
1264 * topology. Four input lines are each fed through their own all-pass
1265 * filter and then into the mixing matrix. The four outputs of the
1266 * mixing matrix are then cycled back to the inputs. Each output feeds
1267 * a different input to form a circlular feed cycle.
1269 * The mixing matrix used is a 4D skew-symmetric rotation matrix
1270 * derived using a single unitary rotational parameter:
1272 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
1273 * [ -a, d, c, -b ]
1274 * [ -b, -c, d, a ]
1275 * [ -c, b, -a, d ]
1277 * The rotation is constructed from the effect's diffusion parameter,
1278 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
1279 * with differing signs, and d is the coefficient x. The matrix is
1280 * thus:
1282 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
1283 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
1284 * [ y, -y, x, y ] x = cos(t)
1285 * [ -y, -y, -y, x ] y = sin(t) / n
1287 * To reduce the number of multiplies, the x coefficient is applied
1288 * with the cyclical delay line coefficients. Thus only the y
1289 * coefficient is applied when mixing, and is modified to be: y / x.
1291 f[0] = d[0] + (State->Late.MixCoeff * ( d[1] + -d[2] + d[3]));
1292 f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
1293 f[2] = d[2] + (State->Late.MixCoeff * ( d[0] + -d[1] + d[3]));
1294 f[3] = d[3] + (State->Late.MixCoeff * (-d[0] + -d[1] + -d[2] ));
1296 /* Re-feed the cyclical delay lines. */
1297 DelayLineIn(&State->Late.Delay[0], offset, f[0]);
1298 DelayLineIn(&State->Late.Delay[1], offset, f[1]);
1299 DelayLineIn(&State->Late.Delay[2], offset, f[2]);
1300 DelayLineIn(&State->Late.Delay[3], offset, f[3]);
1301 offset++;
1303 /* Output the results of the matrix for all four channels,
1304 * attenuated by the late reverb gain (which is attenuated by the
1305 * 'x' mix coefficient).
1307 tmp[i][0] = State->Late.Gain * f[0];
1308 tmp[i][1] = State->Late.Gain * f[1];
1309 tmp[i][2] = State->Late.Gain * f[2];
1310 tmp[i][3] = State->Late.Gain * f[3];
1313 /* Deinterlace to output */
1314 for(i = 0;i < tmp_todo;i++) out[0][base+i] = tmp[i][0];
1315 for(i = 0;i < tmp_todo;i++) out[1][base+i] = tmp[i][1];
1316 for(i = 0;i < tmp_todo;i++) out[2][base+i] = tmp[i][2];
1317 for(i = 0;i < tmp_todo;i++) out[3][base+i] = tmp[i][3];
1319 base += tmp_todo;
1323 // Given an input sample, this function mixes echo into the four-channel late
1324 // reverb.
1325 static inline ALvoid EAXEcho(ALreverbState *State, ALuint todo, ALfloat (*restrict late)[MAX_UPDATE_SAMPLES])
1327 ALfloat out[MAX_UPDATE_SAMPLES];
1328 ALfloat feed;
1329 ALuint offset;
1330 ALuint i;
1332 offset = State->Offset;
1333 for(i = 0;i < todo;i++)
1335 // Get the latest attenuated echo sample for output.
1336 feed = DelayLineOut(&State->Echo.Delay, offset-State->Echo.Offset) *
1337 State->Echo.Coeff;
1339 // Write the output.
1340 out[i] = State->Echo.MixCoeff * feed;
1342 // Mix the energy-attenuated input with the output and pass it through
1343 // the echo low-pass filter.
1344 feed += DelayLineOut(&State->Delay, offset-State->DelayTap[1]) *
1345 State->Echo.DensityGain;
1346 feed = lerp(feed, State->Echo.LpSample, State->Echo.LpCoeff);
1347 State->Echo.LpSample = feed;
1349 // Then the echo all-pass filter.
1350 feed = AllpassInOut(&State->Echo.ApDelay, offset-State->Echo.ApOffset,
1351 offset, feed, State->Echo.ApFeedCoeff,
1352 State->Echo.ApCoeff);
1354 // Feed the delay with the mixed and filtered sample.
1355 DelayLineIn(&State->Echo.Delay, offset, feed);
1356 offset++;
1359 // Mix the output into the late reverb channels.
1360 for(i = 0;i < todo;i++) late[0][i] += out[i];
1361 for(i = 0;i < todo;i++) late[1][i] += out[i];
1362 for(i = 0;i < todo;i++) late[2][i] += out[i];
1363 for(i = 0;i < todo;i++) late[3][i] += out[i];
1366 // Perform the non-EAX reverb pass on a given input sample, resulting in
1367 // four-channel output.
1368 static inline ALvoid VerbPass(ALreverbState *State, ALuint todo, const ALfloat *input, ALfloat (*restrict early)[MAX_UPDATE_SAMPLES], ALfloat (*restrict late)[MAX_UPDATE_SAMPLES])
1370 ALuint i;
1372 // Low-pass filter the incoming samples (use the early buffer as temp storage).
1373 ALfilterState_process(&State->LpFilter, &early[0][0], input, todo);
1374 for(i = 0;i < todo;i++)
1375 DelayLineIn(&State->Delay, State->Offset+i, early[0][i]);
1377 // Calculate the early reflection from the first delay tap.
1378 EarlyReflection(State, todo, early);
1380 // Calculate the late reverb from the decorrelator taps.
1381 LateReverb(State, todo, late);
1383 // Step all delays forward one sample.
1384 State->Offset += todo;
1387 // Perform the EAX reverb pass on a given input sample, resulting in four-
1388 // channel output.
1389 static inline ALvoid EAXVerbPass(ALreverbState *State, ALuint todo, const ALfloat *input, ALfloat (*restrict early)[MAX_UPDATE_SAMPLES], ALfloat (*restrict late)[MAX_UPDATE_SAMPLES])
1391 ALuint i;
1393 /* Perform any modulation on the input (use the early buffer as temp storage). */
1394 EAXModulation(State, State->Offset, &early[0][0], input, todo);
1395 /* Band-pass the incoming samples */
1396 ALfilterState_process(&State->LpFilter, &early[1][0], &early[0][0], todo);
1397 ALfilterState_process(&State->HpFilter, &early[2][0], &early[1][0], todo);
1399 // Feed the initial delay line.
1400 for(i = 0;i < todo;i++)
1401 DelayLineIn(&State->Delay, State->Offset+i, early[2][i]);
1403 // Calculate the early reflection from the first delay tap.
1404 EarlyReflection(State, todo, early);
1406 // Calculate the late reverb from the decorrelator taps.
1407 LateReverb(State, todo, late);
1409 // Calculate and mix in any echo.
1410 EAXEcho(State, todo, late);
1412 // Step all delays forward.
1413 State->Offset += todo;
1416 static void DoMix(const ALfloat *restrict src, ALfloat (*dst)[BUFFERSIZE], ALuint num_chans,
1417 const ALfloat *restrict target_gains, ALfloat *restrict current_gains,
1418 ALfloat delta, ALuint offset, ALuint total_rem, ALuint todo)
1420 MixGains gains[MAX_OUTPUT_CHANNELS];
1421 ALuint c;
1423 for(c = 0;c < num_chans;c++)
1425 ALfloat diff;
1426 gains[c].Target = target_gains[c];
1427 gains[c].Current = current_gains[c];
1428 diff = gains[c].Target - gains[c].Current;
1429 if(fabsf(diff) >= GAIN_SILENCE_THRESHOLD)
1430 gains[c].Step = diff * delta;
1431 else
1433 gains[c].Current = gains[c].Target;
1434 gains[c].Step = 0.0f;
1438 MixSamples(src, num_chans, dst, gains, total_rem, offset, todo);
1440 for(c = 0;c < num_chans;c++)
1441 current_gains[c] = gains[c].Current;
1444 static ALvoid ALreverbState_processStandard(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
1446 ALfloat (*restrict early)[MAX_UPDATE_SAMPLES] = State->EarlySamples;
1447 ALfloat (*restrict late)[MAX_UPDATE_SAMPLES] = State->ReverbSamples;
1448 ALuint base, c;
1450 /* Process reverb for these samples. */
1451 for(base = 0;base < SamplesToDo;)
1453 const ALfloat delta = 1.0f / (ALfloat)(SamplesToDo-base);
1454 ALuint todo = minu(SamplesToDo-base, MAX_UPDATE_SAMPLES);
1456 VerbPass(State, todo, &SamplesIn[base], early, late);
1458 for(c = 0;c < 4;c++)
1460 DoMix(early[c], SamplesOut, NumChannels, State->Early.PanGain[c],
1461 State->Early.CurrentGain[c], delta, base, SamplesToDo-base, todo
1463 if(State->ExtraChannels > 0)
1464 DoMix(early[c], SamplesOut, State->ExtraChannels,
1465 State->Early.PanGain[c]+NumChannels,
1466 State->Early.CurrentGain[c]+NumChannels, delta, base,
1467 SamplesToDo-base, todo
1470 for(c = 0;c < 4;c++)
1472 DoMix(late[c], SamplesOut, NumChannels, State->Late.PanGain[c],
1473 State->Late.CurrentGain[c], delta, base, SamplesToDo, todo
1475 if(State->ExtraChannels > 0)
1476 DoMix(late[c], SamplesOut, State->ExtraChannels,
1477 State->Late.PanGain[c]+NumChannels,
1478 State->Late.CurrentGain[c]+NumChannels, delta, base,
1479 SamplesToDo-base, todo
1483 base += todo;
1487 static ALvoid ALreverbState_processEax(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
1489 ALfloat (*restrict early)[MAX_UPDATE_SAMPLES] = State->EarlySamples;
1490 ALfloat (*restrict late)[MAX_UPDATE_SAMPLES] = State->ReverbSamples;
1491 ALuint base, c;
1493 /* Process reverb for these samples. */
1494 for(base = 0;base < SamplesToDo;)
1496 const ALfloat delta = 1.0f / (ALfloat)(SamplesToDo-base);
1497 ALuint todo = minu(SamplesToDo-base, MAX_UPDATE_SAMPLES);
1499 EAXVerbPass(State, todo, &SamplesIn[base], early, late);
1501 for(c = 0;c < 4;c++)
1503 DoMix(early[c], SamplesOut, NumChannels, State->Early.PanGain[c],
1504 State->Early.CurrentGain[c], delta, base, SamplesToDo-base, todo
1506 if(State->ExtraChannels > 0)
1507 DoMix(early[c], SamplesOut, State->ExtraChannels,
1508 State->Early.PanGain[c]+NumChannels,
1509 State->Early.CurrentGain[c]+NumChannels, delta, base,
1510 SamplesToDo-base, todo
1513 for(c = 0;c < 4;c++)
1515 DoMix(late[c], SamplesOut, NumChannels, State->Late.PanGain[c],
1516 State->Late.CurrentGain[c], delta, base, SamplesToDo, todo
1518 if(State->ExtraChannels > 0)
1519 DoMix(late[c], SamplesOut, State->ExtraChannels,
1520 State->Late.PanGain[c]+NumChannels,
1521 State->Late.CurrentGain[c]+NumChannels, delta, base,
1522 SamplesToDo-base, todo
1526 base += todo;
1530 static ALvoid ALreverbState_process(ALreverbState *State, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
1532 if(State->IsEax)
1533 ALreverbState_processEax(State, SamplesToDo, SamplesIn[0], SamplesOut, NumChannels);
1534 else
1535 ALreverbState_processStandard(State, SamplesToDo, SamplesIn[0], SamplesOut, NumChannels);
1539 typedef struct ALreverbStateFactory {
1540 DERIVE_FROM_TYPE(ALeffectStateFactory);
1541 } ALreverbStateFactory;
1543 static ALeffectState *ALreverbStateFactory_create(ALreverbStateFactory* UNUSED(factory))
1545 ALreverbState *state;
1547 alcall_once(&mixfunc_inited, init_mixfunc);
1549 NEW_OBJ0(state, ALreverbState)();
1550 if(!state) return NULL;
1552 return STATIC_CAST(ALeffectState, state);
1555 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALreverbStateFactory);
1557 ALeffectStateFactory *ALreverbStateFactory_getFactory(void)
1559 static ALreverbStateFactory ReverbFactory = { { GET_VTABLE2(ALreverbStateFactory, ALeffectStateFactory) } };
1561 return STATIC_CAST(ALeffectStateFactory, &ReverbFactory);
1565 void ALeaxreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
1567 ALeffectProps *props = &effect->Props;
1568 switch(param)
1570 case AL_EAXREVERB_DECAY_HFLIMIT:
1571 if(!(val >= AL_EAXREVERB_MIN_DECAY_HFLIMIT && val <= AL_EAXREVERB_MAX_DECAY_HFLIMIT))
1572 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1573 props->Reverb.DecayHFLimit = val;
1574 break;
1576 default:
1577 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1580 void ALeaxreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
1582 ALeaxreverb_setParami(effect, context, param, vals[0]);
1584 void ALeaxreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
1586 ALeffectProps *props = &effect->Props;
1587 switch(param)
1589 case AL_EAXREVERB_DENSITY:
1590 if(!(val >= AL_EAXREVERB_MIN_DENSITY && val <= AL_EAXREVERB_MAX_DENSITY))
1591 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1592 props->Reverb.Density = val;
1593 break;
1595 case AL_EAXREVERB_DIFFUSION:
1596 if(!(val >= AL_EAXREVERB_MIN_DIFFUSION && val <= AL_EAXREVERB_MAX_DIFFUSION))
1597 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1598 props->Reverb.Diffusion = val;
1599 break;
1601 case AL_EAXREVERB_GAIN:
1602 if(!(val >= AL_EAXREVERB_MIN_GAIN && val <= AL_EAXREVERB_MAX_GAIN))
1603 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1604 props->Reverb.Gain = val;
1605 break;
1607 case AL_EAXREVERB_GAINHF:
1608 if(!(val >= AL_EAXREVERB_MIN_GAINHF && val <= AL_EAXREVERB_MAX_GAINHF))
1609 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1610 props->Reverb.GainHF = val;
1611 break;
1613 case AL_EAXREVERB_GAINLF:
1614 if(!(val >= AL_EAXREVERB_MIN_GAINLF && val <= AL_EAXREVERB_MAX_GAINLF))
1615 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1616 props->Reverb.GainLF = val;
1617 break;
1619 case AL_EAXREVERB_DECAY_TIME:
1620 if(!(val >= AL_EAXREVERB_MIN_DECAY_TIME && val <= AL_EAXREVERB_MAX_DECAY_TIME))
1621 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1622 props->Reverb.DecayTime = val;
1623 break;
1625 case AL_EAXREVERB_DECAY_HFRATIO:
1626 if(!(val >= AL_EAXREVERB_MIN_DECAY_HFRATIO && val <= AL_EAXREVERB_MAX_DECAY_HFRATIO))
1627 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1628 props->Reverb.DecayHFRatio = val;
1629 break;
1631 case AL_EAXREVERB_DECAY_LFRATIO:
1632 if(!(val >= AL_EAXREVERB_MIN_DECAY_LFRATIO && val <= AL_EAXREVERB_MAX_DECAY_LFRATIO))
1633 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1634 props->Reverb.DecayLFRatio = val;
1635 break;
1637 case AL_EAXREVERB_REFLECTIONS_GAIN:
1638 if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_GAIN && val <= AL_EAXREVERB_MAX_REFLECTIONS_GAIN))
1639 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1640 props->Reverb.ReflectionsGain = val;
1641 break;
1643 case AL_EAXREVERB_REFLECTIONS_DELAY:
1644 if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_DELAY && val <= AL_EAXREVERB_MAX_REFLECTIONS_DELAY))
1645 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1646 props->Reverb.ReflectionsDelay = val;
1647 break;
1649 case AL_EAXREVERB_LATE_REVERB_GAIN:
1650 if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_GAIN && val <= AL_EAXREVERB_MAX_LATE_REVERB_GAIN))
1651 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1652 props->Reverb.LateReverbGain = val;
1653 break;
1655 case AL_EAXREVERB_LATE_REVERB_DELAY:
1656 if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_DELAY && val <= AL_EAXREVERB_MAX_LATE_REVERB_DELAY))
1657 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1658 props->Reverb.LateReverbDelay = val;
1659 break;
1661 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
1662 if(!(val >= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF))
1663 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1664 props->Reverb.AirAbsorptionGainHF = val;
1665 break;
1667 case AL_EAXREVERB_ECHO_TIME:
1668 if(!(val >= AL_EAXREVERB_MIN_ECHO_TIME && val <= AL_EAXREVERB_MAX_ECHO_TIME))
1669 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1670 props->Reverb.EchoTime = val;
1671 break;
1673 case AL_EAXREVERB_ECHO_DEPTH:
1674 if(!(val >= AL_EAXREVERB_MIN_ECHO_DEPTH && val <= AL_EAXREVERB_MAX_ECHO_DEPTH))
1675 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1676 props->Reverb.EchoDepth = val;
1677 break;
1679 case AL_EAXREVERB_MODULATION_TIME:
1680 if(!(val >= AL_EAXREVERB_MIN_MODULATION_TIME && val <= AL_EAXREVERB_MAX_MODULATION_TIME))
1681 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1682 props->Reverb.ModulationTime = val;
1683 break;
1685 case AL_EAXREVERB_MODULATION_DEPTH:
1686 if(!(val >= AL_EAXREVERB_MIN_MODULATION_DEPTH && val <= AL_EAXREVERB_MAX_MODULATION_DEPTH))
1687 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1688 props->Reverb.ModulationDepth = val;
1689 break;
1691 case AL_EAXREVERB_HFREFERENCE:
1692 if(!(val >= AL_EAXREVERB_MIN_HFREFERENCE && val <= AL_EAXREVERB_MAX_HFREFERENCE))
1693 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1694 props->Reverb.HFReference = val;
1695 break;
1697 case AL_EAXREVERB_LFREFERENCE:
1698 if(!(val >= AL_EAXREVERB_MIN_LFREFERENCE && val <= AL_EAXREVERB_MAX_LFREFERENCE))
1699 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1700 props->Reverb.LFReference = val;
1701 break;
1703 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
1704 if(!(val >= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR))
1705 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1706 props->Reverb.RoomRolloffFactor = val;
1707 break;
1709 default:
1710 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1713 void ALeaxreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
1715 ALeffectProps *props = &effect->Props;
1716 switch(param)
1718 case AL_EAXREVERB_REFLECTIONS_PAN:
1719 if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
1720 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1721 props->Reverb.ReflectionsPan[0] = vals[0];
1722 props->Reverb.ReflectionsPan[1] = vals[1];
1723 props->Reverb.ReflectionsPan[2] = vals[2];
1724 break;
1725 case AL_EAXREVERB_LATE_REVERB_PAN:
1726 if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
1727 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1728 props->Reverb.LateReverbPan[0] = vals[0];
1729 props->Reverb.LateReverbPan[1] = vals[1];
1730 props->Reverb.LateReverbPan[2] = vals[2];
1731 break;
1733 default:
1734 ALeaxreverb_setParamf(effect, context, param, vals[0]);
1735 break;
1739 void ALeaxreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
1741 const ALeffectProps *props = &effect->Props;
1742 switch(param)
1744 case AL_EAXREVERB_DECAY_HFLIMIT:
1745 *val = props->Reverb.DecayHFLimit;
1746 break;
1748 default:
1749 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1752 void ALeaxreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
1754 ALeaxreverb_getParami(effect, context, param, vals);
1756 void ALeaxreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
1758 const ALeffectProps *props = &effect->Props;
1759 switch(param)
1761 case AL_EAXREVERB_DENSITY:
1762 *val = props->Reverb.Density;
1763 break;
1765 case AL_EAXREVERB_DIFFUSION:
1766 *val = props->Reverb.Diffusion;
1767 break;
1769 case AL_EAXREVERB_GAIN:
1770 *val = props->Reverb.Gain;
1771 break;
1773 case AL_EAXREVERB_GAINHF:
1774 *val = props->Reverb.GainHF;
1775 break;
1777 case AL_EAXREVERB_GAINLF:
1778 *val = props->Reverb.GainLF;
1779 break;
1781 case AL_EAXREVERB_DECAY_TIME:
1782 *val = props->Reverb.DecayTime;
1783 break;
1785 case AL_EAXREVERB_DECAY_HFRATIO:
1786 *val = props->Reverb.DecayHFRatio;
1787 break;
1789 case AL_EAXREVERB_DECAY_LFRATIO:
1790 *val = props->Reverb.DecayLFRatio;
1791 break;
1793 case AL_EAXREVERB_REFLECTIONS_GAIN:
1794 *val = props->Reverb.ReflectionsGain;
1795 break;
1797 case AL_EAXREVERB_REFLECTIONS_DELAY:
1798 *val = props->Reverb.ReflectionsDelay;
1799 break;
1801 case AL_EAXREVERB_LATE_REVERB_GAIN:
1802 *val = props->Reverb.LateReverbGain;
1803 break;
1805 case AL_EAXREVERB_LATE_REVERB_DELAY:
1806 *val = props->Reverb.LateReverbDelay;
1807 break;
1809 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
1810 *val = props->Reverb.AirAbsorptionGainHF;
1811 break;
1813 case AL_EAXREVERB_ECHO_TIME:
1814 *val = props->Reverb.EchoTime;
1815 break;
1817 case AL_EAXREVERB_ECHO_DEPTH:
1818 *val = props->Reverb.EchoDepth;
1819 break;
1821 case AL_EAXREVERB_MODULATION_TIME:
1822 *val = props->Reverb.ModulationTime;
1823 break;
1825 case AL_EAXREVERB_MODULATION_DEPTH:
1826 *val = props->Reverb.ModulationDepth;
1827 break;
1829 case AL_EAXREVERB_HFREFERENCE:
1830 *val = props->Reverb.HFReference;
1831 break;
1833 case AL_EAXREVERB_LFREFERENCE:
1834 *val = props->Reverb.LFReference;
1835 break;
1837 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
1838 *val = props->Reverb.RoomRolloffFactor;
1839 break;
1841 default:
1842 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1845 void ALeaxreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
1847 const ALeffectProps *props = &effect->Props;
1848 switch(param)
1850 case AL_EAXREVERB_REFLECTIONS_PAN:
1851 vals[0] = props->Reverb.ReflectionsPan[0];
1852 vals[1] = props->Reverb.ReflectionsPan[1];
1853 vals[2] = props->Reverb.ReflectionsPan[2];
1854 break;
1855 case AL_EAXREVERB_LATE_REVERB_PAN:
1856 vals[0] = props->Reverb.LateReverbPan[0];
1857 vals[1] = props->Reverb.LateReverbPan[1];
1858 vals[2] = props->Reverb.LateReverbPan[2];
1859 break;
1861 default:
1862 ALeaxreverb_getParamf(effect, context, param, vals);
1863 break;
1867 DEFINE_ALEFFECT_VTABLE(ALeaxreverb);
1869 void ALreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
1871 ALeffectProps *props = &effect->Props;
1872 switch(param)
1874 case AL_REVERB_DECAY_HFLIMIT:
1875 if(!(val >= AL_REVERB_MIN_DECAY_HFLIMIT && val <= AL_REVERB_MAX_DECAY_HFLIMIT))
1876 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1877 props->Reverb.DecayHFLimit = val;
1878 break;
1880 default:
1881 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1884 void ALreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
1886 ALreverb_setParami(effect, context, param, vals[0]);
1888 void ALreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
1890 ALeffectProps *props = &effect->Props;
1891 switch(param)
1893 case AL_REVERB_DENSITY:
1894 if(!(val >= AL_REVERB_MIN_DENSITY && val <= AL_REVERB_MAX_DENSITY))
1895 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1896 props->Reverb.Density = val;
1897 break;
1899 case AL_REVERB_DIFFUSION:
1900 if(!(val >= AL_REVERB_MIN_DIFFUSION && val <= AL_REVERB_MAX_DIFFUSION))
1901 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1902 props->Reverb.Diffusion = val;
1903 break;
1905 case AL_REVERB_GAIN:
1906 if(!(val >= AL_REVERB_MIN_GAIN && val <= AL_REVERB_MAX_GAIN))
1907 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1908 props->Reverb.Gain = val;
1909 break;
1911 case AL_REVERB_GAINHF:
1912 if(!(val >= AL_REVERB_MIN_GAINHF && val <= AL_REVERB_MAX_GAINHF))
1913 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1914 props->Reverb.GainHF = val;
1915 break;
1917 case AL_REVERB_DECAY_TIME:
1918 if(!(val >= AL_REVERB_MIN_DECAY_TIME && val <= AL_REVERB_MAX_DECAY_TIME))
1919 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1920 props->Reverb.DecayTime = val;
1921 break;
1923 case AL_REVERB_DECAY_HFRATIO:
1924 if(!(val >= AL_REVERB_MIN_DECAY_HFRATIO && val <= AL_REVERB_MAX_DECAY_HFRATIO))
1925 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1926 props->Reverb.DecayHFRatio = val;
1927 break;
1929 case AL_REVERB_REFLECTIONS_GAIN:
1930 if(!(val >= AL_REVERB_MIN_REFLECTIONS_GAIN && val <= AL_REVERB_MAX_REFLECTIONS_GAIN))
1931 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1932 props->Reverb.ReflectionsGain = val;
1933 break;
1935 case AL_REVERB_REFLECTIONS_DELAY:
1936 if(!(val >= AL_REVERB_MIN_REFLECTIONS_DELAY && val <= AL_REVERB_MAX_REFLECTIONS_DELAY))
1937 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1938 props->Reverb.ReflectionsDelay = val;
1939 break;
1941 case AL_REVERB_LATE_REVERB_GAIN:
1942 if(!(val >= AL_REVERB_MIN_LATE_REVERB_GAIN && val <= AL_REVERB_MAX_LATE_REVERB_GAIN))
1943 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1944 props->Reverb.LateReverbGain = val;
1945 break;
1947 case AL_REVERB_LATE_REVERB_DELAY:
1948 if(!(val >= AL_REVERB_MIN_LATE_REVERB_DELAY && val <= AL_REVERB_MAX_LATE_REVERB_DELAY))
1949 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1950 props->Reverb.LateReverbDelay = val;
1951 break;
1953 case AL_REVERB_AIR_ABSORPTION_GAINHF:
1954 if(!(val >= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF))
1955 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1956 props->Reverb.AirAbsorptionGainHF = val;
1957 break;
1959 case AL_REVERB_ROOM_ROLLOFF_FACTOR:
1960 if(!(val >= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR))
1961 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1962 props->Reverb.RoomRolloffFactor = val;
1963 break;
1965 default:
1966 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1969 void ALreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
1971 ALreverb_setParamf(effect, context, param, vals[0]);
1974 void ALreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
1976 const ALeffectProps *props = &effect->Props;
1977 switch(param)
1979 case AL_REVERB_DECAY_HFLIMIT:
1980 *val = props->Reverb.DecayHFLimit;
1981 break;
1983 default:
1984 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1987 void ALreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
1989 ALreverb_getParami(effect, context, param, vals);
1991 void ALreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
1993 const ALeffectProps *props = &effect->Props;
1994 switch(param)
1996 case AL_REVERB_DENSITY:
1997 *val = props->Reverb.Density;
1998 break;
2000 case AL_REVERB_DIFFUSION:
2001 *val = props->Reverb.Diffusion;
2002 break;
2004 case AL_REVERB_GAIN:
2005 *val = props->Reverb.Gain;
2006 break;
2008 case AL_REVERB_GAINHF:
2009 *val = props->Reverb.GainHF;
2010 break;
2012 case AL_REVERB_DECAY_TIME:
2013 *val = props->Reverb.DecayTime;
2014 break;
2016 case AL_REVERB_DECAY_HFRATIO:
2017 *val = props->Reverb.DecayHFRatio;
2018 break;
2020 case AL_REVERB_REFLECTIONS_GAIN:
2021 *val = props->Reverb.ReflectionsGain;
2022 break;
2024 case AL_REVERB_REFLECTIONS_DELAY:
2025 *val = props->Reverb.ReflectionsDelay;
2026 break;
2028 case AL_REVERB_LATE_REVERB_GAIN:
2029 *val = props->Reverb.LateReverbGain;
2030 break;
2032 case AL_REVERB_LATE_REVERB_DELAY:
2033 *val = props->Reverb.LateReverbDelay;
2034 break;
2036 case AL_REVERB_AIR_ABSORPTION_GAINHF:
2037 *val = props->Reverb.AirAbsorptionGainHF;
2038 break;
2040 case AL_REVERB_ROOM_ROLLOFF_FACTOR:
2041 *val = props->Reverb.RoomRolloffFactor;
2042 break;
2044 default:
2045 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
2048 void ALreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
2050 ALreverb_getParamf(effect, context, param, vals);
2053 DEFINE_ALEFFECT_VTABLE(ALreverb);