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
30 #include "alAuxEffectSlot.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.
43 typedef struct ALverbState
{
44 // Must be first in all effects!
47 // All delay lines are allocated as a single buffer to reduce memory
48 // fragmentation and management code.
49 ALfloat
*SampleBuffer
;
51 // Master effect low-pass filter (2 chained 1-pole filters).
55 // Modulator delay line.
57 // The vibrato time is tracked with an index over a modulus-wrapped
58 // range (in samples).
61 // The depth of frequency change (also in samples) and its filter.
66 // Initial effect delay.
68 // The tap points for the initial delay. First tap goes to early
69 // reflections, the last to late reverb.
72 // Output gain for early reflections.
74 // Early reflections are done with 4 delay lines.
78 // The gain for each output channel based on 3D panning (only for the
80 ALfloat PanGain
[OUTPUTCHANNELS
];
82 // Decorrelator delay line.
83 DelayLine Decorrelator
;
84 // There are actually 4 decorrelator taps, but the first occurs at the
88 // Output gain for late reverb.
90 // Attenuation to compensate for the modal density and decay rate of
93 // The feed-back and feed-forward all-pass coefficient.
95 // Mixing matrix coefficient.
97 // Late reverb has 4 parallel all-pass filters.
101 // In addition to 4 cyclical delay lines.
105 // The cyclical delay lines are 1-pole low-pass filtered.
108 // The gain for each output channel based on 3D panning (only for the
110 ALfloat PanGain
[OUTPUTCHANNELS
];
113 // Attenuation to compensate for the modal density and decay rate of
116 // Echo delay and all-pass lines.
124 // The echo line is 1-pole low-pass filtered.
127 // Echo mixing coefficients.
130 // The current read offset for all delay lines.
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
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
)
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.
199 // Given the allocated sample buffer, this function updates each delay line
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
;
215 ALfloat
*newBuffer
= NULL
;
217 // All delay line lengths are calculated to accomodate the full range of
218 // lengths given their respective paramters.
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
,
233 // The initial delay is the sum of the reflections and late reverb
236 length
= AL_EAXREVERB_MAX_REFLECTIONS_DELAY
+
237 AL_EAXREVERB_MAX_LATE_REVERB_DELAY
;
239 length
= AL_REVERB_MAX_REFLECTIONS_DELAY
+
240 AL_REVERB_MAX_LATE_REVERB_DELAY
;
241 totalSamples
+= CalcLineLength(length
, totalSamples
, frequency
,
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
]);
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
)
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
]);
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
;
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
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
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
)
356 // The matrix is of order 4, so n is sqrt (4 - 1).
358 t
= diffusion
* atan(n
);
360 // Calculate the first mixing matrix coefficient.
362 // Calculate the second mixing matrix coefficient.
366 // Calculate the limited HF ratio for use with the late reverb low-pass
368 static __inline ALfloat
CalcLimitedHfRatio(ALfloat hfRatio
, ALfloat airAbsorptionGainHF
, ALfloat decayTime
)
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
381 limitRatio
= __max(limitRatio
, 0.1f
);
383 // Using the limit calculated above, apply the upper bound to the HF
385 return __min(hfRatio
, limitRatio
);
388 // Calculate the coefficient for a HF (and eventually LF) decay damping
390 static __inline ALfloat
CalcDampingCoeff(ALfloat hfRatio
, ALfloat length
, ALfloat decayTime
, ALfloat decayCoeff
, ALfloat cw
)
394 // Eventually this should boost the high frequencies when the ratio
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.
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
);
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
)
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
/
433 State
->Mod
.Range
= (ALuint
)length
;
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
447 State
->Mod
.Depth
= modDepth
* MODULATION_DEPTH_COEFF
* modTime
/ 2.0f
/
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
)
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
471 for(index
= 0;index
< 4;index
++)
472 State
->Early
.Coeff
[index
] = CalcDecayCoeff(EARLY_LINE_LENGTH
[index
],
476 // Update the offsets for the decorrelator line.
477 static ALvoid
UpdateDecorrelator(ALfloat density
, ALuint frequency
, ALverbState
*State
)
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
* pow(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
)
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
,
524 // Calculate the all-pass feed-back and feed-forward coefficient.
525 State
->Late
.ApFeedCoeff
= 0.5f
* pow(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
],
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
550 State
->Late
.Coeff
[index
] *= xMix
;
554 // Update the echo gain, line offset, line coefficients, and mixing
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
566 State
->Echo
.DensityGain
= CalcDensityGain(State
->Echo
.Coeff
);
568 // Calculate the echo all-pass feed coefficient.
569 State
->Echo
.ApFeedCoeff
= 0.5f
* pow(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
)
591 ALfloat earlyPan
[3] = { ReflectionsPan
[0], ReflectionsPan
[1],
593 ALfloat latePan
[3] = { LateReverbPan
[0], LateReverbPan
[1],
596 ALfloat
*speakerGain
, dirGain
, ambientGain
;
599 // Calculate the 3D-panning gains for the early reflections and late
601 length
= earlyPan
[0]*earlyPan
[0] + earlyPan
[1]*earlyPan
[1] + earlyPan
[2]*earlyPan
[2];
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];
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
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
)
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
673 static __inline ALfloat
EAXModulation(ALverbState
*State
, ALfloat in
)
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
) *
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
;
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).
740 v
= (d
[0] + d
[1] + d
[2] + d
[3]) * 0.5f
;
741 // The junction is loaded with the input here.
744 // Calculate the feed values for the delay lines.
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
)
794 // Obtain the decayed results of the cyclical delay lines, and add the
795 // corresponding input channels. Then pass the results through the
798 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and back
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
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
861 static __inline ALvoid
EAXEcho(ALverbState
*State
, ALfloat in
, ALfloat
*late
)
865 // Get the latest attenuated echo sample for output.
866 feed
= AttenuatedDelayLineOut(&State
->Echo
.Delay
,
867 State
->Offset
- State
->Echo
.Offset
,
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
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.
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.
926 // Perform the EAX reverb pass on a given input sample, resulting in four-
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
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.
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.
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
;
972 free(State
->SampleBuffer
);
973 State
->SampleBuffer
= NULL
;
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
))
989 alSetError(AL_OUT_OF_MEMORY
);
993 // The early reflection and late all-pass filter line lengths are static,
994 // so their offsets only need to be calculated once.
995 for(index
= 0;index
< 4;index
++)
997 State
->Early
.Offset
[index
] = (ALuint
)(EARLY_LINE_LENGTH
[index
] *
999 State
->Late
.ApOffset
[index
] = (ALuint
)(ALLPASS_LINE_LENGTH
[index
] *
1006 // This updates the device-dependant EAX reverb state. This is called on
1007 // initialization and any time the device parameters (eg. playback frequency,
1008 // format) have been changed.
1009 static ALboolean
EAXVerbDeviceUpdate(ALeffectState
*effect
, ALCdevice
*Device
)
1011 ALverbState
*State
= (ALverbState
*)effect
;
1012 ALuint frequency
= Device
->Frequency
, index
;
1014 // Allocate the delay lines.
1015 if(!AllocLines(AL_TRUE
, frequency
, State
))
1017 alSetError(AL_OUT_OF_MEMORY
);
1021 // Calculate the modulation filter coefficient. Notice that the exponent
1022 // is calculated given the current sample rate. This ensures that the
1023 // resulting filter response over time is consistent across all sample
1025 State
->Mod
.Coeff
= pow(MODULATION_FILTER_COEFF
, MODULATION_FILTER_CONST
/
1028 // The early reflection and late all-pass filter line lengths are static,
1029 // so their offsets only need to be calculated once.
1030 for(index
= 0;index
< 4;index
++)
1032 State
->Early
.Offset
[index
] = (ALuint
)(EARLY_LINE_LENGTH
[index
] *
1034 State
->Late
.ApOffset
[index
] = (ALuint
)(ALLPASS_LINE_LENGTH
[index
] *
1038 // The echo all-pass filter line length is static, so its offset only
1039 // needs to be calculated once.
1040 State
->Echo
.ApOffset
= (ALuint
)(ECHO_ALLPASS_LENGTH
* frequency
);
1045 // This updates the reverb state. This is called any time the reverb effect
1046 // is loaded into a slot.
1047 static ALvoid
VerbUpdate(ALeffectState
*effect
, ALCcontext
*Context
, const ALeffect
*Effect
)
1049 ALverbState
*State
= (ALverbState
*)effect
;
1050 ALuint frequency
= Context
->Device
->Frequency
;
1051 ALfloat cw
, x
, y
, hfRatio
;
1053 // Calculate the master low-pass filter (from the master effect HF gain).
1054 cw
= CalcI3DL2HFreq(Effect
->Reverb
.HFReference
, frequency
);
1055 // This is done with 2 chained 1-pole filters, so no need to square g.
1056 State
->LpFilter
.coeff
= lpCoeffCalc(Effect
->Reverb
.GainHF
, cw
);
1058 // Update the initial effect delay.
1059 UpdateDelayLine(Effect
->Reverb
.ReflectionsDelay
,
1060 Effect
->Reverb
.LateReverbDelay
, frequency
, State
);
1062 // Update the early lines.
1063 UpdateEarlyLines(Effect
->Reverb
.Gain
, Effect
->Reverb
.ReflectionsGain
,
1064 Effect
->Reverb
.LateReverbDelay
, State
);
1066 // Update the decorrelator.
1067 UpdateDecorrelator(Effect
->Reverb
.Density
, frequency
, State
);
1069 // Get the mixing matrix coefficients (x and y).
1070 CalcMatrixCoeffs(Effect
->Reverb
.Diffusion
, &x
, &y
);
1071 // Then divide x into y to simplify the matrix calculation.
1072 State
->Late
.MixCoeff
= y
/ x
;
1074 // If the HF limit parameter is flagged, calculate an appropriate limit
1075 // based on the air absorption parameter.
1076 hfRatio
= Effect
->Reverb
.DecayHFRatio
;
1077 if(Effect
->Reverb
.DecayHFLimit
&& Effect
->Reverb
.AirAbsorptionGainHF
< 1.0f
)
1078 hfRatio
= CalcLimitedHfRatio(hfRatio
, Effect
->Reverb
.AirAbsorptionGainHF
,
1079 Effect
->Reverb
.DecayTime
);
1081 // Update the late lines.
1082 UpdateLateLines(Effect
->Reverb
.Gain
, Effect
->Reverb
.LateReverbGain
,
1083 x
, Effect
->Reverb
.Density
, Effect
->Reverb
.DecayTime
,
1084 Effect
->Reverb
.Diffusion
, hfRatio
, cw
, frequency
, State
);
1087 // This updates the EAX reverb state. This is called any time the EAX reverb
1088 // effect is loaded into a slot.
1089 static ALvoid
EAXVerbUpdate(ALeffectState
*effect
, ALCcontext
*Context
, const ALeffect
*Effect
)
1091 ALverbState
*State
= (ALverbState
*)effect
;
1092 ALuint frequency
= Context
->Device
->Frequency
;
1093 ALfloat cw
, x
, y
, hfRatio
;
1095 // Calculate the master low-pass filter (from the master effect HF gain).
1096 cw
= CalcI3DL2HFreq(Effect
->Reverb
.HFReference
, frequency
);
1097 // This is done with 2 chained 1-pole filters, so no need to square g.
1098 State
->LpFilter
.coeff
= lpCoeffCalc(Effect
->Reverb
.GainHF
, cw
);
1100 // Update the modulator line.
1101 UpdateModulator(Effect
->Reverb
.ModulationTime
,
1102 Effect
->Reverb
.ModulationDepth
, frequency
, State
);
1104 // Update the initial effect delay.
1105 UpdateDelayLine(Effect
->Reverb
.ReflectionsDelay
,
1106 Effect
->Reverb
.LateReverbDelay
, frequency
, State
);
1108 // Update the early lines.
1109 UpdateEarlyLines(Effect
->Reverb
.Gain
, Effect
->Reverb
.ReflectionsGain
,
1110 Effect
->Reverb
.LateReverbDelay
, State
);
1112 // Update the decorrelator.
1113 UpdateDecorrelator(Effect
->Reverb
.Density
, frequency
, State
);
1115 // Get the mixing matrix coefficients (x and y).
1116 CalcMatrixCoeffs(Effect
->Reverb
.Diffusion
, &x
, &y
);
1117 // Then divide x into y to simplify the matrix calculation.
1118 State
->Late
.MixCoeff
= y
/ x
;
1120 // If the HF limit parameter is flagged, calculate an appropriate limit
1121 // based on the air absorption parameter.
1122 hfRatio
= Effect
->Reverb
.DecayHFRatio
;
1123 if(Effect
->Reverb
.DecayHFLimit
&& Effect
->Reverb
.AirAbsorptionGainHF
< 1.0f
)
1124 hfRatio
= CalcLimitedHfRatio(hfRatio
, Effect
->Reverb
.AirAbsorptionGainHF
,
1125 Effect
->Reverb
.DecayTime
);
1127 // Update the late lines.
1128 UpdateLateLines(Effect
->Reverb
.Gain
, Effect
->Reverb
.LateReverbGain
,
1129 x
, Effect
->Reverb
.Density
, Effect
->Reverb
.DecayTime
,
1130 Effect
->Reverb
.Diffusion
, hfRatio
, cw
, frequency
, State
);
1132 // Update the echo line.
1133 UpdateEchoLine(Effect
->Reverb
.Gain
, Effect
->Reverb
.LateReverbGain
,
1134 Effect
->Reverb
.EchoTime
, Effect
->Reverb
.DecayTime
,
1135 Effect
->Reverb
.Diffusion
, Effect
->Reverb
.EchoDepth
,
1136 hfRatio
, cw
, frequency
, State
);
1138 // Update early and late 3D panning.
1139 Update3DPanning(Effect
->Reverb
.ReflectionsPan
, Effect
->Reverb
.LateReverbPan
,
1140 Context
->PanningLUT
, State
);
1143 // This processes the reverb state, given the input samples and an output
1145 static ALvoid
VerbProcess(ALeffectState
*effect
, const ALeffectslot
*Slot
, ALuint SamplesToDo
, const ALfloat
*SamplesIn
, ALfloat (*SamplesOut
)[OUTPUTCHANNELS
])
1147 ALverbState
*State
= (ALverbState
*)effect
;
1149 ALfloat early
[4], late
[4], out
[4];
1150 ALfloat gain
= Slot
->Gain
;
1152 for(index
= 0;index
< SamplesToDo
;index
++)
1154 // Process reverb for this sample.
1155 VerbPass(State
, SamplesIn
[index
], early
, late
);
1157 // Mix early reflections and late reverb.
1158 out
[0] = (early
[0] + late
[0]) * gain
;
1159 out
[1] = (early
[1] + late
[1]) * gain
;
1160 out
[2] = (early
[2] + late
[2]) * gain
;
1161 out
[3] = (early
[3] + late
[3]) * gain
;
1163 // Output the results.
1164 SamplesOut
[index
][FRONT_LEFT
] += out
[0];
1165 SamplesOut
[index
][FRONT_RIGHT
] += out
[1];
1166 SamplesOut
[index
][FRONT_CENTER
] += out
[3];
1167 SamplesOut
[index
][SIDE_LEFT
] += out
[0];
1168 SamplesOut
[index
][SIDE_RIGHT
] += out
[1];
1169 SamplesOut
[index
][BACK_LEFT
] += out
[0];
1170 SamplesOut
[index
][BACK_RIGHT
] += out
[1];
1171 SamplesOut
[index
][BACK_CENTER
] += out
[2];
1175 // This processes the EAX reverb state, given the input samples and an output
1177 static ALvoid
EAXVerbProcess(ALeffectState
*effect
, const ALeffectslot
*Slot
, ALuint SamplesToDo
, const ALfloat
*SamplesIn
, ALfloat (*SamplesOut
)[OUTPUTCHANNELS
])
1179 ALverbState
*State
= (ALverbState
*)effect
;
1181 ALfloat early
[4], late
[4];
1182 ALfloat gain
= Slot
->Gain
;
1184 for(index
= 0;index
< SamplesToDo
;index
++)
1186 // Process reverb for this sample.
1187 EAXVerbPass(State
, SamplesIn
[index
], early
, late
);
1189 // Unfortunately, while the number and configuration of gains for
1190 // panning adjust according to OUTPUTCHANNELS, the output from the
1191 // reverb engine is not so scalable.
1192 SamplesOut
[index
][FRONT_LEFT
] +=
1193 (State
->Early
.PanGain
[FRONT_LEFT
]*early
[0] +
1194 State
->Late
.PanGain
[FRONT_LEFT
]*late
[0]) * gain
;
1195 SamplesOut
[index
][FRONT_RIGHT
] +=
1196 (State
->Early
.PanGain
[FRONT_RIGHT
]*early
[1] +
1197 State
->Late
.PanGain
[FRONT_RIGHT
]*late
[1]) * gain
;
1198 SamplesOut
[index
][FRONT_CENTER
] +=
1199 (State
->Early
.PanGain
[FRONT_CENTER
]*early
[3] +
1200 State
->Late
.PanGain
[FRONT_CENTER
]*late
[3]) * gain
;
1201 SamplesOut
[index
][SIDE_LEFT
] +=
1202 (State
->Early
.PanGain
[SIDE_LEFT
]*early
[0] +
1203 State
->Late
.PanGain
[SIDE_LEFT
]*late
[0]) * gain
;
1204 SamplesOut
[index
][SIDE_RIGHT
] +=
1205 (State
->Early
.PanGain
[SIDE_RIGHT
]*early
[1] +
1206 State
->Late
.PanGain
[SIDE_RIGHT
]*late
[1]) * gain
;
1207 SamplesOut
[index
][BACK_LEFT
] +=
1208 (State
->Early
.PanGain
[BACK_LEFT
]*early
[0] +
1209 State
->Late
.PanGain
[BACK_LEFT
]*late
[0]) * gain
;
1210 SamplesOut
[index
][BACK_RIGHT
] +=
1211 (State
->Early
.PanGain
[BACK_RIGHT
]*early
[1] +
1212 State
->Late
.PanGain
[BACK_RIGHT
]*late
[1]) * gain
;
1213 SamplesOut
[index
][BACK_CENTER
] +=
1214 (State
->Early
.PanGain
[BACK_CENTER
]*early
[2] +
1215 State
->Late
.PanGain
[BACK_CENTER
]*late
[2]) * gain
;
1219 // This creates the reverb state. It should be called only when the reverb
1220 // effect is loaded into a slot that doesn't already have a reverb effect.
1221 ALeffectState
*VerbCreate(void)
1223 ALverbState
*State
= NULL
;
1226 State
= malloc(sizeof(ALverbState
));
1229 alSetError(AL_OUT_OF_MEMORY
);
1233 State
->state
.Destroy
= VerbDestroy
;
1234 State
->state
.DeviceUpdate
= VerbDeviceUpdate
;
1235 State
->state
.Update
= VerbUpdate
;
1236 State
->state
.Process
= VerbProcess
;
1238 State
->TotalSamples
= 0;
1239 State
->SampleBuffer
= NULL
;
1241 State
->LpFilter
.coeff
= 0.0f
;
1242 State
->LpFilter
.history
[0] = 0.0f
;
1243 State
->LpFilter
.history
[1] = 0.0f
;
1245 State
->Mod
.Delay
.Mask
= 0;
1246 State
->Mod
.Delay
.Line
= NULL
;
1247 State
->Mod
.Index
= 0;
1248 State
->Mod
.Range
= 1;
1249 State
->Mod
.Depth
= 0.0f
;
1250 State
->Mod
.Coeff
= 0.0f
;
1251 State
->Mod
.Filter
= 0.0f
;
1253 State
->Delay
.Mask
= 0;
1254 State
->Delay
.Line
= NULL
;
1255 State
->DelayTap
[0] = 0;
1256 State
->DelayTap
[1] = 0;
1258 State
->Early
.Gain
= 0.0f
;
1259 for(index
= 0;index
< 4;index
++)
1261 State
->Early
.Coeff
[index
] = 0.0f
;
1262 State
->Early
.Delay
[index
].Mask
= 0;
1263 State
->Early
.Delay
[index
].Line
= NULL
;
1264 State
->Early
.Offset
[index
] = 0;
1267 State
->Decorrelator
.Mask
= 0;
1268 State
->Decorrelator
.Line
= NULL
;
1269 State
->DecoTap
[0] = 0;
1270 State
->DecoTap
[1] = 0;
1271 State
->DecoTap
[2] = 0;
1273 State
->Late
.Gain
= 0.0f
;
1274 State
->Late
.DensityGain
= 0.0f
;
1275 State
->Late
.ApFeedCoeff
= 0.0f
;
1276 State
->Late
.MixCoeff
= 0.0f
;
1277 for(index
= 0;index
< 4;index
++)
1279 State
->Late
.ApCoeff
[index
] = 0.0f
;
1280 State
->Late
.ApDelay
[index
].Mask
= 0;
1281 State
->Late
.ApDelay
[index
].Line
= NULL
;
1282 State
->Late
.ApOffset
[index
] = 0;
1284 State
->Late
.Coeff
[index
] = 0.0f
;
1285 State
->Late
.Delay
[index
].Mask
= 0;
1286 State
->Late
.Delay
[index
].Line
= NULL
;
1287 State
->Late
.Offset
[index
] = 0;
1289 State
->Late
.LpCoeff
[index
] = 0.0f
;
1290 State
->Late
.LpSample
[index
] = 0.0f
;
1293 for(index
= 0;index
< OUTPUTCHANNELS
;index
++)
1295 State
->Early
.PanGain
[index
] = 0.0f
;
1296 State
->Late
.PanGain
[index
] = 0.0f
;
1299 State
->Echo
.DensityGain
= 0.0f
;
1300 State
->Echo
.Delay
.Mask
= 0;
1301 State
->Echo
.Delay
.Line
= NULL
;
1302 State
->Echo
.ApDelay
.Mask
= 0;
1303 State
->Echo
.ApDelay
.Line
= NULL
;
1304 State
->Echo
.Coeff
= 0.0f
;
1305 State
->Echo
.ApFeedCoeff
= 0.0f
;
1306 State
->Echo
.ApCoeff
= 0.0f
;
1307 State
->Echo
.Offset
= 0;
1308 State
->Echo
.ApOffset
= 0;
1309 State
->Echo
.LpCoeff
= 0.0f
;
1310 State
->Echo
.LpSample
= 0.0f
;
1311 State
->Echo
.MixCoeff
[0] = 0.0f
;
1312 State
->Echo
.MixCoeff
[1] = 0.0f
;
1316 return &State
->state
;
1319 ALeffectState
*EAXVerbCreate(void)
1321 ALeffectState
*State
= VerbCreate();
1324 State
->DeviceUpdate
= EAXVerbDeviceUpdate
;
1325 State
->Update
= EAXVerbUpdate
;
1326 State
->Process
= EAXVerbProcess
;