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
29 #include "alAuxEffectSlot.h"
34 typedef struct DelayLine
36 // The delay lines use sample lengths that are powers of 2 to allow
37 // bitmasking instead of modulus wrapping.
42 typedef struct ALverbState
{
43 // Must be first in all effects!
46 // All delay lines are allocated as a single buffer to reduce memory
47 // fragmentation and management code.
48 ALfloat
*SampleBuffer
;
50 // Master effect low-pass filter (2 chained 1-pole filters).
53 // Initial effect delay and decorrelation.
55 // The tap points for the initial delay. First tap goes to early
56 // reflections, the last four decorrelate to late reverb.
59 // Total gain for early reflections.
61 // Early reflections are done with 4 delay lines.
65 // The gain for each output channel based on 3D panning.
66 ALfloat PanGain
[OUTPUTCHANNELS
];
69 // Total gain for late reverb.
71 // Attenuation to compensate for modal density and decay rate.
73 // The feed-back and feed-forward all-pass coefficient.
75 // Mixing matrix coefficient.
77 // Late reverb has 4 parallel all-pass filters.
81 // In addition to 4 cyclical delay lines.
85 // The cyclical delay lines are 1-pole low-pass filtered.
88 // The gain for each output channel based on 3D panning.
89 ALfloat PanGain
[OUTPUTCHANNELS
];
91 // The current read offset for all delay lines.
95 // All delay line lengths are specified in seconds.
97 // The lengths of the early delay lines.
98 static const ALfloat EARLY_LINE_LENGTH
[4] =
100 0.0015f
, 0.0045f
, 0.0135f
, 0.0405f
103 // The lengths of the late all-pass delay lines.
104 static const ALfloat ALLPASS_LINE_LENGTH
[4] =
106 0.0151f
, 0.0167f
, 0.0183f
, 0.0200f
,
109 // The lengths of the late cyclical delay lines.
110 static const ALfloat LATE_LINE_LENGTH
[4] =
112 0.0211f
, 0.0311f
, 0.0461f
, 0.0680f
115 // The late cyclical delay lines have a variable length dependent on the
116 // effect's density parameter (inverted for some reason) and this multiplier.
117 static const ALfloat LATE_LINE_MULTIPLIER
= 4.0f
;
119 // Input into the late reverb is decorrelated between four channels. Their
120 // timings are dependent on a fraction and multiplier. See VerbUpdate() for
121 // the calculations involved.
122 static const ALfloat DECO_FRACTION
= 1.0f
/ 32.0f
;
123 static const ALfloat DECO_MULTIPLIER
= 2.0f
;
125 // The maximum length of initial delay for the master delay line (a sum of
126 // the maximum early reflection and late reverb delays).
127 static const ALfloat MASTER_LINE_LENGTH
= 0.3f
+ 0.1f
;
129 static ALuint
CalcLengths(ALuint length
[13], ALuint frequency
)
131 ALuint samples
, totalLength
, index
;
133 // All line lengths are powers of 2, calculated from their lengths, with
134 // an additional sample in case of rounding errors.
136 // See VerbUpdate() for an explanation of the additional calculation
137 // added to the master line length.
139 ((MASTER_LINE_LENGTH
+
140 (LATE_LINE_LENGTH
[0] * (1.0f
+ LATE_LINE_MULTIPLIER
) *
141 (DECO_FRACTION
* ((DECO_MULTIPLIER
* DECO_MULTIPLIER
*
142 DECO_MULTIPLIER
) - 1.0f
)))) *
144 length
[0] = NextPowerOf2(samples
);
145 totalLength
= length
[0];
146 for(index
= 0;index
< 4;index
++)
148 samples
= (ALuint
)(EARLY_LINE_LENGTH
[index
] * frequency
) + 1;
149 length
[1 + index
] = NextPowerOf2(samples
);
150 totalLength
+= length
[1 + index
];
152 for(index
= 0;index
< 4;index
++)
154 samples
= (ALuint
)(ALLPASS_LINE_LENGTH
[index
] * frequency
) + 1;
155 length
[5 + index
] = NextPowerOf2(samples
);
156 totalLength
+= length
[5 + index
];
158 for(index
= 0;index
< 4;index
++)
160 samples
= (ALuint
)(LATE_LINE_LENGTH
[index
] *
161 (1.0f
+ LATE_LINE_MULTIPLIER
) * frequency
) + 1;
162 length
[9 + index
] = NextPowerOf2(samples
);
163 totalLength
+= length
[9 + index
];
169 // Basic delay line input/output routines.
170 static __inline ALfloat
DelayLineOut(DelayLine
*Delay
, ALuint offset
)
172 return Delay
->Line
[offset
&Delay
->Mask
];
175 static __inline ALvoid
DelayLineIn(DelayLine
*Delay
, ALuint offset
, ALfloat in
)
177 Delay
->Line
[offset
&Delay
->Mask
] = in
;
180 // Delay line output routine for early reflections.
181 static __inline ALfloat
EarlyDelayLineOut(ALverbState
*State
, ALuint index
)
183 return State
->Early
.Coeff
[index
] *
184 DelayLineOut(&State
->Early
.Delay
[index
],
185 State
->Offset
- State
->Early
.Offset
[index
]);
188 // Given an input sample, this function produces stereo output for early
190 static __inline ALvoid
EarlyReflection(ALverbState
*State
, ALfloat in
, ALfloat
*out
)
192 ALfloat d
[4], v
, f
[4];
194 // Obtain the decayed results of each early delay line.
195 d
[0] = EarlyDelayLineOut(State
, 0);
196 d
[1] = EarlyDelayLineOut(State
, 1);
197 d
[2] = EarlyDelayLineOut(State
, 2);
198 d
[3] = EarlyDelayLineOut(State
, 3);
200 /* The following uses a lossless scattering junction from waveguide
201 * theory. It actually amounts to a householder mixing matrix, which
202 * will produce a maximally diffuse response, and means this can probably
203 * be considered a simple feedback delay network (FDN).
211 v
= (d
[0] + d
[1] + d
[2] + d
[3]) * 0.5f
;
212 // The junction is loaded with the input here.
215 // Calculate the feed values for the delay lines.
221 // Refeed the delay lines.
222 DelayLineIn(&State
->Early
.Delay
[0], State
->Offset
, f
[0]);
223 DelayLineIn(&State
->Early
.Delay
[1], State
->Offset
, f
[1]);
224 DelayLineIn(&State
->Early
.Delay
[2], State
->Offset
, f
[2]);
225 DelayLineIn(&State
->Early
.Delay
[3], State
->Offset
, f
[3]);
227 // Output the results of the junction for all four lines.
228 out
[0] = State
->Early
.Gain
* f
[0];
229 out
[1] = State
->Early
.Gain
* f
[1];
230 out
[2] = State
->Early
.Gain
* f
[2];
231 out
[3] = State
->Early
.Gain
* f
[3];
234 // All-pass input/output routine for late reverb.
235 static __inline ALfloat
LateAllPassInOut(ALverbState
*State
, ALuint index
, ALfloat in
)
239 out
= State
->Late
.ApCoeff
[index
] *
240 DelayLineOut(&State
->Late
.ApDelay
[index
],
241 State
->Offset
- State
->Late
.ApOffset
[index
]);
242 out
-= (State
->Late
.ApFeedCoeff
* in
);
243 DelayLineIn(&State
->Late
.ApDelay
[index
], State
->Offset
,
244 (State
->Late
.ApFeedCoeff
* out
) + in
);
248 // Delay line output routine for late reverb.
249 static __inline ALfloat
LateDelayLineOut(ALverbState
*State
, ALuint index
)
251 return State
->Late
.Coeff
[index
] *
252 DelayLineOut(&State
->Late
.Delay
[index
],
253 State
->Offset
- State
->Late
.Offset
[index
]);
256 // Low-pass filter input/output routine for late reverb.
257 static __inline ALfloat
LateLowPassInOut(ALverbState
*State
, ALuint index
, ALfloat in
)
259 State
->Late
.LpSample
[index
] = in
+
260 ((State
->Late
.LpSample
[index
] - in
) * State
->Late
.LpCoeff
[index
]);
261 return State
->Late
.LpSample
[index
];
264 // Given four decorrelated input samples, this function produces stereo
265 // output for late reverb.
266 static __inline ALvoid
LateReverb(ALverbState
*State
, ALfloat
*in
, ALfloat
*out
)
270 // Obtain the decayed results of the cyclical delay lines, and add the
271 // corresponding input channels attenuated by density. Then pass the
272 // results through the low-pass filters.
273 d
[0] = LateLowPassInOut(State
, 0, (State
->Late
.DensityGain
* in
[0]) +
274 LateDelayLineOut(State
, 0));
275 d
[1] = LateLowPassInOut(State
, 1, (State
->Late
.DensityGain
* in
[1]) +
276 LateDelayLineOut(State
, 1));
277 d
[2] = LateLowPassInOut(State
, 2, (State
->Late
.DensityGain
* in
[2]) +
278 LateDelayLineOut(State
, 2));
279 d
[3] = LateLowPassInOut(State
, 3, (State
->Late
.DensityGain
* in
[3]) +
280 LateDelayLineOut(State
, 3));
282 // To help increase diffusion, run each line through an all-pass filter.
283 // The order of the all-pass filters is selected so that the shortest
284 // all-pass filter will feed the shortest delay line.
285 d
[0] = LateAllPassInOut(State
, 1, d
[0]);
286 d
[1] = LateAllPassInOut(State
, 3, d
[1]);
287 d
[2] = LateAllPassInOut(State
, 0, d
[2]);
288 d
[3] = LateAllPassInOut(State
, 2, d
[3]);
290 /* Late reverb is done with a modified feedback delay network (FDN)
291 * topology. Four input lines are each fed through their own all-pass
292 * filter and then into the mixing matrix. The four outputs of the
293 * mixing matrix are then cycled back to the inputs. Each output feeds
294 * a different input to form a circlular feed cycle.
296 * The mixing matrix used is a 4D skew-symmetric rotation matrix derived
297 * using a single unitary rotational parameter:
299 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
304 * The rotation is constructed from the effect's diffusion parameter,
305 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
306 * with differing signs, and d is the coefficient x. The matrix is thus:
308 * [ x, y, -y, y ] x = 1 - (0.5 diffusion^3)
309 * [ -y, x, y, y ] y = sqrt((1 - x^2) / 3)
313 * To reduce the number of multiplies, the x coefficient is applied with
314 * the cyclical delay line coefficients. Thus only the y coefficient is
315 * applied when mixing, and is modified to be: y / x.
317 f
[0] = d
[0] + (State
->Late
.MixCoeff
* ( d
[1] - d
[2] + d
[3]));
318 f
[1] = d
[1] + (State
->Late
.MixCoeff
* (-d
[0] + d
[2] + d
[3]));
319 f
[2] = d
[2] + (State
->Late
.MixCoeff
* ( d
[0] - d
[1] + d
[3]));
320 f
[3] = d
[3] + (State
->Late
.MixCoeff
* (-d
[0] - d
[1] - d
[2]));
322 // Output the results of the matrix for all four cyclical delay lines,
323 // attenuated by the late reverb gain (which is attenuated by the 'x'
325 out
[0] = State
->Late
.Gain
* f
[0];
326 out
[1] = State
->Late
.Gain
* f
[1];
327 out
[2] = State
->Late
.Gain
* f
[2];
328 out
[3] = State
->Late
.Gain
* f
[3];
330 // The delay lines are fed circularly in the order:
331 // 0 -> 1 -> 3 -> 2 -> 0 ...
332 DelayLineIn(&State
->Late
.Delay
[0], State
->Offset
, f
[2]);
333 DelayLineIn(&State
->Late
.Delay
[1], State
->Offset
, f
[0]);
334 DelayLineIn(&State
->Late
.Delay
[2], State
->Offset
, f
[3]);
335 DelayLineIn(&State
->Late
.Delay
[3], State
->Offset
, f
[1]);
338 // Process the reverb for a given input sample, resulting in separate four-
339 // channel output for both early reflections and late reverb.
340 static __inline ALvoid
ReverbInOut(ALverbState
*State
, ALfloat in
, ALfloat
*early
, ALfloat
*late
)
344 // Low-pass filter the incoming sample.
345 in
= lpFilter2P(&State
->LpFilter
, 0, in
);
347 // Feed the initial delay line.
348 DelayLineIn(&State
->Delay
, State
->Offset
, in
);
350 // Calculate the early reflection from the first delay tap.
351 in
= DelayLineOut(&State
->Delay
, State
->Offset
- State
->Tap
[0]);
352 EarlyReflection(State
, in
, early
);
354 // Calculate the late reverb from the last four delay taps.
355 taps
[0] = DelayLineOut(&State
->Delay
, State
->Offset
- State
->Tap
[1]);
356 taps
[1] = DelayLineOut(&State
->Delay
, State
->Offset
- State
->Tap
[2]);
357 taps
[2] = DelayLineOut(&State
->Delay
, State
->Offset
- State
->Tap
[3]);
358 taps
[3] = DelayLineOut(&State
->Delay
, State
->Offset
- State
->Tap
[4]);
359 LateReverb(State
, taps
, late
);
361 // Step all delays forward one sample.
365 // This destroys the reverb state. It should be called only when the effect
366 // slot has a different (or no) effect loaded over the reverb effect.
367 ALvoid
VerbDestroy(ALeffectState
*effect
)
369 ALverbState
*State
= (ALverbState
*)effect
;
372 free(State
->SampleBuffer
);
373 State
->SampleBuffer
= NULL
;
378 // This updates the device-dependant reverb state. This is called on
379 // initialization and any time the device parameters (eg. playback frequency,
380 // format) have been changed.
381 ALboolean
VerbDeviceUpdate(ALeffectState
*effect
, ALCdevice
*Device
)
383 ALverbState
*State
= (ALverbState
*)effect
;
384 ALuint length
[13], totalLength
;
387 totalLength
= CalcLengths(length
, Device
->Frequency
);
388 if(totalLength
!= State
->TotalLength
)
392 temp
= realloc(State
->SampleBuffer
, totalLength
* sizeof(ALfloat
));
395 alSetError(AL_OUT_OF_MEMORY
);
398 State
->TotalLength
= totalLength
;
399 State
->SampleBuffer
= temp
;
401 // All lines share a single sample buffer
402 State
->Delay
.Mask
= length
[0] - 1;
403 State
->Delay
.Line
= &State
->SampleBuffer
[0];
404 totalLength
= length
[0];
405 for(index
= 0;index
< 4;index
++)
407 State
->Early
.Delay
[index
].Mask
= length
[1 + index
] - 1;
408 State
->Early
.Delay
[index
].Line
= &State
->SampleBuffer
[totalLength
];
409 totalLength
+= length
[1 + index
];
411 for(index
= 0;index
< 4;index
++)
413 State
->Late
.ApDelay
[index
].Mask
= length
[5 + index
] - 1;
414 State
->Late
.ApDelay
[index
].Line
= &State
->SampleBuffer
[totalLength
];
415 totalLength
+= length
[5 + index
];
417 for(index
= 0;index
< 4;index
++)
419 State
->Late
.Delay
[index
].Mask
= length
[9 + index
] - 1;
420 State
->Late
.Delay
[index
].Line
= &State
->SampleBuffer
[totalLength
];
421 totalLength
+= length
[9 + index
];
425 for(index
= 0;index
< 4;index
++)
427 State
->Early
.Offset
[index
] = (ALuint
)(EARLY_LINE_LENGTH
[index
] *
429 State
->Late
.ApOffset
[index
] = (ALuint
)(ALLPASS_LINE_LENGTH
[index
] *
433 for(index
= 0;index
< State
->TotalLength
;index
++)
434 State
->SampleBuffer
[index
] = 0.0f
;
439 // This updates the reverb state. This is called any time the reverb effect
440 // is loaded into a slot.
441 ALvoid
VerbUpdate(ALeffectState
*effect
, ALCcontext
*Context
, const ALeffect
*Effect
)
443 ALverbState
*State
= (ALverbState
*)effect
;
444 ALuint frequency
= Context
->Device
->Frequency
;
446 ALfloat length
, mixCoeff
, cw
, g
, coeff
;
447 ALfloat hfRatio
= Effect
->Reverb
.DecayHFRatio
;
449 // Calculate the master low-pass filter (from the master effect HF gain).
450 cw
= cos(2.0*M_PI
* Effect
->Reverb
.HFReference
/ frequency
);
451 g
= __max(Effect
->Reverb
.GainHF
, 0.0001f
);
452 State
->LpFilter
.coeff
= 0.0f
;
453 if(g
< 0.9999f
) // 1-epsilon
454 State
->LpFilter
.coeff
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) / (1 - g
);
456 // Calculate the initial delay taps.
457 length
= Effect
->Reverb
.ReflectionsDelay
;
458 State
->Tap
[0] = (ALuint
)(length
* frequency
);
460 length
+= Effect
->Reverb
.LateReverbDelay
;
462 /* The four inputs to the late reverb are decorrelated to smooth the
463 * initial reverb and reduce harsh echos. The timings are calculated as
464 * multiples of a fraction of the smallest cyclical delay time. This
465 * result is then adjusted so that the first tap occurs immediately (all
466 * taps are reduced by the shortest fraction).
468 * offset[index] = ((FRACTION MULTIPLIER^index) - 1) delay
470 for(index
= 0;index
< 4;index
++)
472 length
+= LATE_LINE_LENGTH
[0] *
473 (1.0f
+ (Effect
->Reverb
.Density
* LATE_LINE_MULTIPLIER
)) *
474 (DECO_FRACTION
* (pow(DECO_MULTIPLIER
, (ALfloat
)index
) - 1.0f
));
475 State
->Tap
[1 + index
] = (ALuint
)(length
* frequency
);
478 // Calculate the early reflections gain (from the master effect gain, and
479 // reflections gain parameters).
480 State
->Early
.Gain
= Effect
->Reverb
.Gain
* Effect
->Reverb
.ReflectionsGain
;
482 // Calculate the gain (coefficient) for each early delay line.
483 for(index
= 0;index
< 4;index
++)
484 State
->Early
.Coeff
[index
] = pow(10.0f
, EARLY_LINE_LENGTH
[index
] /
485 Effect
->Reverb
.LateReverbDelay
*
488 // Calculate the first mixing matrix coefficient (x).
489 mixCoeff
= 1.0f
- (0.5f
* pow(Effect
->Reverb
.Diffusion
, 3.0f
));
491 // Calculate the late reverb gain (from the master effect gain, and late
492 // reverb gain parameters). Since the output is tapped prior to the
493 // application of the delay line coefficients, this gain needs to be
494 // attenuated by the 'x' mix coefficient from above.
495 State
->Late
.Gain
= Effect
->Reverb
.Gain
* Effect
->Reverb
.LateReverbGain
* mixCoeff
;
497 /* To compensate for changes in modal density and decay time of the late
498 * reverb signal, the input is attenuated based on the maximal energy of
499 * the outgoing signal. This is calculated as the ratio between a
500 * reference value and the current approximation of energy for the output
503 * Reverb output matches exponential decay of the form Sum(a^n), where a
504 * is the attenuation coefficient, and n is the sample ranging from 0 to
505 * infinity. The signal energy can thus be approximated using the area
506 * under this curve, calculated as: 1 / (1 - a).
508 * The reference energy is calculated from a signal at the lowest (effect
509 * at 1.0) density with a decay time of one second.
511 * The coefficient is calculated as the average length of the cyclical
512 * delay lines. This produces a better result than calculating the gain
513 * for each line individually (most likely a side effect of diffusion).
515 * The final result is the square root of the ratio bound to a maximum
516 * value of 1 (no amplification).
518 length
= (LATE_LINE_LENGTH
[0] + LATE_LINE_LENGTH
[1] +
519 LATE_LINE_LENGTH
[2] + LATE_LINE_LENGTH
[3]);
520 g
= length
* (1.0f
+ LATE_LINE_MULTIPLIER
) * 0.25f
;
521 g
= pow(10.0f
, g
* -60.0f
/ 20.0f
);
522 g
= 1.0f
/ (1.0f
- (g
* g
));
523 length
*= 1.0f
+ (Effect
->Reverb
.Density
* LATE_LINE_MULTIPLIER
) * 0.25f
;
524 length
= pow(10.0f
, length
/ Effect
->Reverb
.DecayTime
* -60.0f
/ 20.0f
);
525 length
= 1.0f
/ (1.0f
- (length
* length
));
526 State
->Late
.DensityGain
= __min(aluSqrt(g
/ length
), 1.0f
);
528 // Calculate the all-pass feed-back and feed-forward coefficient.
529 State
->Late
.ApFeedCoeff
= 0.6f
* pow(Effect
->Reverb
.Diffusion
, 3.0f
);
531 // Calculate the mixing matrix coefficient (y / x).
532 g
= aluSqrt((1.0f
- (mixCoeff
* mixCoeff
)) / 3.0f
);
533 State
->Late
.MixCoeff
= g
/ mixCoeff
;
535 for(index
= 0;index
< 4;index
++)
537 // Calculate the gain (coefficient) for each all-pass line.
538 State
->Late
.ApCoeff
[index
] = pow(10.0f
, ALLPASS_LINE_LENGTH
[index
] /
539 Effect
->Reverb
.DecayTime
*
543 // If the HF limit parameter is flagged, calculate an appropriate limit
544 // based on the air absorption parameter.
545 if(Effect
->Reverb
.DecayHFLimit
&& Effect
->Reverb
.AirAbsorptionGainHF
< 1.0f
)
549 // For each of the cyclical delays, find the attenuation due to air
550 // absorption in dB (converting delay time to meters using the speed
551 // of sound). Then reversing the decay equation, solve for HF ratio.
552 // The delay length is cancelled out of the equation, so it can be
553 // calculated once for all lines.
554 limitRatio
= 1.0f
/ (log10(Effect
->Reverb
.AirAbsorptionGainHF
) *
555 SPEEDOFSOUNDMETRESPERSEC
*
556 Effect
->Reverb
.DecayTime
/ -60.0f
* 20.0f
);
557 // Need to limit the result to a minimum of 0.1, just like the HF
559 limitRatio
= __max(limitRatio
, 0.1f
);
561 // Using the limit calculated above, apply the upper bound to the
563 hfRatio
= __min(hfRatio
, limitRatio
);
566 // Calculate the low-pass filter frequency.
567 cw
= cos(2.0*M_PI
* Effect
->Reverb
.HFReference
/ frequency
);
569 for(index
= 0;index
< 4;index
++)
571 // Calculate the length (in seconds) of each cyclical delay line.
572 length
= LATE_LINE_LENGTH
[index
] * (1.0f
+ (Effect
->Reverb
.Density
*
573 LATE_LINE_MULTIPLIER
));
574 // Calculate the delay offset for the cyclical delay lines.
575 State
->Late
.Offset
[index
] = (ALuint
)(length
* frequency
);
577 // Calculate the gain (coefficient) for each cyclical line.
578 State
->Late
.Coeff
[index
] = pow(10.0f
, length
/ Effect
->Reverb
.DecayTime
*
581 // Eventually this should boost the high frequencies when the ratio
586 // Calculate the decay equation for each low-pass filter.
587 g
= pow(10.0f
, length
/ (Effect
->Reverb
.DecayTime
* hfRatio
) *
588 -60.0f
/ 20.0f
) / State
->Late
.Coeff
[index
];
592 // Calculate the gain (coefficient) for each low-pass filter.
593 if(g
< 0.9999f
) // 1-epsilon
594 coeff
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) / (1 - g
);
596 // Very low decay times will produce minimal output, so apply an
597 // upper bound to the coefficient.
598 coeff
= __min(coeff
, 0.98f
);
600 State
->Late
.LpCoeff
[index
] = coeff
;
602 // Attenuate the cyclical line coefficients by the mixing coefficient
604 State
->Late
.Coeff
[index
] *= mixCoeff
;
607 // Calculate the 3D-panning gains for the early reflections and late
608 // reverb (for EAX mode).
610 ALfloat earlyPan
[3] = { Effect
->Reverb
.ReflectionsPan
[0], Effect
->Reverb
.ReflectionsPan
[1], Effect
->Reverb
.ReflectionsPan
[2] };
611 ALfloat latePan
[3] = { Effect
->Reverb
.LateReverbPan
[0], Effect
->Reverb
.LateReverbPan
[1], Effect
->Reverb
.LateReverbPan
[2] };
612 ALfloat
*speakerGain
, dirGain
, ambientGain
;
616 length
= earlyPan
[0]*earlyPan
[0] + earlyPan
[1]*earlyPan
[1] + earlyPan
[2]*earlyPan
[2];
619 length
= 1.0f
/ aluSqrt(length
);
620 earlyPan
[0] *= length
;
621 earlyPan
[1] *= length
;
622 earlyPan
[2] *= length
;
624 length
= latePan
[0]*latePan
[0] + latePan
[1]*latePan
[1] + latePan
[2]*latePan
[2];
627 length
= 1.0f
/ aluSqrt(length
);
628 latePan
[0] *= length
;
629 latePan
[1] *= length
;
630 latePan
[2] *= length
;
633 // This code applies directional reverb just like the mixer applies
634 // directional sources. It diffuses the sound toward all speakers
635 // as the magnitude of the panning vector drops, which is only an
636 // approximation of the expansion of sound across the speakers from
637 // the panning direction.
638 pos
= aluCart2LUTpos(earlyPan
[2], earlyPan
[0]);
639 speakerGain
= &Context
->PanningLUT
[OUTPUTCHANNELS
* pos
];
640 dirGain
= aluSqrt((earlyPan
[0] * earlyPan
[0]) + (earlyPan
[2] * earlyPan
[2]));
641 ambientGain
= (1.0 - dirGain
);
642 for(index
= 0;index
< OUTPUTCHANNELS
;index
++)
643 State
->Early
.PanGain
[index
] = dirGain
* speakerGain
[index
] + ambientGain
;
645 pos
= aluCart2LUTpos(latePan
[2], latePan
[0]);
646 speakerGain
= &Context
->PanningLUT
[OUTPUTCHANNELS
* pos
];
647 dirGain
= aluSqrt((latePan
[0] * latePan
[0]) + (latePan
[2] * latePan
[2]));
648 ambientGain
= (1.0 - dirGain
);
649 for(index
= 0;index
< OUTPUTCHANNELS
;index
++)
650 State
->Late
.PanGain
[index
] = dirGain
* speakerGain
[index
] + ambientGain
;
654 // This processes the reverb state, given the input samples and an output
656 ALvoid
VerbProcess(ALeffectState
*effect
, const ALeffectslot
*Slot
, ALuint SamplesToDo
, const ALfloat
*SamplesIn
, ALfloat (*SamplesOut
)[OUTPUTCHANNELS
])
658 ALverbState
*State
= (ALverbState
*)effect
;
660 ALfloat early
[4], late
[4], out
[4];
661 ALfloat gain
= Slot
->Gain
;
663 for(index
= 0;index
< SamplesToDo
;index
++)
665 // Process reverb for this sample.
666 ReverbInOut(State
, SamplesIn
[index
], early
, late
);
668 // Mix early reflections and late reverb.
669 out
[0] = (early
[0] + late
[0]) * gain
;
670 out
[1] = (early
[1] + late
[1]) * gain
;
671 out
[2] = (early
[2] + late
[2]) * gain
;
672 out
[3] = (early
[3] + late
[3]) * gain
;
674 // Output the results.
675 SamplesOut
[index
][FRONT_LEFT
] += out
[0];
676 SamplesOut
[index
][FRONT_RIGHT
] += out
[1];
677 SamplesOut
[index
][FRONT_CENTER
] += out
[3];
678 SamplesOut
[index
][SIDE_LEFT
] += out
[0];
679 SamplesOut
[index
][SIDE_RIGHT
] += out
[1];
680 SamplesOut
[index
][BACK_LEFT
] += out
[0];
681 SamplesOut
[index
][BACK_RIGHT
] += out
[1];
682 SamplesOut
[index
][BACK_CENTER
] += out
[2];
686 // This processes the EAX reverb state, given the input samples and an output
688 ALvoid
EAXVerbProcess(ALeffectState
*effect
, const ALeffectslot
*Slot
, ALuint SamplesToDo
, const ALfloat
*SamplesIn
, ALfloat (*SamplesOut
)[OUTPUTCHANNELS
])
690 ALverbState
*State
= (ALverbState
*)effect
;
692 ALfloat early
[4], late
[4];
693 ALfloat gain
= Slot
->Gain
;
695 for(index
= 0;index
< SamplesToDo
;index
++)
697 // Process reverb for this sample.
698 ReverbInOut(State
, SamplesIn
[index
], early
, late
);
700 // Unfortunately, while the number and configuration of gains for
701 // panning adjust according to OUTPUTCHANNELS, the output from the
702 // reverb engine is not so scalable.
703 SamplesOut
[index
][FRONT_LEFT
] +=
704 (State
->Early
.PanGain
[FRONT_LEFT
]*early
[0] +
705 State
->Late
.PanGain
[FRONT_LEFT
]*late
[0]) * gain
;
706 SamplesOut
[index
][FRONT_RIGHT
] +=
707 (State
->Early
.PanGain
[FRONT_RIGHT
]*early
[1] +
708 State
->Late
.PanGain
[FRONT_RIGHT
]*late
[1]) * gain
;
709 SamplesOut
[index
][FRONT_CENTER
] +=
710 (State
->Early
.PanGain
[FRONT_CENTER
]*early
[3] +
711 State
->Late
.PanGain
[FRONT_CENTER
]*late
[3]) * gain
;
712 SamplesOut
[index
][SIDE_LEFT
] +=
713 (State
->Early
.PanGain
[SIDE_LEFT
]*early
[0] +
714 State
->Late
.PanGain
[SIDE_LEFT
]*late
[0]) * gain
;
715 SamplesOut
[index
][SIDE_RIGHT
] +=
716 (State
->Early
.PanGain
[SIDE_RIGHT
]*early
[1] +
717 State
->Late
.PanGain
[SIDE_RIGHT
]*late
[1]) * gain
;
718 SamplesOut
[index
][BACK_LEFT
] +=
719 (State
->Early
.PanGain
[BACK_LEFT
]*early
[0] +
720 State
->Late
.PanGain
[BACK_LEFT
]*late
[0]) * gain
;
721 SamplesOut
[index
][BACK_RIGHT
] +=
722 (State
->Early
.PanGain
[BACK_RIGHT
]*early
[1] +
723 State
->Late
.PanGain
[BACK_RIGHT
]*late
[1]) * gain
;
724 SamplesOut
[index
][BACK_CENTER
] +=
725 (State
->Early
.PanGain
[BACK_CENTER
]*early
[2] +
726 State
->Late
.PanGain
[BACK_CENTER
]*late
[2]) * gain
;
730 // This creates the reverb state. It should be called only when the reverb
731 // effect is loaded into a slot that doesn't already have a reverb effect.
732 ALeffectState
*VerbCreate(void)
734 ALverbState
*State
= NULL
;
737 State
= malloc(sizeof(ALverbState
));
740 alSetError(AL_OUT_OF_MEMORY
);
744 State
->state
.Destroy
= VerbDestroy
;
745 State
->state
.DeviceUpdate
= VerbDeviceUpdate
;
746 State
->state
.Update
= VerbUpdate
;
747 State
->state
.Process
= VerbProcess
;
749 State
->TotalLength
= 0;
750 State
->SampleBuffer
= NULL
;
752 State
->LpFilter
.coeff
= 0.0f
;
753 State
->LpFilter
.history
[0] = 0.0f
;
754 State
->LpFilter
.history
[1] = 0.0f
;
755 State
->Delay
.Mask
= 0;
756 State
->Delay
.Line
= NULL
;
764 State
->Early
.Gain
= 0.0f
;
765 for(index
= 0;index
< 4;index
++)
767 State
->Early
.Coeff
[index
] = 0.0f
;
768 State
->Early
.Delay
[index
].Mask
= 0;
769 State
->Early
.Delay
[index
].Line
= NULL
;
770 State
->Early
.Offset
[index
] = 0;
773 State
->Late
.Gain
= 0.0f
;
774 State
->Late
.DensityGain
= 0.0f
;
775 State
->Late
.ApFeedCoeff
= 0.0f
;
776 State
->Late
.MixCoeff
= 0.0f
;
778 for(index
= 0;index
< 4;index
++)
780 State
->Late
.ApCoeff
[index
] = 0.0f
;
781 State
->Late
.ApDelay
[index
].Mask
= 0;
782 State
->Late
.ApDelay
[index
].Line
= NULL
;
783 State
->Late
.ApOffset
[index
] = 0;
785 State
->Late
.Coeff
[index
] = 0.0f
;
786 State
->Late
.Delay
[index
].Mask
= 0;
787 State
->Late
.Delay
[index
].Line
= NULL
;
788 State
->Late
.Offset
[index
] = 0;
790 State
->Late
.LpCoeff
[index
] = 0.0f
;
791 State
->Late
.LpSample
[index
] = 0.0f
;
794 // Panning is applied as an independent gain for each output channel.
795 for(index
= 0;index
< OUTPUTCHANNELS
;index
++)
797 State
->Early
.PanGain
[index
] = 0.0f
;
798 State
->Late
.PanGain
[index
] = 0.0f
;
802 return &State
->state
;
805 ALeffectState
*EAXVerbCreate(void)
807 ALeffectState
*State
= VerbCreate();
808 if(State
) State
->Process
= EAXVerbProcess
;