Use "internal" visibility by default
[openal-soft.git] / Alc / alcReverb.c
blobd4a03cf33821bb071895440b73867c459ab9b362
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[MAXCHANNELS];
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[MAXCHANNELS];
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;
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[MAXCHANNELS * pos];
631 dirGain = aluSqrt((earlyPan[0] * earlyPan[0]) + (earlyPan[2] * earlyPan[2]));
633 for(index = 0;index < MAXCHANNELS;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] = 1.0 + (speakerGain[chan]-1.0)*dirGain;
642 pos = aluCart2LUTpos(latePan[2], latePan[0]);
643 speakerGain = &Device->PanningLUT[MAXCHANNELS * pos];
644 dirGain = aluSqrt((latePan[0] * latePan[0]) + (latePan[2] * latePan[2]));
646 for(index = 0;index < MAXCHANNELS;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] = 1.0 + (speakerGain[chan]-1.0)*dirGain;
655 // Basic delay line input/output routines.
656 static __inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
658 return Delay->Line[offset&Delay->Mask];
661 static __inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
663 Delay->Line[offset&Delay->Mask] = in;
666 // Attenuated delay line output routine.
667 static __inline ALfloat AttenuatedDelayLineOut(DelayLine *Delay, ALuint offset, ALfloat coeff)
669 return coeff * Delay->Line[offset&Delay->Mask];
672 // Basic attenuated all-pass input/output routine.
673 static __inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
675 ALfloat out, feed;
677 out = DelayLineOut(Delay, outOffset);
678 feed = feedCoeff * in;
679 DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
681 // The time-based attenuation is only applied to the delay output to
682 // keep it from affecting the feed-back path (which is already controlled
683 // by the all-pass feed coefficient).
684 return (coeff * out) - feed;
687 // Given an input sample, this function produces modulation for the late
688 // reverb.
689 static __inline ALfloat EAXModulation(ALverbState *State, ALfloat in)
691 ALfloat sinus, frac;
692 ALuint offset;
693 ALfloat out0, out1;
695 // Calculate the sinus rythm (dependent on modulation time and the
696 // sampling rate). The center of the sinus is moved to reduce the delay
697 // of the effect when the time or depth are low.
698 sinus = 1.0f - cos(2.0f * M_PI * State->Mod.Index / State->Mod.Range);
700 // The depth determines the range over which to read the input samples
701 // from, so it must be filtered to reduce the distortion caused by even
702 // small parameter changes.
703 State->Mod.Filter = lerp(State->Mod.Filter, State->Mod.Depth,
704 State->Mod.Coeff);
706 // Calculate the read offset and fraction between it and the next sample.
707 frac = (1.0f + (State->Mod.Filter * sinus));
708 offset = (ALuint)frac;
709 frac -= offset;
711 // Get the two samples crossed by the offset, and feed the delay line
712 // with the next input sample.
713 out0 = DelayLineOut(&State->Mod.Delay, State->Offset - offset);
714 out1 = DelayLineOut(&State->Mod.Delay, State->Offset - offset - 1);
715 DelayLineIn(&State->Mod.Delay, State->Offset, in);
717 // Step the modulation index forward, keeping it bound to its range.
718 State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
720 // The output is obtained by linearly interpolating the two samples that
721 // were acquired above.
722 return lerp(out0, out1, frac);
725 // Delay line output routine for early reflections.
726 static __inline ALfloat EarlyDelayLineOut(ALverbState *State, ALuint index)
728 return AttenuatedDelayLineOut(&State->Early.Delay[index],
729 State->Offset - State->Early.Offset[index],
730 State->Early.Coeff[index]);
733 // Given an input sample, this function produces four-channel output for the
734 // early reflections.
735 static __inline ALvoid EarlyReflection(ALverbState *State, ALfloat in, ALfloat *out)
737 ALfloat d[4], v, f[4];
739 // Obtain the decayed results of each early delay line.
740 d[0] = EarlyDelayLineOut(State, 0);
741 d[1] = EarlyDelayLineOut(State, 1);
742 d[2] = EarlyDelayLineOut(State, 2);
743 d[3] = EarlyDelayLineOut(State, 3);
745 /* The following uses a lossless scattering junction from waveguide
746 * theory. It actually amounts to a householder mixing matrix, which
747 * will produce a maximally diffuse response, and means this can probably
748 * be considered a simple feed-back delay network (FDN).
750 * ---
752 * v = 2/N / d_i
753 * ---
754 * i=1
756 v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
757 // The junction is loaded with the input here.
758 v += in;
760 // Calculate the feed values for the delay lines.
761 f[0] = v - d[0];
762 f[1] = v - d[1];
763 f[2] = v - d[2];
764 f[3] = v - d[3];
766 // Re-feed the delay lines.
767 DelayLineIn(&State->Early.Delay[0], State->Offset, f[0]);
768 DelayLineIn(&State->Early.Delay[1], State->Offset, f[1]);
769 DelayLineIn(&State->Early.Delay[2], State->Offset, f[2]);
770 DelayLineIn(&State->Early.Delay[3], State->Offset, f[3]);
772 // Output the results of the junction for all four channels.
773 out[0] = State->Early.Gain * f[0];
774 out[1] = State->Early.Gain * f[1];
775 out[2] = State->Early.Gain * f[2];
776 out[3] = State->Early.Gain * f[3];
779 // All-pass input/output routine for late reverb.
780 static __inline ALfloat LateAllPassInOut(ALverbState *State, ALuint index, ALfloat in)
782 return AllpassInOut(&State->Late.ApDelay[index],
783 State->Offset - State->Late.ApOffset[index],
784 State->Offset, in, State->Late.ApFeedCoeff,
785 State->Late.ApCoeff[index]);
788 // Delay line output routine for late reverb.
789 static __inline ALfloat LateDelayLineOut(ALverbState *State, ALuint index)
791 return AttenuatedDelayLineOut(&State->Late.Delay[index],
792 State->Offset - State->Late.Offset[index],
793 State->Late.Coeff[index]);
796 // Low-pass filter input/output routine for late reverb.
797 static __inline ALfloat LateLowPassInOut(ALverbState *State, ALuint index, ALfloat in)
799 in = lerp(in, State->Late.LpSample[index], State->Late.LpCoeff[index]);
800 State->Late.LpSample[index] = in;
801 return in;
804 // Given four decorrelated input samples, this function produces four-channel
805 // output for the late reverb.
806 static __inline ALvoid LateReverb(ALverbState *State, ALfloat *in, ALfloat *out)
808 ALfloat d[4], f[4];
810 // Obtain the decayed results of the cyclical delay lines, and add the
811 // corresponding input channels. Then pass the results through the
812 // low-pass filters.
814 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and back
815 // to 0.
816 d[0] = LateLowPassInOut(State, 2, in[2] + LateDelayLineOut(State, 2));
817 d[1] = LateLowPassInOut(State, 0, in[0] + LateDelayLineOut(State, 0));
818 d[2] = LateLowPassInOut(State, 3, in[3] + LateDelayLineOut(State, 3));
819 d[3] = LateLowPassInOut(State, 1, in[1] + LateDelayLineOut(State, 1));
821 // To help increase diffusion, run each line through an all-pass filter.
822 // When there is no diffusion, the shortest all-pass filter will feed the
823 // shortest delay line.
824 d[0] = LateAllPassInOut(State, 0, d[0]);
825 d[1] = LateAllPassInOut(State, 1, d[1]);
826 d[2] = LateAllPassInOut(State, 2, d[2]);
827 d[3] = LateAllPassInOut(State, 3, d[3]);
829 /* Late reverb is done with a modified feed-back delay network (FDN)
830 * topology. Four input lines are each fed through their own all-pass
831 * filter and then into the mixing matrix. The four outputs of the
832 * mixing matrix are then cycled back to the inputs. Each output feeds
833 * a different input to form a circlular feed cycle.
835 * The mixing matrix used is a 4D skew-symmetric rotation matrix derived
836 * using a single unitary rotational parameter:
838 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
839 * [ -a, d, c, -b ]
840 * [ -b, -c, d, a ]
841 * [ -c, b, -a, d ]
843 * The rotation is constructed from the effect's diffusion parameter,
844 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
845 * with differing signs, and d is the coefficient x. The matrix is thus:
847 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
848 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
849 * [ y, -y, x, y ] x = cos(t)
850 * [ -y, -y, -y, x ] y = sin(t) / n
852 * To reduce the number of multiplies, the x coefficient is applied with
853 * the cyclical delay line coefficients. Thus only the y coefficient is
854 * applied when mixing, and is modified to be: y / x.
856 f[0] = d[0] + (State->Late.MixCoeff * ( d[1] + -d[2] + d[3]));
857 f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
858 f[2] = d[2] + (State->Late.MixCoeff * ( d[0] + -d[1] + d[3]));
859 f[3] = d[3] + (State->Late.MixCoeff * (-d[0] + -d[1] + -d[2] ));
861 // Output the results of the matrix for all four channels, attenuated by
862 // the late reverb gain (which is attenuated by the 'x' mix coefficient).
863 out[0] = State->Late.Gain * f[0];
864 out[1] = State->Late.Gain * f[1];
865 out[2] = State->Late.Gain * f[2];
866 out[3] = State->Late.Gain * f[3];
868 // Re-feed the cyclical delay lines.
869 DelayLineIn(&State->Late.Delay[0], State->Offset, f[0]);
870 DelayLineIn(&State->Late.Delay[1], State->Offset, f[1]);
871 DelayLineIn(&State->Late.Delay[2], State->Offset, f[2]);
872 DelayLineIn(&State->Late.Delay[3], State->Offset, f[3]);
875 // Given an input sample, this function mixes echo into the four-channel late
876 // reverb.
877 static __inline ALvoid EAXEcho(ALverbState *State, ALfloat in, ALfloat *late)
879 ALfloat out, feed;
881 // Get the latest attenuated echo sample for output.
882 feed = AttenuatedDelayLineOut(&State->Echo.Delay,
883 State->Offset - State->Echo.Offset,
884 State->Echo.Coeff);
886 // Mix the output into the late reverb channels.
887 out = State->Echo.MixCoeff[0] * feed;
888 late[0] = (State->Echo.MixCoeff[1] * late[0]) + out;
889 late[1] = (State->Echo.MixCoeff[1] * late[1]) + out;
890 late[2] = (State->Echo.MixCoeff[1] * late[2]) + out;
891 late[3] = (State->Echo.MixCoeff[1] * late[3]) + out;
893 // Mix the energy-attenuated input with the output and pass it through
894 // the echo low-pass filter.
895 feed += State->Echo.DensityGain * in;
896 feed = lerp(feed, State->Echo.LpSample, State->Echo.LpCoeff);
897 State->Echo.LpSample = feed;
899 // Then the echo all-pass filter.
900 feed = AllpassInOut(&State->Echo.ApDelay,
901 State->Offset - State->Echo.ApOffset,
902 State->Offset, feed, State->Echo.ApFeedCoeff,
903 State->Echo.ApCoeff);
905 // Feed the delay with the mixed and filtered sample.
906 DelayLineIn(&State->Echo.Delay, State->Offset, feed);
909 // Perform the non-EAX reverb pass on a given input sample, resulting in
910 // four-channel output.
911 static __inline ALvoid VerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
913 ALfloat feed, taps[4];
915 // Low-pass filter the incoming sample.
916 in = lpFilter2P(&State->LpFilter, 0, in);
918 // Feed the initial delay line.
919 DelayLineIn(&State->Delay, State->Offset, in);
921 // Calculate the early reflection from the first delay tap.
922 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
923 EarlyReflection(State, in, early);
925 // Feed the decorrelator from the energy-attenuated output of the second
926 // delay tap.
927 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
928 feed = in * State->Late.DensityGain;
929 DelayLineIn(&State->Decorrelator, State->Offset, feed);
931 // Calculate the late reverb from the decorrelator taps.
932 taps[0] = feed;
933 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
934 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
935 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
936 LateReverb(State, taps, late);
938 // Step all delays forward one sample.
939 State->Offset++;
942 // Perform the EAX reverb pass on a given input sample, resulting in four-
943 // channel output.
944 static __inline ALvoid EAXVerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
946 ALfloat feed, taps[4];
948 // Low-pass filter the incoming sample.
949 in = lpFilter2P(&State->LpFilter, 0, in);
951 // Perform any modulation on the input.
952 in = EAXModulation(State, in);
954 // Feed the initial delay line.
955 DelayLineIn(&State->Delay, State->Offset, in);
957 // Calculate the early reflection from the first delay tap.
958 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
959 EarlyReflection(State, in, early);
961 // Feed the decorrelator from the energy-attenuated output of the second
962 // delay tap.
963 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
964 feed = in * State->Late.DensityGain;
965 DelayLineIn(&State->Decorrelator, State->Offset, feed);
967 // Calculate the late reverb from the decorrelator taps.
968 taps[0] = feed;
969 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
970 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
971 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
972 LateReverb(State, taps, late);
974 // Calculate and mix in any echo.
975 EAXEcho(State, in, late);
977 // Step all delays forward one sample.
978 State->Offset++;
981 // This destroys the reverb state. It should be called only when the effect
982 // slot has a different (or no) effect loaded over the reverb effect.
983 static ALvoid VerbDestroy(ALeffectState *effect)
985 ALverbState *State = (ALverbState*)effect;
986 if(State)
988 free(State->SampleBuffer);
989 State->SampleBuffer = NULL;
990 free(State);
994 // This updates the device-dependant reverb state. This is called on
995 // initialization and any time the device parameters (eg. playback frequency,
996 // or format) have been changed.
997 static ALboolean VerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
999 ALverbState *State = (ALverbState*)effect;
1000 ALuint frequency = Device->Frequency;
1001 ALuint index;
1003 // Allocate the delay lines.
1004 if(!AllocLines(AL_FALSE, frequency, State))
1005 return AL_FALSE;
1007 // The early reflection and late all-pass filter line lengths are static,
1008 // so their offsets only need to be calculated once.
1009 for(index = 0;index < 4;index++)
1011 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
1012 frequency);
1013 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
1014 frequency);
1017 for(index = 0;index < MAXCHANNELS;index++)
1018 State->Gain[index] = 0.0f;
1019 for(index = 0;index < Device->NumChan;index++)
1021 Channel chan = Device->Speaker2Chan[index];
1022 State->Gain[chan] = 1.0f;
1025 return AL_TRUE;
1028 // This updates the device-dependant EAX reverb state. This is called on
1029 // initialization and any time the device parameters (eg. playback frequency,
1030 // format) have been changed.
1031 static ALboolean EAXVerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
1033 ALverbState *State = (ALverbState*)effect;
1034 ALuint frequency = Device->Frequency, index;
1036 // Allocate the delay lines.
1037 if(!AllocLines(AL_TRUE, frequency, State))
1038 return AL_FALSE;
1040 // Calculate the modulation filter coefficient. Notice that the exponent
1041 // is calculated given the current sample rate. This ensures that the
1042 // resulting filter response over time is consistent across all sample
1043 // rates.
1044 State->Mod.Coeff = aluPow(MODULATION_FILTER_COEFF,
1045 MODULATION_FILTER_CONST / frequency);
1047 // The early reflection and late all-pass filter line lengths are static,
1048 // so their offsets only need to be calculated once.
1049 for(index = 0;index < 4;index++)
1051 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
1052 frequency);
1053 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
1054 frequency);
1057 // The echo all-pass filter line length is static, so its offset only
1058 // needs to be calculated once.
1059 State->Echo.ApOffset = (ALuint)(ECHO_ALLPASS_LENGTH * frequency);
1061 return AL_TRUE;
1064 // This updates the reverb state. This is called any time the reverb effect
1065 // is loaded into a slot.
1066 static ALvoid VerbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
1068 ALverbState *State = (ALverbState*)effect;
1069 ALuint frequency = Context->Device->Frequency;
1070 ALfloat cw, x, y, hfRatio;
1072 // Calculate the master low-pass filter (from the master effect HF gain).
1073 cw = CalcI3DL2HFreq(Effect->Reverb.HFReference, frequency);
1074 // This is done with 2 chained 1-pole filters, so no need to square g.
1075 State->LpFilter.coeff = lpCoeffCalc(Effect->Reverb.GainHF, cw);
1077 // Update the initial effect delay.
1078 UpdateDelayLine(Effect->Reverb.ReflectionsDelay,
1079 Effect->Reverb.LateReverbDelay, frequency, State);
1081 // Update the early lines.
1082 UpdateEarlyLines(Effect->Reverb.Gain, Effect->Reverb.ReflectionsGain,
1083 Effect->Reverb.LateReverbDelay, State);
1085 // Update the decorrelator.
1086 UpdateDecorrelator(Effect->Reverb.Density, frequency, State);
1088 // Get the mixing matrix coefficients (x and y).
1089 CalcMatrixCoeffs(Effect->Reverb.Diffusion, &x, &y);
1090 // Then divide x into y to simplify the matrix calculation.
1091 State->Late.MixCoeff = y / x;
1093 // If the HF limit parameter is flagged, calculate an appropriate limit
1094 // based on the air absorption parameter.
1095 hfRatio = Effect->Reverb.DecayHFRatio;
1096 if(Effect->Reverb.DecayHFLimit && Effect->Reverb.AirAbsorptionGainHF < 1.0f)
1097 hfRatio = CalcLimitedHfRatio(hfRatio, Effect->Reverb.AirAbsorptionGainHF,
1098 Effect->Reverb.DecayTime);
1100 // Update the late lines.
1101 UpdateLateLines(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1102 x, Effect->Reverb.Density, Effect->Reverb.DecayTime,
1103 Effect->Reverb.Diffusion, hfRatio, cw, frequency, State);
1106 // This updates the EAX reverb state. This is called any time the EAX reverb
1107 // effect is loaded into a slot.
1108 static ALvoid EAXVerbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
1110 ALverbState *State = (ALverbState*)effect;
1111 ALuint frequency = Context->Device->Frequency;
1112 ALfloat cw, x, y, hfRatio;
1114 // Calculate the master low-pass filter (from the master effect HF gain).
1115 cw = CalcI3DL2HFreq(Effect->Reverb.HFReference, frequency);
1116 // This is done with 2 chained 1-pole filters, so no need to square g.
1117 State->LpFilter.coeff = lpCoeffCalc(Effect->Reverb.GainHF, cw);
1119 // Update the modulator line.
1120 UpdateModulator(Effect->Reverb.ModulationTime,
1121 Effect->Reverb.ModulationDepth, frequency, State);
1123 // Update the initial effect delay.
1124 UpdateDelayLine(Effect->Reverb.ReflectionsDelay,
1125 Effect->Reverb.LateReverbDelay, frequency, State);
1127 // Update the early lines.
1128 UpdateEarlyLines(Effect->Reverb.Gain, Effect->Reverb.ReflectionsGain,
1129 Effect->Reverb.LateReverbDelay, State);
1131 // Update the decorrelator.
1132 UpdateDecorrelator(Effect->Reverb.Density, frequency, State);
1134 // Get the mixing matrix coefficients (x and y).
1135 CalcMatrixCoeffs(Effect->Reverb.Diffusion, &x, &y);
1136 // Then divide x into y to simplify the matrix calculation.
1137 State->Late.MixCoeff = y / x;
1139 // If the HF limit parameter is flagged, calculate an appropriate limit
1140 // based on the air absorption parameter.
1141 hfRatio = Effect->Reverb.DecayHFRatio;
1142 if(Effect->Reverb.DecayHFLimit && Effect->Reverb.AirAbsorptionGainHF < 1.0f)
1143 hfRatio = CalcLimitedHfRatio(hfRatio, Effect->Reverb.AirAbsorptionGainHF,
1144 Effect->Reverb.DecayTime);
1146 // Update the late lines.
1147 UpdateLateLines(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1148 x, Effect->Reverb.Density, Effect->Reverb.DecayTime,
1149 Effect->Reverb.Diffusion, hfRatio, cw, frequency, State);
1151 // Update the echo line.
1152 UpdateEchoLine(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1153 Effect->Reverb.EchoTime, Effect->Reverb.DecayTime,
1154 Effect->Reverb.Diffusion, Effect->Reverb.EchoDepth,
1155 hfRatio, cw, frequency, State);
1157 // Update early and late 3D panning.
1158 Update3DPanning(Context->Device, Effect->Reverb.ReflectionsPan,
1159 Effect->Reverb.LateReverbPan, State);
1162 // This processes the reverb state, given the input samples and an output
1163 // buffer.
1164 static ALvoid VerbProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
1166 ALverbState *State = (ALverbState*)effect;
1167 ALuint index;
1168 ALfloat early[4], late[4], out[4];
1169 ALfloat gain = Slot->Gain;
1170 const ALfloat *panGain = State->Gain;
1172 for(index = 0;index < SamplesToDo;index++)
1174 // Process reverb for this sample.
1175 VerbPass(State, SamplesIn[index], early, late);
1177 // Mix early reflections and late reverb.
1178 out[0] = (early[0] + late[0]) * gain;
1179 out[1] = (early[1] + late[1]) * gain;
1180 out[2] = (early[2] + late[2]) * gain;
1181 out[3] = (early[3] + late[3]) * gain;
1183 // Output the results.
1184 SamplesOut[index][FRONT_LEFT] += panGain[FRONT_LEFT] * out[0];
1185 SamplesOut[index][FRONT_RIGHT] += panGain[FRONT_RIGHT] * out[1];
1186 SamplesOut[index][FRONT_CENTER] += panGain[FRONT_CENTER] * out[3];
1187 SamplesOut[index][SIDE_LEFT] += panGain[SIDE_LEFT] * out[0];
1188 SamplesOut[index][SIDE_RIGHT] += panGain[SIDE_RIGHT] * out[1];
1189 SamplesOut[index][BACK_LEFT] += panGain[BACK_LEFT] * out[0];
1190 SamplesOut[index][BACK_RIGHT] += panGain[BACK_RIGHT] * out[1];
1191 SamplesOut[index][BACK_CENTER] += panGain[BACK_CENTER] * out[2];
1195 // This processes the EAX reverb state, given the input samples and an output
1196 // buffer.
1197 static ALvoid EAXVerbProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
1199 ALverbState *State = (ALverbState*)effect;
1200 ALuint index;
1201 ALfloat early[4], late[4];
1202 ALfloat gain = Slot->Gain;
1204 for(index = 0;index < SamplesToDo;index++)
1206 // Process reverb for this sample.
1207 EAXVerbPass(State, SamplesIn[index], early, late);
1209 // Unfortunately, while the number and configuration of gains for
1210 // panning adjust according to MAXCHANNELS, the output from the
1211 // reverb engine is not so scalable.
1212 SamplesOut[index][FRONT_LEFT] +=
1213 (State->Early.PanGain[FRONT_LEFT]*early[0] +
1214 State->Late.PanGain[FRONT_LEFT]*late[0]) * gain;
1215 SamplesOut[index][FRONT_RIGHT] +=
1216 (State->Early.PanGain[FRONT_RIGHT]*early[1] +
1217 State->Late.PanGain[FRONT_RIGHT]*late[1]) * gain;
1218 SamplesOut[index][FRONT_CENTER] +=
1219 (State->Early.PanGain[FRONT_CENTER]*early[3] +
1220 State->Late.PanGain[FRONT_CENTER]*late[3]) * gain;
1221 SamplesOut[index][SIDE_LEFT] +=
1222 (State->Early.PanGain[SIDE_LEFT]*early[0] +
1223 State->Late.PanGain[SIDE_LEFT]*late[0]) * gain;
1224 SamplesOut[index][SIDE_RIGHT] +=
1225 (State->Early.PanGain[SIDE_RIGHT]*early[1] +
1226 State->Late.PanGain[SIDE_RIGHT]*late[1]) * gain;
1227 SamplesOut[index][BACK_LEFT] +=
1228 (State->Early.PanGain[BACK_LEFT]*early[0] +
1229 State->Late.PanGain[BACK_LEFT]*late[0]) * gain;
1230 SamplesOut[index][BACK_RIGHT] +=
1231 (State->Early.PanGain[BACK_RIGHT]*early[1] +
1232 State->Late.PanGain[BACK_RIGHT]*late[1]) * gain;
1233 SamplesOut[index][BACK_CENTER] +=
1234 (State->Early.PanGain[BACK_CENTER]*early[2] +
1235 State->Late.PanGain[BACK_CENTER]*late[2]) * gain;
1239 // This creates the reverb state. It should be called only when the reverb
1240 // effect is loaded into a slot that doesn't already have a reverb effect.
1241 ALeffectState *VerbCreate(void)
1243 ALverbState *State = NULL;
1244 ALuint index;
1246 State = malloc(sizeof(ALverbState));
1247 if(!State)
1248 return NULL;
1250 State->state.Destroy = VerbDestroy;
1251 State->state.DeviceUpdate = VerbDeviceUpdate;
1252 State->state.Update = VerbUpdate;
1253 State->state.Process = VerbProcess;
1255 State->TotalSamples = 0;
1256 State->SampleBuffer = NULL;
1258 State->LpFilter.coeff = 0.0f;
1259 State->LpFilter.history[0] = 0.0f;
1260 State->LpFilter.history[1] = 0.0f;
1262 State->Mod.Delay.Mask = 0;
1263 State->Mod.Delay.Line = NULL;
1264 State->Mod.Index = 0;
1265 State->Mod.Range = 1;
1266 State->Mod.Depth = 0.0f;
1267 State->Mod.Coeff = 0.0f;
1268 State->Mod.Filter = 0.0f;
1270 State->Delay.Mask = 0;
1271 State->Delay.Line = NULL;
1272 State->DelayTap[0] = 0;
1273 State->DelayTap[1] = 0;
1275 State->Early.Gain = 0.0f;
1276 for(index = 0;index < 4;index++)
1278 State->Early.Coeff[index] = 0.0f;
1279 State->Early.Delay[index].Mask = 0;
1280 State->Early.Delay[index].Line = NULL;
1281 State->Early.Offset[index] = 0;
1284 State->Decorrelator.Mask = 0;
1285 State->Decorrelator.Line = NULL;
1286 State->DecoTap[0] = 0;
1287 State->DecoTap[1] = 0;
1288 State->DecoTap[2] = 0;
1290 State->Late.Gain = 0.0f;
1291 State->Late.DensityGain = 0.0f;
1292 State->Late.ApFeedCoeff = 0.0f;
1293 State->Late.MixCoeff = 0.0f;
1294 for(index = 0;index < 4;index++)
1296 State->Late.ApCoeff[index] = 0.0f;
1297 State->Late.ApDelay[index].Mask = 0;
1298 State->Late.ApDelay[index].Line = NULL;
1299 State->Late.ApOffset[index] = 0;
1301 State->Late.Coeff[index] = 0.0f;
1302 State->Late.Delay[index].Mask = 0;
1303 State->Late.Delay[index].Line = NULL;
1304 State->Late.Offset[index] = 0;
1306 State->Late.LpCoeff[index] = 0.0f;
1307 State->Late.LpSample[index] = 0.0f;
1310 for(index = 0;index < MAXCHANNELS;index++)
1312 State->Early.PanGain[index] = 0.0f;
1313 State->Late.PanGain[index] = 0.0f;
1316 State->Echo.DensityGain = 0.0f;
1317 State->Echo.Delay.Mask = 0;
1318 State->Echo.Delay.Line = NULL;
1319 State->Echo.ApDelay.Mask = 0;
1320 State->Echo.ApDelay.Line = NULL;
1321 State->Echo.Coeff = 0.0f;
1322 State->Echo.ApFeedCoeff = 0.0f;
1323 State->Echo.ApCoeff = 0.0f;
1324 State->Echo.Offset = 0;
1325 State->Echo.ApOffset = 0;
1326 State->Echo.LpCoeff = 0.0f;
1327 State->Echo.LpSample = 0.0f;
1328 State->Echo.MixCoeff[0] = 0.0f;
1329 State->Echo.MixCoeff[1] = 0.0f;
1331 State->Offset = 0;
1333 State->Gain = State->Late.PanGain;
1335 return &State->state;
1338 ALeffectState *EAXVerbCreate(void)
1340 ALeffectState *State = VerbCreate();
1341 if(State)
1343 State->DeviceUpdate = EAXVerbDeviceUpdate;
1344 State->Update = EAXVerbUpdate;
1345 State->Process = EAXVerbProcess;
1347 return State;