Hold the effect and filter maps while handling effects and filters
[openal-soft.git] / Alc / effects / reverb.c
blob0f851295aa13ddcd1ee978676328397c40375777
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"
35 /* This is the maximum number of samples processed for each inner loop
36 * iteration. */
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.
43 ALuint Mask;
44 ALfloat *Line;
45 } DelayLine;
47 typedef struct ALreverbState {
48 DERIVE_FROM_TYPE(ALeffectState);
50 ALboolean IsEax;
51 ALuint ExtraChannels; // For HRTF
53 // All delay lines are allocated as a single buffer to reduce memory
54 // fragmentation and management code.
55 ALfloat *SampleBuffer;
56 ALuint TotalSamples;
58 // Master effect filters
59 ALfilterState LpFilter;
60 ALfilterState HpFilter; // EAX only
62 struct {
63 // Modulator delay line.
64 DelayLine Delay;
66 // The vibrato time is tracked with an index over a modulus-wrapped
67 // range (in samples).
68 ALuint Index;
69 ALuint Range;
71 // The depth of frequency change (also in samples) and its filter.
72 ALfloat Depth;
73 ALfloat Coeff;
74 ALfloat Filter;
75 } Mod; // EAX only
77 // Initial effect delay.
78 DelayLine Delay;
79 // The tap points for the initial delay. First tap goes to early
80 // reflections, the last to late reverb.
81 ALuint DelayTap[2];
83 struct {
84 // Early reflections are done with 4 delay lines.
85 ALfloat Coeff[4];
86 DelayLine Delay[4];
87 ALuint Offset[4];
89 // The gain for each output channel based on 3D panning.
90 // NOTE: With certain output modes, we may be rendering to the dry
91 // buffer and the "real" buffer. The two combined may be using more
92 // than the max output channels, so we need some extra for the real
93 // output too.
94 ALfloat PanGain[4][MAX_OUTPUT_CHANNELS*2];
95 } Early;
97 // Decorrelator delay line.
98 DelayLine Decorrelator;
99 // There are actually 4 decorrelator taps, but the first occurs at the
100 // initial sample.
101 ALuint DecoTap[3];
103 struct {
104 // Output gain for late reverb.
105 ALfloat Gain;
107 // Attenuation to compensate for the modal density and decay rate of
108 // the late lines.
109 ALfloat DensityGain;
111 // The feed-back and feed-forward all-pass coefficient.
112 ALfloat ApFeedCoeff;
114 // Mixing matrix coefficient.
115 ALfloat MixCoeff;
117 // Late reverb has 4 parallel all-pass filters.
118 ALfloat ApCoeff[4];
119 DelayLine ApDelay[4];
120 ALuint ApOffset[4];
122 // In addition to 4 cyclical delay lines.
123 ALfloat Coeff[4];
124 DelayLine Delay[4];
125 ALuint Offset[4];
127 // The cyclical delay lines are 1-pole low-pass filtered.
128 ALfloat LpCoeff[4];
129 ALfloat LpSample[4];
131 // The gain for each output channel based on 3D panning.
132 // NOTE: Add some extra in case (see note about early pan).
133 ALfloat PanGain[4][MAX_OUTPUT_CHANNELS*2];
134 } Late;
136 struct {
137 // Attenuation to compensate for the modal density and decay rate of
138 // the echo line.
139 ALfloat DensityGain;
141 // Echo delay and all-pass lines.
142 DelayLine Delay;
143 DelayLine ApDelay;
145 ALfloat Coeff;
146 ALfloat ApFeedCoeff;
147 ALfloat ApCoeff;
149 ALuint Offset;
150 ALuint ApOffset;
152 // The echo line is 1-pole low-pass filtered.
153 ALfloat LpCoeff;
154 ALfloat LpSample;
156 // Echo mixing coefficient.
157 ALfloat MixCoeff;
158 } Echo; // EAX only
160 // The current read offset for all delay lines.
161 ALuint Offset;
163 /* Temporary storage used when processing. */
164 ALfloat ReverbSamples[MAX_UPDATE_SAMPLES][4];
165 ALfloat EarlySamples[MAX_UPDATE_SAMPLES][4];
166 } ALreverbState;
168 static ALvoid ALreverbState_Destruct(ALreverbState *State)
170 free(State->SampleBuffer);
171 State->SampleBuffer = NULL;
172 ALeffectState_Destruct(STATIC_CAST(ALeffectState,State));
175 static ALboolean ALreverbState_deviceUpdate(ALreverbState *State, ALCdevice *Device);
176 static ALvoid ALreverbState_update(ALreverbState *State, const ALCdevice *Device, const ALeffectslot *Slot);
177 static ALvoid ALreverbState_processStandard(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
178 static ALvoid ALreverbState_processEax(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
179 static ALvoid ALreverbState_process(ALreverbState *State, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
180 DECLARE_DEFAULT_ALLOCATORS(ALreverbState)
182 DEFINE_ALEFFECTSTATE_VTABLE(ALreverbState);
184 /* This is a user config option for modifying the overall output of the reverb
185 * effect.
187 ALfloat ReverbBoost = 1.0f;
189 /* Specifies whether to use a standard reverb effect in place of EAX reverb (no
190 * high-pass, modulation, or echo).
192 ALboolean EmulateEAXReverb = AL_FALSE;
194 /* This coefficient is used to define the maximum frequency range controlled
195 * by the modulation depth. The current value of 0.1 will allow it to swing
196 * from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the
197 * sampler to stall on the downswing, and above 1 it will cause it to sample
198 * backwards.
200 static const ALfloat MODULATION_DEPTH_COEFF = 0.1f;
202 /* A filter is used to avoid the terrible distortion caused by changing
203 * modulation time and/or depth. To be consistent across different sample
204 * rates, the coefficient must be raised to a constant divided by the sample
205 * rate: coeff^(constant / rate).
207 static const ALfloat MODULATION_FILTER_COEFF = 0.048f;
208 static const ALfloat MODULATION_FILTER_CONST = 100000.0f;
210 // When diffusion is above 0, an all-pass filter is used to take the edge off
211 // the echo effect. It uses the following line length (in seconds).
212 static const ALfloat ECHO_ALLPASS_LENGTH = 0.0133f;
214 // Input into the late reverb is decorrelated between four channels. Their
215 // timings are dependent on a fraction and multiplier. See the
216 // UpdateDecorrelator() routine for the calculations involved.
217 static const ALfloat DECO_FRACTION = 0.15f;
218 static const ALfloat DECO_MULTIPLIER = 2.0f;
220 // All delay line lengths are specified in seconds.
222 // The lengths of the early delay lines.
223 static const ALfloat EARLY_LINE_LENGTH[4] =
225 0.0015f, 0.0045f, 0.0135f, 0.0405f
228 // The lengths of the late all-pass delay lines.
229 static const ALfloat ALLPASS_LINE_LENGTH[4] =
231 0.0151f, 0.0167f, 0.0183f, 0.0200f,
234 // The lengths of the late cyclical delay lines.
235 static const ALfloat LATE_LINE_LENGTH[4] =
237 0.0211f, 0.0311f, 0.0461f, 0.0680f
240 // The late cyclical delay lines have a variable length dependent on the
241 // effect's density parameter (inverted for some reason) and this multiplier.
242 static const ALfloat LATE_LINE_MULTIPLIER = 4.0f;
245 #if defined(_WIN32) && !defined (_M_X64) && !defined(_M_ARM)
246 /* HACK: Workaround for a modff bug in 32-bit Windows, which attempts to write
247 * a 64-bit double to the 32-bit float parameter.
249 static inline float hack_modff(float x, float *y)
251 double di;
252 double df = modf((double)x, &di);
253 *y = (float)di;
254 return (float)df;
256 #define modff hack_modff
257 #endif
260 /**************************************
261 * Device Update *
262 **************************************/
264 // Given the allocated sample buffer, this function updates each delay line
265 // offset.
266 static inline ALvoid RealizeLineOffset(ALfloat *sampleBuffer, DelayLine *Delay)
268 Delay->Line = &sampleBuffer[(ptrdiff_t)Delay->Line];
271 // Calculate the length of a delay line and store its mask and offset.
272 static ALuint CalcLineLength(ALfloat length, ptrdiff_t offset, ALuint frequency, ALuint extra, DelayLine *Delay)
274 ALuint samples;
276 // All line lengths are powers of 2, calculated from their lengths, with
277 // an additional sample in case of rounding errors.
278 samples = fastf2u(length*frequency) + extra;
279 samples = NextPowerOf2(samples + 1);
280 // All lines share a single sample buffer.
281 Delay->Mask = samples - 1;
282 Delay->Line = (ALfloat*)offset;
283 // Return the sample count for accumulation.
284 return samples;
287 /* Calculates the delay line metrics and allocates the shared sample buffer
288 * for all lines given the sample rate (frequency). If an allocation failure
289 * occurs, it returns AL_FALSE.
291 static ALboolean AllocLines(ALuint frequency, ALreverbState *State)
293 ALuint totalSamples, index;
294 ALfloat length;
295 ALfloat *newBuffer = NULL;
297 // All delay line lengths are calculated to accomodate the full range of
298 // lengths given their respective paramters.
299 totalSamples = 0;
301 /* The modulator's line length is calculated from the maximum modulation
302 * time and depth coefficient, and halfed for the low-to-high frequency
303 * swing. An additional sample is added to keep it stable when there is no
304 * modulation.
306 length = (AL_EAXREVERB_MAX_MODULATION_TIME*MODULATION_DEPTH_COEFF/2.0f);
307 totalSamples += CalcLineLength(length, totalSamples, frequency, 1,
308 &State->Mod.Delay);
310 // The initial delay is the sum of the reflections and late reverb
311 // delays. This must include space for storing a loop update to feed the
312 // early reflections, decorrelator, and echo.
313 length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY +
314 AL_EAXREVERB_MAX_LATE_REVERB_DELAY;
315 totalSamples += CalcLineLength(length, totalSamples, frequency,
316 MAX_UPDATE_SAMPLES, &State->Delay);
318 // The early reflection lines.
319 for(index = 0;index < 4;index++)
320 totalSamples += CalcLineLength(EARLY_LINE_LENGTH[index], totalSamples,
321 frequency, 0, &State->Early.Delay[index]);
323 // The decorrelator line is calculated from the lowest reverb density (a
324 // parameter value of 1). This must include space for storing a loop update
325 // to feed the late reverb.
326 length = (DECO_FRACTION * DECO_MULTIPLIER * DECO_MULTIPLIER) *
327 LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER);
328 totalSamples += CalcLineLength(length, totalSamples, frequency, MAX_UPDATE_SAMPLES,
329 &State->Decorrelator);
331 // The late all-pass lines.
332 for(index = 0;index < 4;index++)
333 totalSamples += CalcLineLength(ALLPASS_LINE_LENGTH[index], totalSamples,
334 frequency, 0, &State->Late.ApDelay[index]);
336 // The late delay lines are calculated from the lowest reverb density.
337 for(index = 0;index < 4;index++)
339 length = LATE_LINE_LENGTH[index] * (1.0f + LATE_LINE_MULTIPLIER);
340 totalSamples += CalcLineLength(length, totalSamples, frequency, 0,
341 &State->Late.Delay[index]);
344 // The echo all-pass and delay lines.
345 totalSamples += CalcLineLength(ECHO_ALLPASS_LENGTH, totalSamples,
346 frequency, 0, &State->Echo.ApDelay);
347 totalSamples += CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME, totalSamples,
348 frequency, 0, &State->Echo.Delay);
350 if(totalSamples != State->TotalSamples)
352 TRACE("New reverb buffer length: %u samples (%f sec)\n", totalSamples, totalSamples/(float)frequency);
353 newBuffer = realloc(State->SampleBuffer, sizeof(ALfloat) * totalSamples);
354 if(newBuffer == NULL)
355 return AL_FALSE;
356 State->SampleBuffer = newBuffer;
357 State->TotalSamples = totalSamples;
360 // Update all delays to reflect the new sample buffer.
361 RealizeLineOffset(State->SampleBuffer, &State->Delay);
362 RealizeLineOffset(State->SampleBuffer, &State->Decorrelator);
363 for(index = 0;index < 4;index++)
365 RealizeLineOffset(State->SampleBuffer, &State->Early.Delay[index]);
366 RealizeLineOffset(State->SampleBuffer, &State->Late.ApDelay[index]);
367 RealizeLineOffset(State->SampleBuffer, &State->Late.Delay[index]);
369 RealizeLineOffset(State->SampleBuffer, &State->Mod.Delay);
370 RealizeLineOffset(State->SampleBuffer, &State->Echo.ApDelay);
371 RealizeLineOffset(State->SampleBuffer, &State->Echo.Delay);
373 // Clear the sample buffer.
374 for(index = 0;index < State->TotalSamples;index++)
375 State->SampleBuffer[index] = 0.0f;
377 return AL_TRUE;
380 static ALboolean ALreverbState_deviceUpdate(ALreverbState *State, ALCdevice *Device)
382 ALuint frequency = Device->Frequency, index;
384 // Allocate the delay lines.
385 if(!AllocLines(frequency, State))
386 return AL_FALSE;
388 /* WARNING: This assumes the real output follows the virtual output in the
389 * device's DryBuffer.
391 if(Device->Hrtf || Device->Uhj_Encoder)
392 State->ExtraChannels = ChannelsFromDevFmt(Device->FmtChans);
393 else
394 State->ExtraChannels = 0;
396 // Calculate the modulation filter coefficient. Notice that the exponent
397 // is calculated given the current sample rate. This ensures that the
398 // resulting filter response over time is consistent across all sample
399 // rates.
400 State->Mod.Coeff = powf(MODULATION_FILTER_COEFF,
401 MODULATION_FILTER_CONST / frequency);
403 // The early reflection and late all-pass filter line lengths are static,
404 // so their offsets only need to be calculated once.
405 for(index = 0;index < 4;index++)
407 State->Early.Offset[index] = fastf2u(EARLY_LINE_LENGTH[index] * frequency);
408 State->Late.ApOffset[index] = fastf2u(ALLPASS_LINE_LENGTH[index] * frequency);
411 // The echo all-pass filter line length is static, so its offset only
412 // needs to be calculated once.
413 State->Echo.ApOffset = fastf2u(ECHO_ALLPASS_LENGTH * frequency);
415 return AL_TRUE;
418 /**************************************
419 * Effect Update *
420 **************************************/
422 // Calculate a decay coefficient given the length of each cycle and the time
423 // until the decay reaches -60 dB.
424 static inline ALfloat CalcDecayCoeff(ALfloat length, ALfloat decayTime)
426 return powf(0.001f/*-60 dB*/, length/decayTime);
429 // Calculate a decay length from a coefficient and the time until the decay
430 // reaches -60 dB.
431 static inline ALfloat CalcDecayLength(ALfloat coeff, ALfloat decayTime)
433 return log10f(coeff) * decayTime / log10f(0.001f)/*-60 dB*/;
436 // Calculate an attenuation to be applied to the input of any echo models to
437 // compensate for modal density and decay time.
438 static inline ALfloat CalcDensityGain(ALfloat a)
440 /* The energy of a signal can be obtained by finding the area under the
441 * squared signal. This takes the form of Sum(x_n^2), where x is the
442 * amplitude for the sample n.
444 * Decaying feedback matches exponential decay of the form Sum(a^n),
445 * where a is the attenuation coefficient, and n is the sample. The area
446 * under this decay curve can be calculated as: 1 / (1 - a).
448 * Modifying the above equation to find the squared area under the curve
449 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
450 * calculated by inverting the square root of this approximation,
451 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
453 return sqrtf(1.0f - (a * a));
456 // Calculate the mixing matrix coefficients given a diffusion factor.
457 static inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)
459 ALfloat n, t;
461 // The matrix is of order 4, so n is sqrt (4 - 1).
462 n = sqrtf(3.0f);
463 t = diffusion * atanf(n);
465 // Calculate the first mixing matrix coefficient.
466 *x = cosf(t);
467 // Calculate the second mixing matrix coefficient.
468 *y = sinf(t) / n;
471 // Calculate the limited HF ratio for use with the late reverb low-pass
472 // filters.
473 static ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)
475 ALfloat limitRatio;
477 /* Find the attenuation due to air absorption in dB (converting delay
478 * time to meters using the speed of sound). Then reversing the decay
479 * equation, solve for HF ratio. The delay length is cancelled out of
480 * the equation, so it can be calculated once for all lines.
482 limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *
483 SPEEDOFSOUNDMETRESPERSEC);
484 /* Using the limit calculated above, apply the upper bound to the HF
485 * ratio. Also need to limit the result to a minimum of 0.1, just like the
486 * HF ratio parameter. */
487 return clampf(limitRatio, 0.1f, hfRatio);
490 // Calculate the coefficient for a HF (and eventually LF) decay damping
491 // filter.
492 static inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)
494 ALfloat coeff, g;
496 // Eventually this should boost the high frequencies when the ratio
497 // exceeds 1.
498 coeff = 0.0f;
499 if (hfRatio < 1.0f)
501 // Calculate the low-pass coefficient by dividing the HF decay
502 // coefficient by the full decay coefficient.
503 g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;
505 // Damping is done with a 1-pole filter, so g needs to be squared.
506 g *= g;
507 if(g < 0.9999f) /* 1-epsilon */
509 /* Be careful with gains < 0.001, as that causes the coefficient
510 * head towards 1, which will flatten the signal. */
511 g = maxf(g, 0.001f);
512 coeff = (1 - g*cw - sqrtf(2*g*(1-cw) - g*g*(1 - cw*cw))) /
513 (1 - g);
516 // Very low decay times will produce minimal output, so apply an
517 // upper bound to the coefficient.
518 coeff = minf(coeff, 0.98f);
520 return coeff;
523 // Update the EAX modulation index, range, and depth. Keep in mind that this
524 // kind of vibrato is additive and not multiplicative as one may expect. The
525 // downswing will sound stronger than the upswing.
526 static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALreverbState *State)
528 ALuint range;
530 /* Modulation is calculated in two parts.
532 * The modulation time effects the sinus applied to the change in
533 * frequency. An index out of the current time range (both in samples)
534 * is incremented each sample. The range is bound to a reasonable
535 * minimum (1 sample) and when the timing changes, the index is rescaled
536 * to the new range (to keep the sinus consistent).
538 range = maxu(fastf2u(modTime*frequency), 1);
539 State->Mod.Index = (ALuint)(State->Mod.Index * (ALuint64)range /
540 State->Mod.Range);
541 State->Mod.Range = range;
543 /* The modulation depth effects the amount of frequency change over the
544 * range of the sinus. It needs to be scaled by the modulation time so
545 * that a given depth produces a consistent change in frequency over all
546 * ranges of time. Since the depth is applied to a sinus value, it needs
547 * to be halfed once for the sinus range and again for the sinus swing
548 * in time (half of it is spent decreasing the frequency, half is spent
549 * increasing it).
551 State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /
552 2.0f * frequency;
555 // Update the offsets for the initial effect delay line.
556 static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALreverbState *State)
558 // Calculate the initial delay taps.
559 State->DelayTap[0] = fastf2u(earlyDelay * frequency);
560 State->DelayTap[1] = fastf2u((earlyDelay + lateDelay) * frequency);
563 // Update the early reflections mix and line coefficients.
564 static ALvoid UpdateEarlyLines(ALfloat lateDelay, ALreverbState *State)
566 ALuint index;
568 // Calculate the gain (coefficient) for each early delay line using the
569 // late delay time. This expands the early reflections to the start of
570 // the late reverb.
571 for(index = 0;index < 4;index++)
572 State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],
573 lateDelay);
576 // Update the offsets for the decorrelator line.
577 static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALreverbState *State)
579 ALuint index;
580 ALfloat length;
582 /* The late reverb inputs are decorrelated to smooth the reverb tail and
583 * reduce harsh echos. The first tap occurs immediately, while the
584 * remaining taps are delayed by multiples of a fraction of the smallest
585 * cyclical delay time.
587 * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
589 for(index = 0;index < 3;index++)
591 length = (DECO_FRACTION * powf(DECO_MULTIPLIER, (ALfloat)index)) *
592 LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));
593 State->DecoTap[index] = fastf2u(length * frequency);
597 // Update the late reverb mix, line lengths, and line coefficients.
598 static ALvoid UpdateLateLines(ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALreverbState *State)
600 ALfloat length;
601 ALuint index;
603 /* Calculate the late reverb gain. Since the output is tapped prior to the
604 * application of the next delay line coefficients, this gain needs to be
605 * attenuated by the 'x' mixing matrix coefficient as well. Also attenuate
606 * the late reverb when echo depth is high and diffusion is low, so the
607 * echo is slightly stronger than the decorrelated echos in the reverb
608 * tail.
610 State->Late.Gain = xMix * (1.0f - (echoDepth*0.5f*(1.0f - diffusion)));
612 /* To compensate for changes in modal density and decay time of the late
613 * reverb signal, the input is attenuated based on the maximal energy of
614 * the outgoing signal. This approximation is used to keep the apparent
615 * energy of the signal equal for all ranges of density and decay time.
617 * The average length of the cyclcical delay lines is used to calculate
618 * the attenuation coefficient.
620 length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
621 LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;
622 length *= 1.0f + (density * LATE_LINE_MULTIPLIER);
623 State->Late.DensityGain = CalcDensityGain(
624 CalcDecayCoeff(length, decayTime)
627 // Calculate the all-pass feed-back and feed-forward coefficient.
628 State->Late.ApFeedCoeff = 0.5f * powf(diffusion, 2.0f);
630 for(index = 0;index < 4;index++)
632 // Calculate the gain (coefficient) for each all-pass line.
633 State->Late.ApCoeff[index] = CalcDecayCoeff(
634 ALLPASS_LINE_LENGTH[index], decayTime
637 // Calculate the length (in seconds) of each cyclical delay line.
638 length = LATE_LINE_LENGTH[index] *
639 (1.0f + (density * LATE_LINE_MULTIPLIER));
641 // Calculate the delay offset for each cyclical delay line.
642 State->Late.Offset[index] = fastf2u(length * frequency);
644 // Calculate the gain (coefficient) for each cyclical line.
645 State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);
647 // Calculate the damping coefficient for each low-pass filter.
648 State->Late.LpCoeff[index] = CalcDampingCoeff(
649 hfRatio, length, decayTime, State->Late.Coeff[index], cw
652 // Attenuate the cyclical line coefficients by the mixing coefficient
653 // (x).
654 State->Late.Coeff[index] *= xMix;
658 // Update the echo gain, line offset, line coefficients, and mixing
659 // coefficients.
660 static ALvoid UpdateEchoLine(ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALreverbState *State)
662 // Update the offset and coefficient for the echo delay line.
663 State->Echo.Offset = fastf2u(echoTime * frequency);
665 // Calculate the decay coefficient for the echo line.
666 State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);
668 // Calculate the energy-based attenuation coefficient for the echo delay
669 // line.
670 State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);
672 // Calculate the echo all-pass feed coefficient.
673 State->Echo.ApFeedCoeff = 0.5f * powf(diffusion, 2.0f);
675 // Calculate the echo all-pass attenuation coefficient.
676 State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);
678 // Calculate the damping coefficient for each low-pass filter.
679 State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,
680 State->Echo.Coeff, cw);
682 /* Calculate the echo mixing coefficient. This is applied to the output mix
683 * only, not the feedback.
685 State->Echo.MixCoeff = echoDepth;
688 // Update the early and late 3D panning gains.
689 static ALvoid UpdateMixedPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
691 ALfloat DirGains[MAX_OUTPUT_CHANNELS];
692 ALfloat coeffs[MAX_AMBI_COEFFS];
693 ALfloat length;
694 ALuint i;
696 /* With HRTF or UHJ, the normal output provides a panned reverb channel
697 * when a non-0-length vector is specified, while the real stereo output
698 * provides two other "direct" non-panned reverb channels.
700 * WARNING: This assumes the real output follows the virtual output in the
701 * device's DryBuffer.
703 memset(State->Early.PanGain, 0, sizeof(State->Early.PanGain));
704 length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
705 if(!(length > FLT_EPSILON))
707 for(i = 0;i < Device->RealOut.NumChannels;i++)
708 State->Early.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * EarlyGain;
710 else
712 /* Note that EAX Reverb's panning vectors are using right-handed
713 * coordinates, rather that the OpenAL's left-handed coordinates.
714 * Negate Z to fix this.
716 ALfloat pan[3] = {
717 ReflectionsPan[0] / length,
718 ReflectionsPan[1] / length,
719 -ReflectionsPan[2] / length,
721 length = minf(length, 1.0f);
723 CalcDirectionCoeffs(pan, 0.0f, coeffs);
724 ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
725 for(i = 0;i < Device->Dry.NumChannels;i++)
726 State->Early.PanGain[3][i] = DirGains[i] * EarlyGain * length;
727 for(i = 0;i < Device->RealOut.NumChannels;i++)
728 State->Early.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * EarlyGain * (1.0f-length);
731 memset(State->Late.PanGain, 0, sizeof(State->Late.PanGain));
732 length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
733 if(!(length > FLT_EPSILON))
735 for(i = 0;i < Device->RealOut.NumChannels;i++)
736 State->Late.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * LateGain;
738 else
740 ALfloat pan[3] = {
741 LateReverbPan[0] / length,
742 LateReverbPan[1] / length,
743 -LateReverbPan[2] / length,
745 length = minf(length, 1.0f);
747 CalcDirectionCoeffs(pan, 0.0f, coeffs);
748 ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
749 for(i = 0;i < Device->Dry.NumChannels;i++)
750 State->Late.PanGain[3][i] = DirGains[i] * LateGain * length;
751 for(i = 0;i < Device->RealOut.NumChannels;i++)
752 State->Late.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * LateGain * (1.0f-length);
756 static ALvoid UpdateDirectPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
758 ALfloat AmbientGains[MAX_OUTPUT_CHANNELS];
759 ALfloat DirGains[MAX_OUTPUT_CHANNELS];
760 ALfloat coeffs[MAX_AMBI_COEFFS];
761 ALfloat length;
762 ALuint i;
764 /* Apply a boost of about 3dB to better match the expected stereo output volume. */
765 ComputeAmbientGains(Device->Dry, Gain*1.414213562f, AmbientGains);
767 memset(State->Early.PanGain, 0, sizeof(State->Early.PanGain));
768 length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
769 if(!(length > FLT_EPSILON))
771 for(i = 0;i < Device->Dry.NumChannels;i++)
772 State->Early.PanGain[i&3][i] = AmbientGains[i] * EarlyGain;
774 else
776 /* Note that EAX Reverb's panning vectors are using right-handed
777 * coordinates, rather that the OpenAL's left-handed coordinates.
778 * Negate Z to fix this.
780 ALfloat pan[3] = {
781 ReflectionsPan[0] / length,
782 ReflectionsPan[1] / length,
783 -ReflectionsPan[2] / length,
785 length = minf(length, 1.0f);
787 CalcDirectionCoeffs(pan, 0.0f, coeffs);
788 ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
789 for(i = 0;i < Device->Dry.NumChannels;i++)
790 State->Early.PanGain[i&3][i] = lerp(AmbientGains[i], DirGains[i], length) * EarlyGain;
793 memset(State->Late.PanGain, 0, sizeof(State->Late.PanGain));
794 length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
795 if(!(length > FLT_EPSILON))
797 for(i = 0;i < Device->Dry.NumChannels;i++)
798 State->Late.PanGain[i&3][i] = AmbientGains[i] * LateGain;
800 else
802 ALfloat pan[3] = {
803 LateReverbPan[0] / length,
804 LateReverbPan[1] / length,
805 -LateReverbPan[2] / length,
807 length = minf(length, 1.0f);
809 CalcDirectionCoeffs(pan, 0.0f, coeffs);
810 ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
811 for(i = 0;i < Device->Dry.NumChannels;i++)
812 State->Late.PanGain[i&3][i] = lerp(AmbientGains[i], DirGains[i], length) * LateGain;
816 static ALvoid Update3DPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
818 static const ALfloat PanDirs[4][3] = {
819 { -0.707106781f, 0.0f, -0.707106781f }, /* Front left */
820 { 0.707106781f, 0.0f, -0.707106781f }, /* Front right */
821 { 0.707106781f, 0.0f, 0.707106781f }, /* Back right */
822 { -0.707106781f, 0.0f, 0.707106781f } /* Back left */
824 ALfloat coeffs[MAX_AMBI_COEFFS];
825 ALfloat gain[4];
826 ALfloat length;
827 ALuint i;
829 /* 0.5 would be the gain scaling when the panning vector is 0. This also
830 * equals sqrt(1/4), a nice gain scaling for the four virtual points
831 * producing an "ambient" response.
833 gain[0] = gain[1] = gain[2] = gain[3] = 0.5f;
834 length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
835 if(length > 1.0f)
837 ALfloat pan[3] = {
838 ReflectionsPan[0] / length,
839 ReflectionsPan[1] / length,
840 -ReflectionsPan[2] / length,
842 for(i = 0;i < 4;i++)
844 ALfloat dotp = pan[0]*PanDirs[i][0] + pan[1]*PanDirs[i][1] + pan[2]*PanDirs[i][2];
845 gain[i] = dotp*0.5f + 0.5f;
848 else if(length > FLT_EPSILON)
850 for(i = 0;i < 4;i++)
852 ALfloat dotp = ReflectionsPan[0]*PanDirs[i][0] + ReflectionsPan[1]*PanDirs[i][1] +
853 -ReflectionsPan[2]*PanDirs[i][2];
854 gain[i] = dotp*0.5f + 0.5f;
857 for(i = 0;i < 4;i++)
859 CalcDirectionCoeffs(PanDirs[i], 0.0f, coeffs);
860 ComputePanningGains(Device->Dry, coeffs, Gain*EarlyGain*gain[i],
861 State->Early.PanGain[i]);
864 gain[0] = gain[1] = gain[2] = gain[3] = 0.5f;
865 length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
866 if(length > 1.0f)
868 ALfloat pan[3] = {
869 LateReverbPan[0] / length,
870 LateReverbPan[1] / length,
871 -LateReverbPan[2] / length,
873 for(i = 0;i < 4;i++)
875 ALfloat dotp = pan[0]*PanDirs[i][0] + pan[1]*PanDirs[i][1] + pan[2]*PanDirs[i][2];
876 gain[i] = dotp*0.5f + 0.5f;
879 else if(length > FLT_EPSILON)
881 for(i = 0;i < 4;i++)
883 ALfloat dotp = LateReverbPan[0]*PanDirs[i][0] + LateReverbPan[1]*PanDirs[i][1] +
884 -LateReverbPan[2]*PanDirs[i][2];
885 gain[i] = dotp*0.5f + 0.5f;
888 for(i = 0;i < 4;i++)
890 CalcDirectionCoeffs(PanDirs[i], 0.0f, coeffs);
891 ComputePanningGains(Device->Dry, coeffs, Gain*LateGain*gain[i],
892 State->Late.PanGain[i]);
896 static ALvoid ALreverbState_update(ALreverbState *State, const ALCdevice *Device, const ALeffectslot *Slot)
898 const ALeffectProps *props = &Slot->Params.EffectProps;
899 ALuint frequency = Device->Frequency;
900 ALfloat lfscale, hfscale, hfRatio;
901 ALfloat gain, gainlf, gainhf;
902 ALfloat cw, x, y;
904 if(Slot->Params.EffectType == AL_EFFECT_EAXREVERB && !EmulateEAXReverb)
905 State->IsEax = AL_TRUE;
906 else if(Slot->Params.EffectType == AL_EFFECT_REVERB || EmulateEAXReverb)
907 State->IsEax = AL_FALSE;
909 // Calculate the master filters
910 hfscale = props->Reverb.HFReference / frequency;
911 gainhf = maxf(props->Reverb.GainHF, 0.0001f);
912 ALfilterState_setParams(&State->LpFilter, ALfilterType_HighShelf,
913 gainhf, hfscale, calc_rcpQ_from_slope(gainhf, 0.75f));
914 lfscale = props->Reverb.LFReference / frequency;
915 gainlf = maxf(props->Reverb.GainLF, 0.0001f);
916 ALfilterState_setParams(&State->HpFilter, ALfilterType_LowShelf,
917 gainlf, lfscale, calc_rcpQ_from_slope(gainlf, 0.75f));
919 // Update the modulator line.
920 UpdateModulator(props->Reverb.ModulationTime, props->Reverb.ModulationDepth,
921 frequency, State);
923 // Update the initial effect delay.
924 UpdateDelayLine(props->Reverb.ReflectionsDelay, props->Reverb.LateReverbDelay,
925 frequency, State);
927 // Update the early lines.
928 UpdateEarlyLines(props->Reverb.LateReverbDelay, State);
930 // Update the decorrelator.
931 UpdateDecorrelator(props->Reverb.Density, frequency, State);
933 // Get the mixing matrix coefficients (x and y).
934 CalcMatrixCoeffs(props->Reverb.Diffusion, &x, &y);
935 // Then divide x into y to simplify the matrix calculation.
936 State->Late.MixCoeff = y / x;
938 // If the HF limit parameter is flagged, calculate an appropriate limit
939 // based on the air absorption parameter.
940 hfRatio = props->Reverb.DecayHFRatio;
941 if(props->Reverb.DecayHFLimit && props->Reverb.AirAbsorptionGainHF < 1.0f)
942 hfRatio = CalcLimitedHfRatio(hfRatio, props->Reverb.AirAbsorptionGainHF,
943 props->Reverb.DecayTime);
945 cw = cosf(F_TAU * hfscale);
946 // Update the late lines.
947 UpdateLateLines(x, props->Reverb.Density, props->Reverb.DecayTime,
948 props->Reverb.Diffusion, props->Reverb.EchoDepth,
949 hfRatio, cw, frequency, State);
951 // Update the echo line.
952 UpdateEchoLine(props->Reverb.EchoTime, props->Reverb.DecayTime,
953 props->Reverb.Diffusion, props->Reverb.EchoDepth,
954 hfRatio, cw, frequency, State);
956 gain = props->Reverb.Gain * Slot->Params.Gain * ReverbBoost;
957 // Update early and late 3D panning.
958 if(Device->Hrtf || Device->Uhj_Encoder)
959 UpdateMixedPanning(Device, props->Reverb.ReflectionsPan,
960 props->Reverb.LateReverbPan, gain,
961 props->Reverb.ReflectionsGain,
962 props->Reverb.LateReverbGain, State);
963 else if(Device->FmtChans == DevFmtBFormat3D || Device->AmbiDecoder)
964 Update3DPanning(Device, props->Reverb.ReflectionsPan,
965 props->Reverb.LateReverbPan, gain,
966 props->Reverb.ReflectionsGain,
967 props->Reverb.LateReverbGain, State);
968 else
969 UpdateDirectPanning(Device, props->Reverb.ReflectionsPan,
970 props->Reverb.LateReverbPan, gain,
971 props->Reverb.ReflectionsGain,
972 props->Reverb.LateReverbGain, State);
976 /**************************************
977 * Effect Processing *
978 **************************************/
980 // Basic delay line input/output routines.
981 static inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
983 return Delay->Line[offset&Delay->Mask];
986 static inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
988 Delay->Line[offset&Delay->Mask] = in;
991 // Given an input sample, this function produces modulation for the late
992 // reverb.
993 static inline ALfloat EAXModulation(ALreverbState *State, ALuint offset, ALfloat in)
995 ALfloat sinus, frac, fdelay;
996 ALfloat out0, out1;
997 ALuint delay;
999 // Calculate the sinus rythm (dependent on modulation time and the
1000 // sampling rate). The center of the sinus is moved to reduce the delay
1001 // of the effect when the time or depth are low.
1002 sinus = 1.0f - cosf(F_TAU * State->Mod.Index / State->Mod.Range);
1004 // Step the modulation index forward, keeping it bound to its range.
1005 State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
1007 // The depth determines the range over which to read the input samples
1008 // from, so it must be filtered to reduce the distortion caused by even
1009 // small parameter changes.
1010 State->Mod.Filter = lerp(State->Mod.Filter, State->Mod.Depth,
1011 State->Mod.Coeff);
1013 // Calculate the read offset and fraction between it and the next sample.
1014 frac = modff(State->Mod.Filter*sinus, &fdelay);
1015 delay = fastf2u(fdelay);
1017 /* Add the incoming sample to the delay line first, so a 0 delay gets the
1018 * incoming sample.
1020 DelayLineIn(&State->Mod.Delay, offset, in);
1021 /* Get the two samples crossed by the offset delay */
1022 out0 = DelayLineOut(&State->Mod.Delay, offset - delay);
1023 out1 = DelayLineOut(&State->Mod.Delay, offset - delay - 1);
1025 // The output is obtained by linearly interpolating the two samples that
1026 // were acquired above.
1027 return lerp(out0, out1, frac);
1030 // Given some input sample, this function produces four-channel outputs for the
1031 // early reflections.
1032 static inline ALvoid EarlyReflection(ALreverbState *State, ALuint todo, ALfloat (*restrict out)[4])
1034 ALfloat d[4], v, f[4];
1035 ALuint i;
1037 for(i = 0;i < todo;i++)
1039 ALuint offset = State->Offset+i;
1041 // Obtain the decayed results of each early delay line.
1042 d[0] = DelayLineOut(&State->Early.Delay[0], offset-State->Early.Offset[0]) * State->Early.Coeff[0];
1043 d[1] = DelayLineOut(&State->Early.Delay[1], offset-State->Early.Offset[1]) * State->Early.Coeff[1];
1044 d[2] = DelayLineOut(&State->Early.Delay[2], offset-State->Early.Offset[2]) * State->Early.Coeff[2];
1045 d[3] = DelayLineOut(&State->Early.Delay[3], offset-State->Early.Offset[3]) * State->Early.Coeff[3];
1047 /* The following uses a lossless scattering junction from waveguide
1048 * theory. It actually amounts to a householder mixing matrix, which
1049 * will produce a maximally diffuse response, and means this can
1050 * probably be considered a simple feed-back delay network (FDN).
1052 * ---
1054 * v = 2/N / d_i
1055 * ---
1056 * i=1
1058 v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
1059 // The junction is loaded with the input here.
1060 v += DelayLineOut(&State->Delay, offset-State->DelayTap[0]);
1062 // Calculate the feed values for the delay lines.
1063 f[0] = v - d[0];
1064 f[1] = v - d[1];
1065 f[2] = v - d[2];
1066 f[3] = v - d[3];
1068 // Re-feed the delay lines.
1069 DelayLineIn(&State->Early.Delay[0], offset, f[0]);
1070 DelayLineIn(&State->Early.Delay[1], offset, f[1]);
1071 DelayLineIn(&State->Early.Delay[2], offset, f[2]);
1072 DelayLineIn(&State->Early.Delay[3], offset, f[3]);
1074 /* Output the results of the junction for all four channels with a
1075 * constant attenuation of 0.5.
1077 out[i][0] = f[0] * 0.5f;
1078 out[i][1] = f[1] * 0.5f;
1079 out[i][2] = f[2] * 0.5f;
1080 out[i][3] = f[3] * 0.5f;
1084 // Basic attenuated all-pass input/output routine.
1085 static inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
1087 ALfloat out, feed;
1089 out = DelayLineOut(Delay, outOffset);
1090 feed = feedCoeff * in;
1091 DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
1093 // The time-based attenuation is only applied to the delay output to
1094 // keep it from affecting the feed-back path (which is already controlled
1095 // by the all-pass feed coefficient).
1096 return (coeff * out) - feed;
1099 // All-pass input/output routine for late reverb.
1100 static inline ALfloat LateAllPassInOut(ALreverbState *State, ALuint offset, ALuint index, ALfloat in)
1102 return AllpassInOut(&State->Late.ApDelay[index],
1103 offset - State->Late.ApOffset[index],
1104 offset, in, State->Late.ApFeedCoeff,
1105 State->Late.ApCoeff[index]);
1108 // Low-pass filter input/output routine for late reverb.
1109 static inline ALfloat LateLowPassInOut(ALreverbState *State, ALuint index, ALfloat in)
1111 in = lerp(in, State->Late.LpSample[index], State->Late.LpCoeff[index]);
1112 State->Late.LpSample[index] = in;
1113 return in;
1116 // Given four decorrelated input samples, this function produces four-channel
1117 // output for the late reverb.
1118 static inline ALvoid LateReverb(ALreverbState *State, ALuint todo, ALfloat (*restrict out)[4])
1120 ALfloat d[4], f[4];
1121 ALuint i;
1123 // Feed the decorrelator from the energy-attenuated output of the second
1124 // delay tap.
1125 for(i = 0;i < todo;i++)
1127 ALuint offset = State->Offset+i;
1128 ALfloat sample = DelayLineOut(&State->Delay, offset - State->DelayTap[1]) *
1129 State->Late.DensityGain;
1130 DelayLineIn(&State->Decorrelator, offset, sample);
1133 for(i = 0;i < todo;i++)
1135 ALuint offset = State->Offset+i;
1137 /* Obtain four decorrelated input samples. */
1138 f[0] = DelayLineOut(&State->Decorrelator, offset);
1139 f[1] = DelayLineOut(&State->Decorrelator, offset-State->DecoTap[0]);
1140 f[2] = DelayLineOut(&State->Decorrelator, offset-State->DecoTap[1]);
1141 f[3] = DelayLineOut(&State->Decorrelator, offset-State->DecoTap[2]);
1143 /* Add the decayed results of the cyclical delay lines, then pass the
1144 * results through the low-pass filters.
1146 f[0] += DelayLineOut(&State->Late.Delay[0], offset-State->Late.Offset[0]) * State->Late.Coeff[0];
1147 f[1] += DelayLineOut(&State->Late.Delay[1], offset-State->Late.Offset[1]) * State->Late.Coeff[1];
1148 f[2] += DelayLineOut(&State->Late.Delay[2], offset-State->Late.Offset[2]) * State->Late.Coeff[2];
1149 f[3] += DelayLineOut(&State->Late.Delay[3], offset-State->Late.Offset[3]) * State->Late.Coeff[3];
1151 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and
1152 // back to 0.
1153 d[0] = LateLowPassInOut(State, 2, f[2]);
1154 d[1] = LateLowPassInOut(State, 0, f[0]);
1155 d[2] = LateLowPassInOut(State, 3, f[3]);
1156 d[3] = LateLowPassInOut(State, 1, f[1]);
1158 // To help increase diffusion, run each line through an all-pass filter.
1159 // When there is no diffusion, the shortest all-pass filter will feed
1160 // the shortest delay line.
1161 d[0] = LateAllPassInOut(State, offset, 0, d[0]);
1162 d[1] = LateAllPassInOut(State, offset, 1, d[1]);
1163 d[2] = LateAllPassInOut(State, offset, 2, d[2]);
1164 d[3] = LateAllPassInOut(State, offset, 3, d[3]);
1166 /* Late reverb is done with a modified feed-back delay network (FDN)
1167 * topology. Four input lines are each fed through their own all-pass
1168 * filter and then into the mixing matrix. The four outputs of the
1169 * mixing matrix are then cycled back to the inputs. Each output feeds
1170 * a different input to form a circlular feed cycle.
1172 * The mixing matrix used is a 4D skew-symmetric rotation matrix
1173 * derived using a single unitary rotational parameter:
1175 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
1176 * [ -a, d, c, -b ]
1177 * [ -b, -c, d, a ]
1178 * [ -c, b, -a, d ]
1180 * The rotation is constructed from the effect's diffusion parameter,
1181 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
1182 * with differing signs, and d is the coefficient x. The matrix is
1183 * thus:
1185 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
1186 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
1187 * [ y, -y, x, y ] x = cos(t)
1188 * [ -y, -y, -y, x ] y = sin(t) / n
1190 * To reduce the number of multiplies, the x coefficient is applied
1191 * with the cyclical delay line coefficients. Thus only the y
1192 * coefficient is applied when mixing, and is modified to be: y / x.
1194 f[0] = d[0] + (State->Late.MixCoeff * ( d[1] + -d[2] + d[3]));
1195 f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
1196 f[2] = d[2] + (State->Late.MixCoeff * ( d[0] + -d[1] + d[3]));
1197 f[3] = d[3] + (State->Late.MixCoeff * (-d[0] + -d[1] + -d[2] ));
1199 // Output the results of the matrix for all four channels, attenuated by
1200 // the late reverb gain (which is attenuated by the 'x' mix coefficient).
1201 out[i][0] = State->Late.Gain * f[0];
1202 out[i][1] = State->Late.Gain * f[1];
1203 out[i][2] = State->Late.Gain * f[2];
1204 out[i][3] = State->Late.Gain * f[3];
1206 // Re-feed the cyclical delay lines.
1207 DelayLineIn(&State->Late.Delay[0], offset, f[0]);
1208 DelayLineIn(&State->Late.Delay[1], offset, f[1]);
1209 DelayLineIn(&State->Late.Delay[2], offset, f[2]);
1210 DelayLineIn(&State->Late.Delay[3], offset, f[3]);
1214 // Given an input sample, this function mixes echo into the four-channel late
1215 // reverb.
1216 static inline ALvoid EAXEcho(ALreverbState *State, ALuint todo, ALfloat (*restrict late)[4])
1218 ALfloat out, feed;
1219 ALuint i;
1221 for(i = 0;i < todo;i++)
1223 ALuint offset = State->Offset+i;
1225 // Get the latest attenuated echo sample for output.
1226 feed = DelayLineOut(&State->Echo.Delay, offset-State->Echo.Offset) *
1227 State->Echo.Coeff;
1229 // Mix the output into the late reverb channels.
1230 out = State->Echo.MixCoeff * feed;
1231 late[i][0] += out;
1232 late[i][1] += out;
1233 late[i][2] += out;
1234 late[i][3] += out;
1236 // Mix the energy-attenuated input with the output and pass it through
1237 // the echo low-pass filter.
1238 feed += DelayLineOut(&State->Delay, offset-State->DelayTap[1]) *
1239 State->Echo.DensityGain;
1240 feed = lerp(feed, State->Echo.LpSample, State->Echo.LpCoeff);
1241 State->Echo.LpSample = feed;
1243 // Then the echo all-pass filter.
1244 feed = AllpassInOut(&State->Echo.ApDelay, offset-State->Echo.ApOffset,
1245 offset, feed, State->Echo.ApFeedCoeff,
1246 State->Echo.ApCoeff);
1248 // Feed the delay with the mixed and filtered sample.
1249 DelayLineIn(&State->Echo.Delay, offset, feed);
1253 // Perform the non-EAX reverb pass on a given input sample, resulting in
1254 // four-channel output.
1255 static inline ALvoid VerbPass(ALreverbState *State, ALuint todo, const ALfloat *in, ALfloat (*restrict early)[4], ALfloat (*restrict late)[4])
1257 ALuint i;
1259 // Low-pass filter the incoming samples.
1260 for(i = 0;i < todo;i++)
1261 DelayLineIn(&State->Delay, State->Offset+i,
1262 ALfilterState_processSingle(&State->LpFilter, in[i])
1265 // Calculate the early reflection from the first delay tap.
1266 EarlyReflection(State, todo, early);
1268 // Calculate the late reverb from the decorrelator taps.
1269 LateReverb(State, todo, late);
1271 // Step all delays forward one sample.
1272 State->Offset += todo;
1275 // Perform the EAX reverb pass on a given input sample, resulting in four-
1276 // channel output.
1277 static inline ALvoid EAXVerbPass(ALreverbState *State, ALuint todo, const ALfloat *input, ALfloat (*restrict early)[4], ALfloat (*restrict late)[4])
1279 ALuint i;
1281 // Band-pass and modulate the incoming samples.
1282 for(i = 0;i < todo;i++)
1284 ALfloat sample = input[i];
1285 sample = ALfilterState_processSingle(&State->LpFilter, sample);
1286 sample = ALfilterState_processSingle(&State->HpFilter, sample);
1288 // Perform any modulation on the input.
1289 sample = EAXModulation(State, State->Offset+i, sample);
1291 // Feed the initial delay line.
1292 DelayLineIn(&State->Delay, State->Offset+i, sample);
1295 // Calculate the early reflection from the first delay tap.
1296 EarlyReflection(State, todo, early);
1298 // Calculate the late reverb from the decorrelator taps.
1299 LateReverb(State, todo, late);
1301 // Calculate and mix in any echo.
1302 EAXEcho(State, todo, late);
1304 // Step all delays forward.
1305 State->Offset += todo;
1308 static ALvoid ALreverbState_processStandard(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
1310 ALfloat (*restrict early)[4] = State->EarlySamples;
1311 ALfloat (*restrict late)[4] = State->ReverbSamples;
1312 ALuint index, c, i, l;
1313 ALfloat gain;
1315 /* Process reverb for these samples. */
1316 for(index = 0;index < SamplesToDo;)
1318 ALuint todo = minu(SamplesToDo-index, MAX_UPDATE_SAMPLES);
1320 VerbPass(State, todo, &SamplesIn[index], early, late);
1322 for(l = 0;l < 4;l++)
1324 for(c = 0;c < NumChannels;c++)
1326 gain = State->Early.PanGain[l][c];
1327 if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
1329 for(i = 0;i < todo;i++)
1330 SamplesOut[c][index+i] += gain*early[i][l];
1332 gain = State->Late.PanGain[l][c];
1333 if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
1335 for(i = 0;i < todo;i++)
1336 SamplesOut[c][index+i] += gain*late[i][l];
1341 index += todo;
1345 static ALvoid ALreverbState_processEax(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
1347 ALfloat (*restrict early)[4] = State->EarlySamples;
1348 ALfloat (*restrict late)[4] = State->ReverbSamples;
1349 ALuint index, c, i, l;
1350 ALfloat gain;
1352 /* Process reverb for these samples. */
1353 for(index = 0;index < SamplesToDo;)
1355 ALuint todo = minu(SamplesToDo-index, MAX_UPDATE_SAMPLES);
1357 EAXVerbPass(State, todo, &SamplesIn[index], early, late);
1359 for(l = 0;l < 4;l++)
1361 for(c = 0;c < NumChannels;c++)
1363 gain = State->Early.PanGain[l][c];
1364 if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
1366 for(i = 0;i < todo;i++)
1367 SamplesOut[c][index+i] += gain*early[i][l];
1369 gain = State->Late.PanGain[l][c];
1370 if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
1372 for(i = 0;i < todo;i++)
1373 SamplesOut[c][index+i] += gain*late[i][l];
1378 index += todo;
1382 static ALvoid ALreverbState_process(ALreverbState *State, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
1384 NumChannels += State->ExtraChannels;
1385 if(State->IsEax)
1386 ALreverbState_processEax(State, SamplesToDo, SamplesIn[0], SamplesOut, NumChannels);
1387 else
1388 ALreverbState_processStandard(State, SamplesToDo, SamplesIn[0], SamplesOut, NumChannels);
1392 typedef struct ALreverbStateFactory {
1393 DERIVE_FROM_TYPE(ALeffectStateFactory);
1394 } ALreverbStateFactory;
1396 static ALeffectState *ALreverbStateFactory_create(ALreverbStateFactory* UNUSED(factory))
1398 ALreverbState *state;
1399 ALuint index, l;
1401 state = ALreverbState_New(sizeof(*state));
1402 if(!state) return NULL;
1403 SET_VTABLE2(ALreverbState, ALeffectState, state);
1405 state->IsEax = AL_FALSE;
1406 state->ExtraChannels = 0;
1408 state->TotalSamples = 0;
1409 state->SampleBuffer = NULL;
1411 ALfilterState_clear(&state->LpFilter);
1412 ALfilterState_clear(&state->HpFilter);
1414 state->Mod.Delay.Mask = 0;
1415 state->Mod.Delay.Line = NULL;
1416 state->Mod.Index = 0;
1417 state->Mod.Range = 1;
1418 state->Mod.Depth = 0.0f;
1419 state->Mod.Coeff = 0.0f;
1420 state->Mod.Filter = 0.0f;
1422 state->Delay.Mask = 0;
1423 state->Delay.Line = NULL;
1424 state->DelayTap[0] = 0;
1425 state->DelayTap[1] = 0;
1427 for(index = 0;index < 4;index++)
1429 state->Early.Coeff[index] = 0.0f;
1430 state->Early.Delay[index].Mask = 0;
1431 state->Early.Delay[index].Line = NULL;
1432 state->Early.Offset[index] = 0;
1435 state->Decorrelator.Mask = 0;
1436 state->Decorrelator.Line = NULL;
1437 state->DecoTap[0] = 0;
1438 state->DecoTap[1] = 0;
1439 state->DecoTap[2] = 0;
1441 state->Late.Gain = 0.0f;
1442 state->Late.DensityGain = 0.0f;
1443 state->Late.ApFeedCoeff = 0.0f;
1444 state->Late.MixCoeff = 0.0f;
1445 for(index = 0;index < 4;index++)
1447 state->Late.ApCoeff[index] = 0.0f;
1448 state->Late.ApDelay[index].Mask = 0;
1449 state->Late.ApDelay[index].Line = NULL;
1450 state->Late.ApOffset[index] = 0;
1452 state->Late.Coeff[index] = 0.0f;
1453 state->Late.Delay[index].Mask = 0;
1454 state->Late.Delay[index].Line = NULL;
1455 state->Late.Offset[index] = 0;
1457 state->Late.LpCoeff[index] = 0.0f;
1458 state->Late.LpSample[index] = 0.0f;
1461 for(l = 0;l < 4;l++)
1463 for(index = 0;index < MAX_OUTPUT_CHANNELS;index++)
1465 state->Early.PanGain[l][index] = 0.0f;
1466 state->Late.PanGain[l][index] = 0.0f;
1470 state->Echo.DensityGain = 0.0f;
1471 state->Echo.Delay.Mask = 0;
1472 state->Echo.Delay.Line = NULL;
1473 state->Echo.ApDelay.Mask = 0;
1474 state->Echo.ApDelay.Line = NULL;
1475 state->Echo.Coeff = 0.0f;
1476 state->Echo.ApFeedCoeff = 0.0f;
1477 state->Echo.ApCoeff = 0.0f;
1478 state->Echo.Offset = 0;
1479 state->Echo.ApOffset = 0;
1480 state->Echo.LpCoeff = 0.0f;
1481 state->Echo.LpSample = 0.0f;
1482 state->Echo.MixCoeff = 0.0f;
1484 state->Offset = 0;
1486 return STATIC_CAST(ALeffectState, state);
1489 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALreverbStateFactory);
1491 ALeffectStateFactory *ALreverbStateFactory_getFactory(void)
1493 static ALreverbStateFactory ReverbFactory = { { GET_VTABLE2(ALreverbStateFactory, ALeffectStateFactory) } };
1495 return STATIC_CAST(ALeffectStateFactory, &ReverbFactory);
1499 void ALeaxreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
1501 ALeffectProps *props = &effect->Props;
1502 switch(param)
1504 case AL_EAXREVERB_DECAY_HFLIMIT:
1505 if(!(val >= AL_EAXREVERB_MIN_DECAY_HFLIMIT && val <= AL_EAXREVERB_MAX_DECAY_HFLIMIT))
1506 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1507 props->Reverb.DecayHFLimit = val;
1508 break;
1510 default:
1511 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1514 void ALeaxreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
1516 ALeaxreverb_setParami(effect, context, param, vals[0]);
1518 void ALeaxreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
1520 ALeffectProps *props = &effect->Props;
1521 switch(param)
1523 case AL_EAXREVERB_DENSITY:
1524 if(!(val >= AL_EAXREVERB_MIN_DENSITY && val <= AL_EAXREVERB_MAX_DENSITY))
1525 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1526 props->Reverb.Density = val;
1527 break;
1529 case AL_EAXREVERB_DIFFUSION:
1530 if(!(val >= AL_EAXREVERB_MIN_DIFFUSION && val <= AL_EAXREVERB_MAX_DIFFUSION))
1531 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1532 props->Reverb.Diffusion = val;
1533 break;
1535 case AL_EAXREVERB_GAIN:
1536 if(!(val >= AL_EAXREVERB_MIN_GAIN && val <= AL_EAXREVERB_MAX_GAIN))
1537 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1538 props->Reverb.Gain = val;
1539 break;
1541 case AL_EAXREVERB_GAINHF:
1542 if(!(val >= AL_EAXREVERB_MIN_GAINHF && val <= AL_EAXREVERB_MAX_GAINHF))
1543 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1544 props->Reverb.GainHF = val;
1545 break;
1547 case AL_EAXREVERB_GAINLF:
1548 if(!(val >= AL_EAXREVERB_MIN_GAINLF && val <= AL_EAXREVERB_MAX_GAINLF))
1549 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1550 props->Reverb.GainLF = val;
1551 break;
1553 case AL_EAXREVERB_DECAY_TIME:
1554 if(!(val >= AL_EAXREVERB_MIN_DECAY_TIME && val <= AL_EAXREVERB_MAX_DECAY_TIME))
1555 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1556 props->Reverb.DecayTime = val;
1557 break;
1559 case AL_EAXREVERB_DECAY_HFRATIO:
1560 if(!(val >= AL_EAXREVERB_MIN_DECAY_HFRATIO && val <= AL_EAXREVERB_MAX_DECAY_HFRATIO))
1561 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1562 props->Reverb.DecayHFRatio = val;
1563 break;
1565 case AL_EAXREVERB_DECAY_LFRATIO:
1566 if(!(val >= AL_EAXREVERB_MIN_DECAY_LFRATIO && val <= AL_EAXREVERB_MAX_DECAY_LFRATIO))
1567 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1568 props->Reverb.DecayLFRatio = val;
1569 break;
1571 case AL_EAXREVERB_REFLECTIONS_GAIN:
1572 if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_GAIN && val <= AL_EAXREVERB_MAX_REFLECTIONS_GAIN))
1573 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1574 props->Reverb.ReflectionsGain = val;
1575 break;
1577 case AL_EAXREVERB_REFLECTIONS_DELAY:
1578 if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_DELAY && val <= AL_EAXREVERB_MAX_REFLECTIONS_DELAY))
1579 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1580 props->Reverb.ReflectionsDelay = val;
1581 break;
1583 case AL_EAXREVERB_LATE_REVERB_GAIN:
1584 if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_GAIN && val <= AL_EAXREVERB_MAX_LATE_REVERB_GAIN))
1585 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1586 props->Reverb.LateReverbGain = val;
1587 break;
1589 case AL_EAXREVERB_LATE_REVERB_DELAY:
1590 if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_DELAY && val <= AL_EAXREVERB_MAX_LATE_REVERB_DELAY))
1591 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1592 props->Reverb.LateReverbDelay = val;
1593 break;
1595 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
1596 if(!(val >= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF))
1597 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1598 props->Reverb.AirAbsorptionGainHF = val;
1599 break;
1601 case AL_EAXREVERB_ECHO_TIME:
1602 if(!(val >= AL_EAXREVERB_MIN_ECHO_TIME && val <= AL_EAXREVERB_MAX_ECHO_TIME))
1603 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1604 props->Reverb.EchoTime = val;
1605 break;
1607 case AL_EAXREVERB_ECHO_DEPTH:
1608 if(!(val >= AL_EAXREVERB_MIN_ECHO_DEPTH && val <= AL_EAXREVERB_MAX_ECHO_DEPTH))
1609 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1610 props->Reverb.EchoDepth = val;
1611 break;
1613 case AL_EAXREVERB_MODULATION_TIME:
1614 if(!(val >= AL_EAXREVERB_MIN_MODULATION_TIME && val <= AL_EAXREVERB_MAX_MODULATION_TIME))
1615 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1616 props->Reverb.ModulationTime = val;
1617 break;
1619 case AL_EAXREVERB_MODULATION_DEPTH:
1620 if(!(val >= AL_EAXREVERB_MIN_MODULATION_DEPTH && val <= AL_EAXREVERB_MAX_MODULATION_DEPTH))
1621 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1622 props->Reverb.ModulationDepth = val;
1623 break;
1625 case AL_EAXREVERB_HFREFERENCE:
1626 if(!(val >= AL_EAXREVERB_MIN_HFREFERENCE && val <= AL_EAXREVERB_MAX_HFREFERENCE))
1627 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1628 props->Reverb.HFReference = val;
1629 break;
1631 case AL_EAXREVERB_LFREFERENCE:
1632 if(!(val >= AL_EAXREVERB_MIN_LFREFERENCE && val <= AL_EAXREVERB_MAX_LFREFERENCE))
1633 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1634 props->Reverb.LFReference = val;
1635 break;
1637 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
1638 if(!(val >= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR))
1639 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1640 props->Reverb.RoomRolloffFactor = val;
1641 break;
1643 default:
1644 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1647 void ALeaxreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
1649 ALeffectProps *props = &effect->Props;
1650 switch(param)
1652 case AL_EAXREVERB_REFLECTIONS_PAN:
1653 if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
1654 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1655 props->Reverb.ReflectionsPan[0] = vals[0];
1656 props->Reverb.ReflectionsPan[1] = vals[1];
1657 props->Reverb.ReflectionsPan[2] = vals[2];
1658 break;
1659 case AL_EAXREVERB_LATE_REVERB_PAN:
1660 if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
1661 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1662 props->Reverb.LateReverbPan[0] = vals[0];
1663 props->Reverb.LateReverbPan[1] = vals[1];
1664 props->Reverb.LateReverbPan[2] = vals[2];
1665 break;
1667 default:
1668 ALeaxreverb_setParamf(effect, context, param, vals[0]);
1669 break;
1673 void ALeaxreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
1675 const ALeffectProps *props = &effect->Props;
1676 switch(param)
1678 case AL_EAXREVERB_DECAY_HFLIMIT:
1679 *val = props->Reverb.DecayHFLimit;
1680 break;
1682 default:
1683 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1686 void ALeaxreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
1688 ALeaxreverb_getParami(effect, context, param, vals);
1690 void ALeaxreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
1692 const ALeffectProps *props = &effect->Props;
1693 switch(param)
1695 case AL_EAXREVERB_DENSITY:
1696 *val = props->Reverb.Density;
1697 break;
1699 case AL_EAXREVERB_DIFFUSION:
1700 *val = props->Reverb.Diffusion;
1701 break;
1703 case AL_EAXREVERB_GAIN:
1704 *val = props->Reverb.Gain;
1705 break;
1707 case AL_EAXREVERB_GAINHF:
1708 *val = props->Reverb.GainHF;
1709 break;
1711 case AL_EAXREVERB_GAINLF:
1712 *val = props->Reverb.GainLF;
1713 break;
1715 case AL_EAXREVERB_DECAY_TIME:
1716 *val = props->Reverb.DecayTime;
1717 break;
1719 case AL_EAXREVERB_DECAY_HFRATIO:
1720 *val = props->Reverb.DecayHFRatio;
1721 break;
1723 case AL_EAXREVERB_DECAY_LFRATIO:
1724 *val = props->Reverb.DecayLFRatio;
1725 break;
1727 case AL_EAXREVERB_REFLECTIONS_GAIN:
1728 *val = props->Reverb.ReflectionsGain;
1729 break;
1731 case AL_EAXREVERB_REFLECTIONS_DELAY:
1732 *val = props->Reverb.ReflectionsDelay;
1733 break;
1735 case AL_EAXREVERB_LATE_REVERB_GAIN:
1736 *val = props->Reverb.LateReverbGain;
1737 break;
1739 case AL_EAXREVERB_LATE_REVERB_DELAY:
1740 *val = props->Reverb.LateReverbDelay;
1741 break;
1743 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
1744 *val = props->Reverb.AirAbsorptionGainHF;
1745 break;
1747 case AL_EAXREVERB_ECHO_TIME:
1748 *val = props->Reverb.EchoTime;
1749 break;
1751 case AL_EAXREVERB_ECHO_DEPTH:
1752 *val = props->Reverb.EchoDepth;
1753 break;
1755 case AL_EAXREVERB_MODULATION_TIME:
1756 *val = props->Reverb.ModulationTime;
1757 break;
1759 case AL_EAXREVERB_MODULATION_DEPTH:
1760 *val = props->Reverb.ModulationDepth;
1761 break;
1763 case AL_EAXREVERB_HFREFERENCE:
1764 *val = props->Reverb.HFReference;
1765 break;
1767 case AL_EAXREVERB_LFREFERENCE:
1768 *val = props->Reverb.LFReference;
1769 break;
1771 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
1772 *val = props->Reverb.RoomRolloffFactor;
1773 break;
1775 default:
1776 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1779 void ALeaxreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
1781 const ALeffectProps *props = &effect->Props;
1782 switch(param)
1784 case AL_EAXREVERB_REFLECTIONS_PAN:
1785 vals[0] = props->Reverb.ReflectionsPan[0];
1786 vals[1] = props->Reverb.ReflectionsPan[1];
1787 vals[2] = props->Reverb.ReflectionsPan[2];
1788 break;
1789 case AL_EAXREVERB_LATE_REVERB_PAN:
1790 vals[0] = props->Reverb.LateReverbPan[0];
1791 vals[1] = props->Reverb.LateReverbPan[1];
1792 vals[2] = props->Reverb.LateReverbPan[2];
1793 break;
1795 default:
1796 ALeaxreverb_getParamf(effect, context, param, vals);
1797 break;
1801 DEFINE_ALEFFECT_VTABLE(ALeaxreverb);
1803 void ALreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
1805 ALeffectProps *props = &effect->Props;
1806 switch(param)
1808 case AL_REVERB_DECAY_HFLIMIT:
1809 if(!(val >= AL_REVERB_MIN_DECAY_HFLIMIT && val <= AL_REVERB_MAX_DECAY_HFLIMIT))
1810 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1811 props->Reverb.DecayHFLimit = val;
1812 break;
1814 default:
1815 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1818 void ALreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
1820 ALreverb_setParami(effect, context, param, vals[0]);
1822 void ALreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
1824 ALeffectProps *props = &effect->Props;
1825 switch(param)
1827 case AL_REVERB_DENSITY:
1828 if(!(val >= AL_REVERB_MIN_DENSITY && val <= AL_REVERB_MAX_DENSITY))
1829 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1830 props->Reverb.Density = val;
1831 break;
1833 case AL_REVERB_DIFFUSION:
1834 if(!(val >= AL_REVERB_MIN_DIFFUSION && val <= AL_REVERB_MAX_DIFFUSION))
1835 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1836 props->Reverb.Diffusion = val;
1837 break;
1839 case AL_REVERB_GAIN:
1840 if(!(val >= AL_REVERB_MIN_GAIN && val <= AL_REVERB_MAX_GAIN))
1841 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1842 props->Reverb.Gain = val;
1843 break;
1845 case AL_REVERB_GAINHF:
1846 if(!(val >= AL_REVERB_MIN_GAINHF && val <= AL_REVERB_MAX_GAINHF))
1847 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1848 props->Reverb.GainHF = val;
1849 break;
1851 case AL_REVERB_DECAY_TIME:
1852 if(!(val >= AL_REVERB_MIN_DECAY_TIME && val <= AL_REVERB_MAX_DECAY_TIME))
1853 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1854 props->Reverb.DecayTime = val;
1855 break;
1857 case AL_REVERB_DECAY_HFRATIO:
1858 if(!(val >= AL_REVERB_MIN_DECAY_HFRATIO && val <= AL_REVERB_MAX_DECAY_HFRATIO))
1859 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1860 props->Reverb.DecayHFRatio = val;
1861 break;
1863 case AL_REVERB_REFLECTIONS_GAIN:
1864 if(!(val >= AL_REVERB_MIN_REFLECTIONS_GAIN && val <= AL_REVERB_MAX_REFLECTIONS_GAIN))
1865 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1866 props->Reverb.ReflectionsGain = val;
1867 break;
1869 case AL_REVERB_REFLECTIONS_DELAY:
1870 if(!(val >= AL_REVERB_MIN_REFLECTIONS_DELAY && val <= AL_REVERB_MAX_REFLECTIONS_DELAY))
1871 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1872 props->Reverb.ReflectionsDelay = val;
1873 break;
1875 case AL_REVERB_LATE_REVERB_GAIN:
1876 if(!(val >= AL_REVERB_MIN_LATE_REVERB_GAIN && val <= AL_REVERB_MAX_LATE_REVERB_GAIN))
1877 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1878 props->Reverb.LateReverbGain = val;
1879 break;
1881 case AL_REVERB_LATE_REVERB_DELAY:
1882 if(!(val >= AL_REVERB_MIN_LATE_REVERB_DELAY && val <= AL_REVERB_MAX_LATE_REVERB_DELAY))
1883 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1884 props->Reverb.LateReverbDelay = val;
1885 break;
1887 case AL_REVERB_AIR_ABSORPTION_GAINHF:
1888 if(!(val >= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF))
1889 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1890 props->Reverb.AirAbsorptionGainHF = val;
1891 break;
1893 case AL_REVERB_ROOM_ROLLOFF_FACTOR:
1894 if(!(val >= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR))
1895 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1896 props->Reverb.RoomRolloffFactor = val;
1897 break;
1899 default:
1900 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1903 void ALreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
1905 ALreverb_setParamf(effect, context, param, vals[0]);
1908 void ALreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
1910 const ALeffectProps *props = &effect->Props;
1911 switch(param)
1913 case AL_REVERB_DECAY_HFLIMIT:
1914 *val = props->Reverb.DecayHFLimit;
1915 break;
1917 default:
1918 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1921 void ALreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
1923 ALreverb_getParami(effect, context, param, vals);
1925 void ALreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
1927 const ALeffectProps *props = &effect->Props;
1928 switch(param)
1930 case AL_REVERB_DENSITY:
1931 *val = props->Reverb.Density;
1932 break;
1934 case AL_REVERB_DIFFUSION:
1935 *val = props->Reverb.Diffusion;
1936 break;
1938 case AL_REVERB_GAIN:
1939 *val = props->Reverb.Gain;
1940 break;
1942 case AL_REVERB_GAINHF:
1943 *val = props->Reverb.GainHF;
1944 break;
1946 case AL_REVERB_DECAY_TIME:
1947 *val = props->Reverb.DecayTime;
1948 break;
1950 case AL_REVERB_DECAY_HFRATIO:
1951 *val = props->Reverb.DecayHFRatio;
1952 break;
1954 case AL_REVERB_REFLECTIONS_GAIN:
1955 *val = props->Reverb.ReflectionsGain;
1956 break;
1958 case AL_REVERB_REFLECTIONS_DELAY:
1959 *val = props->Reverb.ReflectionsDelay;
1960 break;
1962 case AL_REVERB_LATE_REVERB_GAIN:
1963 *val = props->Reverb.LateReverbGain;
1964 break;
1966 case AL_REVERB_LATE_REVERB_DELAY:
1967 *val = props->Reverb.LateReverbDelay;
1968 break;
1970 case AL_REVERB_AIR_ABSORPTION_GAINHF:
1971 *val = props->Reverb.AirAbsorptionGainHF;
1972 break;
1974 case AL_REVERB_ROOM_ROLLOFF_FACTOR:
1975 *val = props->Reverb.RoomRolloffFactor;
1976 break;
1978 default:
1979 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1982 void ALreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
1984 ALreverb_getParamf(effect, context, param, vals);
1987 DEFINE_ALEFFECT_VTABLE(ALreverb);