Fix retrieved update size from pulseaudio
[openal-soft.git] / Alc / alcReverb.c
blob4d800187aa9f26a4c923717784cf1b48f4b2cf09
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, ALuint 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[(ALuint)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 pow(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 the I3DL2 coefficient given the gain and frequency parameters.
332 * To allow for optimization when using multiple chained filters, the gain
333 * is not squared in this function. Callers using a single filter should
334 * square it to produce the correct coefficient. Those using multiple
335 * filters should find its N-1 root (where N is the number of chained
336 * filters).
338 static __inline ALfloat CalcI3DL2Coeff(ALfloat g, ALfloat cw)
340 ALfloat coeff;
342 coeff = 0.0f;
343 if(g < 0.9999f) // 1-epsilon
344 coeff = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
346 return coeff;
349 // Calculate an attenuation to be applied to the input of any echo models to
350 // compensate for modal density and decay time.
351 static __inline ALfloat CalcDensityGain(ALfloat a)
353 /* The energy of a signal can be obtained by finding the area under the
354 * squared signal. This takes the form of Sum(x_n^2), where x is the
355 * amplitude for the sample n.
357 * Decaying feedback matches exponential decay of the form Sum(a^n),
358 * where a is the attenuation coefficient, and n is the sample. The area
359 * under this decay curve can be calculated as: 1 / (1 - a).
361 * Modifying the above equation to find the squared area under the curve
362 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
363 * calculated by inverting the square root of this approximation,
364 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
366 return aluSqrt(1.0f - (a * a));
369 // Calculate the mixing matrix coefficients given a diffusion factor.
370 static __inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)
372 ALfloat n, t;
374 // The matrix is of order 4, so n is sqrt (4 - 1).
375 n = aluSqrt(3.0f);
376 t = diffusion * atan(n);
378 // Calculate the first mixing matrix coefficient.
379 *x = cos(t);
380 // Calculate the second mixing matrix coefficient.
381 *y = sin(t) / n;
384 // Calculate the limited HF ratio for use with the late reverb low-pass
385 // filters.
386 static __inline ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)
388 ALfloat limitRatio;
390 /* Find the attenuation due to air absorption in dB (converting delay
391 * time to meters using the speed of sound). Then reversing the decay
392 * equation, solve for HF ratio. The delay length is cancelled out of
393 * the equation, so it can be calculated once for all lines.
395 limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *
396 SPEEDOFSOUNDMETRESPERSEC);
397 // Need to limit the result to a minimum of 0.1, just like the HF ratio
398 // parameter.
399 limitRatio = __max(limitRatio, 0.1f);
401 // Using the limit calculated above, apply the upper bound to the HF
402 // ratio.
403 return __min(hfRatio, limitRatio);
406 // Calculate the coefficient for a HF (and eventually LF) decay damping
407 // filter.
408 static __inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)
410 ALfloat coeff, g;
412 // Eventually this should boost the high frequencies when the ratio
413 // exceeds 1.
414 coeff = 0.0f;
415 if (hfRatio < 1.0f)
417 // Calculate the low-pass coefficient by dividing the HF decay
418 // coefficient by the full decay coefficient.
419 g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;
420 g = __max(g, 0.1f);
422 // Damping is done with a 1-pole filter, so g needs to be squared.
423 g *= g;
424 coeff = CalcI3DL2Coeff(g, cw);
426 // Very low decay times will produce minimal output, so apply an
427 // upper bound to the coefficient.
428 coeff = __min(coeff, 0.98f);
430 return coeff;
433 // Update the EAX modulation index, range, and depth. Keep in mind that this
434 // kind of vibrato is additive and not multiplicative as one may expect. The
435 // downswing will sound stronger than the upswing.
436 static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALverbState *State)
438 ALfloat length;
440 /* Modulation is calculated in two parts.
442 * The modulation time effects the sinus applied to the change in
443 * frequency. An index out of the current time range (both in samples)
444 * is incremented each sample. The range is bound to a reasonable
445 * minimum (1 sample) and when the timing changes, the index is rescaled
446 * to the new range (to keep the sinus consistent).
448 length = modTime * frequency;
449 if (length >= 1.0f) {
450 State->Mod.Index = (ALuint)(State->Mod.Index * length /
451 State->Mod.Range);
452 State->Mod.Range = (ALuint)length;
453 } else {
454 State->Mod.Index = 0;
455 State->Mod.Range = 1;
458 /* The modulation depth effects the amount of frequency change over the
459 * range of the sinus. It needs to be scaled by the modulation time so
460 * that a given depth produces a consistent change in frequency over all
461 * ranges of time. Since the depth is applied to a sinus value, it needs
462 * to be halfed once for the sinus range and again for the sinus swing
463 * in time (half of it is spent decreasing the frequency, half is spent
464 * increasing it).
466 State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /
467 2.0f * frequency;
470 // Update the offsets for the initial effect delay line.
471 static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALverbState *State)
473 // Calculate the initial delay taps.
474 State->DelayTap[0] = (ALuint)(earlyDelay * frequency);
475 State->DelayTap[1] = (ALuint)((earlyDelay + lateDelay) * frequency);
478 // Update the early reflections gain and line coefficients.
479 static ALvoid UpdateEarlyLines(ALfloat reverbGain, ALfloat earlyGain, ALfloat lateDelay, ALverbState *State)
481 ALuint index;
483 // Calculate the early reflections gain (from the master effect gain, and
484 // reflections gain parameters) with a constant attenuation of 0.5.
485 State->Early.Gain = 0.5f * reverbGain * earlyGain;
487 // Calculate the gain (coefficient) for each early delay line using the
488 // late delay time. This expands the early reflections to the start of
489 // the late reverb.
490 for(index = 0;index < 4;index++)
491 State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],
492 lateDelay);
495 // Update the offsets for the decorrelator line.
496 static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALverbState *State)
498 ALuint index;
499 ALfloat length;
501 /* The late reverb inputs are decorrelated to smooth the reverb tail and
502 * reduce harsh echos. The first tap occurs immediately, while the
503 * remaining taps are delayed by multiples of a fraction of the smallest
504 * cyclical delay time.
506 * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
508 for(index = 0;index < 3;index++)
510 length = (DECO_FRACTION * pow(DECO_MULTIPLIER, (ALfloat)index)) *
511 LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));
512 State->DecoTap[index] = (ALuint)(length * frequency);
516 // Update the late reverb gains, line lengths, and line coefficients.
517 static ALvoid UpdateLateLines(ALfloat reverbGain, ALfloat lateGain, ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)
519 ALfloat length;
520 ALuint index;
522 /* Calculate the late reverb gain (from the master effect gain, and late
523 * reverb gain parameters). Since the output is tapped prior to the
524 * application of the next delay line coefficients, this gain needs to be
525 * attenuated by the 'x' mixing matrix coefficient as well.
527 State->Late.Gain = reverbGain * lateGain * xMix;
529 /* To compensate for changes in modal density and decay time of the late
530 * reverb signal, the input is attenuated based on the maximal energy of
531 * the outgoing signal. This approximation is used to keep the apparent
532 * energy of the signal equal for all ranges of density and decay time.
534 * The average length of the cyclcical delay lines is used to calculate
535 * the attenuation coefficient.
537 length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
538 LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;
539 length *= 1.0f + (density * LATE_LINE_MULTIPLIER);
540 State->Late.DensityGain = CalcDensityGain(CalcDecayCoeff(length,
541 decayTime));
543 // Calculate the all-pass feed-back and feed-forward coefficient.
544 State->Late.ApFeedCoeff = 0.5f * pow(diffusion, 2.0f);
546 for(index = 0;index < 4;index++)
548 // Calculate the gain (coefficient) for each all-pass line.
549 State->Late.ApCoeff[index] = CalcDecayCoeff(ALLPASS_LINE_LENGTH[index],
550 decayTime);
552 // Calculate the length (in seconds) of each cyclical delay line.
553 length = LATE_LINE_LENGTH[index] * (1.0f + (density *
554 LATE_LINE_MULTIPLIER));
556 // Calculate the delay offset for each cyclical delay line.
557 State->Late.Offset[index] = (ALuint)(length * frequency);
559 // Calculate the gain (coefficient) for each cyclical line.
560 State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);
562 // Calculate the damping coefficient for each low-pass filter.
563 State->Late.LpCoeff[index] =
564 CalcDampingCoeff(hfRatio, length, decayTime,
565 State->Late.Coeff[index], cw);
567 // Attenuate the cyclical line coefficients by the mixing coefficient
568 // (x).
569 State->Late.Coeff[index] *= xMix;
573 // Update the echo gain, line offset, line coefficients, and mixing
574 // coefficients.
575 static ALvoid UpdateEchoLine(ALfloat reverbGain, ALfloat lateGain, ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)
577 // Update the offset and coefficient for the echo delay line.
578 State->Echo.Offset = (ALuint)(echoTime * frequency);
580 // Calculate the decay coefficient for the echo line.
581 State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);
583 // Calculate the energy-based attenuation coefficient for the echo delay
584 // line.
585 State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);
587 // Calculate the echo all-pass feed coefficient.
588 State->Echo.ApFeedCoeff = 0.5f * pow(diffusion, 2.0f);
590 // Calculate the echo all-pass attenuation coefficient.
591 State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);
593 // Calculate the damping coefficient for each low-pass filter.
594 State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,
595 State->Echo.Coeff, cw);
597 /* Calculate the echo mixing coefficients. The first is applied to the
598 * echo itself. The second is used to attenuate the late reverb when
599 * echo depth is high and diffusion is low, so the echo is slightly
600 * stronger than the decorrelated echos in the reverb tail.
602 State->Echo.MixCoeff[0] = reverbGain * lateGain * echoDepth;
603 State->Echo.MixCoeff[1] = 1.0f - (echoDepth * 0.5f * (1.0f - diffusion));
606 // Update the early and late 3D panning gains.
607 static ALvoid Update3DPanning(const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat *PanningLUT, ALverbState *State)
609 ALfloat length;
610 ALfloat earlyPan[3] = { ReflectionsPan[0], ReflectionsPan[1],
611 ReflectionsPan[2] };
612 ALfloat latePan[3] = { LateReverbPan[0], LateReverbPan[1],
613 LateReverbPan[2] };
614 ALint pos;
615 ALfloat *speakerGain, dirGain, ambientGain;
616 ALuint index;
618 // Calculate the 3D-panning gains for the early reflections and late
619 // reverb.
620 length = earlyPan[0]*earlyPan[0] + earlyPan[1]*earlyPan[1] + earlyPan[2]*earlyPan[2];
621 if(length > 1.0f)
623 length = 1.0f / aluSqrt(length);
624 earlyPan[0] *= length;
625 earlyPan[1] *= length;
626 earlyPan[2] *= length;
628 length = latePan[0]*latePan[0] + latePan[1]*latePan[1] + latePan[2]*latePan[2];
629 if(length > 1.0f)
631 length = 1.0f / aluSqrt(length);
632 latePan[0] *= length;
633 latePan[1] *= length;
634 latePan[2] *= length;
637 /* This code applies directional reverb just like the mixer applies
638 * directional sources. It diffuses the sound toward all speakers as the
639 * magnitude of the panning vector drops, which is only a rough
640 * approximation of the expansion of sound across the speakers from the
641 * panning direction.
643 pos = aluCart2LUTpos(earlyPan[2], earlyPan[0]);
644 speakerGain = &PanningLUT[OUTPUTCHANNELS * pos];
645 dirGain = aluSqrt((earlyPan[0] * earlyPan[0]) + (earlyPan[2] * earlyPan[2]));
646 ambientGain = (1.0 - dirGain);
647 for(index = 0;index < OUTPUTCHANNELS;index++)
648 State->Early.PanGain[index] = dirGain * speakerGain[index] + ambientGain;
650 pos = aluCart2LUTpos(latePan[2], latePan[0]);
651 speakerGain = &PanningLUT[OUTPUTCHANNELS * pos];
652 dirGain = aluSqrt((latePan[0] * latePan[0]) + (latePan[2] * latePan[2]));
653 ambientGain = (1.0 - dirGain);
654 for(index = 0;index < OUTPUTCHANNELS;index++)
655 State->Late.PanGain[index] = dirGain * speakerGain[index] + ambientGain;
658 // Basic delay line input/output routines.
659 static __inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
661 return Delay->Line[offset&Delay->Mask];
664 static __inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
666 Delay->Line[offset&Delay->Mask] = in;
669 // Attenuated delay line output routine.
670 static __inline ALfloat AttenuatedDelayLineOut(DelayLine *Delay, ALuint offset, ALfloat coeff)
672 return coeff * Delay->Line[offset&Delay->Mask];
675 // Basic attenuated all-pass input/output routine.
676 static __inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
678 ALfloat out, feed;
680 out = DelayLineOut(Delay, outOffset);
681 feed = feedCoeff * in;
682 DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
684 // The time-based attenuation is only applied to the delay output to
685 // keep it from affecting the feed-back path (which is already controlled
686 // by the all-pass feed coefficient).
687 return (coeff * out) - feed;
690 // Given an input sample, this function produces modulation for the late
691 // reverb.
692 static __inline ALfloat EAXModulation(ALverbState *State, ALfloat in)
694 ALfloat sinus, frac;
695 ALuint offset;
696 ALfloat out0, out1;
698 // Calculate the sinus rythm (dependent on modulation time and the
699 // sampling rate). The center of the sinus is moved to reduce the delay
700 // of the effect when the time or depth are low.
701 sinus = 1.0f - cos(2.0f * M_PI * State->Mod.Index / State->Mod.Range);
703 // The depth determines the range over which to read the input samples
704 // from, so it must be filtered to reduce the distortion caused by even
705 // small parameter changes.
706 State->Mod.Filter += (State->Mod.Depth - State->Mod.Filter) *
707 State->Mod.Coeff;
709 // Calculate the read offset and fraction between it and the next sample.
710 frac = (1.0f + (State->Mod.Filter * sinus));
711 offset = (ALuint)frac;
712 frac -= offset;
714 // Get the two samples crossed by the offset, and feed the delay line
715 // with the next input sample.
716 out0 = DelayLineOut(&State->Mod.Delay, State->Offset - offset);
717 out1 = DelayLineOut(&State->Mod.Delay, State->Offset - offset - 1);
718 DelayLineIn(&State->Mod.Delay, State->Offset, in);
720 // Step the modulation index forward, keeping it bound to its range.
721 State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
723 // The output is obtained by linearly interpolating the two samples that
724 // were acquired above.
725 return out0 + ((out1 - out0) * frac);
728 // Delay line output routine for early reflections.
729 static __inline ALfloat EarlyDelayLineOut(ALverbState *State, ALuint index)
731 return AttenuatedDelayLineOut(&State->Early.Delay[index],
732 State->Offset - State->Early.Offset[index],
733 State->Early.Coeff[index]);
736 // Given an input sample, this function produces four-channel output for the
737 // early reflections.
738 static __inline ALvoid EarlyReflection(ALverbState *State, ALfloat in, ALfloat *out)
740 ALfloat d[4], v, f[4];
742 // Obtain the decayed results of each early delay line.
743 d[0] = EarlyDelayLineOut(State, 0);
744 d[1] = EarlyDelayLineOut(State, 1);
745 d[2] = EarlyDelayLineOut(State, 2);
746 d[3] = EarlyDelayLineOut(State, 3);
748 /* The following uses a lossless scattering junction from waveguide
749 * theory. It actually amounts to a householder mixing matrix, which
750 * will produce a maximally diffuse response, and means this can probably
751 * be considered a simple feed-back delay network (FDN).
753 * ---
755 * v = 2/N / d_i
756 * ---
757 * i=1
759 v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
760 // The junction is loaded with the input here.
761 v += in;
763 // Calculate the feed values for the delay lines.
764 f[0] = v - d[0];
765 f[1] = v - d[1];
766 f[2] = v - d[2];
767 f[3] = v - d[3];
769 // Re-feed the delay lines.
770 DelayLineIn(&State->Early.Delay[0], State->Offset, f[0]);
771 DelayLineIn(&State->Early.Delay[1], State->Offset, f[1]);
772 DelayLineIn(&State->Early.Delay[2], State->Offset, f[2]);
773 DelayLineIn(&State->Early.Delay[3], State->Offset, f[3]);
775 // Output the results of the junction for all four channels.
776 out[0] = State->Early.Gain * f[0];
777 out[1] = State->Early.Gain * f[1];
778 out[2] = State->Early.Gain * f[2];
779 out[3] = State->Early.Gain * f[3];
782 // All-pass input/output routine for late reverb.
783 static __inline ALfloat LateAllPassInOut(ALverbState *State, ALuint index, ALfloat in)
785 return AllpassInOut(&State->Late.ApDelay[index],
786 State->Offset - State->Late.ApOffset[index],
787 State->Offset, in, State->Late.ApFeedCoeff,
788 State->Late.ApCoeff[index]);
791 // Delay line output routine for late reverb.
792 static __inline ALfloat LateDelayLineOut(ALverbState *State, ALuint index)
794 return AttenuatedDelayLineOut(&State->Late.Delay[index],
795 State->Offset - State->Late.Offset[index],
796 State->Late.Coeff[index]);
799 // Low-pass filter input/output routine for late reverb.
800 static __inline ALfloat LateLowPassInOut(ALverbState *State, ALuint index, ALfloat in)
802 State->Late.LpSample[index] = in +
803 ((State->Late.LpSample[index] - in) * State->Late.LpCoeff[index]);
804 return State->Late.LpSample[index];
807 // Given four decorrelated input samples, this function produces four-channel
808 // output for the late reverb.
809 static __inline ALvoid LateReverb(ALverbState *State, ALfloat *in, ALfloat *out)
811 ALfloat d[4], f[4];
813 // Obtain the decayed results of the cyclical delay lines, and add the
814 // corresponding input channels. Then pass the results through the
815 // low-pass filters.
817 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and back
818 // to 0.
819 d[0] = LateLowPassInOut(State, 2, in[2] + LateDelayLineOut(State, 2));
820 d[1] = LateLowPassInOut(State, 0, in[0] + LateDelayLineOut(State, 0));
821 d[2] = LateLowPassInOut(State, 3, in[3] + LateDelayLineOut(State, 3));
822 d[3] = LateLowPassInOut(State, 1, in[1] + LateDelayLineOut(State, 1));
824 // To help increase diffusion, run each line through an all-pass filter.
825 // When there is no diffusion, the shortest all-pass filter will feed the
826 // shortest delay line.
827 d[0] = LateAllPassInOut(State, 0, d[0]);
828 d[1] = LateAllPassInOut(State, 1, d[1]);
829 d[2] = LateAllPassInOut(State, 2, d[2]);
830 d[3] = LateAllPassInOut(State, 3, d[3]);
832 /* Late reverb is done with a modified feed-back delay network (FDN)
833 * topology. Four input lines are each fed through their own all-pass
834 * filter and then into the mixing matrix. The four outputs of the
835 * mixing matrix are then cycled back to the inputs. Each output feeds
836 * a different input to form a circlular feed cycle.
838 * The mixing matrix used is a 4D skew-symmetric rotation matrix derived
839 * using a single unitary rotational parameter:
841 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
842 * [ -a, d, c, -b ]
843 * [ -b, -c, d, a ]
844 * [ -c, b, -a, d ]
846 * The rotation is constructed from the effect's diffusion parameter,
847 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
848 * with differing signs, and d is the coefficient x. The matrix is thus:
850 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
851 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
852 * [ y, -y, x, y ] x = cos(t)
853 * [ -y, -y, -y, x ] y = sin(t) / n
855 * To reduce the number of multiplies, the x coefficient is applied with
856 * the cyclical delay line coefficients. Thus only the y coefficient is
857 * applied when mixing, and is modified to be: y / x.
859 f[0] = d[0] + (State->Late.MixCoeff * ( d[1] - d[2] + d[3]));
860 f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
861 f[2] = d[2] + (State->Late.MixCoeff * ( d[0] - d[1] + d[3]));
862 f[3] = d[3] + (State->Late.MixCoeff * (-d[0] - d[1] - d[2]));
864 // Output the results of the matrix for all four channels, attenuated by
865 // the late reverb gain (which is attenuated by the 'x' mix coefficient).
866 out[0] = State->Late.Gain * f[0];
867 out[1] = State->Late.Gain * f[1];
868 out[2] = State->Late.Gain * f[2];
869 out[3] = State->Late.Gain * f[3];
871 // Re-feed the cyclical delay lines.
872 DelayLineIn(&State->Late.Delay[0], State->Offset, f[0]);
873 DelayLineIn(&State->Late.Delay[1], State->Offset, f[1]);
874 DelayLineIn(&State->Late.Delay[2], State->Offset, f[2]);
875 DelayLineIn(&State->Late.Delay[3], State->Offset, f[3]);
878 // Given an input sample, this function mixes echo into the four-channel late
879 // reverb.
880 static __inline ALvoid EAXEcho(ALverbState *State, ALfloat in, ALfloat *late)
882 ALfloat out, feed;
884 // Get the latest attenuated echo sample for output.
885 feed = AttenuatedDelayLineOut(&State->Echo.Delay,
886 State->Offset - State->Echo.Offset,
887 State->Echo.Coeff);
889 // Mix the output into the late reverb channels.
890 out = State->Echo.MixCoeff[0] * feed;
891 late[0] = (State->Echo.MixCoeff[1] * late[0]) + out;
892 late[1] = (State->Echo.MixCoeff[1] * late[1]) + out;
893 late[2] = (State->Echo.MixCoeff[1] * late[2]) + out;
894 late[3] = (State->Echo.MixCoeff[1] * late[3]) + out;
896 // Mix the energy-attenuated input with the output and pass it through
897 // the echo low-pass filter.
898 feed += State->Echo.DensityGain * in;
899 feed += ((State->Echo.LpSample - feed) * State->Echo.LpCoeff);
900 State->Echo.LpSample = feed;
902 // Then the echo all-pass filter.
903 feed = AllpassInOut(&State->Echo.ApDelay,
904 State->Offset - State->Echo.ApOffset,
905 State->Offset, feed, State->Echo.ApFeedCoeff,
906 State->Echo.ApCoeff);
908 // Feed the delay with the mixed and filtered sample.
909 DelayLineIn(&State->Echo.Delay, State->Offset, feed);
912 // Perform the non-EAX reverb pass on a given input sample, resulting in
913 // four-channel output.
914 static __inline ALvoid VerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
916 ALfloat feed, taps[4];
918 // Low-pass filter the incoming sample.
919 in = lpFilter2P(&State->LpFilter, 0, in);
921 // Feed the initial delay line.
922 DelayLineIn(&State->Delay, State->Offset, in);
924 // Calculate the early reflection from the first delay tap.
925 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
926 EarlyReflection(State, in, early);
928 // Feed the decorrelator from the energy-attenuated output of the second
929 // delay tap.
930 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
931 feed = in * State->Late.DensityGain;
932 DelayLineIn(&State->Decorrelator, State->Offset, feed);
934 // Calculate the late reverb from the decorrelator taps.
935 taps[0] = feed;
936 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
937 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
938 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
939 LateReverb(State, taps, late);
941 // Step all delays forward one sample.
942 State->Offset++;
945 // Perform the EAX reverb pass on a given input sample, resulting in four-
946 // channel output.
947 static __inline ALvoid EAXVerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
949 ALfloat feed, taps[4];
951 // Low-pass filter the incoming sample.
952 in = lpFilter2P(&State->LpFilter, 0, in);
954 // Perform any modulation on the input.
955 in = EAXModulation(State, in);
957 // Feed the initial delay line.
958 DelayLineIn(&State->Delay, State->Offset, in);
960 // Calculate the early reflection from the first delay tap.
961 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
962 EarlyReflection(State, in, early);
964 // Feed the decorrelator from the energy-attenuated output of the second
965 // delay tap.
966 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
967 feed = in * State->Late.DensityGain;
968 DelayLineIn(&State->Decorrelator, State->Offset, feed);
970 // Calculate the late reverb from the decorrelator taps.
971 taps[0] = feed;
972 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
973 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
974 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
975 LateReverb(State, taps, late);
977 // Calculate and mix in any echo.
978 EAXEcho(State, in, late);
980 // Step all delays forward one sample.
981 State->Offset++;
984 // This destroys the reverb state. It should be called only when the effect
985 // slot has a different (or no) effect loaded over the reverb effect.
986 static ALvoid VerbDestroy(ALeffectState *effect)
988 ALverbState *State = (ALverbState*)effect;
989 if(State)
991 free(State->SampleBuffer);
992 State->SampleBuffer = NULL;
993 free(State);
997 // This updates the device-dependant reverb state. This is called on
998 // initialization and any time the device parameters (eg. playback frequency,
999 // or format) have been changed.
1000 static ALboolean VerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
1002 ALverbState *State = (ALverbState*)effect;
1003 ALuint frequency = Device->Frequency, index;
1005 // Allocate the delay lines.
1006 if(!AllocLines(AL_FALSE, frequency, State))
1008 alSetError(AL_OUT_OF_MEMORY);
1009 return AL_FALSE;
1012 // The early reflection and late all-pass filter line lengths are static,
1013 // so their offsets only need to be calculated once.
1014 for(index = 0;index < 4;index++)
1016 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
1017 frequency);
1018 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
1019 frequency);
1022 return AL_TRUE;
1025 // This updates the device-dependant EAX reverb state. This is called on
1026 // initialization and any time the device parameters (eg. playback frequency,
1027 // format) have been changed.
1028 static ALboolean EAXVerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
1030 ALverbState *State = (ALverbState*)effect;
1031 ALuint frequency = Device->Frequency, index;
1033 // Allocate the delay lines.
1034 if(!AllocLines(AL_TRUE, frequency, State))
1036 alSetError(AL_OUT_OF_MEMORY);
1037 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 = pow(MODULATION_FILTER_COEFF, MODULATION_FILTER_CONST /
1045 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, g, x, y, hfRatio;
1072 // Calculate the master low-pass filter (from the master effect HF gain).
1073 cw = CalcI3DL2HFreq(Effect->Reverb.HFReference, frequency);
1074 g = __max(Effect->Reverb.GainHF, 0.0001f);
1075 // This is done with 2 chained 1-pole filters, so no need to square g.
1076 State->LpFilter.coeff = CalcI3DL2Coeff(g, cw);
1078 // Update the initial effect delay.
1079 UpdateDelayLine(Effect->Reverb.ReflectionsDelay,
1080 Effect->Reverb.LateReverbDelay, frequency, State);
1082 // Update the early lines.
1083 UpdateEarlyLines(Effect->Reverb.Gain, Effect->Reverb.ReflectionsGain,
1084 Effect->Reverb.LateReverbDelay, State);
1086 // Update the decorrelator.
1087 UpdateDecorrelator(Effect->Reverb.Density, frequency, State);
1089 // Get the mixing matrix coefficients (x and y).
1090 CalcMatrixCoeffs(Effect->Reverb.Diffusion, &x, &y);
1091 // Then divide x into y to simplify the matrix calculation.
1092 State->Late.MixCoeff = y / x;
1094 // If the HF limit parameter is flagged, calculate an appropriate limit
1095 // based on the air absorption parameter.
1096 hfRatio = Effect->Reverb.DecayHFRatio;
1097 if(Effect->Reverb.DecayHFLimit && Effect->Reverb.AirAbsorptionGainHF < 1.0f)
1098 hfRatio = CalcLimitedHfRatio(hfRatio, Effect->Reverb.AirAbsorptionGainHF,
1099 Effect->Reverb.DecayTime);
1101 // Update the late lines.
1102 UpdateLateLines(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1103 x, Effect->Reverb.Density, Effect->Reverb.DecayTime,
1104 Effect->Reverb.Diffusion, hfRatio, cw, frequency, State);
1107 // This updates the EAX reverb state. This is called any time the EAX reverb
1108 // effect is loaded into a slot.
1109 static ALvoid EAXVerbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
1111 ALverbState *State = (ALverbState*)effect;
1112 ALuint frequency = Context->Device->Frequency;
1113 ALfloat cw, g, x, y, hfRatio;
1115 // Calculate the master low-pass filter (from the master effect HF gain).
1116 cw = CalcI3DL2HFreq(Effect->Reverb.HFReference, frequency);
1117 g = __max(Effect->Reverb.GainHF, 0.0001f);
1118 // This is done with 2 chained 1-pole filters, so no need to square g.
1119 State->LpFilter.coeff = CalcI3DL2Coeff(g, cw);
1121 // Update the modulator line.
1122 UpdateModulator(Effect->Reverb.ModulationTime,
1123 Effect->Reverb.ModulationDepth, frequency, State);
1125 // Update the initial effect delay.
1126 UpdateDelayLine(Effect->Reverb.ReflectionsDelay,
1127 Effect->Reverb.LateReverbDelay, frequency, State);
1129 // Update the early lines.
1130 UpdateEarlyLines(Effect->Reverb.Gain, Effect->Reverb.ReflectionsGain,
1131 Effect->Reverb.LateReverbDelay, State);
1133 // Update the decorrelator.
1134 UpdateDecorrelator(Effect->Reverb.Density, frequency, State);
1136 // Get the mixing matrix coefficients (x and y).
1137 CalcMatrixCoeffs(Effect->Reverb.Diffusion, &x, &y);
1138 // Then divide x into y to simplify the matrix calculation.
1139 State->Late.MixCoeff = y / x;
1141 // If the HF limit parameter is flagged, calculate an appropriate limit
1142 // based on the air absorption parameter.
1143 hfRatio = Effect->Reverb.DecayHFRatio;
1144 if(Effect->Reverb.DecayHFLimit && Effect->Reverb.AirAbsorptionGainHF < 1.0f)
1145 hfRatio = CalcLimitedHfRatio(hfRatio, Effect->Reverb.AirAbsorptionGainHF,
1146 Effect->Reverb.DecayTime);
1148 // Update the late lines.
1149 UpdateLateLines(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1150 x, Effect->Reverb.Density, Effect->Reverb.DecayTime,
1151 Effect->Reverb.Diffusion, hfRatio, cw, frequency, State);
1153 // Update the echo line.
1154 UpdateEchoLine(Effect->Reverb.Gain, Effect->Reverb.LateReverbGain,
1155 Effect->Reverb.EchoTime, Effect->Reverb.DecayTime,
1156 Effect->Reverb.Diffusion, Effect->Reverb.EchoDepth,
1157 hfRatio, cw, frequency, State);
1159 // Update early and late 3D panning.
1160 Update3DPanning(Effect->Reverb.ReflectionsPan, Effect->Reverb.LateReverbPan,
1161 Context->PanningLUT, State);
1164 // This processes the reverb state, given the input samples and an output
1165 // buffer.
1166 static ALvoid VerbProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
1168 ALverbState *State = (ALverbState*)effect;
1169 ALuint index;
1170 ALfloat early[4], late[4], out[4];
1171 ALfloat gain = Slot->Gain;
1173 for(index = 0;index < SamplesToDo;index++)
1175 // Process reverb for this sample.
1176 VerbPass(State, SamplesIn[index], early, late);
1178 // Mix early reflections and late reverb.
1179 out[0] = (early[0] + late[0]) * gain;
1180 out[1] = (early[1] + late[1]) * gain;
1181 out[2] = (early[2] + late[2]) * gain;
1182 out[3] = (early[3] + late[3]) * gain;
1184 // Output the results.
1185 SamplesOut[index][FRONT_LEFT] += out[0];
1186 SamplesOut[index][FRONT_RIGHT] += out[1];
1187 SamplesOut[index][FRONT_CENTER] += out[3];
1188 SamplesOut[index][SIDE_LEFT] += out[0];
1189 SamplesOut[index][SIDE_RIGHT] += out[1];
1190 SamplesOut[index][BACK_LEFT] += out[0];
1191 SamplesOut[index][BACK_RIGHT] += out[1];
1192 SamplesOut[index][BACK_CENTER] += out[2];
1196 // This processes the EAX reverb state, given the input samples and an output
1197 // buffer.
1198 static ALvoid EAXVerbProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
1200 ALverbState *State = (ALverbState*)effect;
1201 ALuint index;
1202 ALfloat early[4], late[4];
1203 ALfloat gain = Slot->Gain;
1205 for(index = 0;index < SamplesToDo;index++)
1207 // Process reverb for this sample.
1208 EAXVerbPass(State, SamplesIn[index], early, late);
1210 // Unfortunately, while the number and configuration of gains for
1211 // panning adjust according to OUTPUTCHANNELS, the output from the
1212 // reverb engine is not so scalable.
1213 SamplesOut[index][FRONT_LEFT] +=
1214 (State->Early.PanGain[FRONT_LEFT]*early[0] +
1215 State->Late.PanGain[FRONT_LEFT]*late[0]) * gain;
1216 SamplesOut[index][FRONT_RIGHT] +=
1217 (State->Early.PanGain[FRONT_RIGHT]*early[1] +
1218 State->Late.PanGain[FRONT_RIGHT]*late[1]) * gain;
1219 SamplesOut[index][FRONT_CENTER] +=
1220 (State->Early.PanGain[FRONT_CENTER]*early[3] +
1221 State->Late.PanGain[FRONT_CENTER]*late[3]) * gain;
1222 SamplesOut[index][SIDE_LEFT] +=
1223 (State->Early.PanGain[SIDE_LEFT]*early[0] +
1224 State->Late.PanGain[SIDE_LEFT]*late[0]) * gain;
1225 SamplesOut[index][SIDE_RIGHT] +=
1226 (State->Early.PanGain[SIDE_RIGHT]*early[1] +
1227 State->Late.PanGain[SIDE_RIGHT]*late[1]) * gain;
1228 SamplesOut[index][BACK_LEFT] +=
1229 (State->Early.PanGain[BACK_LEFT]*early[0] +
1230 State->Late.PanGain[BACK_LEFT]*late[0]) * gain;
1231 SamplesOut[index][BACK_RIGHT] +=
1232 (State->Early.PanGain[BACK_RIGHT]*early[1] +
1233 State->Late.PanGain[BACK_RIGHT]*late[1]) * gain;
1234 SamplesOut[index][BACK_CENTER] +=
1235 (State->Early.PanGain[BACK_CENTER]*early[2] +
1236 State->Late.PanGain[BACK_CENTER]*late[2]) * gain;
1240 // This creates the reverb state. It should be called only when the reverb
1241 // effect is loaded into a slot that doesn't already have a reverb effect.
1242 ALeffectState *VerbCreate(void)
1244 ALverbState *State = NULL;
1245 ALuint index;
1247 State = malloc(sizeof(ALverbState));
1248 if(!State)
1250 alSetError(AL_OUT_OF_MEMORY);
1251 return NULL;
1254 State->state.Destroy = VerbDestroy;
1255 State->state.DeviceUpdate = VerbDeviceUpdate;
1256 State->state.Update = VerbUpdate;
1257 State->state.Process = VerbProcess;
1259 State->TotalSamples = 0;
1260 State->SampleBuffer = NULL;
1262 State->LpFilter.coeff = 0.0f;
1263 State->LpFilter.history[0] = 0.0f;
1264 State->LpFilter.history[1] = 0.0f;
1266 State->Mod.Delay.Mask = 0;
1267 State->Mod.Delay.Line = NULL;
1268 State->Mod.Index = 0;
1269 State->Mod.Range = 1;
1270 State->Mod.Depth = 0.0f;
1271 State->Mod.Coeff = 0.0f;
1272 State->Mod.Filter = 0.0f;
1274 State->Delay.Mask = 0;
1275 State->Delay.Line = NULL;
1276 State->DelayTap[0] = 0;
1277 State->DelayTap[1] = 0;
1279 State->Early.Gain = 0.0f;
1280 for(index = 0;index < 4;index++)
1282 State->Early.Coeff[index] = 0.0f;
1283 State->Early.Delay[index].Mask = 0;
1284 State->Early.Delay[index].Line = NULL;
1285 State->Early.Offset[index] = 0;
1288 State->Decorrelator.Mask = 0;
1289 State->Decorrelator.Line = NULL;
1290 State->DecoTap[0] = 0;
1291 State->DecoTap[1] = 0;
1292 State->DecoTap[2] = 0;
1294 State->Late.Gain = 0.0f;
1295 State->Late.DensityGain = 0.0f;
1296 State->Late.ApFeedCoeff = 0.0f;
1297 State->Late.MixCoeff = 0.0f;
1298 for(index = 0;index < 4;index++)
1300 State->Late.ApCoeff[index] = 0.0f;
1301 State->Late.ApDelay[index].Mask = 0;
1302 State->Late.ApDelay[index].Line = NULL;
1303 State->Late.ApOffset[index] = 0;
1305 State->Late.Coeff[index] = 0.0f;
1306 State->Late.Delay[index].Mask = 0;
1307 State->Late.Delay[index].Line = NULL;
1308 State->Late.Offset[index] = 0;
1310 State->Late.LpCoeff[index] = 0.0f;
1311 State->Late.LpSample[index] = 0.0f;
1314 for(index = 0;index < OUTPUTCHANNELS;index++)
1316 State->Early.PanGain[index] = 0.0f;
1317 State->Late.PanGain[index] = 0.0f;
1320 State->Echo.DensityGain = 0.0f;
1321 State->Echo.Delay.Mask = 0;
1322 State->Echo.Delay.Line = NULL;
1323 State->Echo.ApDelay.Mask = 0;
1324 State->Echo.ApDelay.Line = NULL;
1325 State->Echo.Coeff = 0.0f;
1326 State->Echo.ApFeedCoeff = 0.0f;
1327 State->Echo.ApCoeff = 0.0f;
1328 State->Echo.Offset = 0;
1329 State->Echo.ApOffset = 0;
1330 State->Echo.LpCoeff = 0.0f;
1331 State->Echo.LpSample = 0.0f;
1332 State->Echo.MixCoeff[0] = 0.0f;
1333 State->Echo.MixCoeff[1] = 1.0f;
1335 State->Offset = 0;
1337 return &State->state;
1340 ALeffectState *EAXVerbCreate(void)
1342 ALeffectState *State = VerbCreate();
1343 if(State)
1345 State->DeviceUpdate = EAXVerbDeviceUpdate;
1346 State->Update = EAXVerbUpdate;
1347 State->Process = EAXVerbProcess;
1349 return State;