Use the 3D panned output for reverb with HQ decoding
[openal-soft.git] / Alc / effects / reverb.c
blob2aea0763b145f0b2e333acde7dcaccc2980d197d
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;
174 static ALboolean ALreverbState_deviceUpdate(ALreverbState *State, ALCdevice *Device);
175 static ALvoid ALreverbState_update(ALreverbState *State, const ALCdevice *Device, const ALeffectslot *Slot);
176 static ALvoid ALreverbState_processStandard(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
177 static ALvoid ALreverbState_processEax(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
178 static ALvoid ALreverbState_process(ALreverbState *State, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
179 DECLARE_DEFAULT_ALLOCATORS(ALreverbState)
181 DEFINE_ALEFFECTSTATE_VTABLE(ALreverbState);
183 /* This is a user config option for modifying the overall output of the reverb
184 * effect.
186 ALfloat ReverbBoost = 1.0f;
188 /* Specifies whether to use a standard reverb effect in place of EAX reverb (no
189 * high-pass, modulation, or echo).
191 ALboolean EmulateEAXReverb = AL_FALSE;
193 /* This coefficient is used to define the maximum frequency range controlled
194 * by the modulation depth. The current value of 0.1 will allow it to swing
195 * from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the
196 * sampler to stall on the downswing, and above 1 it will cause it to sample
197 * backwards.
199 static const ALfloat MODULATION_DEPTH_COEFF = 0.1f;
201 /* A filter is used to avoid the terrible distortion caused by changing
202 * modulation time and/or depth. To be consistent across different sample
203 * rates, the coefficient must be raised to a constant divided by the sample
204 * rate: coeff^(constant / rate).
206 static const ALfloat MODULATION_FILTER_COEFF = 0.048f;
207 static const ALfloat MODULATION_FILTER_CONST = 100000.0f;
209 // When diffusion is above 0, an all-pass filter is used to take the edge off
210 // the echo effect. It uses the following line length (in seconds).
211 static const ALfloat ECHO_ALLPASS_LENGTH = 0.0133f;
213 // Input into the late reverb is decorrelated between four channels. Their
214 // timings are dependent on a fraction and multiplier. See the
215 // UpdateDecorrelator() routine for the calculations involved.
216 static const ALfloat DECO_FRACTION = 0.15f;
217 static const ALfloat DECO_MULTIPLIER = 2.0f;
219 // All delay line lengths are specified in seconds.
221 // The lengths of the early delay lines.
222 static const ALfloat EARLY_LINE_LENGTH[4] =
224 0.0015f, 0.0045f, 0.0135f, 0.0405f
227 // The lengths of the late all-pass delay lines.
228 static const ALfloat ALLPASS_LINE_LENGTH[4] =
230 0.0151f, 0.0167f, 0.0183f, 0.0200f,
233 // The lengths of the late cyclical delay lines.
234 static const ALfloat LATE_LINE_LENGTH[4] =
236 0.0211f, 0.0311f, 0.0461f, 0.0680f
239 // The late cyclical delay lines have a variable length dependent on the
240 // effect's density parameter (inverted for some reason) and this multiplier.
241 static const ALfloat LATE_LINE_MULTIPLIER = 4.0f;
244 #if defined(_WIN32) && !defined (_M_X64) && !defined(_M_ARM)
245 /* HACK: Workaround for a modff bug in 32-bit Windows, which attempts to write
246 * a 64-bit double to the 32-bit float parameter.
248 static inline float hack_modff(float x, float *y)
250 double di;
251 double df = modf((double)x, &di);
252 *y = (float)di;
253 return (float)df;
255 #define modff hack_modff
256 #endif
259 /**************************************
260 * Device Update *
261 **************************************/
263 // Given the allocated sample buffer, this function updates each delay line
264 // offset.
265 static inline ALvoid RealizeLineOffset(ALfloat *sampleBuffer, DelayLine *Delay)
267 Delay->Line = &sampleBuffer[(ptrdiff_t)Delay->Line];
270 // Calculate the length of a delay line and store its mask and offset.
271 static ALuint CalcLineLength(ALfloat length, ptrdiff_t offset, ALuint frequency, ALuint extra, DelayLine *Delay)
273 ALuint samples;
275 // All line lengths are powers of 2, calculated from their lengths, with
276 // an additional sample in case of rounding errors.
277 samples = fastf2u(length*frequency) + extra;
278 samples = NextPowerOf2(samples + 1);
279 // All lines share a single sample buffer.
280 Delay->Mask = samples - 1;
281 Delay->Line = (ALfloat*)offset;
282 // Return the sample count for accumulation.
283 return samples;
286 /* Calculates the delay line metrics and allocates the shared sample buffer
287 * for all lines given the sample rate (frequency). If an allocation failure
288 * occurs, it returns AL_FALSE.
290 static ALboolean AllocLines(ALuint frequency, ALreverbState *State)
292 ALuint totalSamples, index;
293 ALfloat length;
294 ALfloat *newBuffer = NULL;
296 // All delay line lengths are calculated to accomodate the full range of
297 // lengths given their respective paramters.
298 totalSamples = 0;
300 /* The modulator's line length is calculated from the maximum modulation
301 * time and depth coefficient, and halfed for the low-to-high frequency
302 * swing. An additional sample is added to keep it stable when there is no
303 * modulation.
305 length = (AL_EAXREVERB_MAX_MODULATION_TIME*MODULATION_DEPTH_COEFF/2.0f);
306 totalSamples += CalcLineLength(length, totalSamples, frequency, 1,
307 &State->Mod.Delay);
309 // The initial delay is the sum of the reflections and late reverb
310 // delays. This must include space for storing a loop update to feed the
311 // early reflections, decorrelator, and echo.
312 length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY +
313 AL_EAXREVERB_MAX_LATE_REVERB_DELAY;
314 totalSamples += CalcLineLength(length, totalSamples, frequency,
315 MAX_UPDATE_SAMPLES, &State->Delay);
317 // The early reflection lines.
318 for(index = 0;index < 4;index++)
319 totalSamples += CalcLineLength(EARLY_LINE_LENGTH[index], totalSamples,
320 frequency, 0, &State->Early.Delay[index]);
322 // The decorrelator line is calculated from the lowest reverb density (a
323 // parameter value of 1). This must include space for storing a loop update
324 // to feed the late reverb.
325 length = (DECO_FRACTION * DECO_MULTIPLIER * DECO_MULTIPLIER) *
326 LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER);
327 totalSamples += CalcLineLength(length, totalSamples, frequency, MAX_UPDATE_SAMPLES,
328 &State->Decorrelator);
330 // The late all-pass lines.
331 for(index = 0;index < 4;index++)
332 totalSamples += CalcLineLength(ALLPASS_LINE_LENGTH[index], totalSamples,
333 frequency, 0, &State->Late.ApDelay[index]);
335 // The late delay lines are calculated from the lowest reverb density.
336 for(index = 0;index < 4;index++)
338 length = LATE_LINE_LENGTH[index] * (1.0f + LATE_LINE_MULTIPLIER);
339 totalSamples += CalcLineLength(length, totalSamples, frequency, 0,
340 &State->Late.Delay[index]);
343 // The echo all-pass and delay lines.
344 totalSamples += CalcLineLength(ECHO_ALLPASS_LENGTH, totalSamples,
345 frequency, 0, &State->Echo.ApDelay);
346 totalSamples += CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME, totalSamples,
347 frequency, 0, &State->Echo.Delay);
349 if(totalSamples != State->TotalSamples)
351 TRACE("New reverb buffer length: %u samples (%f sec)\n", totalSamples, totalSamples/(float)frequency);
352 newBuffer = realloc(State->SampleBuffer, sizeof(ALfloat) * totalSamples);
353 if(newBuffer == NULL)
354 return AL_FALSE;
355 State->SampleBuffer = newBuffer;
356 State->TotalSamples = totalSamples;
359 // Update all delays to reflect the new sample buffer.
360 RealizeLineOffset(State->SampleBuffer, &State->Delay);
361 RealizeLineOffset(State->SampleBuffer, &State->Decorrelator);
362 for(index = 0;index < 4;index++)
364 RealizeLineOffset(State->SampleBuffer, &State->Early.Delay[index]);
365 RealizeLineOffset(State->SampleBuffer, &State->Late.ApDelay[index]);
366 RealizeLineOffset(State->SampleBuffer, &State->Late.Delay[index]);
368 RealizeLineOffset(State->SampleBuffer, &State->Mod.Delay);
369 RealizeLineOffset(State->SampleBuffer, &State->Echo.ApDelay);
370 RealizeLineOffset(State->SampleBuffer, &State->Echo.Delay);
372 // Clear the sample buffer.
373 for(index = 0;index < State->TotalSamples;index++)
374 State->SampleBuffer[index] = 0.0f;
376 return AL_TRUE;
379 static ALboolean ALreverbState_deviceUpdate(ALreverbState *State, ALCdevice *Device)
381 ALuint frequency = Device->Frequency, index;
383 // Allocate the delay lines.
384 if(!AllocLines(frequency, State))
385 return AL_FALSE;
387 /* WARNING: This assumes the real output follows the virtual output in the
388 * device's DryBuffer.
390 if(Device->Hrtf || Device->Uhj_Encoder)
391 State->ExtraChannels = ChannelsFromDevFmt(Device->FmtChans);
392 else
393 State->ExtraChannels = 0;
395 // Calculate the modulation filter coefficient. Notice that the exponent
396 // is calculated given the current sample rate. This ensures that the
397 // resulting filter response over time is consistent across all sample
398 // rates.
399 State->Mod.Coeff = powf(MODULATION_FILTER_COEFF,
400 MODULATION_FILTER_CONST / frequency);
402 // The early reflection and late all-pass filter line lengths are static,
403 // so their offsets only need to be calculated once.
404 for(index = 0;index < 4;index++)
406 State->Early.Offset[index] = fastf2u(EARLY_LINE_LENGTH[index] * frequency);
407 State->Late.ApOffset[index] = fastf2u(ALLPASS_LINE_LENGTH[index] * frequency);
410 // The echo all-pass filter line length is static, so its offset only
411 // needs to be calculated once.
412 State->Echo.ApOffset = fastf2u(ECHO_ALLPASS_LENGTH * frequency);
414 return AL_TRUE;
417 /**************************************
418 * Effect Update *
419 **************************************/
421 // Calculate a decay coefficient given the length of each cycle and the time
422 // until the decay reaches -60 dB.
423 static inline ALfloat CalcDecayCoeff(ALfloat length, ALfloat decayTime)
425 return powf(0.001f/*-60 dB*/, length/decayTime);
428 // Calculate a decay length from a coefficient and the time until the decay
429 // reaches -60 dB.
430 static inline ALfloat CalcDecayLength(ALfloat coeff, ALfloat decayTime)
432 return log10f(coeff) * decayTime / log10f(0.001f)/*-60 dB*/;
435 // Calculate an attenuation to be applied to the input of any echo models to
436 // compensate for modal density and decay time.
437 static inline ALfloat CalcDensityGain(ALfloat a)
439 /* The energy of a signal can be obtained by finding the area under the
440 * squared signal. This takes the form of Sum(x_n^2), where x is the
441 * amplitude for the sample n.
443 * Decaying feedback matches exponential decay of the form Sum(a^n),
444 * where a is the attenuation coefficient, and n is the sample. The area
445 * under this decay curve can be calculated as: 1 / (1 - a).
447 * Modifying the above equation to find the squared area under the curve
448 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
449 * calculated by inverting the square root of this approximation,
450 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
452 return sqrtf(1.0f - (a * a));
455 // Calculate the mixing matrix coefficients given a diffusion factor.
456 static inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)
458 ALfloat n, t;
460 // The matrix is of order 4, so n is sqrt (4 - 1).
461 n = sqrtf(3.0f);
462 t = diffusion * atanf(n);
464 // Calculate the first mixing matrix coefficient.
465 *x = cosf(t);
466 // Calculate the second mixing matrix coefficient.
467 *y = sinf(t) / n;
470 // Calculate the limited HF ratio for use with the late reverb low-pass
471 // filters.
472 static ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)
474 ALfloat limitRatio;
476 /* Find the attenuation due to air absorption in dB (converting delay
477 * time to meters using the speed of sound). Then reversing the decay
478 * equation, solve for HF ratio. The delay length is cancelled out of
479 * the equation, so it can be calculated once for all lines.
481 limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *
482 SPEEDOFSOUNDMETRESPERSEC);
483 /* Using the limit calculated above, apply the upper bound to the HF
484 * ratio. Also need to limit the result to a minimum of 0.1, just like the
485 * HF ratio parameter. */
486 return clampf(limitRatio, 0.1f, hfRatio);
489 // Calculate the coefficient for a HF (and eventually LF) decay damping
490 // filter.
491 static inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)
493 ALfloat coeff, g;
495 // Eventually this should boost the high frequencies when the ratio
496 // exceeds 1.
497 coeff = 0.0f;
498 if (hfRatio < 1.0f)
500 // Calculate the low-pass coefficient by dividing the HF decay
501 // coefficient by the full decay coefficient.
502 g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;
504 // Damping is done with a 1-pole filter, so g needs to be squared.
505 g *= g;
506 if(g < 0.9999f) /* 1-epsilon */
508 /* Be careful with gains < 0.001, as that causes the coefficient
509 * head towards 1, which will flatten the signal. */
510 g = maxf(g, 0.001f);
511 coeff = (1 - g*cw - sqrtf(2*g*(1-cw) - g*g*(1 - cw*cw))) /
512 (1 - g);
515 // Very low decay times will produce minimal output, so apply an
516 // upper bound to the coefficient.
517 coeff = minf(coeff, 0.98f);
519 return coeff;
522 // Update the EAX modulation index, range, and depth. Keep in mind that this
523 // kind of vibrato is additive and not multiplicative as one may expect. The
524 // downswing will sound stronger than the upswing.
525 static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALreverbState *State)
527 ALuint range;
529 /* Modulation is calculated in two parts.
531 * The modulation time effects the sinus applied to the change in
532 * frequency. An index out of the current time range (both in samples)
533 * is incremented each sample. The range is bound to a reasonable
534 * minimum (1 sample) and when the timing changes, the index is rescaled
535 * to the new range (to keep the sinus consistent).
537 range = maxu(fastf2u(modTime*frequency), 1);
538 State->Mod.Index = (ALuint)(State->Mod.Index * (ALuint64)range /
539 State->Mod.Range);
540 State->Mod.Range = range;
542 /* The modulation depth effects the amount of frequency change over the
543 * range of the sinus. It needs to be scaled by the modulation time so
544 * that a given depth produces a consistent change in frequency over all
545 * ranges of time. Since the depth is applied to a sinus value, it needs
546 * to be halfed once for the sinus range and again for the sinus swing
547 * in time (half of it is spent decreasing the frequency, half is spent
548 * increasing it).
550 State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /
551 2.0f * frequency;
554 // Update the offsets for the initial effect delay line.
555 static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALreverbState *State)
557 // Calculate the initial delay taps.
558 State->DelayTap[0] = fastf2u(earlyDelay * frequency);
559 State->DelayTap[1] = fastf2u((earlyDelay + lateDelay) * frequency);
562 // Update the early reflections mix and line coefficients.
563 static ALvoid UpdateEarlyLines(ALfloat lateDelay, ALreverbState *State)
565 ALuint index;
567 // Calculate the gain (coefficient) for each early delay line using the
568 // late delay time. This expands the early reflections to the start of
569 // the late reverb.
570 for(index = 0;index < 4;index++)
571 State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],
572 lateDelay);
575 // Update the offsets for the decorrelator line.
576 static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALreverbState *State)
578 ALuint index;
579 ALfloat length;
581 /* The late reverb inputs are decorrelated to smooth the reverb tail and
582 * reduce harsh echos. The first tap occurs immediately, while the
583 * remaining taps are delayed by multiples of a fraction of the smallest
584 * cyclical delay time.
586 * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
588 for(index = 0;index < 3;index++)
590 length = (DECO_FRACTION * powf(DECO_MULTIPLIER, (ALfloat)index)) *
591 LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));
592 State->DecoTap[index] = fastf2u(length * frequency);
596 // Update the late reverb mix, line lengths, and line coefficients.
597 static ALvoid UpdateLateLines(ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALreverbState *State)
599 ALfloat length;
600 ALuint index;
602 /* Calculate the late reverb gain. Since the output is tapped prior to the
603 * application of the next delay line coefficients, this gain needs to be
604 * attenuated by the 'x' mixing matrix coefficient as well. Also attenuate
605 * the late reverb when echo depth is high and diffusion is low, so the
606 * echo is slightly stronger than the decorrelated echos in the reverb
607 * tail.
609 State->Late.Gain = xMix * (1.0f - (echoDepth*0.5f*(1.0f - diffusion)));
611 /* To compensate for changes in modal density and decay time of the late
612 * reverb signal, the input is attenuated based on the maximal energy of
613 * the outgoing signal. This approximation is used to keep the apparent
614 * energy of the signal equal for all ranges of density and decay time.
616 * The average length of the cyclcical delay lines is used to calculate
617 * the attenuation coefficient.
619 length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
620 LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;
621 length *= 1.0f + (density * LATE_LINE_MULTIPLIER);
622 State->Late.DensityGain = CalcDensityGain(
623 CalcDecayCoeff(length, decayTime)
626 // Calculate the all-pass feed-back and feed-forward coefficient.
627 State->Late.ApFeedCoeff = 0.5f * powf(diffusion, 2.0f);
629 for(index = 0;index < 4;index++)
631 // Calculate the gain (coefficient) for each all-pass line.
632 State->Late.ApCoeff[index] = CalcDecayCoeff(
633 ALLPASS_LINE_LENGTH[index], decayTime
636 // Calculate the length (in seconds) of each cyclical delay line.
637 length = LATE_LINE_LENGTH[index] *
638 (1.0f + (density * LATE_LINE_MULTIPLIER));
640 // Calculate the delay offset for each cyclical delay line.
641 State->Late.Offset[index] = fastf2u(length * frequency);
643 // Calculate the gain (coefficient) for each cyclical line.
644 State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);
646 // Calculate the damping coefficient for each low-pass filter.
647 State->Late.LpCoeff[index] = CalcDampingCoeff(
648 hfRatio, length, decayTime, State->Late.Coeff[index], cw
651 // Attenuate the cyclical line coefficients by the mixing coefficient
652 // (x).
653 State->Late.Coeff[index] *= xMix;
657 // Update the echo gain, line offset, line coefficients, and mixing
658 // coefficients.
659 static ALvoid UpdateEchoLine(ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALreverbState *State)
661 // Update the offset and coefficient for the echo delay line.
662 State->Echo.Offset = fastf2u(echoTime * frequency);
664 // Calculate the decay coefficient for the echo line.
665 State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);
667 // Calculate the energy-based attenuation coefficient for the echo delay
668 // line.
669 State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);
671 // Calculate the echo all-pass feed coefficient.
672 State->Echo.ApFeedCoeff = 0.5f * powf(diffusion, 2.0f);
674 // Calculate the echo all-pass attenuation coefficient.
675 State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);
677 // Calculate the damping coefficient for each low-pass filter.
678 State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,
679 State->Echo.Coeff, cw);
681 /* Calculate the echo mixing coefficient. This is applied to the output mix
682 * only, not the feedback.
684 State->Echo.MixCoeff = echoDepth;
687 // Update the early and late 3D panning gains.
688 static ALvoid UpdateMixedPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
690 ALfloat DirGains[MAX_OUTPUT_CHANNELS];
691 ALfloat coeffs[MAX_AMBI_COEFFS];
692 ALfloat length;
693 ALuint i;
695 /* With HRTF or UHJ, the normal output provides a panned reverb channel
696 * when a non-0-length vector is specified, while the real stereo output
697 * provides two other "direct" non-panned reverb channels.
699 * WARNING: This assumes the real output follows the virtual output in the
700 * device's DryBuffer.
702 memset(State->Early.PanGain, 0, sizeof(State->Early.PanGain));
703 length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
704 if(!(length > FLT_EPSILON))
706 for(i = 0;i < Device->RealOut.NumChannels;i++)
707 State->Early.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * EarlyGain;
709 else
711 /* Note that EAX Reverb's panning vectors are using right-handed
712 * coordinates, rather that the OpenAL's left-handed coordinates.
713 * Negate Z to fix this.
715 ALfloat pan[3] = {
716 ReflectionsPan[0] / length,
717 ReflectionsPan[1] / length,
718 -ReflectionsPan[2] / length,
720 length = minf(length, 1.0f);
722 CalcDirectionCoeffs(pan, coeffs);
723 ComputePanningGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels, coeffs, Gain, DirGains);
724 for(i = 0;i < Device->Dry.NumChannels;i++)
725 State->Early.PanGain[3][i] = DirGains[i] * EarlyGain * length;
726 for(i = 0;i < Device->RealOut.NumChannels;i++)
727 State->Early.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * EarlyGain * (1.0f-length);
730 memset(State->Late.PanGain, 0, sizeof(State->Late.PanGain));
731 length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
732 if(!(length > FLT_EPSILON))
734 for(i = 0;i < Device->RealOut.NumChannels;i++)
735 State->Late.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * LateGain;
737 else
739 ALfloat pan[3] = {
740 LateReverbPan[0] / length,
741 LateReverbPan[1] / length,
742 -LateReverbPan[2] / length,
744 length = minf(length, 1.0f);
746 CalcDirectionCoeffs(pan, coeffs);
747 ComputePanningGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels, coeffs, Gain, DirGains);
748 for(i = 0;i < Device->Dry.NumChannels;i++)
749 State->Late.PanGain[3][i] = DirGains[i] * LateGain * length;
750 for(i = 0;i < Device->RealOut.NumChannels;i++)
751 State->Late.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * LateGain * (1.0f-length);
755 static ALvoid UpdateDirectPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
757 ALfloat AmbientGains[MAX_OUTPUT_CHANNELS];
758 ALfloat DirGains[MAX_OUTPUT_CHANNELS];
759 ALfloat coeffs[MAX_AMBI_COEFFS];
760 ALfloat length;
761 ALuint i;
763 /* Apply a boost of about 3dB to better match the expected stereo output volume. */
764 ComputeAmbientGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels,
765 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, coeffs);
788 ComputePanningGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels, coeffs,
789 Gain, DirGains);
790 for(i = 0;i < Device->Dry.NumChannels;i++)
791 State->Early.PanGain[i&3][i] = lerp(AmbientGains[i], DirGains[i], length) * EarlyGain;
794 memset(State->Late.PanGain, 0, sizeof(State->Late.PanGain));
795 length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
796 if(!(length > FLT_EPSILON))
798 for(i = 0;i < Device->Dry.NumChannels;i++)
799 State->Late.PanGain[i&3][i] = AmbientGains[i] * LateGain;
801 else
803 ALfloat pan[3] = {
804 LateReverbPan[0] / length,
805 LateReverbPan[1] / length,
806 -LateReverbPan[2] / length,
808 length = minf(length, 1.0f);
810 CalcDirectionCoeffs(pan, coeffs);
811 ComputePanningGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels, coeffs,
812 Gain, DirGains);
813 for(i = 0;i < Device->Dry.NumChannels;i++)
814 State->Late.PanGain[i&3][i] = lerp(AmbientGains[i], DirGains[i], length) * LateGain;
818 static ALvoid Update3DPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
820 static const ALfloat PanDirs[4][3] = {
821 { -0.707106781f, 0.0f, -0.707106781f }, /* Front left */
822 { 0.707106781f, 0.0f, -0.707106781f }, /* Front right */
823 { 0.707106781f, 0.0f, 0.707106781f }, /* Back right */
824 { -0.707106781f, 0.0f, 0.707106781f } /* Back left */
826 ALfloat coeffs[MAX_AMBI_COEFFS];
827 ALfloat gain[4];
828 ALfloat length;
829 ALuint i;
831 /* 0.5 would be the gain scaling when the panning vector is 0. This also
832 * equals sqrt(1/4), a nice gain scaling for the four virtual points
833 * producing an "ambient" response.
835 gain[0] = gain[1] = gain[2] = gain[3] = 0.5f;
836 length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
837 if(length > 1.0f)
839 ALfloat pan[3] = {
840 ReflectionsPan[0] / length,
841 ReflectionsPan[1] / length,
842 -ReflectionsPan[2] / length,
844 for(i = 0;i < 4;i++)
846 ALfloat dotp = pan[0]*PanDirs[i][0] + pan[1]*PanDirs[i][1] + pan[2]*PanDirs[i][2];
847 gain[i] = dotp*0.5f + 0.5f;
850 else if(length > FLT_EPSILON)
852 for(i = 0;i < 4;i++)
854 ALfloat dotp = ReflectionsPan[0]*PanDirs[i][0] + ReflectionsPan[1]*PanDirs[i][1] +
855 -ReflectionsPan[2]*PanDirs[i][2];
856 gain[i] = dotp*0.5f + 0.5f;
859 for(i = 0;i < 4;i++)
861 CalcDirectionCoeffs(PanDirs[i], coeffs);
862 ComputePanningGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels, coeffs,
863 Gain*EarlyGain*gain[i], State->Early.PanGain[i]);
866 gain[0] = gain[1] = gain[2] = gain[3] = 0.5f;
867 length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
868 if(length > 1.0f)
870 ALfloat pan[3] = {
871 LateReverbPan[0] / length,
872 LateReverbPan[1] / length,
873 -LateReverbPan[2] / length,
875 for(i = 0;i < 4;i++)
877 ALfloat dotp = pan[0]*PanDirs[i][0] + pan[1]*PanDirs[i][1] + pan[2]*PanDirs[i][2];
878 gain[i] = dotp*0.5f + 0.5f;
881 else if(length > FLT_EPSILON)
883 for(i = 0;i < 4;i++)
885 ALfloat dotp = LateReverbPan[0]*PanDirs[i][0] + LateReverbPan[1]*PanDirs[i][1] +
886 -LateReverbPan[2]*PanDirs[i][2];
887 gain[i] = dotp*0.5f + 0.5f;
890 for(i = 0;i < 4;i++)
892 CalcDirectionCoeffs(PanDirs[i], coeffs);
893 ComputePanningGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels, coeffs,
894 Gain*LateGain*gain[i], State->Late.PanGain[i]);
898 static ALvoid ALreverbState_update(ALreverbState *State, const ALCdevice *Device, const ALeffectslot *Slot)
900 const ALeffectProps *props = &Slot->EffectProps;
901 ALuint frequency = Device->Frequency;
902 ALfloat lfscale, hfscale, hfRatio;
903 ALfloat gain, gainlf, gainhf;
904 ALfloat cw, x, y;
906 if(Slot->EffectType == AL_EFFECT_EAXREVERB && !EmulateEAXReverb)
907 State->IsEax = AL_TRUE;
908 else if(Slot->EffectType == AL_EFFECT_REVERB || EmulateEAXReverb)
909 State->IsEax = AL_FALSE;
911 // Calculate the master filters
912 hfscale = props->Reverb.HFReference / frequency;
913 gainhf = maxf(props->Reverb.GainHF, 0.0001f);
914 ALfilterState_setParams(&State->LpFilter, ALfilterType_HighShelf,
915 gainhf, hfscale, calc_rcpQ_from_slope(gainhf, 0.75f));
916 lfscale = props->Reverb.LFReference / frequency;
917 gainlf = maxf(props->Reverb.GainLF, 0.0001f);
918 ALfilterState_setParams(&State->HpFilter, ALfilterType_LowShelf,
919 gainlf, lfscale, calc_rcpQ_from_slope(gainlf, 0.75f));
921 // Update the modulator line.
922 UpdateModulator(props->Reverb.ModulationTime, props->Reverb.ModulationDepth,
923 frequency, State);
925 // Update the initial effect delay.
926 UpdateDelayLine(props->Reverb.ReflectionsDelay, props->Reverb.LateReverbDelay,
927 frequency, State);
929 // Update the early lines.
930 UpdateEarlyLines(props->Reverb.LateReverbDelay, State);
932 // Update the decorrelator.
933 UpdateDecorrelator(props->Reverb.Density, frequency, State);
935 // Get the mixing matrix coefficients (x and y).
936 CalcMatrixCoeffs(props->Reverb.Diffusion, &x, &y);
937 // Then divide x into y to simplify the matrix calculation.
938 State->Late.MixCoeff = y / x;
940 // If the HF limit parameter is flagged, calculate an appropriate limit
941 // based on the air absorption parameter.
942 hfRatio = props->Reverb.DecayHFRatio;
943 if(props->Reverb.DecayHFLimit && props->Reverb.AirAbsorptionGainHF < 1.0f)
944 hfRatio = CalcLimitedHfRatio(hfRatio, props->Reverb.AirAbsorptionGainHF,
945 props->Reverb.DecayTime);
947 cw = cosf(F_TAU * hfscale);
948 // Update the late lines.
949 UpdateLateLines(x, props->Reverb.Density, props->Reverb.DecayTime,
950 props->Reverb.Diffusion, props->Reverb.EchoDepth,
951 hfRatio, cw, frequency, State);
953 // Update the echo line.
954 UpdateEchoLine(props->Reverb.EchoTime, props->Reverb.DecayTime,
955 props->Reverb.Diffusion, props->Reverb.EchoDepth,
956 hfRatio, cw, frequency, State);
958 gain = props->Reverb.Gain * Slot->Gain * ReverbBoost;
959 // Update early and late 3D panning.
960 if(Device->Hrtf || Device->Uhj_Encoder)
961 UpdateMixedPanning(Device, props->Reverb.ReflectionsPan,
962 props->Reverb.LateReverbPan, gain,
963 props->Reverb.ReflectionsGain,
964 props->Reverb.LateReverbGain, State);
965 else if(Device->FmtChans == DevFmtBFormat3D || Device->AmbiDecoder)
966 Update3DPanning(Device, props->Reverb.ReflectionsPan,
967 props->Reverb.LateReverbPan, gain,
968 props->Reverb.ReflectionsGain,
969 props->Reverb.LateReverbGain, State);
970 else
971 UpdateDirectPanning(Device, props->Reverb.ReflectionsPan,
972 props->Reverb.LateReverbPan, gain,
973 props->Reverb.ReflectionsGain,
974 props->Reverb.LateReverbGain, State);
978 /**************************************
979 * Effect Processing *
980 **************************************/
982 // Basic delay line input/output routines.
983 static inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
985 return Delay->Line[offset&Delay->Mask];
988 static inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
990 Delay->Line[offset&Delay->Mask] = in;
993 // Given an input sample, this function produces modulation for the late
994 // reverb.
995 static inline ALfloat EAXModulation(ALreverbState *State, ALuint offset, ALfloat in)
997 ALfloat sinus, frac, fdelay;
998 ALfloat out0, out1;
999 ALuint delay;
1001 // Calculate the sinus rythm (dependent on modulation time and the
1002 // sampling rate). The center of the sinus is moved to reduce the delay
1003 // of the effect when the time or depth are low.
1004 sinus = 1.0f - cosf(F_TAU * State->Mod.Index / State->Mod.Range);
1006 // Step the modulation index forward, keeping it bound to its range.
1007 State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
1009 // The depth determines the range over which to read the input samples
1010 // from, so it must be filtered to reduce the distortion caused by even
1011 // small parameter changes.
1012 State->Mod.Filter = lerp(State->Mod.Filter, State->Mod.Depth,
1013 State->Mod.Coeff);
1015 // Calculate the read offset and fraction between it and the next sample.
1016 frac = modff(State->Mod.Filter*sinus, &fdelay);
1017 delay = fastf2u(fdelay);
1019 /* Add the incoming sample to the delay line first, so a 0 delay gets the
1020 * incoming sample.
1022 DelayLineIn(&State->Mod.Delay, offset, in);
1023 /* Get the two samples crossed by the offset delay */
1024 out0 = DelayLineOut(&State->Mod.Delay, offset - delay);
1025 out1 = DelayLineOut(&State->Mod.Delay, offset - delay - 1);
1027 // The output is obtained by linearly interpolating the two samples that
1028 // were acquired above.
1029 return lerp(out0, out1, frac);
1032 // Given some input sample, this function produces four-channel outputs for the
1033 // early reflections.
1034 static inline ALvoid EarlyReflection(ALreverbState *State, ALuint todo, ALfloat (*restrict out)[4])
1036 ALfloat d[4], v, f[4];
1037 ALuint i;
1039 for(i = 0;i < todo;i++)
1041 ALuint offset = State->Offset+i;
1043 // Obtain the decayed results of each early delay line.
1044 d[0] = DelayLineOut(&State->Early.Delay[0], offset-State->Early.Offset[0]) * State->Early.Coeff[0];
1045 d[1] = DelayLineOut(&State->Early.Delay[1], offset-State->Early.Offset[1]) * State->Early.Coeff[1];
1046 d[2] = DelayLineOut(&State->Early.Delay[2], offset-State->Early.Offset[2]) * State->Early.Coeff[2];
1047 d[3] = DelayLineOut(&State->Early.Delay[3], offset-State->Early.Offset[3]) * State->Early.Coeff[3];
1049 /* The following uses a lossless scattering junction from waveguide
1050 * theory. It actually amounts to a householder mixing matrix, which
1051 * will produce a maximally diffuse response, and means this can
1052 * probably be considered a simple feed-back delay network (FDN).
1054 * ---
1056 * v = 2/N / d_i
1057 * ---
1058 * i=1
1060 v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
1061 // The junction is loaded with the input here.
1062 v += DelayLineOut(&State->Delay, offset-State->DelayTap[0]);
1064 // Calculate the feed values for the delay lines.
1065 f[0] = v - d[0];
1066 f[1] = v - d[1];
1067 f[2] = v - d[2];
1068 f[3] = v - d[3];
1070 // Re-feed the delay lines.
1071 DelayLineIn(&State->Early.Delay[0], offset, f[0]);
1072 DelayLineIn(&State->Early.Delay[1], offset, f[1]);
1073 DelayLineIn(&State->Early.Delay[2], offset, f[2]);
1074 DelayLineIn(&State->Early.Delay[3], offset, f[3]);
1076 /* Output the results of the junction for all four channels with a
1077 * constant attenuation of 0.5.
1079 out[i][0] = f[0] * 0.5f;
1080 out[i][1] = f[1] * 0.5f;
1081 out[i][2] = f[2] * 0.5f;
1082 out[i][3] = f[3] * 0.5f;
1086 // Basic attenuated all-pass input/output routine.
1087 static inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
1089 ALfloat out, feed;
1091 out = DelayLineOut(Delay, outOffset);
1092 feed = feedCoeff * in;
1093 DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
1095 // The time-based attenuation is only applied to the delay output to
1096 // keep it from affecting the feed-back path (which is already controlled
1097 // by the all-pass feed coefficient).
1098 return (coeff * out) - feed;
1101 // All-pass input/output routine for late reverb.
1102 static inline ALfloat LateAllPassInOut(ALreverbState *State, ALuint offset, ALuint index, ALfloat in)
1104 return AllpassInOut(&State->Late.ApDelay[index],
1105 offset - State->Late.ApOffset[index],
1106 offset, in, State->Late.ApFeedCoeff,
1107 State->Late.ApCoeff[index]);
1110 // Low-pass filter input/output routine for late reverb.
1111 static inline ALfloat LateLowPassInOut(ALreverbState *State, ALuint index, ALfloat in)
1113 in = lerp(in, State->Late.LpSample[index], State->Late.LpCoeff[index]);
1114 State->Late.LpSample[index] = in;
1115 return in;
1118 // Given four decorrelated input samples, this function produces four-channel
1119 // output for the late reverb.
1120 static inline ALvoid LateReverb(ALreverbState *State, ALuint todo, ALfloat (*restrict out)[4])
1122 ALfloat d[4], f[4];
1123 ALuint i;
1125 // Feed the decorrelator from the energy-attenuated output of the second
1126 // delay tap.
1127 for(i = 0;i < todo;i++)
1129 ALuint offset = State->Offset+i;
1130 ALfloat sample = DelayLineOut(&State->Delay, offset - State->DelayTap[1]) *
1131 State->Late.DensityGain;
1132 DelayLineIn(&State->Decorrelator, offset, sample);
1135 for(i = 0;i < todo;i++)
1137 ALuint offset = State->Offset+i;
1139 /* Obtain four decorrelated input samples. */
1140 f[0] = DelayLineOut(&State->Decorrelator, offset);
1141 f[1] = DelayLineOut(&State->Decorrelator, offset-State->DecoTap[0]);
1142 f[2] = DelayLineOut(&State->Decorrelator, offset-State->DecoTap[1]);
1143 f[3] = DelayLineOut(&State->Decorrelator, offset-State->DecoTap[2]);
1145 /* Add the decayed results of the cyclical delay lines, then pass the
1146 * results through the low-pass filters.
1148 f[0] += DelayLineOut(&State->Late.Delay[0], offset-State->Late.Offset[0]) * State->Late.Coeff[0];
1149 f[1] += DelayLineOut(&State->Late.Delay[1], offset-State->Late.Offset[1]) * State->Late.Coeff[1];
1150 f[2] += DelayLineOut(&State->Late.Delay[2], offset-State->Late.Offset[2]) * State->Late.Coeff[2];
1151 f[3] += DelayLineOut(&State->Late.Delay[3], offset-State->Late.Offset[3]) * State->Late.Coeff[3];
1153 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and
1154 // back to 0.
1155 d[0] = LateLowPassInOut(State, 2, f[2]);
1156 d[1] = LateLowPassInOut(State, 0, f[0]);
1157 d[2] = LateLowPassInOut(State, 3, f[3]);
1158 d[3] = LateLowPassInOut(State, 1, f[1]);
1160 // To help increase diffusion, run each line through an all-pass filter.
1161 // When there is no diffusion, the shortest all-pass filter will feed
1162 // the shortest delay line.
1163 d[0] = LateAllPassInOut(State, offset, 0, d[0]);
1164 d[1] = LateAllPassInOut(State, offset, 1, d[1]);
1165 d[2] = LateAllPassInOut(State, offset, 2, d[2]);
1166 d[3] = LateAllPassInOut(State, offset, 3, d[3]);
1168 /* Late reverb is done with a modified feed-back delay network (FDN)
1169 * topology. Four input lines are each fed through their own all-pass
1170 * filter and then into the mixing matrix. The four outputs of the
1171 * mixing matrix are then cycled back to the inputs. Each output feeds
1172 * a different input to form a circlular feed cycle.
1174 * The mixing matrix used is a 4D skew-symmetric rotation matrix
1175 * derived using a single unitary rotational parameter:
1177 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
1178 * [ -a, d, c, -b ]
1179 * [ -b, -c, d, a ]
1180 * [ -c, b, -a, d ]
1182 * The rotation is constructed from the effect's diffusion parameter,
1183 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
1184 * with differing signs, and d is the coefficient x. The matrix is
1185 * thus:
1187 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
1188 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
1189 * [ y, -y, x, y ] x = cos(t)
1190 * [ -y, -y, -y, x ] y = sin(t) / n
1192 * To reduce the number of multiplies, the x coefficient is applied
1193 * with the cyclical delay line coefficients. Thus only the y
1194 * coefficient is applied when mixing, and is modified to be: y / x.
1196 f[0] = d[0] + (State->Late.MixCoeff * ( d[1] + -d[2] + d[3]));
1197 f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
1198 f[2] = d[2] + (State->Late.MixCoeff * ( d[0] + -d[1] + d[3]));
1199 f[3] = d[3] + (State->Late.MixCoeff * (-d[0] + -d[1] + -d[2] ));
1201 // Output the results of the matrix for all four channels, attenuated by
1202 // the late reverb gain (which is attenuated by the 'x' mix coefficient).
1203 out[i][0] = State->Late.Gain * f[0];
1204 out[i][1] = State->Late.Gain * f[1];
1205 out[i][2] = State->Late.Gain * f[2];
1206 out[i][3] = State->Late.Gain * f[3];
1208 // Re-feed the cyclical delay lines.
1209 DelayLineIn(&State->Late.Delay[0], offset, f[0]);
1210 DelayLineIn(&State->Late.Delay[1], offset, f[1]);
1211 DelayLineIn(&State->Late.Delay[2], offset, f[2]);
1212 DelayLineIn(&State->Late.Delay[3], offset, f[3]);
1216 // Given an input sample, this function mixes echo into the four-channel late
1217 // reverb.
1218 static inline ALvoid EAXEcho(ALreverbState *State, ALuint todo, ALfloat (*restrict late)[4])
1220 ALfloat out, feed;
1221 ALuint i;
1223 for(i = 0;i < todo;i++)
1225 ALuint offset = State->Offset+i;
1227 // Get the latest attenuated echo sample for output.
1228 feed = DelayLineOut(&State->Echo.Delay, offset-State->Echo.Offset) *
1229 State->Echo.Coeff;
1231 // Mix the output into the late reverb channels.
1232 out = State->Echo.MixCoeff * feed;
1233 late[i][0] += out;
1234 late[i][1] += out;
1235 late[i][2] += out;
1236 late[i][3] += out;
1238 // Mix the energy-attenuated input with the output and pass it through
1239 // the echo low-pass filter.
1240 feed += DelayLineOut(&State->Delay, offset-State->DelayTap[1]) *
1241 State->Echo.DensityGain;
1242 feed = lerp(feed, State->Echo.LpSample, State->Echo.LpCoeff);
1243 State->Echo.LpSample = feed;
1245 // Then the echo all-pass filter.
1246 feed = AllpassInOut(&State->Echo.ApDelay, offset-State->Echo.ApOffset,
1247 offset, feed, State->Echo.ApFeedCoeff,
1248 State->Echo.ApCoeff);
1250 // Feed the delay with the mixed and filtered sample.
1251 DelayLineIn(&State->Echo.Delay, offset, feed);
1255 // Perform the non-EAX reverb pass on a given input sample, resulting in
1256 // four-channel output.
1257 static inline ALvoid VerbPass(ALreverbState *State, ALuint todo, const ALfloat *in, ALfloat (*restrict early)[4], ALfloat (*restrict late)[4])
1259 ALuint i;
1261 // Low-pass filter the incoming samples.
1262 for(i = 0;i < todo;i++)
1263 DelayLineIn(&State->Delay, State->Offset+i,
1264 ALfilterState_processSingle(&State->LpFilter, in[i])
1267 // Calculate the early reflection from the first delay tap.
1268 EarlyReflection(State, todo, early);
1270 // Calculate the late reverb from the decorrelator taps.
1271 LateReverb(State, todo, late);
1273 // Step all delays forward one sample.
1274 State->Offset += todo;
1277 // Perform the EAX reverb pass on a given input sample, resulting in four-
1278 // channel output.
1279 static inline ALvoid EAXVerbPass(ALreverbState *State, ALuint todo, const ALfloat *input, ALfloat (*restrict early)[4], ALfloat (*restrict late)[4])
1281 ALuint i;
1283 // Band-pass and modulate the incoming samples.
1284 for(i = 0;i < todo;i++)
1286 ALfloat sample = input[i];
1287 sample = ALfilterState_processSingle(&State->LpFilter, sample);
1288 sample = ALfilterState_processSingle(&State->HpFilter, sample);
1290 // Perform any modulation on the input.
1291 sample = EAXModulation(State, State->Offset+i, sample);
1293 // Feed the initial delay line.
1294 DelayLineIn(&State->Delay, State->Offset+i, sample);
1297 // Calculate the early reflection from the first delay tap.
1298 EarlyReflection(State, todo, early);
1300 // Calculate the late reverb from the decorrelator taps.
1301 LateReverb(State, todo, late);
1303 // Calculate and mix in any echo.
1304 EAXEcho(State, todo, late);
1306 // Step all delays forward.
1307 State->Offset += todo;
1310 static ALvoid ALreverbState_processStandard(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
1312 ALfloat (*restrict early)[4] = State->EarlySamples;
1313 ALfloat (*restrict late)[4] = State->ReverbSamples;
1314 ALuint index, c, i, l;
1315 ALfloat gain;
1317 /* Process reverb for these samples. */
1318 for(index = 0;index < SamplesToDo;)
1320 ALuint todo = minu(SamplesToDo-index, MAX_UPDATE_SAMPLES);
1322 VerbPass(State, todo, &SamplesIn[index], early, late);
1324 for(l = 0;l < 4;l++)
1326 for(c = 0;c < NumChannels;c++)
1328 gain = State->Early.PanGain[l][c];
1329 if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
1331 for(i = 0;i < todo;i++)
1332 SamplesOut[c][index+i] += gain*early[i][l];
1334 gain = State->Late.PanGain[l][c];
1335 if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
1337 for(i = 0;i < todo;i++)
1338 SamplesOut[c][index+i] += gain*late[i][l];
1343 index += todo;
1347 static ALvoid ALreverbState_processEax(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
1349 ALfloat (*restrict early)[4] = State->EarlySamples;
1350 ALfloat (*restrict late)[4] = State->ReverbSamples;
1351 ALuint index, c, i, l;
1352 ALfloat gain;
1354 /* Process reverb for these samples. */
1355 for(index = 0;index < SamplesToDo;)
1357 ALuint todo = minu(SamplesToDo-index, MAX_UPDATE_SAMPLES);
1359 EAXVerbPass(State, todo, &SamplesIn[index], early, late);
1361 for(l = 0;l < 4;l++)
1363 for(c = 0;c < NumChannels;c++)
1365 gain = State->Early.PanGain[l][c];
1366 if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
1368 for(i = 0;i < todo;i++)
1369 SamplesOut[c][index+i] += gain*early[i][l];
1371 gain = State->Late.PanGain[l][c];
1372 if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
1374 for(i = 0;i < todo;i++)
1375 SamplesOut[c][index+i] += gain*late[i][l];
1380 index += todo;
1384 static ALvoid ALreverbState_process(ALreverbState *State, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
1386 NumChannels += State->ExtraChannels;
1387 if(State->IsEax)
1388 ALreverbState_processEax(State, SamplesToDo, SamplesIn[0], SamplesOut, NumChannels);
1389 else
1390 ALreverbState_processStandard(State, SamplesToDo, SamplesIn[0], SamplesOut, NumChannels);
1394 typedef struct ALreverbStateFactory {
1395 DERIVE_FROM_TYPE(ALeffectStateFactory);
1396 } ALreverbStateFactory;
1398 static ALeffectState *ALreverbStateFactory_create(ALreverbStateFactory* UNUSED(factory))
1400 ALreverbState *state;
1401 ALuint index, l;
1403 state = ALreverbState_New(sizeof(*state));
1404 if(!state) return NULL;
1405 SET_VTABLE2(ALreverbState, ALeffectState, state);
1407 state->IsEax = AL_FALSE;
1408 state->ExtraChannels = 0;
1410 state->TotalSamples = 0;
1411 state->SampleBuffer = NULL;
1413 ALfilterState_clear(&state->LpFilter);
1414 ALfilterState_clear(&state->HpFilter);
1416 state->Mod.Delay.Mask = 0;
1417 state->Mod.Delay.Line = NULL;
1418 state->Mod.Index = 0;
1419 state->Mod.Range = 1;
1420 state->Mod.Depth = 0.0f;
1421 state->Mod.Coeff = 0.0f;
1422 state->Mod.Filter = 0.0f;
1424 state->Delay.Mask = 0;
1425 state->Delay.Line = NULL;
1426 state->DelayTap[0] = 0;
1427 state->DelayTap[1] = 0;
1429 for(index = 0;index < 4;index++)
1431 state->Early.Coeff[index] = 0.0f;
1432 state->Early.Delay[index].Mask = 0;
1433 state->Early.Delay[index].Line = NULL;
1434 state->Early.Offset[index] = 0;
1437 state->Decorrelator.Mask = 0;
1438 state->Decorrelator.Line = NULL;
1439 state->DecoTap[0] = 0;
1440 state->DecoTap[1] = 0;
1441 state->DecoTap[2] = 0;
1443 state->Late.Gain = 0.0f;
1444 state->Late.DensityGain = 0.0f;
1445 state->Late.ApFeedCoeff = 0.0f;
1446 state->Late.MixCoeff = 0.0f;
1447 for(index = 0;index < 4;index++)
1449 state->Late.ApCoeff[index] = 0.0f;
1450 state->Late.ApDelay[index].Mask = 0;
1451 state->Late.ApDelay[index].Line = NULL;
1452 state->Late.ApOffset[index] = 0;
1454 state->Late.Coeff[index] = 0.0f;
1455 state->Late.Delay[index].Mask = 0;
1456 state->Late.Delay[index].Line = NULL;
1457 state->Late.Offset[index] = 0;
1459 state->Late.LpCoeff[index] = 0.0f;
1460 state->Late.LpSample[index] = 0.0f;
1463 for(l = 0;l < 4;l++)
1465 for(index = 0;index < MAX_OUTPUT_CHANNELS;index++)
1467 state->Early.PanGain[l][index] = 0.0f;
1468 state->Late.PanGain[l][index] = 0.0f;
1472 state->Echo.DensityGain = 0.0f;
1473 state->Echo.Delay.Mask = 0;
1474 state->Echo.Delay.Line = NULL;
1475 state->Echo.ApDelay.Mask = 0;
1476 state->Echo.ApDelay.Line = NULL;
1477 state->Echo.Coeff = 0.0f;
1478 state->Echo.ApFeedCoeff = 0.0f;
1479 state->Echo.ApCoeff = 0.0f;
1480 state->Echo.Offset = 0;
1481 state->Echo.ApOffset = 0;
1482 state->Echo.LpCoeff = 0.0f;
1483 state->Echo.LpSample = 0.0f;
1484 state->Echo.MixCoeff = 0.0f;
1486 state->Offset = 0;
1488 return STATIC_CAST(ALeffectState, state);
1491 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALreverbStateFactory);
1493 ALeffectStateFactory *ALreverbStateFactory_getFactory(void)
1495 static ALreverbStateFactory ReverbFactory = { { GET_VTABLE2(ALreverbStateFactory, ALeffectStateFactory) } };
1497 return STATIC_CAST(ALeffectStateFactory, &ReverbFactory);
1501 void ALeaxreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
1503 ALeffectProps *props = &effect->Props;
1504 switch(param)
1506 case AL_EAXREVERB_DECAY_HFLIMIT:
1507 if(!(val >= AL_EAXREVERB_MIN_DECAY_HFLIMIT && val <= AL_EAXREVERB_MAX_DECAY_HFLIMIT))
1508 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1509 props->Reverb.DecayHFLimit = val;
1510 break;
1512 default:
1513 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1516 void ALeaxreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
1518 ALeaxreverb_setParami(effect, context, param, vals[0]);
1520 void ALeaxreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
1522 ALeffectProps *props = &effect->Props;
1523 switch(param)
1525 case AL_EAXREVERB_DENSITY:
1526 if(!(val >= AL_EAXREVERB_MIN_DENSITY && val <= AL_EAXREVERB_MAX_DENSITY))
1527 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1528 props->Reverb.Density = val;
1529 break;
1531 case AL_EAXREVERB_DIFFUSION:
1532 if(!(val >= AL_EAXREVERB_MIN_DIFFUSION && val <= AL_EAXREVERB_MAX_DIFFUSION))
1533 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1534 props->Reverb.Diffusion = val;
1535 break;
1537 case AL_EAXREVERB_GAIN:
1538 if(!(val >= AL_EAXREVERB_MIN_GAIN && val <= AL_EAXREVERB_MAX_GAIN))
1539 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1540 props->Reverb.Gain = val;
1541 break;
1543 case AL_EAXREVERB_GAINHF:
1544 if(!(val >= AL_EAXREVERB_MIN_GAINHF && val <= AL_EAXREVERB_MAX_GAINHF))
1545 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1546 props->Reverb.GainHF = val;
1547 break;
1549 case AL_EAXREVERB_GAINLF:
1550 if(!(val >= AL_EAXREVERB_MIN_GAINLF && val <= AL_EAXREVERB_MAX_GAINLF))
1551 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1552 props->Reverb.GainLF = val;
1553 break;
1555 case AL_EAXREVERB_DECAY_TIME:
1556 if(!(val >= AL_EAXREVERB_MIN_DECAY_TIME && val <= AL_EAXREVERB_MAX_DECAY_TIME))
1557 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1558 props->Reverb.DecayTime = val;
1559 break;
1561 case AL_EAXREVERB_DECAY_HFRATIO:
1562 if(!(val >= AL_EAXREVERB_MIN_DECAY_HFRATIO && val <= AL_EAXREVERB_MAX_DECAY_HFRATIO))
1563 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1564 props->Reverb.DecayHFRatio = val;
1565 break;
1567 case AL_EAXREVERB_DECAY_LFRATIO:
1568 if(!(val >= AL_EAXREVERB_MIN_DECAY_LFRATIO && val <= AL_EAXREVERB_MAX_DECAY_LFRATIO))
1569 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1570 props->Reverb.DecayLFRatio = val;
1571 break;
1573 case AL_EAXREVERB_REFLECTIONS_GAIN:
1574 if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_GAIN && val <= AL_EAXREVERB_MAX_REFLECTIONS_GAIN))
1575 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1576 props->Reverb.ReflectionsGain = val;
1577 break;
1579 case AL_EAXREVERB_REFLECTIONS_DELAY:
1580 if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_DELAY && val <= AL_EAXREVERB_MAX_REFLECTIONS_DELAY))
1581 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1582 props->Reverb.ReflectionsDelay = val;
1583 break;
1585 case AL_EAXREVERB_LATE_REVERB_GAIN:
1586 if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_GAIN && val <= AL_EAXREVERB_MAX_LATE_REVERB_GAIN))
1587 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1588 props->Reverb.LateReverbGain = val;
1589 break;
1591 case AL_EAXREVERB_LATE_REVERB_DELAY:
1592 if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_DELAY && val <= AL_EAXREVERB_MAX_LATE_REVERB_DELAY))
1593 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1594 props->Reverb.LateReverbDelay = val;
1595 break;
1597 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
1598 if(!(val >= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF))
1599 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1600 props->Reverb.AirAbsorptionGainHF = val;
1601 break;
1603 case AL_EAXREVERB_ECHO_TIME:
1604 if(!(val >= AL_EAXREVERB_MIN_ECHO_TIME && val <= AL_EAXREVERB_MAX_ECHO_TIME))
1605 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1606 props->Reverb.EchoTime = val;
1607 break;
1609 case AL_EAXREVERB_ECHO_DEPTH:
1610 if(!(val >= AL_EAXREVERB_MIN_ECHO_DEPTH && val <= AL_EAXREVERB_MAX_ECHO_DEPTH))
1611 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1612 props->Reverb.EchoDepth = val;
1613 break;
1615 case AL_EAXREVERB_MODULATION_TIME:
1616 if(!(val >= AL_EAXREVERB_MIN_MODULATION_TIME && val <= AL_EAXREVERB_MAX_MODULATION_TIME))
1617 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1618 props->Reverb.ModulationTime = val;
1619 break;
1621 case AL_EAXREVERB_MODULATION_DEPTH:
1622 if(!(val >= AL_EAXREVERB_MIN_MODULATION_DEPTH && val <= AL_EAXREVERB_MAX_MODULATION_DEPTH))
1623 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1624 props->Reverb.ModulationDepth = val;
1625 break;
1627 case AL_EAXREVERB_HFREFERENCE:
1628 if(!(val >= AL_EAXREVERB_MIN_HFREFERENCE && val <= AL_EAXREVERB_MAX_HFREFERENCE))
1629 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1630 props->Reverb.HFReference = val;
1631 break;
1633 case AL_EAXREVERB_LFREFERENCE:
1634 if(!(val >= AL_EAXREVERB_MIN_LFREFERENCE && val <= AL_EAXREVERB_MAX_LFREFERENCE))
1635 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1636 props->Reverb.LFReference = val;
1637 break;
1639 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
1640 if(!(val >= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR))
1641 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1642 props->Reverb.RoomRolloffFactor = val;
1643 break;
1645 default:
1646 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1649 void ALeaxreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
1651 ALeffectProps *props = &effect->Props;
1652 switch(param)
1654 case AL_EAXREVERB_REFLECTIONS_PAN:
1655 if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
1656 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1657 LockContext(context);
1658 props->Reverb.ReflectionsPan[0] = vals[0];
1659 props->Reverb.ReflectionsPan[1] = vals[1];
1660 props->Reverb.ReflectionsPan[2] = vals[2];
1661 UnlockContext(context);
1662 break;
1663 case AL_EAXREVERB_LATE_REVERB_PAN:
1664 if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
1665 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1666 LockContext(context);
1667 props->Reverb.LateReverbPan[0] = vals[0];
1668 props->Reverb.LateReverbPan[1] = vals[1];
1669 props->Reverb.LateReverbPan[2] = vals[2];
1670 UnlockContext(context);
1671 break;
1673 default:
1674 ALeaxreverb_setParamf(effect, context, param, vals[0]);
1675 break;
1679 void ALeaxreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
1681 const ALeffectProps *props = &effect->Props;
1682 switch(param)
1684 case AL_EAXREVERB_DECAY_HFLIMIT:
1685 *val = props->Reverb.DecayHFLimit;
1686 break;
1688 default:
1689 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1692 void ALeaxreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
1694 ALeaxreverb_getParami(effect, context, param, vals);
1696 void ALeaxreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
1698 const ALeffectProps *props = &effect->Props;
1699 switch(param)
1701 case AL_EAXREVERB_DENSITY:
1702 *val = props->Reverb.Density;
1703 break;
1705 case AL_EAXREVERB_DIFFUSION:
1706 *val = props->Reverb.Diffusion;
1707 break;
1709 case AL_EAXREVERB_GAIN:
1710 *val = props->Reverb.Gain;
1711 break;
1713 case AL_EAXREVERB_GAINHF:
1714 *val = props->Reverb.GainHF;
1715 break;
1717 case AL_EAXREVERB_GAINLF:
1718 *val = props->Reverb.GainLF;
1719 break;
1721 case AL_EAXREVERB_DECAY_TIME:
1722 *val = props->Reverb.DecayTime;
1723 break;
1725 case AL_EAXREVERB_DECAY_HFRATIO:
1726 *val = props->Reverb.DecayHFRatio;
1727 break;
1729 case AL_EAXREVERB_DECAY_LFRATIO:
1730 *val = props->Reverb.DecayLFRatio;
1731 break;
1733 case AL_EAXREVERB_REFLECTIONS_GAIN:
1734 *val = props->Reverb.ReflectionsGain;
1735 break;
1737 case AL_EAXREVERB_REFLECTIONS_DELAY:
1738 *val = props->Reverb.ReflectionsDelay;
1739 break;
1741 case AL_EAXREVERB_LATE_REVERB_GAIN:
1742 *val = props->Reverb.LateReverbGain;
1743 break;
1745 case AL_EAXREVERB_LATE_REVERB_DELAY:
1746 *val = props->Reverb.LateReverbDelay;
1747 break;
1749 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
1750 *val = props->Reverb.AirAbsorptionGainHF;
1751 break;
1753 case AL_EAXREVERB_ECHO_TIME:
1754 *val = props->Reverb.EchoTime;
1755 break;
1757 case AL_EAXREVERB_ECHO_DEPTH:
1758 *val = props->Reverb.EchoDepth;
1759 break;
1761 case AL_EAXREVERB_MODULATION_TIME:
1762 *val = props->Reverb.ModulationTime;
1763 break;
1765 case AL_EAXREVERB_MODULATION_DEPTH:
1766 *val = props->Reverb.ModulationDepth;
1767 break;
1769 case AL_EAXREVERB_HFREFERENCE:
1770 *val = props->Reverb.HFReference;
1771 break;
1773 case AL_EAXREVERB_LFREFERENCE:
1774 *val = props->Reverb.LFReference;
1775 break;
1777 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
1778 *val = props->Reverb.RoomRolloffFactor;
1779 break;
1781 default:
1782 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1785 void ALeaxreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
1787 const ALeffectProps *props = &effect->Props;
1788 switch(param)
1790 case AL_EAXREVERB_REFLECTIONS_PAN:
1791 LockContext(context);
1792 vals[0] = props->Reverb.ReflectionsPan[0];
1793 vals[1] = props->Reverb.ReflectionsPan[1];
1794 vals[2] = props->Reverb.ReflectionsPan[2];
1795 UnlockContext(context);
1796 break;
1797 case AL_EAXREVERB_LATE_REVERB_PAN:
1798 LockContext(context);
1799 vals[0] = props->Reverb.LateReverbPan[0];
1800 vals[1] = props->Reverb.LateReverbPan[1];
1801 vals[2] = props->Reverb.LateReverbPan[2];
1802 UnlockContext(context);
1803 break;
1805 default:
1806 ALeaxreverb_getParamf(effect, context, param, vals);
1807 break;
1811 DEFINE_ALEFFECT_VTABLE(ALeaxreverb);
1813 void ALreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
1815 ALeffectProps *props = &effect->Props;
1816 switch(param)
1818 case AL_REVERB_DECAY_HFLIMIT:
1819 if(!(val >= AL_REVERB_MIN_DECAY_HFLIMIT && val <= AL_REVERB_MAX_DECAY_HFLIMIT))
1820 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1821 props->Reverb.DecayHFLimit = val;
1822 break;
1824 default:
1825 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1828 void ALreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
1830 ALreverb_setParami(effect, context, param, vals[0]);
1832 void ALreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
1834 ALeffectProps *props = &effect->Props;
1835 switch(param)
1837 case AL_REVERB_DENSITY:
1838 if(!(val >= AL_REVERB_MIN_DENSITY && val <= AL_REVERB_MAX_DENSITY))
1839 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1840 props->Reverb.Density = val;
1841 break;
1843 case AL_REVERB_DIFFUSION:
1844 if(!(val >= AL_REVERB_MIN_DIFFUSION && val <= AL_REVERB_MAX_DIFFUSION))
1845 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1846 props->Reverb.Diffusion = val;
1847 break;
1849 case AL_REVERB_GAIN:
1850 if(!(val >= AL_REVERB_MIN_GAIN && val <= AL_REVERB_MAX_GAIN))
1851 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1852 props->Reverb.Gain = val;
1853 break;
1855 case AL_REVERB_GAINHF:
1856 if(!(val >= AL_REVERB_MIN_GAINHF && val <= AL_REVERB_MAX_GAINHF))
1857 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1858 props->Reverb.GainHF = val;
1859 break;
1861 case AL_REVERB_DECAY_TIME:
1862 if(!(val >= AL_REVERB_MIN_DECAY_TIME && val <= AL_REVERB_MAX_DECAY_TIME))
1863 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1864 props->Reverb.DecayTime = val;
1865 break;
1867 case AL_REVERB_DECAY_HFRATIO:
1868 if(!(val >= AL_REVERB_MIN_DECAY_HFRATIO && val <= AL_REVERB_MAX_DECAY_HFRATIO))
1869 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1870 props->Reverb.DecayHFRatio = val;
1871 break;
1873 case AL_REVERB_REFLECTIONS_GAIN:
1874 if(!(val >= AL_REVERB_MIN_REFLECTIONS_GAIN && val <= AL_REVERB_MAX_REFLECTIONS_GAIN))
1875 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1876 props->Reverb.ReflectionsGain = val;
1877 break;
1879 case AL_REVERB_REFLECTIONS_DELAY:
1880 if(!(val >= AL_REVERB_MIN_REFLECTIONS_DELAY && val <= AL_REVERB_MAX_REFLECTIONS_DELAY))
1881 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1882 props->Reverb.ReflectionsDelay = val;
1883 break;
1885 case AL_REVERB_LATE_REVERB_GAIN:
1886 if(!(val >= AL_REVERB_MIN_LATE_REVERB_GAIN && val <= AL_REVERB_MAX_LATE_REVERB_GAIN))
1887 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1888 props->Reverb.LateReverbGain = val;
1889 break;
1891 case AL_REVERB_LATE_REVERB_DELAY:
1892 if(!(val >= AL_REVERB_MIN_LATE_REVERB_DELAY && val <= AL_REVERB_MAX_LATE_REVERB_DELAY))
1893 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1894 props->Reverb.LateReverbDelay = val;
1895 break;
1897 case AL_REVERB_AIR_ABSORPTION_GAINHF:
1898 if(!(val >= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF))
1899 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1900 props->Reverb.AirAbsorptionGainHF = val;
1901 break;
1903 case AL_REVERB_ROOM_ROLLOFF_FACTOR:
1904 if(!(val >= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR))
1905 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
1906 props->Reverb.RoomRolloffFactor = val;
1907 break;
1909 default:
1910 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1913 void ALreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
1915 ALreverb_setParamf(effect, context, param, vals[0]);
1918 void ALreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
1920 const ALeffectProps *props = &effect->Props;
1921 switch(param)
1923 case AL_REVERB_DECAY_HFLIMIT:
1924 *val = props->Reverb.DecayHFLimit;
1925 break;
1927 default:
1928 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1931 void ALreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
1933 ALreverb_getParami(effect, context, param, vals);
1935 void ALreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
1937 const ALeffectProps *props = &effect->Props;
1938 switch(param)
1940 case AL_REVERB_DENSITY:
1941 *val = props->Reverb.Density;
1942 break;
1944 case AL_REVERB_DIFFUSION:
1945 *val = props->Reverb.Diffusion;
1946 break;
1948 case AL_REVERB_GAIN:
1949 *val = props->Reverb.Gain;
1950 break;
1952 case AL_REVERB_GAINHF:
1953 *val = props->Reverb.GainHF;
1954 break;
1956 case AL_REVERB_DECAY_TIME:
1957 *val = props->Reverb.DecayTime;
1958 break;
1960 case AL_REVERB_DECAY_HFRATIO:
1961 *val = props->Reverb.DecayHFRatio;
1962 break;
1964 case AL_REVERB_REFLECTIONS_GAIN:
1965 *val = props->Reverb.ReflectionsGain;
1966 break;
1968 case AL_REVERB_REFLECTIONS_DELAY:
1969 *val = props->Reverb.ReflectionsDelay;
1970 break;
1972 case AL_REVERB_LATE_REVERB_GAIN:
1973 *val = props->Reverb.LateReverbGain;
1974 break;
1976 case AL_REVERB_LATE_REVERB_DELAY:
1977 *val = props->Reverb.LateReverbDelay;
1978 break;
1980 case AL_REVERB_AIR_ABSORPTION_GAINHF:
1981 *val = props->Reverb.AirAbsorptionGainHF;
1982 break;
1984 case AL_REVERB_ROOM_ROLLOFF_FACTOR:
1985 *val = props->Reverb.RoomRolloffFactor;
1986 break;
1988 default:
1989 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
1992 void ALreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
1994 ALreverb_getParamf(effect, context, param, vals);
1997 DEFINE_ALEFFECT_VTABLE(ALreverb);