Use unsigned types for mono and stereo source count hints
[openal-soft.git] / Alc / alcReverb.c
blob86eeb71dc04dddbd59167072e2e61f4004311640
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;
132 } ALverbState;
134 /* This coefficient is used to define the maximum frequency range controlled
135 * by the modulation depth. The current value of 0.1 will allow it to swing
136 * from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the
137 * sampler to stall on the downswing, and above 1 it will cause it to sample
138 * backwards.
140 static const ALfloat MODULATION_DEPTH_COEFF = 0.1f;
142 /* A filter is used to avoid the terrible distortion caused by changing
143 * modulation time and/or depth. To be consistent across different sample
144 * rates, the coefficient must be raised to a constant divided by the sample
145 * rate: coeff^(constant / rate).
147 static const ALfloat MODULATION_FILTER_COEFF = 0.048f;
148 static const ALfloat MODULATION_FILTER_CONST = 100000.0f;
150 // When diffusion is above 0, an all-pass filter is used to take the edge off
151 // the echo effect. It uses the following line length (in seconds).
152 static const ALfloat ECHO_ALLPASS_LENGTH = 0.0133f;
154 // Input into the late reverb is decorrelated between four channels. Their
155 // timings are dependent on a fraction and multiplier. See the
156 // UpdateDecorrelator() routine for the calculations involved.
157 static const ALfloat DECO_FRACTION = 0.15f;
158 static const ALfloat DECO_MULTIPLIER = 2.0f;
160 // All delay line lengths are specified in seconds.
162 // The lengths of the early delay lines.
163 static const ALfloat EARLY_LINE_LENGTH[4] =
165 0.0015f, 0.0045f, 0.0135f, 0.0405f
168 // The lengths of the late all-pass delay lines.
169 static const ALfloat ALLPASS_LINE_LENGTH[4] =
171 0.0151f, 0.0167f, 0.0183f, 0.0200f,
174 // The lengths of the late cyclical delay lines.
175 static const ALfloat LATE_LINE_LENGTH[4] =
177 0.0211f, 0.0311f, 0.0461f, 0.0680f
180 // The late cyclical delay lines have a variable length dependent on the
181 // effect's density parameter (inverted for some reason) and this multiplier.
182 static const ALfloat LATE_LINE_MULTIPLIER = 4.0f;
184 // Calculate the length of a delay line and store its mask and offset.
185 static ALuint CalcLineLength(ALfloat length, ALintptrEXT offset, ALuint frequency, DelayLine *Delay)
187 ALuint samples;
189 // All line lengths are powers of 2, calculated from their lengths, with
190 // an additional sample in case of rounding errors.
191 samples = NextPowerOf2((ALuint)(length * frequency) + 1);
192 // All lines share a single sample buffer.
193 Delay->Mask = samples - 1;
194 Delay->Line = (ALfloat*)offset;
195 // Return the sample count for accumulation.
196 return samples;
199 // Given the allocated sample buffer, this function updates each delay line
200 // offset.
201 static __inline ALvoid RealizeLineOffset(ALfloat * sampleBuffer, DelayLine *Delay)
203 Delay->Line = &sampleBuffer[(ALintptrEXT)Delay->Line];
206 /* Calculates the delay line metrics and allocates the shared sample buffer
207 * for all lines given a flag indicating whether or not to allocate the EAX-
208 * related delays (eaxFlag) and the sample rate (frequency). If an
209 * allocation failure occurs, it returns AL_FALSE.
211 static ALboolean AllocLines(ALboolean eaxFlag, ALuint frequency, ALverbState *State)
213 ALuint totalSamples, index;
214 ALfloat length;
215 ALfloat *newBuffer = NULL;
217 // All delay line lengths are calculated to accomodate the full range of
218 // lengths given their respective paramters.
219 totalSamples = 0;
220 if(eaxFlag)
222 /* The modulator's line length is calculated from the maximum
223 * modulation time and depth coefficient, and halfed for the low-to-
224 * high frequency swing. An additional sample is added to keep it
225 * stable when there is no modulation.
227 length = (AL_EAXREVERB_MAX_MODULATION_TIME * MODULATION_DEPTH_COEFF /
228 2.0f) + (1.0f / frequency);
229 totalSamples += CalcLineLength(length, totalSamples, frequency,
230 &State->Mod.Delay);
233 // The initial delay is the sum of the reflections and late reverb
234 // delays.
235 if(eaxFlag)
236 length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY +
237 AL_EAXREVERB_MAX_LATE_REVERB_DELAY;
238 else
239 length = AL_REVERB_MAX_REFLECTIONS_DELAY +
240 AL_REVERB_MAX_LATE_REVERB_DELAY;
241 totalSamples += CalcLineLength(length, totalSamples, frequency,
242 &State->Delay);
244 // The early reflection lines.
245 for(index = 0;index < 4;index++)
246 totalSamples += CalcLineLength(EARLY_LINE_LENGTH[index], totalSamples,
247 frequency, &State->Early.Delay[index]);
249 // The decorrelator line is calculated from the lowest reverb density (a
250 // parameter value of 1).
251 length = (DECO_FRACTION * DECO_MULTIPLIER * DECO_MULTIPLIER) *
252 LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER);
253 totalSamples += CalcLineLength(length, totalSamples, frequency,
254 &State->Decorrelator);
256 // The late all-pass lines.
257 for(index = 0;index < 4;index++)
258 totalSamples += CalcLineLength(ALLPASS_LINE_LENGTH[index], totalSamples,
259 frequency, &State->Late.ApDelay[index]);
261 // The late delay lines are calculated from the lowest reverb density.
262 for(index = 0;index < 4;index++)
264 length = LATE_LINE_LENGTH[index] * (1.0f + LATE_LINE_MULTIPLIER);
265 totalSamples += CalcLineLength(length, totalSamples, frequency,
266 &State->Late.Delay[index]);
269 if(eaxFlag)
271 // The echo all-pass and delay lines.
272 totalSamples += CalcLineLength(ECHO_ALLPASS_LENGTH, totalSamples,
273 frequency, &State->Echo.ApDelay);
274 totalSamples += CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME, totalSamples,
275 frequency, &State->Echo.Delay);
278 if(totalSamples != State->TotalSamples)
280 newBuffer = realloc(State->SampleBuffer, sizeof(ALfloat) * totalSamples);
281 if(newBuffer == NULL)
282 return AL_FALSE;
283 State->SampleBuffer = newBuffer;
284 State->TotalSamples = totalSamples;
287 // Update all delays to reflect the new sample buffer.
288 RealizeLineOffset(State->SampleBuffer, &State->Delay);
289 RealizeLineOffset(State->SampleBuffer, &State->Decorrelator);
290 for(index = 0;index < 4;index++)
292 RealizeLineOffset(State->SampleBuffer, &State->Early.Delay[index]);
293 RealizeLineOffset(State->SampleBuffer, &State->Late.ApDelay[index]);
294 RealizeLineOffset(State->SampleBuffer, &State->Late.Delay[index]);
296 if(eaxFlag)
298 RealizeLineOffset(State->SampleBuffer, &State->Mod.Delay);
299 RealizeLineOffset(State->SampleBuffer, &State->Echo.ApDelay);
300 RealizeLineOffset(State->SampleBuffer, &State->Echo.Delay);
303 // Clear the sample buffer.
304 for(index = 0;index < State->TotalSamples;index++)
305 State->SampleBuffer[index] = 0.0f;
307 return AL_TRUE;
310 // Calculate a decay coefficient given the length of each cycle and the time
311 // until the decay reaches -60 dB.
312 static __inline ALfloat CalcDecayCoeff(ALfloat length, ALfloat decayTime)
314 return aluPow(10.0f, length / decayTime * -60.0f / 20.0f);
317 // Calculate a decay length from a coefficient and the time until the decay
318 // reaches -60 dB.
319 static __inline ALfloat CalcDecayLength(ALfloat coeff, ALfloat decayTime)
321 return log10(coeff) / -60.0 * 20.0f * decayTime;
324 // Calculate the high frequency parameter for the I3DL2 coefficient
325 // calculation.
326 static __inline ALfloat CalcI3DL2HFreq(ALfloat hfRef, ALuint frequency)
328 return cos(2.0f * M_PI * hfRef / frequency);
331 // Calculate an attenuation to be applied to the input of any echo models to
332 // compensate for modal density and decay time.
333 static __inline ALfloat CalcDensityGain(ALfloat a)
335 /* The energy of a signal can be obtained by finding the area under the
336 * squared signal. This takes the form of Sum(x_n^2), where x is the
337 * amplitude for the sample n.
339 * Decaying feedback matches exponential decay of the form Sum(a^n),
340 * where a is the attenuation coefficient, and n is the sample. The area
341 * under this decay curve can be calculated as: 1 / (1 - a).
343 * Modifying the above equation to find the squared area under the curve
344 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
345 * calculated by inverting the square root of this approximation,
346 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
348 return aluSqrt(1.0f - (a * a));
351 // Calculate the mixing matrix coefficients given a diffusion factor.
352 static __inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)
354 ALfloat n, t;
356 // The matrix is of order 4, so n is sqrt (4 - 1).
357 n = aluSqrt(3.0f);
358 t = diffusion * atan(n);
360 // Calculate the first mixing matrix coefficient.
361 *x = cos(t);
362 // Calculate the second mixing matrix coefficient.
363 *y = sin(t) / n;
366 // Calculate the limited HF ratio for use with the late reverb low-pass
367 // filters.
368 static __inline ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)
370 ALfloat limitRatio;
372 /* Find the attenuation due to air absorption in dB (converting delay
373 * time to meters using the speed of sound). Then reversing the decay
374 * equation, solve for HF ratio. The delay length is cancelled out of
375 * the equation, so it can be calculated once for all lines.
377 limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *
378 SPEEDOFSOUNDMETRESPERSEC);
379 // Need to limit the result to a minimum of 0.1, just like the HF ratio
380 // parameter.
381 limitRatio = __max(limitRatio, 0.1f);
383 // Using the limit calculated above, apply the upper bound to the HF
384 // ratio.
385 return __min(hfRatio, limitRatio);
388 // Calculate the coefficient for a HF (and eventually LF) decay damping
389 // filter.
390 static __inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)
392 ALfloat coeff, g;
394 // Eventually this should boost the high frequencies when the ratio
395 // exceeds 1.
396 coeff = 0.0f;
397 if (hfRatio < 1.0f)
399 // Calculate the low-pass coefficient by dividing the HF decay
400 // coefficient by the full decay coefficient.
401 g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;
403 // Damping is done with a 1-pole filter, so g needs to be squared.
404 g *= g;
405 coeff = lpCoeffCalc(g, cw);
407 // Very low decay times will produce minimal output, so apply an
408 // upper bound to the coefficient.
409 coeff = __min(coeff, 0.98f);
411 return coeff;
414 // Update the EAX modulation index, range, and depth. Keep in mind that this
415 // kind of vibrato is additive and not multiplicative as one may expect. The
416 // downswing will sound stronger than the upswing.
417 static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALverbState *State)
419 ALfloat length;
421 /* Modulation is calculated in two parts.
423 * The modulation time effects the sinus applied to the change in
424 * frequency. An index out of the current time range (both in samples)
425 * is incremented each sample. The range is bound to a reasonable
426 * minimum (1 sample) and when the timing changes, the index is rescaled
427 * to the new range (to keep the sinus consistent).
429 length = modTime * frequency;
430 if (length >= 1.0f) {
431 State->Mod.Index = (ALuint)(State->Mod.Index * length /
432 State->Mod.Range);
433 State->Mod.Range = (ALuint)length;
434 } else {
435 State->Mod.Index = 0;
436 State->Mod.Range = 1;
439 /* The modulation depth effects the amount of frequency change over the
440 * range of the sinus. It needs to be scaled by the modulation time so
441 * that a given depth produces a consistent change in frequency over all
442 * ranges of time. Since the depth is applied to a sinus value, it needs
443 * to be halfed once for the sinus range and again for the sinus swing
444 * in time (half of it is spent decreasing the frequency, half is spent
445 * increasing it).
447 State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /
448 2.0f * frequency;
451 // Update the offsets for the initial effect delay line.
452 static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALverbState *State)
454 // Calculate the initial delay taps.
455 State->DelayTap[0] = (ALuint)(earlyDelay * frequency);
456 State->DelayTap[1] = (ALuint)((earlyDelay + lateDelay) * frequency);
459 // Update the early reflections gain and line coefficients.
460 static ALvoid UpdateEarlyLines(ALfloat reverbGain, ALfloat earlyGain, ALfloat lateDelay, ALverbState *State)
462 ALuint index;
464 // Calculate the early reflections gain (from the master effect gain, and
465 // reflections gain parameters) with a constant attenuation of 0.5.
466 State->Early.Gain = 0.5f * reverbGain * earlyGain;
468 // Calculate the gain (coefficient) for each early delay line using the
469 // late delay time. This expands the early reflections to the start of
470 // the late reverb.
471 for(index = 0;index < 4;index++)
472 State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],
473 lateDelay);
476 // Update the offsets for the decorrelator line.
477 static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALverbState *State)
479 ALuint index;
480 ALfloat length;
482 /* The late reverb inputs are decorrelated to smooth the reverb tail and
483 * reduce harsh echos. The first tap occurs immediately, while the
484 * remaining taps are delayed by multiples of a fraction of the smallest
485 * cyclical delay time.
487 * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
489 for(index = 0;index < 3;index++)
491 length = (DECO_FRACTION * aluPow(DECO_MULTIPLIER, (ALfloat)index)) *
492 LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));
493 State->DecoTap[index] = (ALuint)(length * frequency);
497 // Update the late reverb gains, line lengths, and line coefficients.
498 static ALvoid UpdateLateLines(ALfloat reverbGain, ALfloat lateGain, ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)
500 ALfloat length;
501 ALuint index;
503 /* Calculate the late reverb gain (from the master effect gain, and late
504 * reverb gain parameters). Since the output is tapped prior to the
505 * application of the next delay line coefficients, this gain needs to be
506 * attenuated by the 'x' mixing matrix coefficient as well.
508 State->Late.Gain = reverbGain * lateGain * xMix;
510 /* To compensate for changes in modal density and decay time of the late
511 * reverb signal, the input is attenuated based on the maximal energy of
512 * the outgoing signal. This approximation is used to keep the apparent
513 * energy of the signal equal for all ranges of density and decay time.
515 * The average length of the cyclcical delay lines is used to calculate
516 * the attenuation coefficient.
518 length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
519 LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;
520 length *= 1.0f + (density * LATE_LINE_MULTIPLIER);
521 State->Late.DensityGain = CalcDensityGain(CalcDecayCoeff(length,
522 decayTime));
524 // Calculate the all-pass feed-back and feed-forward coefficient.
525 State->Late.ApFeedCoeff = 0.5f * aluPow(diffusion, 2.0f);
527 for(index = 0;index < 4;index++)
529 // Calculate the gain (coefficient) for each all-pass line.
530 State->Late.ApCoeff[index] = CalcDecayCoeff(ALLPASS_LINE_LENGTH[index],
531 decayTime);
533 // Calculate the length (in seconds) of each cyclical delay line.
534 length = LATE_LINE_LENGTH[index] * (1.0f + (density *
535 LATE_LINE_MULTIPLIER));
537 // Calculate the delay offset for each cyclical delay line.
538 State->Late.Offset[index] = (ALuint)(length * frequency);
540 // Calculate the gain (coefficient) for each cyclical line.
541 State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);
543 // Calculate the damping coefficient for each low-pass filter.
544 State->Late.LpCoeff[index] =
545 CalcDampingCoeff(hfRatio, length, decayTime,
546 State->Late.Coeff[index], cw);
548 // Attenuate the cyclical line coefficients by the mixing coefficient
549 // (x).
550 State->Late.Coeff[index] *= xMix;
554 // Update the echo gain, line offset, line coefficients, and mixing
555 // coefficients.
556 static ALvoid UpdateEchoLine(ALfloat reverbGain, ALfloat lateGain, ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)
558 // Update the offset and coefficient for the echo delay line.
559 State->Echo.Offset = (ALuint)(echoTime * frequency);
561 // Calculate the decay coefficient for the echo line.
562 State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);
564 // Calculate the energy-based attenuation coefficient for the echo delay
565 // line.
566 State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);
568 // Calculate the echo all-pass feed coefficient.
569 State->Echo.ApFeedCoeff = 0.5f * aluPow(diffusion, 2.0f);
571 // Calculate the echo all-pass attenuation coefficient.
572 State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);
574 // Calculate the damping coefficient for each low-pass filter.
575 State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,
576 State->Echo.Coeff, cw);
578 /* Calculate the echo mixing coefficients. The first is applied to the
579 * echo itself. The second is used to attenuate the late reverb when
580 * echo depth is high and diffusion is low, so the echo is slightly
581 * stronger than the decorrelated echos in the reverb tail.
583 State->Echo.MixCoeff[0] = reverbGain * lateGain * echoDepth;
584 State->Echo.MixCoeff[1] = 1.0f - (echoDepth * 0.5f * (1.0f - diffusion));
587 // Update the early and late 3D panning gains.
588 static ALvoid Update3DPanning(const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat *PanningLUT, ALverbState *State)
590 ALfloat length;
591 ALfloat earlyPan[3] = { ReflectionsPan[0], ReflectionsPan[1],
592 ReflectionsPan[2] };
593 ALfloat latePan[3] = { LateReverbPan[0], LateReverbPan[1],
594 LateReverbPan[2] };
595 ALint pos;
596 ALfloat *speakerGain, dirGain, ambientGain;
597 ALuint index;
599 // Calculate the 3D-panning gains for the early reflections and late
600 // reverb.
601 length = earlyPan[0]*earlyPan[0] + earlyPan[1]*earlyPan[1] + earlyPan[2]*earlyPan[2];
602 if(length > 1.0f)
604 length = 1.0f / aluSqrt(length);
605 earlyPan[0] *= length;
606 earlyPan[1] *= length;
607 earlyPan[2] *= length;
609 length = latePan[0]*latePan[0] + latePan[1]*latePan[1] + latePan[2]*latePan[2];
610 if(length > 1.0f)
612 length = 1.0f / aluSqrt(length);
613 latePan[0] *= length;
614 latePan[1] *= length;
615 latePan[2] *= length;
618 /* This code applies directional reverb just like the mixer applies
619 * directional sources. It diffuses the sound toward all speakers as the
620 * magnitude of the panning vector drops, which is only a rough
621 * approximation of the expansion of sound across the speakers from the
622 * panning direction.
624 pos = aluCart2LUTpos(earlyPan[2], earlyPan[0]);
625 speakerGain = &PanningLUT[OUTPUTCHANNELS * pos];
626 dirGain = aluSqrt((earlyPan[0] * earlyPan[0]) + (earlyPan[2] * earlyPan[2]));
627 ambientGain = (1.0 - dirGain);
628 for(index = 0;index < OUTPUTCHANNELS;index++)
629 State->Early.PanGain[index] = dirGain * speakerGain[index] + ambientGain;
631 pos = aluCart2LUTpos(latePan[2], latePan[0]);
632 speakerGain = &PanningLUT[OUTPUTCHANNELS * pos];
633 dirGain = aluSqrt((latePan[0] * latePan[0]) + (latePan[2] * latePan[2]));
634 ambientGain = (1.0 - dirGain);
635 for(index = 0;index < OUTPUTCHANNELS;index++)
636 State->Late.PanGain[index] = dirGain * speakerGain[index] + ambientGain;
639 // Basic delay line input/output routines.
640 static __inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
642 return Delay->Line[offset&Delay->Mask];
645 static __inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
647 Delay->Line[offset&Delay->Mask] = in;
650 // Attenuated delay line output routine.
651 static __inline ALfloat AttenuatedDelayLineOut(DelayLine *Delay, ALuint offset, ALfloat coeff)
653 return coeff * Delay->Line[offset&Delay->Mask];
656 // Basic attenuated all-pass input/output routine.
657 static __inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
659 ALfloat out, feed;
661 out = DelayLineOut(Delay, outOffset);
662 feed = feedCoeff * in;
663 DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
665 // The time-based attenuation is only applied to the delay output to
666 // keep it from affecting the feed-back path (which is already controlled
667 // by the all-pass feed coefficient).
668 return (coeff * out) - feed;
671 // Given an input sample, this function produces modulation for the late
672 // reverb.
673 static __inline ALfloat EAXModulation(ALverbState *State, ALfloat in)
675 ALfloat sinus, frac;
676 ALuint offset;
677 ALfloat out0, out1;
679 // Calculate the sinus rythm (dependent on modulation time and the
680 // sampling rate). The center of the sinus is moved to reduce the delay
681 // of the effect when the time or depth are low.
682 sinus = 1.0f - cos(2.0f * M_PI * State->Mod.Index / State->Mod.Range);
684 // The depth determines the range over which to read the input samples
685 // from, so it must be filtered to reduce the distortion caused by even
686 // small parameter changes.
687 State->Mod.Filter += (State->Mod.Depth - State->Mod.Filter) *
688 State->Mod.Coeff;
690 // Calculate the read offset and fraction between it and the next sample.
691 frac = (1.0f + (State->Mod.Filter * sinus));
692 offset = (ALuint)frac;
693 frac -= offset;
695 // Get the two samples crossed by the offset, and feed the delay line
696 // with the next input sample.
697 out0 = DelayLineOut(&State->Mod.Delay, State->Offset - offset);
698 out1 = DelayLineOut(&State->Mod.Delay, State->Offset - offset - 1);
699 DelayLineIn(&State->Mod.Delay, State->Offset, in);
701 // Step the modulation index forward, keeping it bound to its range.
702 State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
704 // The output is obtained by linearly interpolating the two samples that
705 // were acquired above.
706 return out0 + ((out1 - out0) * frac);
709 // Delay line output routine for early reflections.
710 static __inline ALfloat EarlyDelayLineOut(ALverbState *State, ALuint index)
712 return AttenuatedDelayLineOut(&State->Early.Delay[index],
713 State->Offset - State->Early.Offset[index],
714 State->Early.Coeff[index]);
717 // Given an input sample, this function produces four-channel output for the
718 // early reflections.
719 static __inline ALvoid EarlyReflection(ALverbState *State, ALfloat in, ALfloat *out)
721 ALfloat d[4], v, f[4];
723 // Obtain the decayed results of each early delay line.
724 d[0] = EarlyDelayLineOut(State, 0);
725 d[1] = EarlyDelayLineOut(State, 1);
726 d[2] = EarlyDelayLineOut(State, 2);
727 d[3] = EarlyDelayLineOut(State, 3);
729 /* The following uses a lossless scattering junction from waveguide
730 * theory. It actually amounts to a householder mixing matrix, which
731 * will produce a maximally diffuse response, and means this can probably
732 * be considered a simple feed-back delay network (FDN).
734 * ---
736 * v = 2/N / d_i
737 * ---
738 * i=1
740 v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
741 // The junction is loaded with the input here.
742 v += in;
744 // Calculate the feed values for the delay lines.
745 f[0] = v - d[0];
746 f[1] = v - d[1];
747 f[2] = v - d[2];
748 f[3] = v - d[3];
750 // Re-feed the delay lines.
751 DelayLineIn(&State->Early.Delay[0], State->Offset, f[0]);
752 DelayLineIn(&State->Early.Delay[1], State->Offset, f[1]);
753 DelayLineIn(&State->Early.Delay[2], State->Offset, f[2]);
754 DelayLineIn(&State->Early.Delay[3], State->Offset, f[3]);
756 // Output the results of the junction for all four channels.
757 out[0] = State->Early.Gain * f[0];
758 out[1] = State->Early.Gain * f[1];
759 out[2] = State->Early.Gain * f[2];
760 out[3] = State->Early.Gain * f[3];
763 // All-pass input/output routine for late reverb.
764 static __inline ALfloat LateAllPassInOut(ALverbState *State, ALuint index, ALfloat in)
766 return AllpassInOut(&State->Late.ApDelay[index],
767 State->Offset - State->Late.ApOffset[index],
768 State->Offset, in, State->Late.ApFeedCoeff,
769 State->Late.ApCoeff[index]);
772 // Delay line output routine for late reverb.
773 static __inline ALfloat LateDelayLineOut(ALverbState *State, ALuint index)
775 return AttenuatedDelayLineOut(&State->Late.Delay[index],
776 State->Offset - State->Late.Offset[index],
777 State->Late.Coeff[index]);
780 // Low-pass filter input/output routine for late reverb.
781 static __inline ALfloat LateLowPassInOut(ALverbState *State, ALuint index, ALfloat in)
783 State->Late.LpSample[index] = in +
784 ((State->Late.LpSample[index] - in) * State->Late.LpCoeff[index]);
785 return State->Late.LpSample[index];
788 // Given four decorrelated input samples, this function produces four-channel
789 // output for the late reverb.
790 static __inline ALvoid LateReverb(ALverbState *State, ALfloat *in, ALfloat *out)
792 ALfloat d[4], f[4];
794 // Obtain the decayed results of the cyclical delay lines, and add the
795 // corresponding input channels. Then pass the results through the
796 // low-pass filters.
798 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and back
799 // to 0.
800 d[0] = LateLowPassInOut(State, 2, in[2] + LateDelayLineOut(State, 2));
801 d[1] = LateLowPassInOut(State, 0, in[0] + LateDelayLineOut(State, 0));
802 d[2] = LateLowPassInOut(State, 3, in[3] + LateDelayLineOut(State, 3));
803 d[3] = LateLowPassInOut(State, 1, in[1] + LateDelayLineOut(State, 1));
805 // To help increase diffusion, run each line through an all-pass filter.
806 // When there is no diffusion, the shortest all-pass filter will feed the
807 // shortest delay line.
808 d[0] = LateAllPassInOut(State, 0, d[0]);
809 d[1] = LateAllPassInOut(State, 1, d[1]);
810 d[2] = LateAllPassInOut(State, 2, d[2]);
811 d[3] = LateAllPassInOut(State, 3, d[3]);
813 /* Late reverb is done with a modified feed-back delay network (FDN)
814 * topology. Four input lines are each fed through their own all-pass
815 * filter and then into the mixing matrix. The four outputs of the
816 * mixing matrix are then cycled back to the inputs. Each output feeds
817 * a different input to form a circlular feed cycle.
819 * The mixing matrix used is a 4D skew-symmetric rotation matrix derived
820 * using a single unitary rotational parameter:
822 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
823 * [ -a, d, c, -b ]
824 * [ -b, -c, d, a ]
825 * [ -c, b, -a, d ]
827 * The rotation is constructed from the effect's diffusion parameter,
828 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
829 * with differing signs, and d is the coefficient x. The matrix is thus:
831 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
832 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
833 * [ y, -y, x, y ] x = cos(t)
834 * [ -y, -y, -y, x ] y = sin(t) / n
836 * To reduce the number of multiplies, the x coefficient is applied with
837 * the cyclical delay line coefficients. Thus only the y coefficient is
838 * applied when mixing, and is modified to be: y / x.
840 f[0] = d[0] + (State->Late.MixCoeff * ( d[1] - d[2] + d[3]));
841 f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
842 f[2] = d[2] + (State->Late.MixCoeff * ( d[0] - d[1] + d[3]));
843 f[3] = d[3] + (State->Late.MixCoeff * (-d[0] - d[1] - d[2]));
845 // Output the results of the matrix for all four channels, attenuated by
846 // the late reverb gain (which is attenuated by the 'x' mix coefficient).
847 out[0] = State->Late.Gain * f[0];
848 out[1] = State->Late.Gain * f[1];
849 out[2] = State->Late.Gain * f[2];
850 out[3] = State->Late.Gain * f[3];
852 // Re-feed the cyclical delay lines.
853 DelayLineIn(&State->Late.Delay[0], State->Offset, f[0]);
854 DelayLineIn(&State->Late.Delay[1], State->Offset, f[1]);
855 DelayLineIn(&State->Late.Delay[2], State->Offset, f[2]);
856 DelayLineIn(&State->Late.Delay[3], State->Offset, f[3]);
859 // Given an input sample, this function mixes echo into the four-channel late
860 // reverb.
861 static __inline ALvoid EAXEcho(ALverbState *State, ALfloat in, ALfloat *late)
863 ALfloat out, feed;
865 // Get the latest attenuated echo sample for output.
866 feed = AttenuatedDelayLineOut(&State->Echo.Delay,
867 State->Offset - State->Echo.Offset,
868 State->Echo.Coeff);
870 // Mix the output into the late reverb channels.
871 out = State->Echo.MixCoeff[0] * feed;
872 late[0] = (State->Echo.MixCoeff[1] * late[0]) + out;
873 late[1] = (State->Echo.MixCoeff[1] * late[1]) + out;
874 late[2] = (State->Echo.MixCoeff[1] * late[2]) + out;
875 late[3] = (State->Echo.MixCoeff[1] * late[3]) + out;
877 // Mix the energy-attenuated input with the output and pass it through
878 // the echo low-pass filter.
879 feed += State->Echo.DensityGain * in;
880 feed += ((State->Echo.LpSample - feed) * State->Echo.LpCoeff);
881 State->Echo.LpSample = feed;
883 // Then the echo all-pass filter.
884 feed = AllpassInOut(&State->Echo.ApDelay,
885 State->Offset - State->Echo.ApOffset,
886 State->Offset, feed, State->Echo.ApFeedCoeff,
887 State->Echo.ApCoeff);
889 // Feed the delay with the mixed and filtered sample.
890 DelayLineIn(&State->Echo.Delay, State->Offset, feed);
893 // Perform the non-EAX reverb pass on a given input sample, resulting in
894 // four-channel output.
895 static __inline ALvoid VerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
897 ALfloat feed, taps[4];
899 // Low-pass filter the incoming sample.
900 in = lpFilter2P(&State->LpFilter, 0, in);
902 // Feed the initial delay line.
903 DelayLineIn(&State->Delay, State->Offset, in);
905 // Calculate the early reflection from the first delay tap.
906 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
907 EarlyReflection(State, in, early);
909 // Feed the decorrelator from the energy-attenuated output of the second
910 // delay tap.
911 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
912 feed = in * State->Late.DensityGain;
913 DelayLineIn(&State->Decorrelator, State->Offset, feed);
915 // Calculate the late reverb from the decorrelator taps.
916 taps[0] = feed;
917 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
918 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
919 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
920 LateReverb(State, taps, late);
922 // Step all delays forward one sample.
923 State->Offset++;
926 // Perform the EAX reverb pass on a given input sample, resulting in four-
927 // channel output.
928 static __inline ALvoid EAXVerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
930 ALfloat feed, taps[4];
932 // Low-pass filter the incoming sample.
933 in = lpFilter2P(&State->LpFilter, 0, in);
935 // Perform any modulation on the input.
936 in = EAXModulation(State, in);
938 // Feed the initial delay line.
939 DelayLineIn(&State->Delay, State->Offset, in);
941 // Calculate the early reflection from the first delay tap.
942 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
943 EarlyReflection(State, in, early);
945 // Feed the decorrelator from the energy-attenuated output of the second
946 // delay tap.
947 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
948 feed = in * State->Late.DensityGain;
949 DelayLineIn(&State->Decorrelator, State->Offset, feed);
951 // Calculate the late reverb from the decorrelator taps.
952 taps[0] = feed;
953 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
954 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
955 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
956 LateReverb(State, taps, late);
958 // Calculate and mix in any echo.
959 EAXEcho(State, in, late);
961 // Step all delays forward one sample.
962 State->Offset++;
965 // This destroys the reverb state. It should be called only when the effect
966 // slot has a different (or no) effect loaded over the reverb effect.
967 static ALvoid VerbDestroy(ALeffectState *effect)
969 ALverbState *State = (ALverbState*)effect;
970 if(State)
972 free(State->SampleBuffer);
973 State->SampleBuffer = NULL;
974 free(State);
978 // This updates the device-dependant reverb state. This is called on
979 // initialization and any time the device parameters (eg. playback frequency,
980 // or format) have been changed.
981 static ALboolean VerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
983 ALverbState *State = (ALverbState*)effect;
984 ALuint frequency = Device->Frequency, index;
986 // Allocate the delay lines.
987 if(!AllocLines(AL_FALSE, frequency, State))
988 return AL_FALSE;
990 // The early reflection and late all-pass filter line lengths are static,
991 // so their offsets only need to be calculated once.
992 for(index = 0;index < 4;index++)
994 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
995 frequency);
996 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
997 frequency);
1000 return AL_TRUE;
1003 // This updates the device-dependant EAX reverb state. This is called on
1004 // initialization and any time the device parameters (eg. playback frequency,
1005 // format) have been changed.
1006 static ALboolean EAXVerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
1008 ALverbState *State = (ALverbState*)effect;
1009 ALuint frequency = Device->Frequency, index;
1011 // Allocate the delay lines.
1012 if(!AllocLines(AL_TRUE, frequency, State))
1013 return AL_FALSE;
1015 // Calculate the modulation filter coefficient. Notice that the exponent
1016 // is calculated given the current sample rate. This ensures that the
1017 // resulting filter response over time is consistent across all sample
1018 // rates.
1019 State->Mod.Coeff = aluPow(MODULATION_FILTER_COEFF,
1020 MODULATION_FILTER_CONST / frequency);
1022 // The early reflection and late all-pass filter line lengths are static,
1023 // so their offsets only need to be calculated once.
1024 for(index = 0;index < 4;index++)
1026 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
1027 frequency);
1028 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
1029 frequency);
1032 // The echo all-pass filter line length is static, so its offset only
1033 // needs to be calculated once.
1034 State->Echo.ApOffset = (ALuint)(ECHO_ALLPASS_LENGTH * frequency);
1036 return AL_TRUE;
1039 // This updates the reverb state. This is called any time the reverb effect
1040 // is loaded into a slot.
1041 static ALvoid VerbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
1043 ALverbState *State = (ALverbState*)effect;
1044 ALuint frequency = Context->Device->Frequency;
1045 ALfloat cw, x, y, hfRatio;
1047 // Calculate the master low-pass filter (from the master effect HF gain).
1048 cw = CalcI3DL2HFreq(Effect->Reverb.HFReference, frequency);
1049 // This is done with 2 chained 1-pole filters, so no need to square g.
1050 State->LpFilter.coeff = lpCoeffCalc(Effect->Reverb.GainHF, cw);
1052 // Update the initial effect delay.
1053 UpdateDelayLine(Effect->Reverb.ReflectionsDelay,
1054 Effect->Reverb.LateReverbDelay, frequency, State);
1056 // Update the early lines.
1057 UpdateEarlyLines(Effect->Reverb.Gain, Effect->Reverb.ReflectionsGain,
1058 Effect->Reverb.LateReverbDelay, State);
1060 // Update the decorrelator.
1061 UpdateDecorrelator(Effect->Reverb.Density, frequency, State);
1063 // Get the mixing matrix coefficients (x and y).
1064 CalcMatrixCoeffs(Effect->Reverb.Diffusion, &x, &y);
1065 // Then divide x into y to simplify the matrix calculation.
1066 State->Late.MixCoeff = y / x;
1068 // If the HF limit parameter is flagged, calculate an appropriate limit
1069 // based on the air absorption parameter.
1070 hfRatio = Effect->Reverb.DecayHFRatio;
1071 if(Effect->Reverb.DecayHFLimit && Effect->Reverb.AirAbsorptionGainHF < 1.0f)
1072 hfRatio = CalcLimitedHfRatio(hfRatio, Effect->Reverb.AirAbsorptionGainHF,
1073 Effect->Reverb.DecayTime);
1075 // Update the late lines.
1076 UpdateLateLines(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1077 x, Effect->Reverb.Density, Effect->Reverb.DecayTime,
1078 Effect->Reverb.Diffusion, hfRatio, cw, frequency, State);
1081 // This updates the EAX reverb state. This is called any time the EAX reverb
1082 // effect is loaded into a slot.
1083 static ALvoid EAXVerbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
1085 ALverbState *State = (ALverbState*)effect;
1086 ALuint frequency = Context->Device->Frequency;
1087 ALfloat cw, x, y, hfRatio;
1089 // Calculate the master low-pass filter (from the master effect HF gain).
1090 cw = CalcI3DL2HFreq(Effect->Reverb.HFReference, frequency);
1091 // This is done with 2 chained 1-pole filters, so no need to square g.
1092 State->LpFilter.coeff = lpCoeffCalc(Effect->Reverb.GainHF, cw);
1094 // Update the modulator line.
1095 UpdateModulator(Effect->Reverb.ModulationTime,
1096 Effect->Reverb.ModulationDepth, frequency, State);
1098 // Update the initial effect delay.
1099 UpdateDelayLine(Effect->Reverb.ReflectionsDelay,
1100 Effect->Reverb.LateReverbDelay, frequency, State);
1102 // Update the early lines.
1103 UpdateEarlyLines(Effect->Reverb.Gain, Effect->Reverb.ReflectionsGain,
1104 Effect->Reverb.LateReverbDelay, State);
1106 // Update the decorrelator.
1107 UpdateDecorrelator(Effect->Reverb.Density, frequency, State);
1109 // Get the mixing matrix coefficients (x and y).
1110 CalcMatrixCoeffs(Effect->Reverb.Diffusion, &x, &y);
1111 // Then divide x into y to simplify the matrix calculation.
1112 State->Late.MixCoeff = y / x;
1114 // If the HF limit parameter is flagged, calculate an appropriate limit
1115 // based on the air absorption parameter.
1116 hfRatio = Effect->Reverb.DecayHFRatio;
1117 if(Effect->Reverb.DecayHFLimit && Effect->Reverb.AirAbsorptionGainHF < 1.0f)
1118 hfRatio = CalcLimitedHfRatio(hfRatio, Effect->Reverb.AirAbsorptionGainHF,
1119 Effect->Reverb.DecayTime);
1121 // Update the late lines.
1122 UpdateLateLines(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1123 x, Effect->Reverb.Density, Effect->Reverb.DecayTime,
1124 Effect->Reverb.Diffusion, hfRatio, cw, frequency, State);
1126 // Update the echo line.
1127 UpdateEchoLine(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1128 Effect->Reverb.EchoTime, Effect->Reverb.DecayTime,
1129 Effect->Reverb.Diffusion, Effect->Reverb.EchoDepth,
1130 hfRatio, cw, frequency, State);
1132 // Update early and late 3D panning.
1133 Update3DPanning(Effect->Reverb.ReflectionsPan, Effect->Reverb.LateReverbPan,
1134 Context->PanningLUT, State);
1137 // This processes the reverb state, given the input samples and an output
1138 // buffer.
1139 static ALvoid VerbProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
1141 ALverbState *State = (ALverbState*)effect;
1142 ALuint index;
1143 ALfloat early[4], late[4], out[4];
1144 ALfloat gain = Slot->Gain;
1146 for(index = 0;index < SamplesToDo;index++)
1148 // Process reverb for this sample.
1149 VerbPass(State, SamplesIn[index], early, late);
1151 // Mix early reflections and late reverb.
1152 out[0] = (early[0] + late[0]) * gain;
1153 out[1] = (early[1] + late[1]) * gain;
1154 out[2] = (early[2] + late[2]) * gain;
1155 out[3] = (early[3] + late[3]) * gain;
1157 // Output the results.
1158 SamplesOut[index][FRONT_LEFT] += out[0];
1159 SamplesOut[index][FRONT_RIGHT] += out[1];
1160 SamplesOut[index][FRONT_CENTER] += out[3];
1161 SamplesOut[index][SIDE_LEFT] += out[0];
1162 SamplesOut[index][SIDE_RIGHT] += out[1];
1163 SamplesOut[index][BACK_LEFT] += out[0];
1164 SamplesOut[index][BACK_RIGHT] += out[1];
1165 SamplesOut[index][BACK_CENTER] += out[2];
1169 // This processes the EAX reverb state, given the input samples and an output
1170 // buffer.
1171 static ALvoid EAXVerbProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
1173 ALverbState *State = (ALverbState*)effect;
1174 ALuint index;
1175 ALfloat early[4], late[4];
1176 ALfloat gain = Slot->Gain;
1178 for(index = 0;index < SamplesToDo;index++)
1180 // Process reverb for this sample.
1181 EAXVerbPass(State, SamplesIn[index], early, late);
1183 // Unfortunately, while the number and configuration of gains for
1184 // panning adjust according to OUTPUTCHANNELS, the output from the
1185 // reverb engine is not so scalable.
1186 SamplesOut[index][FRONT_LEFT] +=
1187 (State->Early.PanGain[FRONT_LEFT]*early[0] +
1188 State->Late.PanGain[FRONT_LEFT]*late[0]) * gain;
1189 SamplesOut[index][FRONT_RIGHT] +=
1190 (State->Early.PanGain[FRONT_RIGHT]*early[1] +
1191 State->Late.PanGain[FRONT_RIGHT]*late[1]) * gain;
1192 SamplesOut[index][FRONT_CENTER] +=
1193 (State->Early.PanGain[FRONT_CENTER]*early[3] +
1194 State->Late.PanGain[FRONT_CENTER]*late[3]) * gain;
1195 SamplesOut[index][SIDE_LEFT] +=
1196 (State->Early.PanGain[SIDE_LEFT]*early[0] +
1197 State->Late.PanGain[SIDE_LEFT]*late[0]) * gain;
1198 SamplesOut[index][SIDE_RIGHT] +=
1199 (State->Early.PanGain[SIDE_RIGHT]*early[1] +
1200 State->Late.PanGain[SIDE_RIGHT]*late[1]) * gain;
1201 SamplesOut[index][BACK_LEFT] +=
1202 (State->Early.PanGain[BACK_LEFT]*early[0] +
1203 State->Late.PanGain[BACK_LEFT]*late[0]) * gain;
1204 SamplesOut[index][BACK_RIGHT] +=
1205 (State->Early.PanGain[BACK_RIGHT]*early[1] +
1206 State->Late.PanGain[BACK_RIGHT]*late[1]) * gain;
1207 SamplesOut[index][BACK_CENTER] +=
1208 (State->Early.PanGain[BACK_CENTER]*early[2] +
1209 State->Late.PanGain[BACK_CENTER]*late[2]) * gain;
1213 // This creates the reverb state. It should be called only when the reverb
1214 // effect is loaded into a slot that doesn't already have a reverb effect.
1215 ALeffectState *VerbCreate(void)
1217 ALverbState *State = NULL;
1218 ALuint index;
1220 State = malloc(sizeof(ALverbState));
1221 if(!State)
1222 return NULL;
1224 State->state.Destroy = VerbDestroy;
1225 State->state.DeviceUpdate = VerbDeviceUpdate;
1226 State->state.Update = VerbUpdate;
1227 State->state.Process = VerbProcess;
1229 State->TotalSamples = 0;
1230 State->SampleBuffer = NULL;
1232 State->LpFilter.coeff = 0.0f;
1233 State->LpFilter.history[0] = 0.0f;
1234 State->LpFilter.history[1] = 0.0f;
1236 State->Mod.Delay.Mask = 0;
1237 State->Mod.Delay.Line = NULL;
1238 State->Mod.Index = 0;
1239 State->Mod.Range = 1;
1240 State->Mod.Depth = 0.0f;
1241 State->Mod.Coeff = 0.0f;
1242 State->Mod.Filter = 0.0f;
1244 State->Delay.Mask = 0;
1245 State->Delay.Line = NULL;
1246 State->DelayTap[0] = 0;
1247 State->DelayTap[1] = 0;
1249 State->Early.Gain = 0.0f;
1250 for(index = 0;index < 4;index++)
1252 State->Early.Coeff[index] = 0.0f;
1253 State->Early.Delay[index].Mask = 0;
1254 State->Early.Delay[index].Line = NULL;
1255 State->Early.Offset[index] = 0;
1258 State->Decorrelator.Mask = 0;
1259 State->Decorrelator.Line = NULL;
1260 State->DecoTap[0] = 0;
1261 State->DecoTap[1] = 0;
1262 State->DecoTap[2] = 0;
1264 State->Late.Gain = 0.0f;
1265 State->Late.DensityGain = 0.0f;
1266 State->Late.ApFeedCoeff = 0.0f;
1267 State->Late.MixCoeff = 0.0f;
1268 for(index = 0;index < 4;index++)
1270 State->Late.ApCoeff[index] = 0.0f;
1271 State->Late.ApDelay[index].Mask = 0;
1272 State->Late.ApDelay[index].Line = NULL;
1273 State->Late.ApOffset[index] = 0;
1275 State->Late.Coeff[index] = 0.0f;
1276 State->Late.Delay[index].Mask = 0;
1277 State->Late.Delay[index].Line = NULL;
1278 State->Late.Offset[index] = 0;
1280 State->Late.LpCoeff[index] = 0.0f;
1281 State->Late.LpSample[index] = 0.0f;
1284 for(index = 0;index < OUTPUTCHANNELS;index++)
1286 State->Early.PanGain[index] = 0.0f;
1287 State->Late.PanGain[index] = 0.0f;
1290 State->Echo.DensityGain = 0.0f;
1291 State->Echo.Delay.Mask = 0;
1292 State->Echo.Delay.Line = NULL;
1293 State->Echo.ApDelay.Mask = 0;
1294 State->Echo.ApDelay.Line = NULL;
1295 State->Echo.Coeff = 0.0f;
1296 State->Echo.ApFeedCoeff = 0.0f;
1297 State->Echo.ApCoeff = 0.0f;
1298 State->Echo.Offset = 0;
1299 State->Echo.ApOffset = 0;
1300 State->Echo.LpCoeff = 0.0f;
1301 State->Echo.LpSample = 0.0f;
1302 State->Echo.MixCoeff[0] = 0.0f;
1303 State->Echo.MixCoeff[1] = 0.0f;
1305 State->Offset = 0;
1307 return &State->state;
1310 ALeffectState *EAXVerbCreate(void)
1312 ALeffectState *State = VerbCreate();
1313 if(State)
1315 State->DeviceUpdate = EAXVerbDeviceUpdate;
1316 State->Update = EAXVerbUpdate;
1317 State->Process = EAXVerbProcess;
1319 return State;