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
[MAXCHANNELS
];
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
[MAXCHANNELS
];
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.
133 // The gain for each output channel (non-EAX path only; aliased from
138 /* This is a user config option for modifying the overall output of the reverb
141 ALfloat ReverbBoost
= 1.0f
;
143 /* Specifies whether to use a standard reverb effect in place of EAX reverb */
144 ALboolean EmulateEAXReverb
= AL_FALSE
;
146 /* This coefficient is used to define the maximum frequency range controlled
147 * by the modulation depth. The current value of 0.1 will allow it to swing
148 * from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the
149 * sampler to stall on the downswing, and above 1 it will cause it to sample
152 static const ALfloat MODULATION_DEPTH_COEFF
= 0.1f
;
154 /* A filter is used to avoid the terrible distortion caused by changing
155 * modulation time and/or depth. To be consistent across different sample
156 * rates, the coefficient must be raised to a constant divided by the sample
157 * rate: coeff^(constant / rate).
159 static const ALfloat MODULATION_FILTER_COEFF
= 0.048f
;
160 static const ALfloat MODULATION_FILTER_CONST
= 100000.0f
;
162 // When diffusion is above 0, an all-pass filter is used to take the edge off
163 // the echo effect. It uses the following line length (in seconds).
164 static const ALfloat ECHO_ALLPASS_LENGTH
= 0.0133f
;
166 // Input into the late reverb is decorrelated between four channels. Their
167 // timings are dependent on a fraction and multiplier. See the
168 // UpdateDecorrelator() routine for the calculations involved.
169 static const ALfloat DECO_FRACTION
= 0.15f
;
170 static const ALfloat DECO_MULTIPLIER
= 2.0f
;
172 // All delay line lengths are specified in seconds.
174 // The lengths of the early delay lines.
175 static const ALfloat EARLY_LINE_LENGTH
[4] =
177 0.0015f
, 0.0045f
, 0.0135f
, 0.0405f
180 // The lengths of the late all-pass delay lines.
181 static const ALfloat ALLPASS_LINE_LENGTH
[4] =
183 0.0151f
, 0.0167f
, 0.0183f
, 0.0200f
,
186 // The lengths of the late cyclical delay lines.
187 static const ALfloat LATE_LINE_LENGTH
[4] =
189 0.0211f
, 0.0311f
, 0.0461f
, 0.0680f
192 // The late cyclical delay lines have a variable length dependent on the
193 // effect's density parameter (inverted for some reason) and this multiplier.
194 static const ALfloat LATE_LINE_MULTIPLIER
= 4.0f
;
196 // Calculate the length of a delay line and store its mask and offset.
197 static ALuint
CalcLineLength(ALfloat length
, ALintptrEXT offset
, ALuint frequency
, DelayLine
*Delay
)
201 // All line lengths are powers of 2, calculated from their lengths, with
202 // an additional sample in case of rounding errors.
203 samples
= NextPowerOf2((ALuint
)(length
* frequency
) + 1);
204 // All lines share a single sample buffer.
205 Delay
->Mask
= samples
- 1;
206 Delay
->Line
= (ALfloat
*)offset
;
207 // Return the sample count for accumulation.
211 // Given the allocated sample buffer, this function updates each delay line
213 static __inline ALvoid
RealizeLineOffset(ALfloat
* sampleBuffer
, DelayLine
*Delay
)
215 Delay
->Line
= &sampleBuffer
[(ALintptrEXT
)Delay
->Line
];
218 /* Calculates the delay line metrics and allocates the shared sample buffer
219 * for all lines given a flag indicating whether or not to allocate the EAX-
220 * related delays (eaxFlag) and the sample rate (frequency). If an
221 * allocation failure occurs, it returns AL_FALSE.
223 static ALboolean
AllocLines(ALboolean eaxFlag
, ALuint frequency
, ALverbState
*State
)
225 ALuint totalSamples
, index
;
227 ALfloat
*newBuffer
= NULL
;
229 // All delay line lengths are calculated to accomodate the full range of
230 // lengths given their respective paramters.
234 /* The modulator's line length is calculated from the maximum
235 * modulation time and depth coefficient, and halfed for the low-to-
236 * high frequency swing. An additional sample is added to keep it
237 * stable when there is no modulation.
239 length
= (AL_EAXREVERB_MAX_MODULATION_TIME
* MODULATION_DEPTH_COEFF
/
240 2.0f
) + (1.0f
/ frequency
);
241 totalSamples
+= CalcLineLength(length
, totalSamples
, frequency
,
245 // The initial delay is the sum of the reflections and late reverb
248 length
= AL_EAXREVERB_MAX_REFLECTIONS_DELAY
+
249 AL_EAXREVERB_MAX_LATE_REVERB_DELAY
;
251 length
= AL_REVERB_MAX_REFLECTIONS_DELAY
+
252 AL_REVERB_MAX_LATE_REVERB_DELAY
;
253 totalSamples
+= CalcLineLength(length
, totalSamples
, frequency
,
256 // The early reflection lines.
257 for(index
= 0;index
< 4;index
++)
258 totalSamples
+= CalcLineLength(EARLY_LINE_LENGTH
[index
], totalSamples
,
259 frequency
, &State
->Early
.Delay
[index
]);
261 // The decorrelator line is calculated from the lowest reverb density (a
262 // parameter value of 1).
263 length
= (DECO_FRACTION
* DECO_MULTIPLIER
* DECO_MULTIPLIER
) *
264 LATE_LINE_LENGTH
[0] * (1.0f
+ LATE_LINE_MULTIPLIER
);
265 totalSamples
+= CalcLineLength(length
, totalSamples
, frequency
,
266 &State
->Decorrelator
);
268 // The late all-pass lines.
269 for(index
= 0;index
< 4;index
++)
270 totalSamples
+= CalcLineLength(ALLPASS_LINE_LENGTH
[index
], totalSamples
,
271 frequency
, &State
->Late
.ApDelay
[index
]);
273 // The late delay lines are calculated from the lowest reverb density.
274 for(index
= 0;index
< 4;index
++)
276 length
= LATE_LINE_LENGTH
[index
] * (1.0f
+ LATE_LINE_MULTIPLIER
);
277 totalSamples
+= CalcLineLength(length
, totalSamples
, frequency
,
278 &State
->Late
.Delay
[index
]);
283 // The echo all-pass and delay lines.
284 totalSamples
+= CalcLineLength(ECHO_ALLPASS_LENGTH
, totalSamples
,
285 frequency
, &State
->Echo
.ApDelay
);
286 totalSamples
+= CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME
, totalSamples
,
287 frequency
, &State
->Echo
.Delay
);
290 if(totalSamples
!= State
->TotalSamples
)
292 newBuffer
= realloc(State
->SampleBuffer
, sizeof(ALfloat
) * totalSamples
);
293 if(newBuffer
== NULL
)
295 State
->SampleBuffer
= newBuffer
;
296 State
->TotalSamples
= totalSamples
;
299 // Update all delays to reflect the new sample buffer.
300 RealizeLineOffset(State
->SampleBuffer
, &State
->Delay
);
301 RealizeLineOffset(State
->SampleBuffer
, &State
->Decorrelator
);
302 for(index
= 0;index
< 4;index
++)
304 RealizeLineOffset(State
->SampleBuffer
, &State
->Early
.Delay
[index
]);
305 RealizeLineOffset(State
->SampleBuffer
, &State
->Late
.ApDelay
[index
]);
306 RealizeLineOffset(State
->SampleBuffer
, &State
->Late
.Delay
[index
]);
310 RealizeLineOffset(State
->SampleBuffer
, &State
->Mod
.Delay
);
311 RealizeLineOffset(State
->SampleBuffer
, &State
->Echo
.ApDelay
);
312 RealizeLineOffset(State
->SampleBuffer
, &State
->Echo
.Delay
);
315 // Clear the sample buffer.
316 for(index
= 0;index
< State
->TotalSamples
;index
++)
317 State
->SampleBuffer
[index
] = 0.0f
;
322 // Calculate a decay coefficient given the length of each cycle and the time
323 // until the decay reaches -60 dB.
324 static __inline ALfloat
CalcDecayCoeff(ALfloat length
, ALfloat decayTime
)
326 return aluPow(0.001f
/*-60 dB*/, length
/decayTime
);
329 // Calculate a decay length from a coefficient and the time until the decay
331 static __inline ALfloat
CalcDecayLength(ALfloat coeff
, ALfloat decayTime
)
333 return log10(coeff
) * decayTime
/ -3.0f
/*log10(0.001)*/;
336 // Calculate the high frequency parameter for the I3DL2 coefficient
338 static __inline ALfloat
CalcI3DL2HFreq(ALfloat hfRef
, ALuint frequency
)
340 return cos(2.0f
* M_PI
* hfRef
/ frequency
);
343 // Calculate an attenuation to be applied to the input of any echo models to
344 // compensate for modal density and decay time.
345 static __inline ALfloat
CalcDensityGain(ALfloat a
)
347 /* The energy of a signal can be obtained by finding the area under the
348 * squared signal. This takes the form of Sum(x_n^2), where x is the
349 * amplitude for the sample n.
351 * Decaying feedback matches exponential decay of the form Sum(a^n),
352 * where a is the attenuation coefficient, and n is the sample. The area
353 * under this decay curve can be calculated as: 1 / (1 - a).
355 * Modifying the above equation to find the squared area under the curve
356 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
357 * calculated by inverting the square root of this approximation,
358 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
360 return aluSqrt(1.0f
- (a
* a
));
363 // Calculate the mixing matrix coefficients given a diffusion factor.
364 static __inline ALvoid
CalcMatrixCoeffs(ALfloat diffusion
, ALfloat
*x
, ALfloat
*y
)
368 // The matrix is of order 4, so n is sqrt (4 - 1).
370 t
= diffusion
* atan(n
);
372 // Calculate the first mixing matrix coefficient.
374 // Calculate the second mixing matrix coefficient.
378 // Calculate the limited HF ratio for use with the late reverb low-pass
380 static ALfloat
CalcLimitedHfRatio(ALfloat hfRatio
, ALfloat airAbsorptionGainHF
, ALfloat decayTime
)
384 /* Find the attenuation due to air absorption in dB (converting delay
385 * time to meters using the speed of sound). Then reversing the decay
386 * equation, solve for HF ratio. The delay length is cancelled out of
387 * the equation, so it can be calculated once for all lines.
389 limitRatio
= 1.0f
/ (CalcDecayLength(airAbsorptionGainHF
, decayTime
) *
390 SPEEDOFSOUNDMETRESPERSEC
);
391 /* Using the limit calculated above, apply the upper bound to the HF
392 * ratio. Also need to limit the result to a minimum of 0.1, just like the
393 * HF ratio parameter. */
394 return clampf(limitRatio
, 0.1f
, hfRatio
);
397 // Calculate the coefficient for a HF (and eventually LF) decay damping
399 static __inline ALfloat
CalcDampingCoeff(ALfloat hfRatio
, ALfloat length
, ALfloat decayTime
, ALfloat decayCoeff
, ALfloat cw
)
403 // Eventually this should boost the high frequencies when the ratio
408 // Calculate the low-pass coefficient by dividing the HF decay
409 // coefficient by the full decay coefficient.
410 g
= CalcDecayCoeff(length
, decayTime
* hfRatio
) / decayCoeff
;
412 // Damping is done with a 1-pole filter, so g needs to be squared.
414 coeff
= lpCoeffCalc(g
, cw
);
416 // Very low decay times will produce minimal output, so apply an
417 // upper bound to the coefficient.
418 coeff
= minf(coeff
, 0.98f
);
423 // Update the EAX modulation index, range, and depth. Keep in mind that this
424 // kind of vibrato is additive and not multiplicative as one may expect. The
425 // downswing will sound stronger than the upswing.
426 static ALvoid
UpdateModulator(ALfloat modTime
, ALfloat modDepth
, ALuint frequency
, ALverbState
*State
)
430 /* Modulation is calculated in two parts.
432 * The modulation time effects the sinus applied to the change in
433 * frequency. An index out of the current time range (both in samples)
434 * is incremented each sample. The range is bound to a reasonable
435 * minimum (1 sample) and when the timing changes, the index is rescaled
436 * to the new range (to keep the sinus consistent).
438 length
= modTime
* frequency
;
439 if (length
>= 1.0f
) {
440 State
->Mod
.Index
= (ALuint
)(State
->Mod
.Index
* length
/
442 State
->Mod
.Range
= (ALuint
)length
;
444 State
->Mod
.Index
= 0;
445 State
->Mod
.Range
= 1;
448 /* The modulation depth effects the amount of frequency change over the
449 * range of the sinus. It needs to be scaled by the modulation time so
450 * that a given depth produces a consistent change in frequency over all
451 * ranges of time. Since the depth is applied to a sinus value, it needs
452 * to be halfed once for the sinus range and again for the sinus swing
453 * in time (half of it is spent decreasing the frequency, half is spent
456 State
->Mod
.Depth
= modDepth
* MODULATION_DEPTH_COEFF
* modTime
/ 2.0f
/
460 // Update the offsets for the initial effect delay line.
461 static ALvoid
UpdateDelayLine(ALfloat earlyDelay
, ALfloat lateDelay
, ALuint frequency
, ALverbState
*State
)
463 // Calculate the initial delay taps.
464 State
->DelayTap
[0] = (ALuint
)(earlyDelay
* frequency
);
465 State
->DelayTap
[1] = (ALuint
)((earlyDelay
+ lateDelay
) * frequency
);
468 // Update the early reflections gain and line coefficients.
469 static ALvoid
UpdateEarlyLines(ALfloat reverbGain
, ALfloat earlyGain
, ALfloat lateDelay
, ALverbState
*State
)
473 // Calculate the early reflections gain (from the master effect gain, and
474 // reflections gain parameters) with a constant attenuation of 0.5.
475 State
->Early
.Gain
= 0.5f
* reverbGain
* earlyGain
;
477 // Calculate the gain (coefficient) for each early delay line using the
478 // late delay time. This expands the early reflections to the start of
480 for(index
= 0;index
< 4;index
++)
481 State
->Early
.Coeff
[index
] = CalcDecayCoeff(EARLY_LINE_LENGTH
[index
],
485 // Update the offsets for the decorrelator line.
486 static ALvoid
UpdateDecorrelator(ALfloat density
, ALuint frequency
, ALverbState
*State
)
491 /* The late reverb inputs are decorrelated to smooth the reverb tail and
492 * reduce harsh echos. The first tap occurs immediately, while the
493 * remaining taps are delayed by multiples of a fraction of the smallest
494 * cyclical delay time.
496 * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
498 for(index
= 0;index
< 3;index
++)
500 length
= (DECO_FRACTION
* aluPow(DECO_MULTIPLIER
, (ALfloat
)index
)) *
501 LATE_LINE_LENGTH
[0] * (1.0f
+ (density
* LATE_LINE_MULTIPLIER
));
502 State
->DecoTap
[index
] = (ALuint
)(length
* frequency
);
506 // Update the late reverb gains, line lengths, and line coefficients.
507 static ALvoid
UpdateLateLines(ALfloat reverbGain
, ALfloat lateGain
, ALfloat xMix
, ALfloat density
, ALfloat decayTime
, ALfloat diffusion
, ALfloat hfRatio
, ALfloat cw
, ALuint frequency
, ALverbState
*State
)
512 /* Calculate the late reverb gain (from the master effect gain, and late
513 * reverb gain parameters). Since the output is tapped prior to the
514 * application of the next delay line coefficients, this gain needs to be
515 * attenuated by the 'x' mixing matrix coefficient as well.
517 State
->Late
.Gain
= reverbGain
* lateGain
* xMix
;
519 /* To compensate for changes in modal density and decay time of the late
520 * reverb signal, the input is attenuated based on the maximal energy of
521 * the outgoing signal. This approximation is used to keep the apparent
522 * energy of the signal equal for all ranges of density and decay time.
524 * The average length of the cyclcical delay lines is used to calculate
525 * the attenuation coefficient.
527 length
= (LATE_LINE_LENGTH
[0] + LATE_LINE_LENGTH
[1] +
528 LATE_LINE_LENGTH
[2] + LATE_LINE_LENGTH
[3]) / 4.0f
;
529 length
*= 1.0f
+ (density
* LATE_LINE_MULTIPLIER
);
530 State
->Late
.DensityGain
= CalcDensityGain(CalcDecayCoeff(length
,
533 // Calculate the all-pass feed-back and feed-forward coefficient.
534 State
->Late
.ApFeedCoeff
= 0.5f
* aluPow(diffusion
, 2.0f
);
536 for(index
= 0;index
< 4;index
++)
538 // Calculate the gain (coefficient) for each all-pass line.
539 State
->Late
.ApCoeff
[index
] = CalcDecayCoeff(ALLPASS_LINE_LENGTH
[index
],
542 // Calculate the length (in seconds) of each cyclical delay line.
543 length
= LATE_LINE_LENGTH
[index
] * (1.0f
+ (density
*
544 LATE_LINE_MULTIPLIER
));
546 // Calculate the delay offset for each cyclical delay line.
547 State
->Late
.Offset
[index
] = (ALuint
)(length
* frequency
);
549 // Calculate the gain (coefficient) for each cyclical line.
550 State
->Late
.Coeff
[index
] = CalcDecayCoeff(length
, decayTime
);
552 // Calculate the damping coefficient for each low-pass filter.
553 State
->Late
.LpCoeff
[index
] =
554 CalcDampingCoeff(hfRatio
, length
, decayTime
,
555 State
->Late
.Coeff
[index
], cw
);
557 // Attenuate the cyclical line coefficients by the mixing coefficient
559 State
->Late
.Coeff
[index
] *= xMix
;
563 // Update the echo gain, line offset, line coefficients, and mixing
565 static ALvoid
UpdateEchoLine(ALfloat reverbGain
, ALfloat lateGain
, ALfloat echoTime
, ALfloat decayTime
, ALfloat diffusion
, ALfloat echoDepth
, ALfloat hfRatio
, ALfloat cw
, ALuint frequency
, ALverbState
*State
)
567 // Update the offset and coefficient for the echo delay line.
568 State
->Echo
.Offset
= (ALuint
)(echoTime
* frequency
);
570 // Calculate the decay coefficient for the echo line.
571 State
->Echo
.Coeff
= CalcDecayCoeff(echoTime
, decayTime
);
573 // Calculate the energy-based attenuation coefficient for the echo delay
575 State
->Echo
.DensityGain
= CalcDensityGain(State
->Echo
.Coeff
);
577 // Calculate the echo all-pass feed coefficient.
578 State
->Echo
.ApFeedCoeff
= 0.5f
* aluPow(diffusion
, 2.0f
);
580 // Calculate the echo all-pass attenuation coefficient.
581 State
->Echo
.ApCoeff
= CalcDecayCoeff(ECHO_ALLPASS_LENGTH
, decayTime
);
583 // Calculate the damping coefficient for each low-pass filter.
584 State
->Echo
.LpCoeff
= CalcDampingCoeff(hfRatio
, echoTime
, decayTime
,
585 State
->Echo
.Coeff
, cw
);
587 /* Calculate the echo mixing coefficients. The first is applied to the
588 * echo itself. The second is used to attenuate the late reverb when
589 * echo depth is high and diffusion is low, so the echo is slightly
590 * stronger than the decorrelated echos in the reverb tail.
592 State
->Echo
.MixCoeff
[0] = reverbGain
* lateGain
* echoDepth
;
593 State
->Echo
.MixCoeff
[1] = 1.0f
- (echoDepth
* 0.5f
* (1.0f
- diffusion
));
596 // Update the early and late 3D panning gains.
597 static ALvoid
Update3DPanning(const ALCdevice
*Device
, const ALfloat
*ReflectionsPan
, const ALfloat
*LateReverbPan
, ALfloat Gain
, ALverbState
*State
)
599 ALfloat earlyPan
[3] = { ReflectionsPan
[0], ReflectionsPan
[1],
601 ALfloat latePan
[3] = { LateReverbPan
[0], LateReverbPan
[1],
603 const ALfloat
*speakerGain
;
612 // Attenuate non-directional reverb according to the number of channels
613 ambientGain
= aluSqrt(2.0f
/Device
->NumChan
);
615 // Calculate the 3D-panning gains for the early reflections and late
617 length
= earlyPan
[0]*earlyPan
[0] + earlyPan
[1]*earlyPan
[1] + earlyPan
[2]*earlyPan
[2];
620 length
= 1.0f
/ aluSqrt(length
);
621 earlyPan
[0] *= length
;
622 earlyPan
[1] *= length
;
623 earlyPan
[2] *= length
;
625 length
= latePan
[0]*latePan
[0] + latePan
[1]*latePan
[1] + latePan
[2]*latePan
[2];
628 length
= 1.0f
/ aluSqrt(length
);
629 latePan
[0] *= length
;
630 latePan
[1] *= length
;
631 latePan
[2] *= length
;
634 /* This code applies directional reverb just like the mixer applies
635 * directional sources. It diffuses the sound toward all speakers as the
636 * magnitude of the panning vector drops, which is only a rough
637 * approximation of the expansion of sound across the speakers from the
640 pos
= aluCart2LUTpos(earlyPan
[2], earlyPan
[0]);
641 speakerGain
= Device
->PanningLUT
[pos
];
642 dirGain
= aluSqrt((earlyPan
[0] * earlyPan
[0]) + (earlyPan
[2] * earlyPan
[2]));
644 for(index
= 0;index
< MAXCHANNELS
;index
++)
645 State
->Early
.PanGain
[index
] = 0.0f
;
646 for(index
= 0;index
< Device
->NumChan
;index
++)
648 enum Channel chan
= Device
->Speaker2Chan
[index
];
649 State
->Early
.PanGain
[chan
] = lerp(ambientGain
, speakerGain
[chan
], dirGain
) * Gain
;
653 pos
= aluCart2LUTpos(latePan
[2], latePan
[0]);
654 speakerGain
= Device
->PanningLUT
[pos
];
655 dirGain
= aluSqrt((latePan
[0] * latePan
[0]) + (latePan
[2] * latePan
[2]));
657 for(index
= 0;index
< MAXCHANNELS
;index
++)
658 State
->Late
.PanGain
[index
] = 0.0f
;
659 for(index
= 0;index
< Device
->NumChan
;index
++)
661 enum Channel chan
= Device
->Speaker2Chan
[index
];
662 State
->Late
.PanGain
[chan
] = lerp(ambientGain
, speakerGain
[chan
], dirGain
) * Gain
;
666 // Basic delay line input/output routines.
667 static __inline ALfloat
DelayLineOut(DelayLine
*Delay
, ALuint offset
)
669 return Delay
->Line
[offset
&Delay
->Mask
];
672 static __inline ALvoid
DelayLineIn(DelayLine
*Delay
, ALuint offset
, ALfloat in
)
674 Delay
->Line
[offset
&Delay
->Mask
] = in
;
677 // Attenuated delay line output routine.
678 static __inline ALfloat
AttenuatedDelayLineOut(DelayLine
*Delay
, ALuint offset
, ALfloat coeff
)
680 return coeff
* Delay
->Line
[offset
&Delay
->Mask
];
683 // Basic attenuated all-pass input/output routine.
684 static __inline ALfloat
AllpassInOut(DelayLine
*Delay
, ALuint outOffset
, ALuint inOffset
, ALfloat in
, ALfloat feedCoeff
, ALfloat coeff
)
688 out
= DelayLineOut(Delay
, outOffset
);
689 feed
= feedCoeff
* in
;
690 DelayLineIn(Delay
, inOffset
, (feedCoeff
* (out
- feed
)) + in
);
692 // The time-based attenuation is only applied to the delay output to
693 // keep it from affecting the feed-back path (which is already controlled
694 // by the all-pass feed coefficient).
695 return (coeff
* out
) - feed
;
698 // Given an input sample, this function produces modulation for the late
700 static __inline ALfloat
EAXModulation(ALverbState
*State
, ALfloat in
)
706 // Calculate the sinus rythm (dependent on modulation time and the
707 // sampling rate). The center of the sinus is moved to reduce the delay
708 // of the effect when the time or depth are low.
709 sinus
= 1.0f
- cos(2.0f
* M_PI
* State
->Mod
.Index
/ State
->Mod
.Range
);
711 // The depth determines the range over which to read the input samples
712 // from, so it must be filtered to reduce the distortion caused by even
713 // small parameter changes.
714 State
->Mod
.Filter
= lerp(State
->Mod
.Filter
, State
->Mod
.Depth
,
717 // Calculate the read offset and fraction between it and the next sample.
718 frac
= (1.0f
+ (State
->Mod
.Filter
* sinus
));
719 offset
= (ALuint
)frac
;
722 // Get the two samples crossed by the offset, and feed the delay line
723 // with the next input sample.
724 out0
= DelayLineOut(&State
->Mod
.Delay
, State
->Offset
- offset
);
725 out1
= DelayLineOut(&State
->Mod
.Delay
, State
->Offset
- offset
- 1);
726 DelayLineIn(&State
->Mod
.Delay
, State
->Offset
, in
);
728 // Step the modulation index forward, keeping it bound to its range.
729 State
->Mod
.Index
= (State
->Mod
.Index
+ 1) % State
->Mod
.Range
;
731 // The output is obtained by linearly interpolating the two samples that
732 // were acquired above.
733 return lerp(out0
, out1
, frac
);
736 // Delay line output routine for early reflections.
737 static __inline ALfloat
EarlyDelayLineOut(ALverbState
*State
, ALuint index
)
739 return AttenuatedDelayLineOut(&State
->Early
.Delay
[index
],
740 State
->Offset
- State
->Early
.Offset
[index
],
741 State
->Early
.Coeff
[index
]);
744 // Given an input sample, this function produces four-channel output for the
745 // early reflections.
746 static __inline ALvoid
EarlyReflection(ALverbState
*State
, ALfloat in
, ALfloat
*out
)
748 ALfloat d
[4], v
, f
[4];
750 // Obtain the decayed results of each early delay line.
751 d
[0] = EarlyDelayLineOut(State
, 0);
752 d
[1] = EarlyDelayLineOut(State
, 1);
753 d
[2] = EarlyDelayLineOut(State
, 2);
754 d
[3] = EarlyDelayLineOut(State
, 3);
756 /* The following uses a lossless scattering junction from waveguide
757 * theory. It actually amounts to a householder mixing matrix, which
758 * will produce a maximally diffuse response, and means this can probably
759 * be considered a simple feed-back delay network (FDN).
767 v
= (d
[0] + d
[1] + d
[2] + d
[3]) * 0.5f
;
768 // The junction is loaded with the input here.
771 // Calculate the feed values for the delay lines.
777 // Re-feed the delay lines.
778 DelayLineIn(&State
->Early
.Delay
[0], State
->Offset
, f
[0]);
779 DelayLineIn(&State
->Early
.Delay
[1], State
->Offset
, f
[1]);
780 DelayLineIn(&State
->Early
.Delay
[2], State
->Offset
, f
[2]);
781 DelayLineIn(&State
->Early
.Delay
[3], State
->Offset
, f
[3]);
783 // Output the results of the junction for all four channels.
784 out
[0] = State
->Early
.Gain
* f
[0];
785 out
[1] = State
->Early
.Gain
* f
[1];
786 out
[2] = State
->Early
.Gain
* f
[2];
787 out
[3] = State
->Early
.Gain
* f
[3];
790 // All-pass input/output routine for late reverb.
791 static __inline ALfloat
LateAllPassInOut(ALverbState
*State
, ALuint index
, ALfloat in
)
793 return AllpassInOut(&State
->Late
.ApDelay
[index
],
794 State
->Offset
- State
->Late
.ApOffset
[index
],
795 State
->Offset
, in
, State
->Late
.ApFeedCoeff
,
796 State
->Late
.ApCoeff
[index
]);
799 // Delay line output routine for late reverb.
800 static __inline ALfloat
LateDelayLineOut(ALverbState
*State
, ALuint index
)
802 return AttenuatedDelayLineOut(&State
->Late
.Delay
[index
],
803 State
->Offset
- State
->Late
.Offset
[index
],
804 State
->Late
.Coeff
[index
]);
807 // Low-pass filter input/output routine for late reverb.
808 static __inline ALfloat
LateLowPassInOut(ALverbState
*State
, ALuint index
, ALfloat in
)
810 in
= lerp(in
, State
->Late
.LpSample
[index
], State
->Late
.LpCoeff
[index
]);
811 State
->Late
.LpSample
[index
] = in
;
815 // Given four decorrelated input samples, this function produces four-channel
816 // output for the late reverb.
817 static __inline ALvoid
LateReverb(ALverbState
*State
, ALfloat
*in
, ALfloat
*out
)
821 // Obtain the decayed results of the cyclical delay lines, and add the
822 // corresponding input channels. Then pass the results through the
825 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and back
827 d
[0] = LateLowPassInOut(State
, 2, in
[2] + LateDelayLineOut(State
, 2));
828 d
[1] = LateLowPassInOut(State
, 0, in
[0] + LateDelayLineOut(State
, 0));
829 d
[2] = LateLowPassInOut(State
, 3, in
[3] + LateDelayLineOut(State
, 3));
830 d
[3] = LateLowPassInOut(State
, 1, in
[1] + LateDelayLineOut(State
, 1));
832 // To help increase diffusion, run each line through an all-pass filter.
833 // When there is no diffusion, the shortest all-pass filter will feed the
834 // shortest delay line.
835 d
[0] = LateAllPassInOut(State
, 0, d
[0]);
836 d
[1] = LateAllPassInOut(State
, 1, d
[1]);
837 d
[2] = LateAllPassInOut(State
, 2, d
[2]);
838 d
[3] = LateAllPassInOut(State
, 3, d
[3]);
840 /* Late reverb is done with a modified feed-back delay network (FDN)
841 * topology. Four input lines are each fed through their own all-pass
842 * filter and then into the mixing matrix. The four outputs of the
843 * mixing matrix are then cycled back to the inputs. Each output feeds
844 * a different input to form a circlular feed cycle.
846 * The mixing matrix used is a 4D skew-symmetric rotation matrix derived
847 * using a single unitary rotational parameter:
849 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
854 * The rotation is constructed from the effect's diffusion parameter,
855 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
856 * with differing signs, and d is the coefficient x. The matrix is thus:
858 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
859 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
860 * [ y, -y, x, y ] x = cos(t)
861 * [ -y, -y, -y, x ] y = sin(t) / n
863 * To reduce the number of multiplies, the x coefficient is applied with
864 * the cyclical delay line coefficients. Thus only the y coefficient is
865 * applied when mixing, and is modified to be: y / x.
867 f
[0] = d
[0] + (State
->Late
.MixCoeff
* ( d
[1] + -d
[2] + d
[3]));
868 f
[1] = d
[1] + (State
->Late
.MixCoeff
* (-d
[0] + d
[2] + d
[3]));
869 f
[2] = d
[2] + (State
->Late
.MixCoeff
* ( d
[0] + -d
[1] + d
[3]));
870 f
[3] = d
[3] + (State
->Late
.MixCoeff
* (-d
[0] + -d
[1] + -d
[2] ));
872 // Output the results of the matrix for all four channels, attenuated by
873 // the late reverb gain (which is attenuated by the 'x' mix coefficient).
874 out
[0] = State
->Late
.Gain
* f
[0];
875 out
[1] = State
->Late
.Gain
* f
[1];
876 out
[2] = State
->Late
.Gain
* f
[2];
877 out
[3] = State
->Late
.Gain
* f
[3];
879 // Re-feed the cyclical delay lines.
880 DelayLineIn(&State
->Late
.Delay
[0], State
->Offset
, f
[0]);
881 DelayLineIn(&State
->Late
.Delay
[1], State
->Offset
, f
[1]);
882 DelayLineIn(&State
->Late
.Delay
[2], State
->Offset
, f
[2]);
883 DelayLineIn(&State
->Late
.Delay
[3], State
->Offset
, f
[3]);
886 // Given an input sample, this function mixes echo into the four-channel late
888 static __inline ALvoid
EAXEcho(ALverbState
*State
, ALfloat in
, ALfloat
*late
)
892 // Get the latest attenuated echo sample for output.
893 feed
= AttenuatedDelayLineOut(&State
->Echo
.Delay
,
894 State
->Offset
- State
->Echo
.Offset
,
897 // Mix the output into the late reverb channels.
898 out
= State
->Echo
.MixCoeff
[0] * feed
;
899 late
[0] = (State
->Echo
.MixCoeff
[1] * late
[0]) + out
;
900 late
[1] = (State
->Echo
.MixCoeff
[1] * late
[1]) + out
;
901 late
[2] = (State
->Echo
.MixCoeff
[1] * late
[2]) + out
;
902 late
[3] = (State
->Echo
.MixCoeff
[1] * late
[3]) + out
;
904 // Mix the energy-attenuated input with the output and pass it through
905 // the echo low-pass filter.
906 feed
+= State
->Echo
.DensityGain
* in
;
907 feed
= lerp(feed
, State
->Echo
.LpSample
, State
->Echo
.LpCoeff
);
908 State
->Echo
.LpSample
= feed
;
910 // Then the echo all-pass filter.
911 feed
= AllpassInOut(&State
->Echo
.ApDelay
,
912 State
->Offset
- State
->Echo
.ApOffset
,
913 State
->Offset
, feed
, State
->Echo
.ApFeedCoeff
,
914 State
->Echo
.ApCoeff
);
916 // Feed the delay with the mixed and filtered sample.
917 DelayLineIn(&State
->Echo
.Delay
, State
->Offset
, feed
);
920 // Perform the non-EAX reverb pass on a given input sample, resulting in
921 // four-channel output.
922 static __inline ALvoid
VerbPass(ALverbState
*State
, ALfloat in
, ALfloat
*early
, ALfloat
*late
)
924 ALfloat feed
, taps
[4];
926 // Low-pass filter the incoming sample.
927 in
= lpFilter2P(&State
->LpFilter
, 0, in
);
929 // Feed the initial delay line.
930 DelayLineIn(&State
->Delay
, State
->Offset
, in
);
932 // Calculate the early reflection from the first delay tap.
933 in
= DelayLineOut(&State
->Delay
, State
->Offset
- State
->DelayTap
[0]);
934 EarlyReflection(State
, in
, early
);
936 // Feed the decorrelator from the energy-attenuated output of the second
938 in
= DelayLineOut(&State
->Delay
, State
->Offset
- State
->DelayTap
[1]);
939 feed
= in
* State
->Late
.DensityGain
;
940 DelayLineIn(&State
->Decorrelator
, State
->Offset
, feed
);
942 // Calculate the late reverb from the decorrelator taps.
944 taps
[1] = DelayLineOut(&State
->Decorrelator
, State
->Offset
- State
->DecoTap
[0]);
945 taps
[2] = DelayLineOut(&State
->Decorrelator
, State
->Offset
- State
->DecoTap
[1]);
946 taps
[3] = DelayLineOut(&State
->Decorrelator
, State
->Offset
- State
->DecoTap
[2]);
947 LateReverb(State
, taps
, late
);
949 // Step all delays forward one sample.
953 // Perform the EAX reverb pass on a given input sample, resulting in four-
955 static __inline ALvoid
EAXVerbPass(ALverbState
*State
, ALfloat in
, ALfloat
*early
, ALfloat
*late
)
957 ALfloat feed
, taps
[4];
959 // Low-pass filter the incoming sample.
960 in
= lpFilter2P(&State
->LpFilter
, 0, in
);
962 // Perform any modulation on the input.
963 in
= EAXModulation(State
, in
);
965 // Feed the initial delay line.
966 DelayLineIn(&State
->Delay
, State
->Offset
, in
);
968 // Calculate the early reflection from the first delay tap.
969 in
= DelayLineOut(&State
->Delay
, State
->Offset
- State
->DelayTap
[0]);
970 EarlyReflection(State
, in
, early
);
972 // Feed the decorrelator from the energy-attenuated output of the second
974 in
= DelayLineOut(&State
->Delay
, State
->Offset
- State
->DelayTap
[1]);
975 feed
= in
* State
->Late
.DensityGain
;
976 DelayLineIn(&State
->Decorrelator
, State
->Offset
, feed
);
978 // Calculate the late reverb from the decorrelator taps.
980 taps
[1] = DelayLineOut(&State
->Decorrelator
, State
->Offset
- State
->DecoTap
[0]);
981 taps
[2] = DelayLineOut(&State
->Decorrelator
, State
->Offset
- State
->DecoTap
[1]);
982 taps
[3] = DelayLineOut(&State
->Decorrelator
, State
->Offset
- State
->DecoTap
[2]);
983 LateReverb(State
, taps
, late
);
985 // Calculate and mix in any echo.
986 EAXEcho(State
, in
, late
);
988 // Step all delays forward one sample.
992 // This destroys the reverb state. It should be called only when the effect
993 // slot has a different (or no) effect loaded over the reverb effect.
994 static ALvoid
VerbDestroy(ALeffectState
*effect
)
996 ALverbState
*State
= (ALverbState
*)effect
;
999 free(State
->SampleBuffer
);
1000 State
->SampleBuffer
= NULL
;
1005 // This updates the device-dependant reverb state. This is called on
1006 // initialization and any time the device parameters (eg. playback frequency,
1007 // or format) have been changed.
1008 static ALboolean
VerbDeviceUpdate(ALeffectState
*effect
, ALCdevice
*Device
)
1010 ALverbState
*State
= (ALverbState
*)effect
;
1011 ALuint frequency
= Device
->Frequency
;
1014 // Allocate the delay lines.
1015 if(!AllocLines(AL_FALSE
, frequency
, State
))
1018 // The early reflection and late all-pass filter line lengths are static,
1019 // so their offsets only need to be calculated once.
1020 for(index
= 0;index
< 4;index
++)
1022 State
->Early
.Offset
[index
] = (ALuint
)(EARLY_LINE_LENGTH
[index
] *
1024 State
->Late
.ApOffset
[index
] = (ALuint
)(ALLPASS_LINE_LENGTH
[index
] *
1031 // This updates the device-dependant EAX reverb state. This is called on
1032 // initialization and any time the device parameters (eg. playback frequency,
1033 // format) have been changed.
1034 static ALboolean
EAXVerbDeviceUpdate(ALeffectState
*effect
, ALCdevice
*Device
)
1036 ALverbState
*State
= (ALverbState
*)effect
;
1037 ALuint frequency
= Device
->Frequency
, index
;
1039 // Allocate the delay lines.
1040 if(!AllocLines(AL_TRUE
, frequency
, State
))
1043 // Calculate the modulation filter coefficient. Notice that the exponent
1044 // is calculated given the current sample rate. This ensures that the
1045 // resulting filter response over time is consistent across all sample
1047 State
->Mod
.Coeff
= aluPow(MODULATION_FILTER_COEFF
,
1048 MODULATION_FILTER_CONST
/ frequency
);
1050 // The early reflection and late all-pass filter line lengths are static,
1051 // so their offsets only need to be calculated once.
1052 for(index
= 0;index
< 4;index
++)
1054 State
->Early
.Offset
[index
] = (ALuint
)(EARLY_LINE_LENGTH
[index
] *
1056 State
->Late
.ApOffset
[index
] = (ALuint
)(ALLPASS_LINE_LENGTH
[index
] *
1060 // The echo all-pass filter line length is static, so its offset only
1061 // needs to be calculated once.
1062 State
->Echo
.ApOffset
= (ALuint
)(ECHO_ALLPASS_LENGTH
* frequency
);
1067 // This updates the reverb state. This is called any time the reverb effect
1068 // is loaded into a slot.
1069 static ALvoid
VerbUpdate(ALeffectState
*effect
, ALCcontext
*Context
, const ALeffectslot
*Slot
)
1071 ALverbState
*State
= (ALverbState
*)effect
;
1072 ALCdevice
*Device
= Context
->Device
;
1073 ALuint frequency
= Device
->Frequency
;
1074 ALfloat cw
, x
, y
, hfRatio
, gain
;
1077 // Calculate the master low-pass filter (from the master effect HF gain).
1078 cw
= CalcI3DL2HFreq(Slot
->effect
.Params
.Reverb
.HFReference
, frequency
);
1079 // This is done with 2 chained 1-pole filters, so no need to square g.
1080 State
->LpFilter
.coeff
= lpCoeffCalc(Slot
->effect
.Params
.Reverb
.GainHF
, cw
);
1082 // Update the initial effect delay.
1083 UpdateDelayLine(Slot
->effect
.Params
.Reverb
.ReflectionsDelay
,
1084 Slot
->effect
.Params
.Reverb
.LateReverbDelay
,
1087 // Update the early lines.
1088 UpdateEarlyLines(Slot
->effect
.Params
.Reverb
.Gain
,
1089 Slot
->effect
.Params
.Reverb
.ReflectionsGain
,
1090 Slot
->effect
.Params
.Reverb
.LateReverbDelay
, State
);
1092 // Update the decorrelator.
1093 UpdateDecorrelator(Slot
->effect
.Params
.Reverb
.Density
, frequency
, State
);
1095 // Get the mixing matrix coefficients (x and y).
1096 CalcMatrixCoeffs(Slot
->effect
.Params
.Reverb
.Diffusion
, &x
, &y
);
1097 // Then divide x into y to simplify the matrix calculation.
1098 State
->Late
.MixCoeff
= y
/ x
;
1100 // If the HF limit parameter is flagged, calculate an appropriate limit
1101 // based on the air absorption parameter.
1102 hfRatio
= Slot
->effect
.Params
.Reverb
.DecayHFRatio
;
1103 if(Slot
->effect
.Params
.Reverb
.DecayHFLimit
&&
1104 Slot
->effect
.Params
.Reverb
.AirAbsorptionGainHF
< 1.0f
)
1105 hfRatio
= CalcLimitedHfRatio(hfRatio
,
1106 Slot
->effect
.Params
.Reverb
.AirAbsorptionGainHF
,
1107 Slot
->effect
.Params
.Reverb
.DecayTime
);
1109 // Update the late lines.
1110 UpdateLateLines(Slot
->effect
.Params
.Reverb
.Gain
, Slot
->effect
.Params
.Reverb
.LateReverbGain
,
1111 x
, Slot
->effect
.Params
.Reverb
.Density
, Slot
->effect
.Params
.Reverb
.DecayTime
,
1112 Slot
->effect
.Params
.Reverb
.Diffusion
, hfRatio
, cw
, frequency
, State
);
1114 // Update channel gains
1116 gain
*= aluSqrt(2.0f
/Device
->NumChan
);
1117 gain
*= ReverbBoost
;
1118 for(index
= 0;index
< MAXCHANNELS
;index
++)
1119 State
->Gain
[index
] = 0.0f
;
1120 for(index
= 0;index
< Device
->NumChan
;index
++)
1122 enum Channel chan
= Device
->Speaker2Chan
[index
];
1123 State
->Gain
[chan
] = gain
;
1127 // This updates the EAX reverb state. This is called any time the EAX reverb
1128 // effect is loaded into a slot.
1129 static ALvoid
EAXVerbUpdate(ALeffectState
*effect
, ALCcontext
*Context
, const ALeffectslot
*Slot
)
1131 ALverbState
*State
= (ALverbState
*)effect
;
1132 ALuint frequency
= Context
->Device
->Frequency
;
1133 ALfloat cw
, x
, y
, hfRatio
;
1135 // Calculate the master low-pass filter (from the master effect HF gain).
1136 cw
= CalcI3DL2HFreq(Slot
->effect
.Params
.Reverb
.HFReference
, frequency
);
1137 // This is done with 2 chained 1-pole filters, so no need to square g.
1138 State
->LpFilter
.coeff
= lpCoeffCalc(Slot
->effect
.Params
.Reverb
.GainHF
, cw
);
1140 // Update the modulator line.
1141 UpdateModulator(Slot
->effect
.Params
.Reverb
.ModulationTime
,
1142 Slot
->effect
.Params
.Reverb
.ModulationDepth
,
1145 // Update the initial effect delay.
1146 UpdateDelayLine(Slot
->effect
.Params
.Reverb
.ReflectionsDelay
,
1147 Slot
->effect
.Params
.Reverb
.LateReverbDelay
,
1150 // Update the early lines.
1151 UpdateEarlyLines(Slot
->effect
.Params
.Reverb
.Gain
,
1152 Slot
->effect
.Params
.Reverb
.ReflectionsGain
,
1153 Slot
->effect
.Params
.Reverb
.LateReverbDelay
, State
);
1155 // Update the decorrelator.
1156 UpdateDecorrelator(Slot
->effect
.Params
.Reverb
.Density
, frequency
, State
);
1158 // Get the mixing matrix coefficients (x and y).
1159 CalcMatrixCoeffs(Slot
->effect
.Params
.Reverb
.Diffusion
, &x
, &y
);
1160 // Then divide x into y to simplify the matrix calculation.
1161 State
->Late
.MixCoeff
= y
/ x
;
1163 // If the HF limit parameter is flagged, calculate an appropriate limit
1164 // based on the air absorption parameter.
1165 hfRatio
= Slot
->effect
.Params
.Reverb
.DecayHFRatio
;
1166 if(Slot
->effect
.Params
.Reverb
.DecayHFLimit
&&
1167 Slot
->effect
.Params
.Reverb
.AirAbsorptionGainHF
< 1.0f
)
1168 hfRatio
= CalcLimitedHfRatio(hfRatio
,
1169 Slot
->effect
.Params
.Reverb
.AirAbsorptionGainHF
,
1170 Slot
->effect
.Params
.Reverb
.DecayTime
);
1172 // Update the late lines.
1173 UpdateLateLines(Slot
->effect
.Params
.Reverb
.Gain
, Slot
->effect
.Params
.Reverb
.LateReverbGain
,
1174 x
, Slot
->effect
.Params
.Reverb
.Density
, Slot
->effect
.Params
.Reverb
.DecayTime
,
1175 Slot
->effect
.Params
.Reverb
.Diffusion
, hfRatio
, cw
, frequency
, State
);
1177 // Update the echo line.
1178 UpdateEchoLine(Slot
->effect
.Params
.Reverb
.Gain
, Slot
->effect
.Params
.Reverb
.LateReverbGain
,
1179 Slot
->effect
.Params
.Reverb
.EchoTime
, Slot
->effect
.Params
.Reverb
.DecayTime
,
1180 Slot
->effect
.Params
.Reverb
.Diffusion
, Slot
->effect
.Params
.Reverb
.EchoDepth
,
1181 hfRatio
, cw
, frequency
, State
);
1183 // Update early and late 3D panning.
1184 Update3DPanning(Context
->Device
, Slot
->effect
.Params
.Reverb
.ReflectionsPan
,
1185 Slot
->effect
.Params
.Reverb
.LateReverbPan
, Slot
->Gain
, State
);
1188 // This processes the reverb state, given the input samples and an output
1190 static ALvoid
VerbProcess(ALeffectState
*effect
, const ALeffectslot
*Slot
, ALuint SamplesToDo
, const ALfloat
*SamplesIn
, ALfloat (*SamplesOut
)[MAXCHANNELS
])
1192 ALverbState
*State
= (ALverbState
*)effect
;
1194 ALfloat early
[4], late
[4], out
[4];
1195 const ALfloat
*panGain
= State
->Gain
;
1198 for(index
= 0;index
< SamplesToDo
;index
++)
1200 // Process reverb for this sample.
1201 VerbPass(State
, SamplesIn
[index
], early
, late
);
1203 // Mix early reflections and late reverb.
1204 out
[0] = (early
[0] + late
[0]);
1205 out
[1] = (early
[1] + late
[1]);
1206 out
[2] = (early
[2] + late
[2]);
1207 out
[3] = (early
[3] + late
[3]);
1209 // Output the results.
1210 SamplesOut
[index
][FRONT_LEFT
] += panGain
[FRONT_LEFT
] * out
[0];
1211 SamplesOut
[index
][FRONT_RIGHT
] += panGain
[FRONT_RIGHT
] * out
[1];
1212 SamplesOut
[index
][FRONT_CENTER
] += panGain
[FRONT_CENTER
] * out
[3];
1213 SamplesOut
[index
][SIDE_LEFT
] += panGain
[SIDE_LEFT
] * out
[0];
1214 SamplesOut
[index
][SIDE_RIGHT
] += panGain
[SIDE_RIGHT
] * out
[1];
1215 SamplesOut
[index
][BACK_LEFT
] += panGain
[BACK_LEFT
] * out
[0];
1216 SamplesOut
[index
][BACK_RIGHT
] += panGain
[BACK_RIGHT
] * out
[1];
1217 SamplesOut
[index
][BACK_CENTER
] += panGain
[BACK_CENTER
] * out
[2];
1221 // This processes the EAX reverb state, given the input samples and an output
1223 static ALvoid
EAXVerbProcess(ALeffectState
*effect
, const ALeffectslot
*Slot
, ALuint SamplesToDo
, const ALfloat
*SamplesIn
, ALfloat (*SamplesOut
)[MAXCHANNELS
])
1225 ALverbState
*State
= (ALverbState
*)effect
;
1227 ALfloat early
[4], late
[4];
1230 for(index
= 0;index
< SamplesToDo
;index
++)
1232 // Process reverb for this sample.
1233 EAXVerbPass(State
, SamplesIn
[index
], early
, late
);
1235 // Unfortunately, while the number and configuration of gains for
1236 // panning adjust according to MAXCHANNELS, the output from the
1237 // reverb engine is not so scalable.
1238 SamplesOut
[index
][FRONT_LEFT
] +=
1239 (State
->Early
.PanGain
[FRONT_LEFT
]*early
[0] +
1240 State
->Late
.PanGain
[FRONT_LEFT
]*late
[0]);
1241 SamplesOut
[index
][FRONT_RIGHT
] +=
1242 (State
->Early
.PanGain
[FRONT_RIGHT
]*early
[1] +
1243 State
->Late
.PanGain
[FRONT_RIGHT
]*late
[1]);
1244 SamplesOut
[index
][FRONT_CENTER
] +=
1245 (State
->Early
.PanGain
[FRONT_CENTER
]*early
[3] +
1246 State
->Late
.PanGain
[FRONT_CENTER
]*late
[3]);
1247 SamplesOut
[index
][SIDE_LEFT
] +=
1248 (State
->Early
.PanGain
[SIDE_LEFT
]*early
[0] +
1249 State
->Late
.PanGain
[SIDE_LEFT
]*late
[0]);
1250 SamplesOut
[index
][SIDE_RIGHT
] +=
1251 (State
->Early
.PanGain
[SIDE_RIGHT
]*early
[1] +
1252 State
->Late
.PanGain
[SIDE_RIGHT
]*late
[1]);
1253 SamplesOut
[index
][BACK_LEFT
] +=
1254 (State
->Early
.PanGain
[BACK_LEFT
]*early
[0] +
1255 State
->Late
.PanGain
[BACK_LEFT
]*late
[0]);
1256 SamplesOut
[index
][BACK_RIGHT
] +=
1257 (State
->Early
.PanGain
[BACK_RIGHT
]*early
[1] +
1258 State
->Late
.PanGain
[BACK_RIGHT
]*late
[1]);
1259 SamplesOut
[index
][BACK_CENTER
] +=
1260 (State
->Early
.PanGain
[BACK_CENTER
]*early
[2] +
1261 State
->Late
.PanGain
[BACK_CENTER
]*late
[2]);
1265 // This creates the reverb state. It should be called only when the reverb
1266 // effect is loaded into a slot that doesn't already have a reverb effect.
1267 ALeffectState
*VerbCreate(void)
1269 ALverbState
*State
= NULL
;
1272 State
= malloc(sizeof(ALverbState
));
1276 State
->state
.Destroy
= VerbDestroy
;
1277 State
->state
.DeviceUpdate
= VerbDeviceUpdate
;
1278 State
->state
.Update
= VerbUpdate
;
1279 State
->state
.Process
= VerbProcess
;
1281 State
->TotalSamples
= 0;
1282 State
->SampleBuffer
= NULL
;
1284 State
->LpFilter
.coeff
= 0.0f
;
1285 State
->LpFilter
.history
[0] = 0.0f
;
1286 State
->LpFilter
.history
[1] = 0.0f
;
1288 State
->Mod
.Delay
.Mask
= 0;
1289 State
->Mod
.Delay
.Line
= NULL
;
1290 State
->Mod
.Index
= 0;
1291 State
->Mod
.Range
= 1;
1292 State
->Mod
.Depth
= 0.0f
;
1293 State
->Mod
.Coeff
= 0.0f
;
1294 State
->Mod
.Filter
= 0.0f
;
1296 State
->Delay
.Mask
= 0;
1297 State
->Delay
.Line
= NULL
;
1298 State
->DelayTap
[0] = 0;
1299 State
->DelayTap
[1] = 0;
1301 State
->Early
.Gain
= 0.0f
;
1302 for(index
= 0;index
< 4;index
++)
1304 State
->Early
.Coeff
[index
] = 0.0f
;
1305 State
->Early
.Delay
[index
].Mask
= 0;
1306 State
->Early
.Delay
[index
].Line
= NULL
;
1307 State
->Early
.Offset
[index
] = 0;
1310 State
->Decorrelator
.Mask
= 0;
1311 State
->Decorrelator
.Line
= NULL
;
1312 State
->DecoTap
[0] = 0;
1313 State
->DecoTap
[1] = 0;
1314 State
->DecoTap
[2] = 0;
1316 State
->Late
.Gain
= 0.0f
;
1317 State
->Late
.DensityGain
= 0.0f
;
1318 State
->Late
.ApFeedCoeff
= 0.0f
;
1319 State
->Late
.MixCoeff
= 0.0f
;
1320 for(index
= 0;index
< 4;index
++)
1322 State
->Late
.ApCoeff
[index
] = 0.0f
;
1323 State
->Late
.ApDelay
[index
].Mask
= 0;
1324 State
->Late
.ApDelay
[index
].Line
= NULL
;
1325 State
->Late
.ApOffset
[index
] = 0;
1327 State
->Late
.Coeff
[index
] = 0.0f
;
1328 State
->Late
.Delay
[index
].Mask
= 0;
1329 State
->Late
.Delay
[index
].Line
= NULL
;
1330 State
->Late
.Offset
[index
] = 0;
1332 State
->Late
.LpCoeff
[index
] = 0.0f
;
1333 State
->Late
.LpSample
[index
] = 0.0f
;
1336 for(index
= 0;index
< MAXCHANNELS
;index
++)
1338 State
->Early
.PanGain
[index
] = 0.0f
;
1339 State
->Late
.PanGain
[index
] = 0.0f
;
1342 State
->Echo
.DensityGain
= 0.0f
;
1343 State
->Echo
.Delay
.Mask
= 0;
1344 State
->Echo
.Delay
.Line
= NULL
;
1345 State
->Echo
.ApDelay
.Mask
= 0;
1346 State
->Echo
.ApDelay
.Line
= NULL
;
1347 State
->Echo
.Coeff
= 0.0f
;
1348 State
->Echo
.ApFeedCoeff
= 0.0f
;
1349 State
->Echo
.ApCoeff
= 0.0f
;
1350 State
->Echo
.Offset
= 0;
1351 State
->Echo
.ApOffset
= 0;
1352 State
->Echo
.LpCoeff
= 0.0f
;
1353 State
->Echo
.LpSample
= 0.0f
;
1354 State
->Echo
.MixCoeff
[0] = 0.0f
;
1355 State
->Echo
.MixCoeff
[1] = 0.0f
;
1359 State
->Gain
= State
->Late
.PanGain
;
1361 return &State
->state
;
1364 ALeffectState
*EAXVerbCreate(void)
1366 ALeffectState
*State
= VerbCreate();
1367 if(State
&& EmulateEAXReverb
== AL_FALSE
)
1369 State
->DeviceUpdate
= EAXVerbDeviceUpdate
;
1370 State
->Update
= EAXVerbUpdate
;
1371 State
->Process
= EAXVerbProcess
;