Allow accessing the buffer data as multiple data types in the mixer
[openal-soft.git] / Alc / alcReverb.c
blobe1f6df45d9b78894280ec058aa69c35fd21e1b70
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 "AL/al.h"
28 #include "AL/alc.h"
29 #include "alMain.h"
30 #include "alAuxEffectSlot.h"
31 #include "alEffect.h"
32 #include "alError.h"
33 #include "alu.h"
35 typedef struct DelayLine
37 // The delay lines use sample lengths that are powers of 2 to allow the
38 // use of bit-masking instead of a modulus for wrapping.
39 ALuint Mask;
40 ALfloat *Line;
41 } DelayLine;
43 typedef struct ALverbState {
44 // Must be first in all effects!
45 ALeffectState state;
47 // All delay lines are allocated as a single buffer to reduce memory
48 // fragmentation and management code.
49 ALfloat *SampleBuffer;
50 ALuint TotalSamples;
51 // Master effect low-pass filter (2 chained 1-pole filters).
52 FILTER LpFilter;
53 ALfloat LpHistory[2];
54 struct {
55 // Modulator delay line.
56 DelayLine Delay;
57 // The vibrato time is tracked with an index over a modulus-wrapped
58 // range (in samples).
59 ALuint Index;
60 ALuint Range;
61 // The depth of frequency change (also in samples) and its filter.
62 ALfloat Depth;
63 ALfloat Coeff;
64 ALfloat Filter;
65 } Mod;
66 // Initial effect delay.
67 DelayLine Delay;
68 // The tap points for the initial delay. First tap goes to early
69 // reflections, the last to late reverb.
70 ALuint DelayTap[2];
71 struct {
72 // Output gain for early reflections.
73 ALfloat Gain;
74 // Early reflections are done with 4 delay lines.
75 ALfloat Coeff[4];
76 DelayLine Delay[4];
77 ALuint Offset[4];
78 // The gain for each output channel based on 3D panning (only for the
79 // EAX path).
80 ALfloat PanGain[OUTPUTCHANNELS];
81 } Early;
82 // Decorrelator delay line.
83 DelayLine Decorrelator;
84 // There are actually 4 decorrelator taps, but the first occurs at the
85 // initial sample.
86 ALuint DecoTap[3];
87 struct {
88 // Output gain for late reverb.
89 ALfloat Gain;
90 // Attenuation to compensate for the modal density and decay rate of
91 // the late lines.
92 ALfloat DensityGain;
93 // The feed-back and feed-forward all-pass coefficient.
94 ALfloat ApFeedCoeff;
95 // Mixing matrix coefficient.
96 ALfloat MixCoeff;
97 // Late reverb has 4 parallel all-pass filters.
98 ALfloat ApCoeff[4];
99 DelayLine ApDelay[4];
100 ALuint ApOffset[4];
101 // In addition to 4 cyclical delay lines.
102 ALfloat Coeff[4];
103 DelayLine Delay[4];
104 ALuint Offset[4];
105 // The cyclical delay lines are 1-pole low-pass filtered.
106 ALfloat LpCoeff[4];
107 ALfloat LpSample[4];
108 // The gain for each output channel based on 3D panning (only for the
109 // EAX path).
110 ALfloat PanGain[OUTPUTCHANNELS];
111 } Late;
112 struct {
113 // Attenuation to compensate for the modal density and decay rate of
114 // the echo line.
115 ALfloat DensityGain;
116 // Echo delay and all-pass lines.
117 DelayLine Delay;
118 DelayLine ApDelay;
119 ALfloat Coeff;
120 ALfloat ApFeedCoeff;
121 ALfloat ApCoeff;
122 ALuint Offset;
123 ALuint ApOffset;
124 // The echo line is 1-pole low-pass filtered.
125 ALfloat LpCoeff;
126 ALfloat LpSample;
127 // Echo mixing coefficients.
128 ALfloat MixCoeff[2];
129 } Echo;
130 // The current read offset for all delay lines.
131 ALuint Offset;
133 // The gain for each output channel (non-EAX path only; aliased from
134 // Late.PanGain)
135 ALfloat *Gain;
136 } ALverbState;
138 /* This coefficient is used to define the maximum frequency range controlled
139 * by the modulation depth. The current value of 0.1 will allow it to swing
140 * from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the
141 * sampler to stall on the downswing, and above 1 it will cause it to sample
142 * backwards.
144 static const ALfloat MODULATION_DEPTH_COEFF = 0.1f;
146 /* A filter is used to avoid the terrible distortion caused by changing
147 * modulation time and/or depth. To be consistent across different sample
148 * rates, the coefficient must be raised to a constant divided by the sample
149 * rate: coeff^(constant / rate).
151 static const ALfloat MODULATION_FILTER_COEFF = 0.048f;
152 static const ALfloat MODULATION_FILTER_CONST = 100000.0f;
154 // When diffusion is above 0, an all-pass filter is used to take the edge off
155 // the echo effect. It uses the following line length (in seconds).
156 static const ALfloat ECHO_ALLPASS_LENGTH = 0.0133f;
158 // Input into the late reverb is decorrelated between four channels. Their
159 // timings are dependent on a fraction and multiplier. See the
160 // UpdateDecorrelator() routine for the calculations involved.
161 static const ALfloat DECO_FRACTION = 0.15f;
162 static const ALfloat DECO_MULTIPLIER = 2.0f;
164 // All delay line lengths are specified in seconds.
166 // The lengths of the early delay lines.
167 static const ALfloat EARLY_LINE_LENGTH[4] =
169 0.0015f, 0.0045f, 0.0135f, 0.0405f
172 // The lengths of the late all-pass delay lines.
173 static const ALfloat ALLPASS_LINE_LENGTH[4] =
175 0.0151f, 0.0167f, 0.0183f, 0.0200f,
178 // The lengths of the late cyclical delay lines.
179 static const ALfloat LATE_LINE_LENGTH[4] =
181 0.0211f, 0.0311f, 0.0461f, 0.0680f
184 // The late cyclical delay lines have a variable length dependent on the
185 // effect's density parameter (inverted for some reason) and this multiplier.
186 static const ALfloat LATE_LINE_MULTIPLIER = 4.0f;
188 // Calculate the length of a delay line and store its mask and offset.
189 static ALuint CalcLineLength(ALfloat length, ALintptrEXT offset, ALuint frequency, DelayLine *Delay)
191 ALuint samples;
193 // All line lengths are powers of 2, calculated from their lengths, with
194 // an additional sample in case of rounding errors.
195 samples = NextPowerOf2((ALuint)(length * frequency) + 1);
196 // All lines share a single sample buffer.
197 Delay->Mask = samples - 1;
198 Delay->Line = (ALfloat*)offset;
199 // Return the sample count for accumulation.
200 return samples;
203 // Given the allocated sample buffer, this function updates each delay line
204 // offset.
205 static __inline ALvoid RealizeLineOffset(ALfloat * sampleBuffer, DelayLine *Delay)
207 Delay->Line = &sampleBuffer[(ALintptrEXT)Delay->Line];
210 /* Calculates the delay line metrics and allocates the shared sample buffer
211 * for all lines given a flag indicating whether or not to allocate the EAX-
212 * related delays (eaxFlag) and the sample rate (frequency). If an
213 * allocation failure occurs, it returns AL_FALSE.
215 static ALboolean AllocLines(ALboolean eaxFlag, ALuint frequency, ALverbState *State)
217 ALuint totalSamples, index;
218 ALfloat length;
219 ALfloat *newBuffer = NULL;
221 // All delay line lengths are calculated to accomodate the full range of
222 // lengths given their respective paramters.
223 totalSamples = 0;
224 if(eaxFlag)
226 /* The modulator's line length is calculated from the maximum
227 * modulation time and depth coefficient, and halfed for the low-to-
228 * high frequency swing. An additional sample is added to keep it
229 * stable when there is no modulation.
231 length = (AL_EAXREVERB_MAX_MODULATION_TIME * MODULATION_DEPTH_COEFF /
232 2.0f) + (1.0f / frequency);
233 totalSamples += CalcLineLength(length, totalSamples, frequency,
234 &State->Mod.Delay);
237 // The initial delay is the sum of the reflections and late reverb
238 // delays.
239 if(eaxFlag)
240 length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY +
241 AL_EAXREVERB_MAX_LATE_REVERB_DELAY;
242 else
243 length = AL_REVERB_MAX_REFLECTIONS_DELAY +
244 AL_REVERB_MAX_LATE_REVERB_DELAY;
245 totalSamples += CalcLineLength(length, totalSamples, frequency,
246 &State->Delay);
248 // The early reflection lines.
249 for(index = 0;index < 4;index++)
250 totalSamples += CalcLineLength(EARLY_LINE_LENGTH[index], totalSamples,
251 frequency, &State->Early.Delay[index]);
253 // The decorrelator line is calculated from the lowest reverb density (a
254 // parameter value of 1).
255 length = (DECO_FRACTION * DECO_MULTIPLIER * DECO_MULTIPLIER) *
256 LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER);
257 totalSamples += CalcLineLength(length, totalSamples, frequency,
258 &State->Decorrelator);
260 // The late all-pass lines.
261 for(index = 0;index < 4;index++)
262 totalSamples += CalcLineLength(ALLPASS_LINE_LENGTH[index], totalSamples,
263 frequency, &State->Late.ApDelay[index]);
265 // The late delay lines are calculated from the lowest reverb density.
266 for(index = 0;index < 4;index++)
268 length = LATE_LINE_LENGTH[index] * (1.0f + LATE_LINE_MULTIPLIER);
269 totalSamples += CalcLineLength(length, totalSamples, frequency,
270 &State->Late.Delay[index]);
273 if(eaxFlag)
275 // The echo all-pass and delay lines.
276 totalSamples += CalcLineLength(ECHO_ALLPASS_LENGTH, totalSamples,
277 frequency, &State->Echo.ApDelay);
278 totalSamples += CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME, totalSamples,
279 frequency, &State->Echo.Delay);
282 if(totalSamples != State->TotalSamples)
284 newBuffer = realloc(State->SampleBuffer, sizeof(ALfloat) * totalSamples);
285 if(newBuffer == NULL)
286 return AL_FALSE;
287 State->SampleBuffer = newBuffer;
288 State->TotalSamples = totalSamples;
291 // Update all delays to reflect the new sample buffer.
292 RealizeLineOffset(State->SampleBuffer, &State->Delay);
293 RealizeLineOffset(State->SampleBuffer, &State->Decorrelator);
294 for(index = 0;index < 4;index++)
296 RealizeLineOffset(State->SampleBuffer, &State->Early.Delay[index]);
297 RealizeLineOffset(State->SampleBuffer, &State->Late.ApDelay[index]);
298 RealizeLineOffset(State->SampleBuffer, &State->Late.Delay[index]);
300 if(eaxFlag)
302 RealizeLineOffset(State->SampleBuffer, &State->Mod.Delay);
303 RealizeLineOffset(State->SampleBuffer, &State->Echo.ApDelay);
304 RealizeLineOffset(State->SampleBuffer, &State->Echo.Delay);
307 // Clear the sample buffer.
308 for(index = 0;index < State->TotalSamples;index++)
309 State->SampleBuffer[index] = 0.0f;
311 return AL_TRUE;
314 // Calculate a decay coefficient given the length of each cycle and the time
315 // until the decay reaches -60 dB.
316 static __inline ALfloat CalcDecayCoeff(ALfloat length, ALfloat decayTime)
318 return aluPow(10.0f, length / decayTime * -60.0f / 20.0f);
321 // Calculate a decay length from a coefficient and the time until the decay
322 // reaches -60 dB.
323 static __inline ALfloat CalcDecayLength(ALfloat coeff, ALfloat decayTime)
325 return log10(coeff) / -60.0 * 20.0f * decayTime;
328 // Calculate the high frequency parameter for the I3DL2 coefficient
329 // calculation.
330 static __inline ALfloat CalcI3DL2HFreq(ALfloat hfRef, ALuint frequency)
332 return cos(2.0f * M_PI * hfRef / frequency);
335 // Calculate an attenuation to be applied to the input of any echo models to
336 // compensate for modal density and decay time.
337 static __inline ALfloat CalcDensityGain(ALfloat a)
339 /* The energy of a signal can be obtained by finding the area under the
340 * squared signal. This takes the form of Sum(x_n^2), where x is the
341 * amplitude for the sample n.
343 * Decaying feedback matches exponential decay of the form Sum(a^n),
344 * where a is the attenuation coefficient, and n is the sample. The area
345 * under this decay curve can be calculated as: 1 / (1 - a).
347 * Modifying the above equation to find the squared area under the curve
348 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
349 * calculated by inverting the square root of this approximation,
350 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
352 return aluSqrt(1.0f - (a * a));
355 // Calculate the mixing matrix coefficients given a diffusion factor.
356 static __inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)
358 ALfloat n, t;
360 // The matrix is of order 4, so n is sqrt (4 - 1).
361 n = aluSqrt(3.0f);
362 t = diffusion * atan(n);
364 // Calculate the first mixing matrix coefficient.
365 *x = cos(t);
366 // Calculate the second mixing matrix coefficient.
367 *y = sin(t) / n;
370 // Calculate the limited HF ratio for use with the late reverb low-pass
371 // filters.
372 static __inline ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)
374 ALfloat limitRatio;
376 /* Find the attenuation due to air absorption in dB (converting delay
377 * time to meters using the speed of sound). Then reversing the decay
378 * equation, solve for HF ratio. The delay length is cancelled out of
379 * the equation, so it can be calculated once for all lines.
381 limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *
382 SPEEDOFSOUNDMETRESPERSEC);
383 // Need to limit the result to a minimum of 0.1, just like the HF ratio
384 // parameter.
385 limitRatio = __max(limitRatio, 0.1f);
387 // Using the limit calculated above, apply the upper bound to the HF
388 // ratio.
389 return __min(hfRatio, limitRatio);
392 // Calculate the coefficient for a HF (and eventually LF) decay damping
393 // filter.
394 static __inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)
396 ALfloat coeff, g;
398 // Eventually this should boost the high frequencies when the ratio
399 // exceeds 1.
400 coeff = 0.0f;
401 if (hfRatio < 1.0f)
403 // Calculate the low-pass coefficient by dividing the HF decay
404 // coefficient by the full decay coefficient.
405 g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;
407 // Damping is done with a 1-pole filter, so g needs to be squared.
408 g *= g;
409 coeff = lpCoeffCalc(g, cw);
411 // Very low decay times will produce minimal output, so apply an
412 // upper bound to the coefficient.
413 coeff = __min(coeff, 0.98f);
415 return coeff;
418 // Update the EAX modulation index, range, and depth. Keep in mind that this
419 // kind of vibrato is additive and not multiplicative as one may expect. The
420 // downswing will sound stronger than the upswing.
421 static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALverbState *State)
423 ALfloat length;
425 /* Modulation is calculated in two parts.
427 * The modulation time effects the sinus applied to the change in
428 * frequency. An index out of the current time range (both in samples)
429 * is incremented each sample. The range is bound to a reasonable
430 * minimum (1 sample) and when the timing changes, the index is rescaled
431 * to the new range (to keep the sinus consistent).
433 length = modTime * frequency;
434 if (length >= 1.0f) {
435 State->Mod.Index = (ALuint)(State->Mod.Index * length /
436 State->Mod.Range);
437 State->Mod.Range = (ALuint)length;
438 } else {
439 State->Mod.Index = 0;
440 State->Mod.Range = 1;
443 /* The modulation depth effects the amount of frequency change over the
444 * range of the sinus. It needs to be scaled by the modulation time so
445 * that a given depth produces a consistent change in frequency over all
446 * ranges of time. Since the depth is applied to a sinus value, it needs
447 * to be halfed once for the sinus range and again for the sinus swing
448 * in time (half of it is spent decreasing the frequency, half is spent
449 * increasing it).
451 State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /
452 2.0f * frequency;
455 // Update the offsets for the initial effect delay line.
456 static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALverbState *State)
458 // Calculate the initial delay taps.
459 State->DelayTap[0] = (ALuint)(earlyDelay * frequency);
460 State->DelayTap[1] = (ALuint)((earlyDelay + lateDelay) * frequency);
463 // Update the early reflections gain and line coefficients.
464 static ALvoid UpdateEarlyLines(ALfloat reverbGain, ALfloat earlyGain, ALfloat lateDelay, ALverbState *State)
466 ALuint index;
468 // Calculate the early reflections gain (from the master effect gain, and
469 // reflections gain parameters) with a constant attenuation of 0.5.
470 State->Early.Gain = 0.5f * reverbGain * earlyGain;
472 // Calculate the gain (coefficient) for each early delay line using the
473 // late delay time. This expands the early reflections to the start of
474 // the late reverb.
475 for(index = 0;index < 4;index++)
476 State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],
477 lateDelay);
480 // Update the offsets for the decorrelator line.
481 static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALverbState *State)
483 ALuint index;
484 ALfloat length;
486 /* The late reverb inputs are decorrelated to smooth the reverb tail and
487 * reduce harsh echos. The first tap occurs immediately, while the
488 * remaining taps are delayed by multiples of a fraction of the smallest
489 * cyclical delay time.
491 * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
493 for(index = 0;index < 3;index++)
495 length = (DECO_FRACTION * aluPow(DECO_MULTIPLIER, (ALfloat)index)) *
496 LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));
497 State->DecoTap[index] = (ALuint)(length * frequency);
501 // Update the late reverb gains, line lengths, and line coefficients.
502 static ALvoid UpdateLateLines(ALfloat reverbGain, ALfloat lateGain, ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)
504 ALfloat length;
505 ALuint index;
507 /* Calculate the late reverb gain (from the master effect gain, and late
508 * reverb gain parameters). Since the output is tapped prior to the
509 * application of the next delay line coefficients, this gain needs to be
510 * attenuated by the 'x' mixing matrix coefficient as well.
512 State->Late.Gain = reverbGain * lateGain * xMix;
514 /* To compensate for changes in modal density and decay time of the late
515 * reverb signal, the input is attenuated based on the maximal energy of
516 * the outgoing signal. This approximation is used to keep the apparent
517 * energy of the signal equal for all ranges of density and decay time.
519 * The average length of the cyclcical delay lines is used to calculate
520 * the attenuation coefficient.
522 length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
523 LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;
524 length *= 1.0f + (density * LATE_LINE_MULTIPLIER);
525 State->Late.DensityGain = CalcDensityGain(CalcDecayCoeff(length,
526 decayTime));
528 // Calculate the all-pass feed-back and feed-forward coefficient.
529 State->Late.ApFeedCoeff = 0.5f * aluPow(diffusion, 2.0f);
531 for(index = 0;index < 4;index++)
533 // Calculate the gain (coefficient) for each all-pass line.
534 State->Late.ApCoeff[index] = CalcDecayCoeff(ALLPASS_LINE_LENGTH[index],
535 decayTime);
537 // Calculate the length (in seconds) of each cyclical delay line.
538 length = LATE_LINE_LENGTH[index] * (1.0f + (density *
539 LATE_LINE_MULTIPLIER));
541 // Calculate the delay offset for each cyclical delay line.
542 State->Late.Offset[index] = (ALuint)(length * frequency);
544 // Calculate the gain (coefficient) for each cyclical line.
545 State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);
547 // Calculate the damping coefficient for each low-pass filter.
548 State->Late.LpCoeff[index] =
549 CalcDampingCoeff(hfRatio, length, decayTime,
550 State->Late.Coeff[index], cw);
552 // Attenuate the cyclical line coefficients by the mixing coefficient
553 // (x).
554 State->Late.Coeff[index] *= xMix;
558 // Update the echo gain, line offset, line coefficients, and mixing
559 // coefficients.
560 static ALvoid UpdateEchoLine(ALfloat reverbGain, ALfloat lateGain, ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)
562 // Update the offset and coefficient for the echo delay line.
563 State->Echo.Offset = (ALuint)(echoTime * frequency);
565 // Calculate the decay coefficient for the echo line.
566 State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);
568 // Calculate the energy-based attenuation coefficient for the echo delay
569 // line.
570 State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);
572 // Calculate the echo all-pass feed coefficient.
573 State->Echo.ApFeedCoeff = 0.5f * aluPow(diffusion, 2.0f);
575 // Calculate the echo all-pass attenuation coefficient.
576 State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);
578 // Calculate the damping coefficient for each low-pass filter.
579 State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,
580 State->Echo.Coeff, cw);
582 /* Calculate the echo mixing coefficients. The first is applied to the
583 * echo itself. The second is used to attenuate the late reverb when
584 * echo depth is high and diffusion is low, so the echo is slightly
585 * stronger than the decorrelated echos in the reverb tail.
587 State->Echo.MixCoeff[0] = reverbGain * lateGain * echoDepth;
588 State->Echo.MixCoeff[1] = 1.0f - (echoDepth * 0.5f * (1.0f - diffusion));
591 // Update the early and late 3D panning gains.
592 static ALvoid Update3DPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALverbState *State)
594 ALfloat earlyPan[3] = { ReflectionsPan[0], ReflectionsPan[1],
595 ReflectionsPan[2] };
596 ALfloat latePan[3] = { LateReverbPan[0], LateReverbPan[1],
597 LateReverbPan[2] };
598 const ALfloat *speakerGain;
599 ALfloat dirGain, ambientGain;
600 ALfloat length;
601 ALuint index;
602 ALint pos;
604 // Calculate the 3D-panning gains for the early reflections and late
605 // reverb.
606 length = earlyPan[0]*earlyPan[0] + earlyPan[1]*earlyPan[1] + earlyPan[2]*earlyPan[2];
607 if(length > 1.0f)
609 length = 1.0f / aluSqrt(length);
610 earlyPan[0] *= length;
611 earlyPan[1] *= length;
612 earlyPan[2] *= length;
614 length = latePan[0]*latePan[0] + latePan[1]*latePan[1] + latePan[2]*latePan[2];
615 if(length > 1.0f)
617 length = 1.0f / aluSqrt(length);
618 latePan[0] *= length;
619 latePan[1] *= length;
620 latePan[2] *= length;
623 /* This code applies directional reverb just like the mixer applies
624 * directional sources. It diffuses the sound toward all speakers as the
625 * magnitude of the panning vector drops, which is only a rough
626 * approximation of the expansion of sound across the speakers from the
627 * panning direction.
629 pos = aluCart2LUTpos(earlyPan[2], earlyPan[0]);
630 speakerGain = &Device->PanningLUT[OUTPUTCHANNELS * pos];
631 dirGain = aluSqrt((earlyPan[0] * earlyPan[0]) + (earlyPan[2] * earlyPan[2]));
632 ambientGain = (1.0 - dirGain);
633 for(index = 0;index < OUTPUTCHANNELS;index++)
634 State->Early.PanGain[index] = 0.0f;
635 for(index = 0;index < Device->NumChan;index++)
637 Channel chan = Device->Speaker2Chan[index];
638 State->Early.PanGain[chan] = speakerGain[chan]*dirGain +
639 ambientGain;
642 pos = aluCart2LUTpos(latePan[2], latePan[0]);
643 speakerGain = &Device->PanningLUT[OUTPUTCHANNELS * pos];
644 dirGain = aluSqrt((latePan[0] * latePan[0]) + (latePan[2] * latePan[2]));
645 ambientGain = (1.0 - dirGain);
646 for(index = 0;index < OUTPUTCHANNELS;index++)
647 State->Late.PanGain[index] = 0.0f;
648 for(index = 0;index < Device->NumChan;index++)
650 Channel chan = Device->Speaker2Chan[index];
651 State->Late.PanGain[chan] = speakerGain[chan]*dirGain +
652 ambientGain;
656 // Basic delay line input/output routines.
657 static __inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
659 return Delay->Line[offset&Delay->Mask];
662 static __inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
664 Delay->Line[offset&Delay->Mask] = in;
667 // Attenuated delay line output routine.
668 static __inline ALfloat AttenuatedDelayLineOut(DelayLine *Delay, ALuint offset, ALfloat coeff)
670 return coeff * Delay->Line[offset&Delay->Mask];
673 // Basic attenuated all-pass input/output routine.
674 static __inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
676 ALfloat out, feed;
678 out = DelayLineOut(Delay, outOffset);
679 feed = feedCoeff * in;
680 DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
682 // The time-based attenuation is only applied to the delay output to
683 // keep it from affecting the feed-back path (which is already controlled
684 // by the all-pass feed coefficient).
685 return (coeff * out) - feed;
688 // Given an input sample, this function produces modulation for the late
689 // reverb.
690 static __inline ALfloat EAXModulation(ALverbState *State, ALfloat in)
692 ALfloat sinus, frac;
693 ALuint offset;
694 ALfloat out0, out1;
696 // Calculate the sinus rythm (dependent on modulation time and the
697 // sampling rate). The center of the sinus is moved to reduce the delay
698 // of the effect when the time or depth are low.
699 sinus = 1.0f - cos(2.0f * M_PI * State->Mod.Index / State->Mod.Range);
701 // The depth determines the range over which to read the input samples
702 // from, so it must be filtered to reduce the distortion caused by even
703 // small parameter changes.
704 State->Mod.Filter += (State->Mod.Depth - State->Mod.Filter) *
705 State->Mod.Coeff;
707 // Calculate the read offset and fraction between it and the next sample.
708 frac = (1.0f + (State->Mod.Filter * sinus));
709 offset = (ALuint)frac;
710 frac -= offset;
712 // Get the two samples crossed by the offset, and feed the delay line
713 // with the next input sample.
714 out0 = DelayLineOut(&State->Mod.Delay, State->Offset - offset);
715 out1 = DelayLineOut(&State->Mod.Delay, State->Offset - offset - 1);
716 DelayLineIn(&State->Mod.Delay, State->Offset, in);
718 // Step the modulation index forward, keeping it bound to its range.
719 State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
721 // The output is obtained by linearly interpolating the two samples that
722 // were acquired above.
723 return out0 + ((out1 - out0) * frac);
726 // Delay line output routine for early reflections.
727 static __inline ALfloat EarlyDelayLineOut(ALverbState *State, ALuint index)
729 return AttenuatedDelayLineOut(&State->Early.Delay[index],
730 State->Offset - State->Early.Offset[index],
731 State->Early.Coeff[index]);
734 // Given an input sample, this function produces four-channel output for the
735 // early reflections.
736 static __inline ALvoid EarlyReflection(ALverbState *State, ALfloat in, ALfloat *out)
738 ALfloat d[4], v, f[4];
740 // Obtain the decayed results of each early delay line.
741 d[0] = EarlyDelayLineOut(State, 0);
742 d[1] = EarlyDelayLineOut(State, 1);
743 d[2] = EarlyDelayLineOut(State, 2);
744 d[3] = EarlyDelayLineOut(State, 3);
746 /* The following uses a lossless scattering junction from waveguide
747 * theory. It actually amounts to a householder mixing matrix, which
748 * will produce a maximally diffuse response, and means this can probably
749 * be considered a simple feed-back delay network (FDN).
751 * ---
753 * v = 2/N / d_i
754 * ---
755 * i=1
757 v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
758 // The junction is loaded with the input here.
759 v += in;
761 // Calculate the feed values for the delay lines.
762 f[0] = v - d[0];
763 f[1] = v - d[1];
764 f[2] = v - d[2];
765 f[3] = v - d[3];
767 // Re-feed the delay lines.
768 DelayLineIn(&State->Early.Delay[0], State->Offset, f[0]);
769 DelayLineIn(&State->Early.Delay[1], State->Offset, f[1]);
770 DelayLineIn(&State->Early.Delay[2], State->Offset, f[2]);
771 DelayLineIn(&State->Early.Delay[3], State->Offset, f[3]);
773 // Output the results of the junction for all four channels.
774 out[0] = State->Early.Gain * f[0];
775 out[1] = State->Early.Gain * f[1];
776 out[2] = State->Early.Gain * f[2];
777 out[3] = State->Early.Gain * f[3];
780 // All-pass input/output routine for late reverb.
781 static __inline ALfloat LateAllPassInOut(ALverbState *State, ALuint index, ALfloat in)
783 return AllpassInOut(&State->Late.ApDelay[index],
784 State->Offset - State->Late.ApOffset[index],
785 State->Offset, in, State->Late.ApFeedCoeff,
786 State->Late.ApCoeff[index]);
789 // Delay line output routine for late reverb.
790 static __inline ALfloat LateDelayLineOut(ALverbState *State, ALuint index)
792 return AttenuatedDelayLineOut(&State->Late.Delay[index],
793 State->Offset - State->Late.Offset[index],
794 State->Late.Coeff[index]);
797 // Low-pass filter input/output routine for late reverb.
798 static __inline ALfloat LateLowPassInOut(ALverbState *State, ALuint index, ALfloat in)
800 State->Late.LpSample[index] = in +
801 ((State->Late.LpSample[index] - in) * State->Late.LpCoeff[index]);
802 return State->Late.LpSample[index];
805 // Given four decorrelated input samples, this function produces four-channel
806 // output for the late reverb.
807 static __inline ALvoid LateReverb(ALverbState *State, ALfloat *in, ALfloat *out)
809 ALfloat d[4], f[4];
811 // Obtain the decayed results of the cyclical delay lines, and add the
812 // corresponding input channels. Then pass the results through the
813 // low-pass filters.
815 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and back
816 // to 0.
817 d[0] = LateLowPassInOut(State, 2, in[2] + LateDelayLineOut(State, 2));
818 d[1] = LateLowPassInOut(State, 0, in[0] + LateDelayLineOut(State, 0));
819 d[2] = LateLowPassInOut(State, 3, in[3] + LateDelayLineOut(State, 3));
820 d[3] = LateLowPassInOut(State, 1, in[1] + LateDelayLineOut(State, 1));
822 // To help increase diffusion, run each line through an all-pass filter.
823 // When there is no diffusion, the shortest all-pass filter will feed the
824 // shortest delay line.
825 d[0] = LateAllPassInOut(State, 0, d[0]);
826 d[1] = LateAllPassInOut(State, 1, d[1]);
827 d[2] = LateAllPassInOut(State, 2, d[2]);
828 d[3] = LateAllPassInOut(State, 3, d[3]);
830 /* Late reverb is done with a modified feed-back delay network (FDN)
831 * topology. Four input lines are each fed through their own all-pass
832 * filter and then into the mixing matrix. The four outputs of the
833 * mixing matrix are then cycled back to the inputs. Each output feeds
834 * a different input to form a circlular feed cycle.
836 * The mixing matrix used is a 4D skew-symmetric rotation matrix derived
837 * using a single unitary rotational parameter:
839 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
840 * [ -a, d, c, -b ]
841 * [ -b, -c, d, a ]
842 * [ -c, b, -a, d ]
844 * The rotation is constructed from the effect's diffusion parameter,
845 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
846 * with differing signs, and d is the coefficient x. The matrix is thus:
848 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
849 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
850 * [ y, -y, x, y ] x = cos(t)
851 * [ -y, -y, -y, x ] y = sin(t) / n
853 * To reduce the number of multiplies, the x coefficient is applied with
854 * the cyclical delay line coefficients. Thus only the y coefficient is
855 * applied when mixing, and is modified to be: y / x.
857 f[0] = d[0] + (State->Late.MixCoeff * ( d[1] - d[2] + d[3]));
858 f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
859 f[2] = d[2] + (State->Late.MixCoeff * ( d[0] - d[1] + d[3]));
860 f[3] = d[3] + (State->Late.MixCoeff * (-d[0] - d[1] - d[2]));
862 // Output the results of the matrix for all four channels, attenuated by
863 // the late reverb gain (which is attenuated by the 'x' mix coefficient).
864 out[0] = State->Late.Gain * f[0];
865 out[1] = State->Late.Gain * f[1];
866 out[2] = State->Late.Gain * f[2];
867 out[3] = State->Late.Gain * f[3];
869 // Re-feed the cyclical delay lines.
870 DelayLineIn(&State->Late.Delay[0], State->Offset, f[0]);
871 DelayLineIn(&State->Late.Delay[1], State->Offset, f[1]);
872 DelayLineIn(&State->Late.Delay[2], State->Offset, f[2]);
873 DelayLineIn(&State->Late.Delay[3], State->Offset, f[3]);
876 // Given an input sample, this function mixes echo into the four-channel late
877 // reverb.
878 static __inline ALvoid EAXEcho(ALverbState *State, ALfloat in, ALfloat *late)
880 ALfloat out, feed;
882 // Get the latest attenuated echo sample for output.
883 feed = AttenuatedDelayLineOut(&State->Echo.Delay,
884 State->Offset - State->Echo.Offset,
885 State->Echo.Coeff);
887 // Mix the output into the late reverb channels.
888 out = State->Echo.MixCoeff[0] * feed;
889 late[0] = (State->Echo.MixCoeff[1] * late[0]) + out;
890 late[1] = (State->Echo.MixCoeff[1] * late[1]) + out;
891 late[2] = (State->Echo.MixCoeff[1] * late[2]) + out;
892 late[3] = (State->Echo.MixCoeff[1] * late[3]) + out;
894 // Mix the energy-attenuated input with the output and pass it through
895 // the echo low-pass filter.
896 feed += State->Echo.DensityGain * in;
897 feed += ((State->Echo.LpSample - feed) * State->Echo.LpCoeff);
898 State->Echo.LpSample = feed;
900 // Then the echo all-pass filter.
901 feed = AllpassInOut(&State->Echo.ApDelay,
902 State->Offset - State->Echo.ApOffset,
903 State->Offset, feed, State->Echo.ApFeedCoeff,
904 State->Echo.ApCoeff);
906 // Feed the delay with the mixed and filtered sample.
907 DelayLineIn(&State->Echo.Delay, State->Offset, feed);
910 // Perform the non-EAX reverb pass on a given input sample, resulting in
911 // four-channel output.
912 static __inline ALvoid VerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
914 ALfloat feed, taps[4];
916 // Low-pass filter the incoming sample.
917 in = lpFilter2P(&State->LpFilter, 0, in);
919 // Feed the initial delay line.
920 DelayLineIn(&State->Delay, State->Offset, in);
922 // Calculate the early reflection from the first delay tap.
923 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
924 EarlyReflection(State, in, early);
926 // Feed the decorrelator from the energy-attenuated output of the second
927 // delay tap.
928 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
929 feed = in * State->Late.DensityGain;
930 DelayLineIn(&State->Decorrelator, State->Offset, feed);
932 // Calculate the late reverb from the decorrelator taps.
933 taps[0] = feed;
934 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
935 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
936 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
937 LateReverb(State, taps, late);
939 // Step all delays forward one sample.
940 State->Offset++;
943 // Perform the EAX reverb pass on a given input sample, resulting in four-
944 // channel output.
945 static __inline ALvoid EAXVerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
947 ALfloat feed, taps[4];
949 // Low-pass filter the incoming sample.
950 in = lpFilter2P(&State->LpFilter, 0, in);
952 // Perform any modulation on the input.
953 in = EAXModulation(State, in);
955 // Feed the initial delay line.
956 DelayLineIn(&State->Delay, State->Offset, in);
958 // Calculate the early reflection from the first delay tap.
959 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
960 EarlyReflection(State, in, early);
962 // Feed the decorrelator from the energy-attenuated output of the second
963 // delay tap.
964 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
965 feed = in * State->Late.DensityGain;
966 DelayLineIn(&State->Decorrelator, State->Offset, feed);
968 // Calculate the late reverb from the decorrelator taps.
969 taps[0] = feed;
970 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
971 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
972 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
973 LateReverb(State, taps, late);
975 // Calculate and mix in any echo.
976 EAXEcho(State, in, late);
978 // Step all delays forward one sample.
979 State->Offset++;
982 // This destroys the reverb state. It should be called only when the effect
983 // slot has a different (or no) effect loaded over the reverb effect.
984 static ALvoid VerbDestroy(ALeffectState *effect)
986 ALverbState *State = (ALverbState*)effect;
987 if(State)
989 free(State->SampleBuffer);
990 State->SampleBuffer = NULL;
991 free(State);
995 // This updates the device-dependant reverb state. This is called on
996 // initialization and any time the device parameters (eg. playback frequency,
997 // or format) have been changed.
998 static ALboolean VerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
1000 ALverbState *State = (ALverbState*)effect;
1001 ALuint frequency = Device->Frequency;
1002 ALuint index;
1004 // Allocate the delay lines.
1005 if(!AllocLines(AL_FALSE, frequency, State))
1006 return AL_FALSE;
1008 // The early reflection and late all-pass filter line lengths are static,
1009 // so their offsets only need to be calculated once.
1010 for(index = 0;index < 4;index++)
1012 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
1013 frequency);
1014 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
1015 frequency);
1018 for(index = 0;index < OUTPUTCHANNELS;index++)
1019 State->Gain[index] = 0.0f;
1020 for(index = 0;index < Device->NumChan;index++)
1022 Channel chan = Device->Speaker2Chan[index];
1023 State->Gain[chan] = 1.0f;
1026 return AL_TRUE;
1029 // This updates the device-dependant EAX reverb state. This is called on
1030 // initialization and any time the device parameters (eg. playback frequency,
1031 // format) have been changed.
1032 static ALboolean EAXVerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
1034 ALverbState *State = (ALverbState*)effect;
1035 ALuint frequency = Device->Frequency, index;
1037 // Allocate the delay lines.
1038 if(!AllocLines(AL_TRUE, frequency, State))
1039 return AL_FALSE;
1041 // Calculate the modulation filter coefficient. Notice that the exponent
1042 // is calculated given the current sample rate. This ensures that the
1043 // resulting filter response over time is consistent across all sample
1044 // rates.
1045 State->Mod.Coeff = aluPow(MODULATION_FILTER_COEFF,
1046 MODULATION_FILTER_CONST / frequency);
1048 // The early reflection and late all-pass filter line lengths are static,
1049 // so their offsets only need to be calculated once.
1050 for(index = 0;index < 4;index++)
1052 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
1053 frequency);
1054 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
1055 frequency);
1058 // The echo all-pass filter line length is static, so its offset only
1059 // needs to be calculated once.
1060 State->Echo.ApOffset = (ALuint)(ECHO_ALLPASS_LENGTH * frequency);
1062 return AL_TRUE;
1065 // This updates the reverb state. This is called any time the reverb effect
1066 // is loaded into a slot.
1067 static ALvoid VerbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
1069 ALverbState *State = (ALverbState*)effect;
1070 ALuint frequency = Context->Device->Frequency;
1071 ALfloat cw, x, y, hfRatio;
1073 // Calculate the master low-pass filter (from the master effect HF gain).
1074 cw = CalcI3DL2HFreq(Effect->Reverb.HFReference, frequency);
1075 // This is done with 2 chained 1-pole filters, so no need to square g.
1076 State->LpFilter.coeff = lpCoeffCalc(Effect->Reverb.GainHF, cw);
1078 // Update the initial effect delay.
1079 UpdateDelayLine(Effect->Reverb.ReflectionsDelay,
1080 Effect->Reverb.LateReverbDelay, frequency, State);
1082 // Update the early lines.
1083 UpdateEarlyLines(Effect->Reverb.Gain, Effect->Reverb.ReflectionsGain,
1084 Effect->Reverb.LateReverbDelay, State);
1086 // Update the decorrelator.
1087 UpdateDecorrelator(Effect->Reverb.Density, frequency, State);
1089 // Get the mixing matrix coefficients (x and y).
1090 CalcMatrixCoeffs(Effect->Reverb.Diffusion, &x, &y);
1091 // Then divide x into y to simplify the matrix calculation.
1092 State->Late.MixCoeff = y / x;
1094 // If the HF limit parameter is flagged, calculate an appropriate limit
1095 // based on the air absorption parameter.
1096 hfRatio = Effect->Reverb.DecayHFRatio;
1097 if(Effect->Reverb.DecayHFLimit && Effect->Reverb.AirAbsorptionGainHF < 1.0f)
1098 hfRatio = CalcLimitedHfRatio(hfRatio, Effect->Reverb.AirAbsorptionGainHF,
1099 Effect->Reverb.DecayTime);
1101 // Update the late lines.
1102 UpdateLateLines(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1103 x, Effect->Reverb.Density, Effect->Reverb.DecayTime,
1104 Effect->Reverb.Diffusion, hfRatio, cw, frequency, State);
1107 // This updates the EAX reverb state. This is called any time the EAX reverb
1108 // effect is loaded into a slot.
1109 static ALvoid EAXVerbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
1111 ALverbState *State = (ALverbState*)effect;
1112 ALuint frequency = Context->Device->Frequency;
1113 ALfloat cw, x, y, hfRatio;
1115 // Calculate the master low-pass filter (from the master effect HF gain).
1116 cw = CalcI3DL2HFreq(Effect->Reverb.HFReference, frequency);
1117 // This is done with 2 chained 1-pole filters, so no need to square g.
1118 State->LpFilter.coeff = lpCoeffCalc(Effect->Reverb.GainHF, cw);
1120 // Update the modulator line.
1121 UpdateModulator(Effect->Reverb.ModulationTime,
1122 Effect->Reverb.ModulationDepth, frequency, State);
1124 // Update the initial effect delay.
1125 UpdateDelayLine(Effect->Reverb.ReflectionsDelay,
1126 Effect->Reverb.LateReverbDelay, frequency, State);
1128 // Update the early lines.
1129 UpdateEarlyLines(Effect->Reverb.Gain, Effect->Reverb.ReflectionsGain,
1130 Effect->Reverb.LateReverbDelay, State);
1132 // Update the decorrelator.
1133 UpdateDecorrelator(Effect->Reverb.Density, frequency, State);
1135 // Get the mixing matrix coefficients (x and y).
1136 CalcMatrixCoeffs(Effect->Reverb.Diffusion, &x, &y);
1137 // Then divide x into y to simplify the matrix calculation.
1138 State->Late.MixCoeff = y / x;
1140 // If the HF limit parameter is flagged, calculate an appropriate limit
1141 // based on the air absorption parameter.
1142 hfRatio = Effect->Reverb.DecayHFRatio;
1143 if(Effect->Reverb.DecayHFLimit && Effect->Reverb.AirAbsorptionGainHF < 1.0f)
1144 hfRatio = CalcLimitedHfRatio(hfRatio, Effect->Reverb.AirAbsorptionGainHF,
1145 Effect->Reverb.DecayTime);
1147 // Update the late lines.
1148 UpdateLateLines(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1149 x, Effect->Reverb.Density, Effect->Reverb.DecayTime,
1150 Effect->Reverb.Diffusion, hfRatio, cw, frequency, State);
1152 // Update the echo line.
1153 UpdateEchoLine(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1154 Effect->Reverb.EchoTime, Effect->Reverb.DecayTime,
1155 Effect->Reverb.Diffusion, Effect->Reverb.EchoDepth,
1156 hfRatio, cw, frequency, State);
1158 // Update early and late 3D panning.
1159 Update3DPanning(Context->Device, Effect->Reverb.ReflectionsPan,
1160 Effect->Reverb.LateReverbPan, State);
1163 // This processes the reverb state, given the input samples and an output
1164 // buffer.
1165 static ALvoid VerbProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
1167 ALverbState *State = (ALverbState*)effect;
1168 ALuint index;
1169 ALfloat early[4], late[4], out[4];
1170 ALfloat gain = Slot->Gain;
1171 const ALfloat *panGain = State->Gain;
1173 for(index = 0;index < SamplesToDo;index++)
1175 // Process reverb for this sample.
1176 VerbPass(State, SamplesIn[index], early, late);
1178 // Mix early reflections and late reverb.
1179 out[0] = (early[0] + late[0]) * gain;
1180 out[1] = (early[1] + late[1]) * gain;
1181 out[2] = (early[2] + late[2]) * gain;
1182 out[3] = (early[3] + late[3]) * gain;
1184 // Output the results.
1185 SamplesOut[index][FRONT_LEFT] += panGain[FRONT_LEFT] * out[0];
1186 SamplesOut[index][FRONT_RIGHT] += panGain[FRONT_RIGHT] * out[1];
1187 SamplesOut[index][FRONT_CENTER] += panGain[FRONT_CENTER] * out[3];
1188 SamplesOut[index][SIDE_LEFT] += panGain[SIDE_LEFT] * out[0];
1189 SamplesOut[index][SIDE_RIGHT] += panGain[SIDE_RIGHT] * out[1];
1190 SamplesOut[index][BACK_LEFT] += panGain[BACK_LEFT] * out[0];
1191 SamplesOut[index][BACK_RIGHT] += panGain[BACK_RIGHT] * out[1];
1192 SamplesOut[index][BACK_CENTER] += panGain[BACK_CENTER] * out[2];
1196 // This processes the EAX reverb state, given the input samples and an output
1197 // buffer.
1198 static ALvoid EAXVerbProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
1200 ALverbState *State = (ALverbState*)effect;
1201 ALuint index;
1202 ALfloat early[4], late[4];
1203 ALfloat gain = Slot->Gain;
1205 for(index = 0;index < SamplesToDo;index++)
1207 // Process reverb for this sample.
1208 EAXVerbPass(State, SamplesIn[index], early, late);
1210 // Unfortunately, while the number and configuration of gains for
1211 // panning adjust according to OUTPUTCHANNELS, the output from the
1212 // reverb engine is not so scalable.
1213 SamplesOut[index][FRONT_LEFT] +=
1214 (State->Early.PanGain[FRONT_LEFT]*early[0] +
1215 State->Late.PanGain[FRONT_LEFT]*late[0]) * gain;
1216 SamplesOut[index][FRONT_RIGHT] +=
1217 (State->Early.PanGain[FRONT_RIGHT]*early[1] +
1218 State->Late.PanGain[FRONT_RIGHT]*late[1]) * gain;
1219 SamplesOut[index][FRONT_CENTER] +=
1220 (State->Early.PanGain[FRONT_CENTER]*early[3] +
1221 State->Late.PanGain[FRONT_CENTER]*late[3]) * gain;
1222 SamplesOut[index][SIDE_LEFT] +=
1223 (State->Early.PanGain[SIDE_LEFT]*early[0] +
1224 State->Late.PanGain[SIDE_LEFT]*late[0]) * gain;
1225 SamplesOut[index][SIDE_RIGHT] +=
1226 (State->Early.PanGain[SIDE_RIGHT]*early[1] +
1227 State->Late.PanGain[SIDE_RIGHT]*late[1]) * gain;
1228 SamplesOut[index][BACK_LEFT] +=
1229 (State->Early.PanGain[BACK_LEFT]*early[0] +
1230 State->Late.PanGain[BACK_LEFT]*late[0]) * gain;
1231 SamplesOut[index][BACK_RIGHT] +=
1232 (State->Early.PanGain[BACK_RIGHT]*early[1] +
1233 State->Late.PanGain[BACK_RIGHT]*late[1]) * gain;
1234 SamplesOut[index][BACK_CENTER] +=
1235 (State->Early.PanGain[BACK_CENTER]*early[2] +
1236 State->Late.PanGain[BACK_CENTER]*late[2]) * gain;
1240 // This creates the reverb state. It should be called only when the reverb
1241 // effect is loaded into a slot that doesn't already have a reverb effect.
1242 ALeffectState *VerbCreate(void)
1244 ALverbState *State = NULL;
1245 ALuint index;
1247 State = malloc(sizeof(ALverbState));
1248 if(!State)
1249 return NULL;
1251 State->state.Destroy = VerbDestroy;
1252 State->state.DeviceUpdate = VerbDeviceUpdate;
1253 State->state.Update = VerbUpdate;
1254 State->state.Process = VerbProcess;
1256 State->TotalSamples = 0;
1257 State->SampleBuffer = NULL;
1259 State->LpFilter.coeff = 0.0f;
1260 State->LpFilter.history[0] = 0.0f;
1261 State->LpFilter.history[1] = 0.0f;
1263 State->Mod.Delay.Mask = 0;
1264 State->Mod.Delay.Line = NULL;
1265 State->Mod.Index = 0;
1266 State->Mod.Range = 1;
1267 State->Mod.Depth = 0.0f;
1268 State->Mod.Coeff = 0.0f;
1269 State->Mod.Filter = 0.0f;
1271 State->Delay.Mask = 0;
1272 State->Delay.Line = NULL;
1273 State->DelayTap[0] = 0;
1274 State->DelayTap[1] = 0;
1276 State->Early.Gain = 0.0f;
1277 for(index = 0;index < 4;index++)
1279 State->Early.Coeff[index] = 0.0f;
1280 State->Early.Delay[index].Mask = 0;
1281 State->Early.Delay[index].Line = NULL;
1282 State->Early.Offset[index] = 0;
1285 State->Decorrelator.Mask = 0;
1286 State->Decorrelator.Line = NULL;
1287 State->DecoTap[0] = 0;
1288 State->DecoTap[1] = 0;
1289 State->DecoTap[2] = 0;
1291 State->Late.Gain = 0.0f;
1292 State->Late.DensityGain = 0.0f;
1293 State->Late.ApFeedCoeff = 0.0f;
1294 State->Late.MixCoeff = 0.0f;
1295 for(index = 0;index < 4;index++)
1297 State->Late.ApCoeff[index] = 0.0f;
1298 State->Late.ApDelay[index].Mask = 0;
1299 State->Late.ApDelay[index].Line = NULL;
1300 State->Late.ApOffset[index] = 0;
1302 State->Late.Coeff[index] = 0.0f;
1303 State->Late.Delay[index].Mask = 0;
1304 State->Late.Delay[index].Line = NULL;
1305 State->Late.Offset[index] = 0;
1307 State->Late.LpCoeff[index] = 0.0f;
1308 State->Late.LpSample[index] = 0.0f;
1311 for(index = 0;index < OUTPUTCHANNELS;index++)
1313 State->Early.PanGain[index] = 0.0f;
1314 State->Late.PanGain[index] = 0.0f;
1317 State->Echo.DensityGain = 0.0f;
1318 State->Echo.Delay.Mask = 0;
1319 State->Echo.Delay.Line = NULL;
1320 State->Echo.ApDelay.Mask = 0;
1321 State->Echo.ApDelay.Line = NULL;
1322 State->Echo.Coeff = 0.0f;
1323 State->Echo.ApFeedCoeff = 0.0f;
1324 State->Echo.ApCoeff = 0.0f;
1325 State->Echo.Offset = 0;
1326 State->Echo.ApOffset = 0;
1327 State->Echo.LpCoeff = 0.0f;
1328 State->Echo.LpSample = 0.0f;
1329 State->Echo.MixCoeff[0] = 0.0f;
1330 State->Echo.MixCoeff[1] = 0.0f;
1332 State->Offset = 0;
1334 State->Gain = State->Late.PanGain;
1336 return &State->state;
1339 ALeffectState *EAXVerbCreate(void)
1341 ALeffectState *State = VerbCreate();
1342 if(State)
1344 State->DeviceUpdate = EAXVerbDeviceUpdate;
1345 State->Update = EAXVerbUpdate;
1346 State->Process = EAXVerbProcess;
1348 return State;