Look for and use atan2f, log10f, and floorf
[openal-soft/android.git] / Alc / alcReverb.c
blob92a82a51555a0985a179972b7e9a496439311fb4
1 /**
2 * Reverb for the OpenAL cross platform audio library
3 * Copyright (C) 2008-2009 by Christopher Fitzgerald.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
27 #include "AL/al.h"
28 #include "AL/alc.h"
29 #include "alMain.h"
30 #include "alAuxEffectSlot.h"
31 #include "alEffect.h"
32 #include "alError.h"
33 #include "alu.h"
35 typedef struct DelayLine
37 // The delay lines use sample lengths that are powers of 2 to allow the
38 // use of bit-masking instead of a modulus for wrapping.
39 ALuint Mask;
40 ALfloat *Line;
41 } DelayLine;
43 typedef struct ALverbState {
44 // Must be first in all effects!
45 ALeffectState state;
47 // All delay lines are allocated as a single buffer to reduce memory
48 // fragmentation and management code.
49 ALfloat *SampleBuffer;
50 ALuint TotalSamples;
51 // Master effect low-pass filter (2 chained 1-pole filters).
52 FILTER LpFilter;
53 ALfloat LpHistory[2];
54 struct {
55 // Modulator delay line.
56 DelayLine Delay;
57 // The vibrato time is tracked with an index over a modulus-wrapped
58 // range (in samples).
59 ALuint Index;
60 ALuint Range;
61 // The depth of frequency change (also in samples) and its filter.
62 ALfloat Depth;
63 ALfloat Coeff;
64 ALfloat Filter;
65 } Mod;
66 // Initial effect delay.
67 DelayLine Delay;
68 // The tap points for the initial delay. First tap goes to early
69 // reflections, the last to late reverb.
70 ALuint DelayTap[2];
71 struct {
72 // Output gain for early reflections.
73 ALfloat Gain;
74 // Early reflections are done with 4 delay lines.
75 ALfloat Coeff[4];
76 DelayLine Delay[4];
77 ALuint Offset[4];
78 // The gain for each output channel based on 3D panning (only for the
79 // EAX path).
80 ALfloat PanGain[MAXCHANNELS];
81 } Early;
82 // Decorrelator delay line.
83 DelayLine Decorrelator;
84 // There are actually 4 decorrelator taps, but the first occurs at the
85 // initial sample.
86 ALuint DecoTap[3];
87 struct {
88 // Output gain for late reverb.
89 ALfloat Gain;
90 // Attenuation to compensate for the modal density and decay rate of
91 // the late lines.
92 ALfloat DensityGain;
93 // The feed-back and feed-forward all-pass coefficient.
94 ALfloat ApFeedCoeff;
95 // Mixing matrix coefficient.
96 ALfloat MixCoeff;
97 // Late reverb has 4 parallel all-pass filters.
98 ALfloat ApCoeff[4];
99 DelayLine ApDelay[4];
100 ALuint ApOffset[4];
101 // In addition to 4 cyclical delay lines.
102 ALfloat Coeff[4];
103 DelayLine Delay[4];
104 ALuint Offset[4];
105 // The cyclical delay lines are 1-pole low-pass filtered.
106 ALfloat LpCoeff[4];
107 ALfloat LpSample[4];
108 // The gain for each output channel based on 3D panning (only for the
109 // EAX path).
110 ALfloat PanGain[MAXCHANNELS];
111 } Late;
112 struct {
113 // Attenuation to compensate for the modal density and decay rate of
114 // the echo line.
115 ALfloat DensityGain;
116 // Echo delay and all-pass lines.
117 DelayLine Delay;
118 DelayLine ApDelay;
119 ALfloat Coeff;
120 ALfloat ApFeedCoeff;
121 ALfloat ApCoeff;
122 ALuint Offset;
123 ALuint ApOffset;
124 // The echo line is 1-pole low-pass filtered.
125 ALfloat LpCoeff;
126 ALfloat LpSample;
127 // Echo mixing coefficients.
128 ALfloat MixCoeff[2];
129 } Echo;
130 // The current read offset for all delay lines.
131 ALuint Offset;
133 // The gain for each output channel (non-EAX path only; aliased from
134 // Late.PanGain)
135 ALfloat *Gain;
136 } ALverbState;
138 /* This is a user config option for modifying the overall output of the reverb
139 * effect.
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
150 * backwards.
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;
197 // Basic delay line input/output routines.
198 static __inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
200 return Delay->Line[offset&Delay->Mask];
203 static __inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
205 Delay->Line[offset&Delay->Mask] = in;
208 // Attenuated delay line output routine.
209 static __inline ALfloat AttenuatedDelayLineOut(DelayLine *Delay, ALuint offset, ALfloat coeff)
211 return coeff * Delay->Line[offset&Delay->Mask];
214 // Basic attenuated all-pass input/output routine.
215 static __inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
217 ALfloat out, feed;
219 out = DelayLineOut(Delay, outOffset);
220 feed = feedCoeff * in;
221 DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
223 // The time-based attenuation is only applied to the delay output to
224 // keep it from affecting the feed-back path (which is already controlled
225 // by the all-pass feed coefficient).
226 return (coeff * out) - feed;
229 // Given an input sample, this function produces modulation for the late
230 // reverb.
231 static __inline ALfloat EAXModulation(ALverbState *State, ALfloat in)
233 ALfloat sinus, frac;
234 ALuint offset;
235 ALfloat out0, out1;
237 // Calculate the sinus rythm (dependent on modulation time and the
238 // sampling rate). The center of the sinus is moved to reduce the delay
239 // of the effect when the time or depth are low.
240 sinus = 1.0f - aluCos(F_PI*2.0f * State->Mod.Index / State->Mod.Range);
242 // The depth determines the range over which to read the input samples
243 // from, so it must be filtered to reduce the distortion caused by even
244 // small parameter changes.
245 State->Mod.Filter = lerp(State->Mod.Filter, State->Mod.Depth,
246 State->Mod.Coeff);
248 // Calculate the read offset and fraction between it and the next sample.
249 frac = (1.0f + (State->Mod.Filter * sinus));
250 offset = (ALuint)frac;
251 frac -= offset;
253 // Get the two samples crossed by the offset, and feed the delay line
254 // with the next input sample.
255 out0 = DelayLineOut(&State->Mod.Delay, State->Offset - offset);
256 out1 = DelayLineOut(&State->Mod.Delay, State->Offset - offset - 1);
257 DelayLineIn(&State->Mod.Delay, State->Offset, in);
259 // Step the modulation index forward, keeping it bound to its range.
260 State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
262 // The output is obtained by linearly interpolating the two samples that
263 // were acquired above.
264 return lerp(out0, out1, frac);
267 // Delay line output routine for early reflections.
268 static __inline ALfloat EarlyDelayLineOut(ALverbState *State, ALuint index)
270 return AttenuatedDelayLineOut(&State->Early.Delay[index],
271 State->Offset - State->Early.Offset[index],
272 State->Early.Coeff[index]);
275 // Given an input sample, this function produces four-channel output for the
276 // early reflections.
277 static __inline ALvoid EarlyReflection(ALverbState *State, ALfloat in, ALfloat *out)
279 ALfloat d[4], v, f[4];
281 // Obtain the decayed results of each early delay line.
282 d[0] = EarlyDelayLineOut(State, 0);
283 d[1] = EarlyDelayLineOut(State, 1);
284 d[2] = EarlyDelayLineOut(State, 2);
285 d[3] = EarlyDelayLineOut(State, 3);
287 /* The following uses a lossless scattering junction from waveguide
288 * theory. It actually amounts to a householder mixing matrix, which
289 * will produce a maximally diffuse response, and means this can probably
290 * be considered a simple feed-back delay network (FDN).
292 * ---
294 * v = 2/N / d_i
295 * ---
296 * i=1
298 v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
299 // The junction is loaded with the input here.
300 v += in;
302 // Calculate the feed values for the delay lines.
303 f[0] = v - d[0];
304 f[1] = v - d[1];
305 f[2] = v - d[2];
306 f[3] = v - d[3];
308 // Re-feed the delay lines.
309 DelayLineIn(&State->Early.Delay[0], State->Offset, f[0]);
310 DelayLineIn(&State->Early.Delay[1], State->Offset, f[1]);
311 DelayLineIn(&State->Early.Delay[2], State->Offset, f[2]);
312 DelayLineIn(&State->Early.Delay[3], State->Offset, f[3]);
314 // Output the results of the junction for all four channels.
315 out[0] = State->Early.Gain * f[0];
316 out[1] = State->Early.Gain * f[1];
317 out[2] = State->Early.Gain * f[2];
318 out[3] = State->Early.Gain * f[3];
321 // All-pass input/output routine for late reverb.
322 static __inline ALfloat LateAllPassInOut(ALverbState *State, ALuint index, ALfloat in)
324 return AllpassInOut(&State->Late.ApDelay[index],
325 State->Offset - State->Late.ApOffset[index],
326 State->Offset, in, State->Late.ApFeedCoeff,
327 State->Late.ApCoeff[index]);
330 // Delay line output routine for late reverb.
331 static __inline ALfloat LateDelayLineOut(ALverbState *State, ALuint index)
333 return AttenuatedDelayLineOut(&State->Late.Delay[index],
334 State->Offset - State->Late.Offset[index],
335 State->Late.Coeff[index]);
338 // Low-pass filter input/output routine for late reverb.
339 static __inline ALfloat LateLowPassInOut(ALverbState *State, ALuint index, ALfloat in)
341 in = lerp(in, State->Late.LpSample[index], State->Late.LpCoeff[index]);
342 State->Late.LpSample[index] = in;
343 return in;
346 // Given four decorrelated input samples, this function produces four-channel
347 // output for the late reverb.
348 static __inline ALvoid LateReverb(ALverbState *State, ALfloat *in, ALfloat *out)
350 ALfloat d[4], f[4];
352 // Obtain the decayed results of the cyclical delay lines, and add the
353 // corresponding input channels. Then pass the results through the
354 // low-pass filters.
356 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and back
357 // to 0.
358 d[0] = LateLowPassInOut(State, 2, in[2] + LateDelayLineOut(State, 2));
359 d[1] = LateLowPassInOut(State, 0, in[0] + LateDelayLineOut(State, 0));
360 d[2] = LateLowPassInOut(State, 3, in[3] + LateDelayLineOut(State, 3));
361 d[3] = LateLowPassInOut(State, 1, in[1] + LateDelayLineOut(State, 1));
363 // To help increase diffusion, run each line through an all-pass filter.
364 // When there is no diffusion, the shortest all-pass filter will feed the
365 // shortest delay line.
366 d[0] = LateAllPassInOut(State, 0, d[0]);
367 d[1] = LateAllPassInOut(State, 1, d[1]);
368 d[2] = LateAllPassInOut(State, 2, d[2]);
369 d[3] = LateAllPassInOut(State, 3, d[3]);
371 /* Late reverb is done with a modified feed-back delay network (FDN)
372 * topology. Four input lines are each fed through their own all-pass
373 * filter and then into the mixing matrix. The four outputs of the
374 * mixing matrix are then cycled back to the inputs. Each output feeds
375 * a different input to form a circlular feed cycle.
377 * The mixing matrix used is a 4D skew-symmetric rotation matrix derived
378 * using a single unitary rotational parameter:
380 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
381 * [ -a, d, c, -b ]
382 * [ -b, -c, d, a ]
383 * [ -c, b, -a, d ]
385 * The rotation is constructed from the effect's diffusion parameter,
386 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
387 * with differing signs, and d is the coefficient x. The matrix is thus:
389 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
390 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
391 * [ y, -y, x, y ] x = cos(t)
392 * [ -y, -y, -y, x ] y = sin(t) / n
394 * To reduce the number of multiplies, the x coefficient is applied with
395 * the cyclical delay line coefficients. Thus only the y coefficient is
396 * applied when mixing, and is modified to be: y / x.
398 f[0] = d[0] + (State->Late.MixCoeff * ( d[1] + -d[2] + d[3]));
399 f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
400 f[2] = d[2] + (State->Late.MixCoeff * ( d[0] + -d[1] + d[3]));
401 f[3] = d[3] + (State->Late.MixCoeff * (-d[0] + -d[1] + -d[2] ));
403 // Output the results of the matrix for all four channels, attenuated by
404 // the late reverb gain (which is attenuated by the 'x' mix coefficient).
405 out[0] = State->Late.Gain * f[0];
406 out[1] = State->Late.Gain * f[1];
407 out[2] = State->Late.Gain * f[2];
408 out[3] = State->Late.Gain * f[3];
410 // Re-feed the cyclical delay lines.
411 DelayLineIn(&State->Late.Delay[0], State->Offset, f[0]);
412 DelayLineIn(&State->Late.Delay[1], State->Offset, f[1]);
413 DelayLineIn(&State->Late.Delay[2], State->Offset, f[2]);
414 DelayLineIn(&State->Late.Delay[3], State->Offset, f[3]);
417 // Given an input sample, this function mixes echo into the four-channel late
418 // reverb.
419 static __inline ALvoid EAXEcho(ALverbState *State, ALfloat in, ALfloat *late)
421 ALfloat out, feed;
423 // Get the latest attenuated echo sample for output.
424 feed = AttenuatedDelayLineOut(&State->Echo.Delay,
425 State->Offset - State->Echo.Offset,
426 State->Echo.Coeff);
428 // Mix the output into the late reverb channels.
429 out = State->Echo.MixCoeff[0] * feed;
430 late[0] = (State->Echo.MixCoeff[1] * late[0]) + out;
431 late[1] = (State->Echo.MixCoeff[1] * late[1]) + out;
432 late[2] = (State->Echo.MixCoeff[1] * late[2]) + out;
433 late[3] = (State->Echo.MixCoeff[1] * late[3]) + out;
435 // Mix the energy-attenuated input with the output and pass it through
436 // the echo low-pass filter.
437 feed += State->Echo.DensityGain * in;
438 feed = lerp(feed, State->Echo.LpSample, State->Echo.LpCoeff);
439 State->Echo.LpSample = feed;
441 // Then the echo all-pass filter.
442 feed = AllpassInOut(&State->Echo.ApDelay,
443 State->Offset - State->Echo.ApOffset,
444 State->Offset, feed, State->Echo.ApFeedCoeff,
445 State->Echo.ApCoeff);
447 // Feed the delay with the mixed and filtered sample.
448 DelayLineIn(&State->Echo.Delay, State->Offset, feed);
451 // Perform the non-EAX reverb pass on a given input sample, resulting in
452 // four-channel output.
453 static __inline ALvoid VerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
455 ALfloat feed, taps[4];
457 // Low-pass filter the incoming sample.
458 in = lpFilter2P(&State->LpFilter, 0, in);
460 // Feed the initial delay line.
461 DelayLineIn(&State->Delay, State->Offset, in);
463 // Calculate the early reflection from the first delay tap.
464 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
465 EarlyReflection(State, in, early);
467 // Feed the decorrelator from the energy-attenuated output of the second
468 // delay tap.
469 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
470 feed = in * State->Late.DensityGain;
471 DelayLineIn(&State->Decorrelator, State->Offset, feed);
473 // Calculate the late reverb from the decorrelator taps.
474 taps[0] = feed;
475 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
476 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
477 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
478 LateReverb(State, taps, late);
480 // Step all delays forward one sample.
481 State->Offset++;
484 // Perform the EAX reverb pass on a given input sample, resulting in four-
485 // channel output.
486 static __inline ALvoid EAXVerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
488 ALfloat feed, taps[4];
490 // Low-pass filter the incoming sample.
491 in = lpFilter2P(&State->LpFilter, 0, in);
493 // Perform any modulation on the input.
494 in = EAXModulation(State, in);
496 // Feed the initial delay line.
497 DelayLineIn(&State->Delay, State->Offset, in);
499 // Calculate the early reflection from the first delay tap.
500 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
501 EarlyReflection(State, in, early);
503 // Feed the decorrelator from the energy-attenuated output of the second
504 // delay tap.
505 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
506 feed = in * State->Late.DensityGain;
507 DelayLineIn(&State->Decorrelator, State->Offset, feed);
509 // Calculate the late reverb from the decorrelator taps.
510 taps[0] = feed;
511 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
512 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
513 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
514 LateReverb(State, taps, late);
516 // Calculate and mix in any echo.
517 EAXEcho(State, in, late);
519 // Step all delays forward one sample.
520 State->Offset++;
523 // This processes the reverb state, given the input samples and an output
524 // buffer.
525 static ALvoid VerbProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
527 ALverbState *State = (ALverbState*)effect;
528 ALuint index;
529 ALfloat early[4], late[4], out[4];
530 const ALfloat *panGain = State->Gain;
532 for(index = 0;index < SamplesToDo;index++)
534 // Process reverb for this sample.
535 VerbPass(State, SamplesIn[index], early, late);
537 // Mix early reflections and late reverb.
538 out[0] = (early[0] + late[0]);
539 out[1] = (early[1] + late[1]);
540 out[2] = (early[2] + late[2]);
541 out[3] = (early[3] + late[3]);
543 // Output the results.
544 SamplesOut[index][FRONT_LEFT] += panGain[FRONT_LEFT] * out[0];
545 SamplesOut[index][FRONT_RIGHT] += panGain[FRONT_RIGHT] * out[1];
546 SamplesOut[index][FRONT_CENTER] += panGain[FRONT_CENTER] * out[3];
547 SamplesOut[index][SIDE_LEFT] += panGain[SIDE_LEFT] * out[0];
548 SamplesOut[index][SIDE_RIGHT] += panGain[SIDE_RIGHT] * out[1];
549 SamplesOut[index][BACK_LEFT] += panGain[BACK_LEFT] * out[0];
550 SamplesOut[index][BACK_RIGHT] += panGain[BACK_RIGHT] * out[1];
551 SamplesOut[index][BACK_CENTER] += panGain[BACK_CENTER] * out[2];
555 // This processes the EAX reverb state, given the input samples and an output
556 // buffer.
557 static ALvoid EAXVerbProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
559 ALverbState *State = (ALverbState*)effect;
560 ALuint index;
561 ALfloat early[4], late[4];
563 for(index = 0;index < SamplesToDo;index++)
565 // Process reverb for this sample.
566 EAXVerbPass(State, SamplesIn[index], early, late);
568 // Unfortunately, while the number and configuration of gains for
569 // panning adjust according to MAXCHANNELS, the output from the
570 // reverb engine is not so scalable.
571 SamplesOut[index][FRONT_LEFT] +=
572 (State->Early.PanGain[FRONT_LEFT]*early[0] +
573 State->Late.PanGain[FRONT_LEFT]*late[0]);
574 SamplesOut[index][FRONT_RIGHT] +=
575 (State->Early.PanGain[FRONT_RIGHT]*early[1] +
576 State->Late.PanGain[FRONT_RIGHT]*late[1]);
577 SamplesOut[index][FRONT_CENTER] +=
578 (State->Early.PanGain[FRONT_CENTER]*early[3] +
579 State->Late.PanGain[FRONT_CENTER]*late[3]);
580 SamplesOut[index][SIDE_LEFT] +=
581 (State->Early.PanGain[SIDE_LEFT]*early[0] +
582 State->Late.PanGain[SIDE_LEFT]*late[0]);
583 SamplesOut[index][SIDE_RIGHT] +=
584 (State->Early.PanGain[SIDE_RIGHT]*early[1] +
585 State->Late.PanGain[SIDE_RIGHT]*late[1]);
586 SamplesOut[index][BACK_LEFT] +=
587 (State->Early.PanGain[BACK_LEFT]*early[0] +
588 State->Late.PanGain[BACK_LEFT]*late[0]);
589 SamplesOut[index][BACK_RIGHT] +=
590 (State->Early.PanGain[BACK_RIGHT]*early[1] +
591 State->Late.PanGain[BACK_RIGHT]*late[1]);
592 SamplesOut[index][BACK_CENTER] +=
593 (State->Early.PanGain[BACK_CENTER]*early[2] +
594 State->Late.PanGain[BACK_CENTER]*late[2]);
599 // Given the allocated sample buffer, this function updates each delay line
600 // offset.
601 static __inline ALvoid RealizeLineOffset(ALfloat * sampleBuffer, DelayLine *Delay)
603 Delay->Line = &sampleBuffer[(ALintptrEXT)Delay->Line];
606 // Calculate the length of a delay line and store its mask and offset.
607 static ALuint CalcLineLength(ALfloat length, ALintptrEXT offset, ALuint frequency, DelayLine *Delay)
609 ALuint samples;
611 // All line lengths are powers of 2, calculated from their lengths, with
612 // an additional sample in case of rounding errors.
613 samples = NextPowerOf2((ALuint)(length * frequency) + 1);
614 // All lines share a single sample buffer.
615 Delay->Mask = samples - 1;
616 Delay->Line = (ALfloat*)offset;
617 // Return the sample count for accumulation.
618 return samples;
621 /* Calculates the delay line metrics and allocates the shared sample buffer
622 * for all lines given the sample rate (frequency). If an allocation failure
623 * occurs, it returns AL_FALSE.
625 static ALboolean AllocLines(ALuint frequency, ALverbState *State)
627 ALuint totalSamples, index;
628 ALfloat length;
629 ALfloat *newBuffer = NULL;
631 // All delay line lengths are calculated to accomodate the full range of
632 // lengths given their respective paramters.
633 totalSamples = 0;
635 /* The modulator's line length is calculated from the maximum modulation
636 * time and depth coefficient, and halfed for the low-to-high frequency
637 * swing. An additional sample is added to keep it stable when there is no
638 * modulation.
640 length = (AL_EAXREVERB_MAX_MODULATION_TIME*MODULATION_DEPTH_COEFF/2.0f) +
641 (1.0f / frequency);
642 totalSamples += CalcLineLength(length, totalSamples, frequency,
643 &State->Mod.Delay);
645 // The initial delay is the sum of the reflections and late reverb
646 // delays.
647 length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY +
648 AL_EAXREVERB_MAX_LATE_REVERB_DELAY;
649 totalSamples += CalcLineLength(length, totalSamples, frequency,
650 &State->Delay);
652 // The early reflection lines.
653 for(index = 0;index < 4;index++)
654 totalSamples += CalcLineLength(EARLY_LINE_LENGTH[index], totalSamples,
655 frequency, &State->Early.Delay[index]);
657 // The decorrelator line is calculated from the lowest reverb density (a
658 // parameter value of 1).
659 length = (DECO_FRACTION * DECO_MULTIPLIER * DECO_MULTIPLIER) *
660 LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER);
661 totalSamples += CalcLineLength(length, totalSamples, frequency,
662 &State->Decorrelator);
664 // The late all-pass lines.
665 for(index = 0;index < 4;index++)
666 totalSamples += CalcLineLength(ALLPASS_LINE_LENGTH[index], totalSamples,
667 frequency, &State->Late.ApDelay[index]);
669 // The late delay lines are calculated from the lowest reverb density.
670 for(index = 0;index < 4;index++)
672 length = LATE_LINE_LENGTH[index] * (1.0f + LATE_LINE_MULTIPLIER);
673 totalSamples += CalcLineLength(length, totalSamples, frequency,
674 &State->Late.Delay[index]);
677 // The echo all-pass and delay lines.
678 totalSamples += CalcLineLength(ECHO_ALLPASS_LENGTH, totalSamples,
679 frequency, &State->Echo.ApDelay);
680 totalSamples += CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME, totalSamples,
681 frequency, &State->Echo.Delay);
683 if(totalSamples != State->TotalSamples)
685 TRACE("New reverb buffer length: %u samples (%f sec)\n", totalSamples, totalSamples/(float)frequency);
686 newBuffer = realloc(State->SampleBuffer, sizeof(ALfloat) * totalSamples);
687 if(newBuffer == NULL)
688 return AL_FALSE;
689 State->SampleBuffer = newBuffer;
690 State->TotalSamples = totalSamples;
693 // Update all delays to reflect the new sample buffer.
694 RealizeLineOffset(State->SampleBuffer, &State->Delay);
695 RealizeLineOffset(State->SampleBuffer, &State->Decorrelator);
696 for(index = 0;index < 4;index++)
698 RealizeLineOffset(State->SampleBuffer, &State->Early.Delay[index]);
699 RealizeLineOffset(State->SampleBuffer, &State->Late.ApDelay[index]);
700 RealizeLineOffset(State->SampleBuffer, &State->Late.Delay[index]);
702 RealizeLineOffset(State->SampleBuffer, &State->Mod.Delay);
703 RealizeLineOffset(State->SampleBuffer, &State->Echo.ApDelay);
704 RealizeLineOffset(State->SampleBuffer, &State->Echo.Delay);
706 // Clear the sample buffer.
707 for(index = 0;index < State->TotalSamples;index++)
708 State->SampleBuffer[index] = 0.0f;
710 return AL_TRUE;
713 // This updates the device-dependant EAX reverb state. This is called on
714 // initialization and any time the device parameters (eg. playback frequency,
715 // format) have been changed.
716 static ALboolean ReverbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
718 ALverbState *State = (ALverbState*)effect;
719 ALuint frequency = Device->Frequency, index;
721 // Allocate the delay lines.
722 if(!AllocLines(frequency, State))
723 return AL_FALSE;
725 // Calculate the modulation filter coefficient. Notice that the exponent
726 // is calculated given the current sample rate. This ensures that the
727 // resulting filter response over time is consistent across all sample
728 // rates.
729 State->Mod.Coeff = aluPow(MODULATION_FILTER_COEFF,
730 MODULATION_FILTER_CONST / frequency);
732 // The early reflection and late all-pass filter line lengths are static,
733 // so their offsets only need to be calculated once.
734 for(index = 0;index < 4;index++)
736 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
737 frequency);
738 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
739 frequency);
742 // The echo all-pass filter line length is static, so its offset only
743 // needs to be calculated once.
744 State->Echo.ApOffset = (ALuint)(ECHO_ALLPASS_LENGTH * frequency);
746 return AL_TRUE;
749 // Calculate a decay coefficient given the length of each cycle and the time
750 // until the decay reaches -60 dB.
751 static __inline ALfloat CalcDecayCoeff(ALfloat length, ALfloat decayTime)
753 return aluPow(0.001f/*-60 dB*/, length/decayTime);
756 // Calculate a decay length from a coefficient and the time until the decay
757 // reaches -60 dB.
758 static __inline ALfloat CalcDecayLength(ALfloat coeff, ALfloat decayTime)
760 return aluLog10(coeff) * decayTime / aluLog10(0.001)/*-60 dB*/;
763 // Calculate the high frequency parameter for the I3DL2 coefficient
764 // calculation.
765 static __inline ALfloat CalcI3DL2HFreq(ALfloat hfRef, ALuint frequency)
767 return aluCos(F_PI*2.0f * hfRef / frequency);
770 // Calculate an attenuation to be applied to the input of any echo models to
771 // compensate for modal density and decay time.
772 static __inline ALfloat CalcDensityGain(ALfloat a)
774 /* The energy of a signal can be obtained by finding the area under the
775 * squared signal. This takes the form of Sum(x_n^2), where x is the
776 * amplitude for the sample n.
778 * Decaying feedback matches exponential decay of the form Sum(a^n),
779 * where a is the attenuation coefficient, and n is the sample. The area
780 * under this decay curve can be calculated as: 1 / (1 - a).
782 * Modifying the above equation to find the squared area under the curve
783 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
784 * calculated by inverting the square root of this approximation,
785 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
787 return aluSqrt(1.0f - (a * a));
790 // Calculate the mixing matrix coefficients given a diffusion factor.
791 static __inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)
793 ALfloat n, t;
795 // The matrix is of order 4, so n is sqrt (4 - 1).
796 n = aluSqrt(3.0f);
797 t = diffusion * aluAtan(n);
799 // Calculate the first mixing matrix coefficient.
800 *x = aluCos(t);
801 // Calculate the second mixing matrix coefficient.
802 *y = aluSin(t) / n;
805 // Calculate the limited HF ratio for use with the late reverb low-pass
806 // filters.
807 static ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)
809 ALfloat limitRatio;
811 /* Find the attenuation due to air absorption in dB (converting delay
812 * time to meters using the speed of sound). Then reversing the decay
813 * equation, solve for HF ratio. The delay length is cancelled out of
814 * the equation, so it can be calculated once for all lines.
816 limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *
817 SPEEDOFSOUNDMETRESPERSEC);
818 /* Using the limit calculated above, apply the upper bound to the HF
819 * ratio. Also need to limit the result to a minimum of 0.1, just like the
820 * HF ratio parameter. */
821 return clampf(limitRatio, 0.1f, hfRatio);
824 // Calculate the coefficient for a HF (and eventually LF) decay damping
825 // filter.
826 static __inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)
828 ALfloat coeff, g;
830 // Eventually this should boost the high frequencies when the ratio
831 // exceeds 1.
832 coeff = 0.0f;
833 if (hfRatio < 1.0f)
835 // Calculate the low-pass coefficient by dividing the HF decay
836 // coefficient by the full decay coefficient.
837 g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;
839 // Damping is done with a 1-pole filter, so g needs to be squared.
840 g *= g;
841 coeff = lpCoeffCalc(g, cw);
843 // Very low decay times will produce minimal output, so apply an
844 // upper bound to the coefficient.
845 coeff = minf(coeff, 0.98f);
847 return coeff;
850 // Update the EAX modulation index, range, and depth. Keep in mind that this
851 // kind of vibrato is additive and not multiplicative as one may expect. The
852 // downswing will sound stronger than the upswing.
853 static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALverbState *State)
855 ALfloat length;
857 /* Modulation is calculated in two parts.
859 * The modulation time effects the sinus applied to the change in
860 * frequency. An index out of the current time range (both in samples)
861 * is incremented each sample. The range is bound to a reasonable
862 * minimum (1 sample) and when the timing changes, the index is rescaled
863 * to the new range (to keep the sinus consistent).
865 length = modTime * frequency;
866 if (length >= 1.0f) {
867 State->Mod.Index = (ALuint)(State->Mod.Index * length /
868 State->Mod.Range);
869 State->Mod.Range = (ALuint)length;
870 } else {
871 State->Mod.Index = 0;
872 State->Mod.Range = 1;
875 /* The modulation depth effects the amount of frequency change over the
876 * range of the sinus. It needs to be scaled by the modulation time so
877 * that a given depth produces a consistent change in frequency over all
878 * ranges of time. Since the depth is applied to a sinus value, it needs
879 * to be halfed once for the sinus range and again for the sinus swing
880 * in time (half of it is spent decreasing the frequency, half is spent
881 * increasing it).
883 State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /
884 2.0f * frequency;
887 // Update the offsets for the initial effect delay line.
888 static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALverbState *State)
890 // Calculate the initial delay taps.
891 State->DelayTap[0] = (ALuint)(earlyDelay * frequency);
892 State->DelayTap[1] = (ALuint)((earlyDelay + lateDelay) * frequency);
895 // Update the early reflections gain and line coefficients.
896 static ALvoid UpdateEarlyLines(ALfloat reverbGain, ALfloat earlyGain, ALfloat lateDelay, ALverbState *State)
898 ALuint index;
900 // Calculate the early reflections gain (from the master effect gain, and
901 // reflections gain parameters) with a constant attenuation of 0.5.
902 State->Early.Gain = 0.5f * reverbGain * earlyGain;
904 // Calculate the gain (coefficient) for each early delay line using the
905 // late delay time. This expands the early reflections to the start of
906 // the late reverb.
907 for(index = 0;index < 4;index++)
908 State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],
909 lateDelay);
912 // Update the offsets for the decorrelator line.
913 static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALverbState *State)
915 ALuint index;
916 ALfloat length;
918 /* The late reverb inputs are decorrelated to smooth the reverb tail and
919 * reduce harsh echos. The first tap occurs immediately, while the
920 * remaining taps are delayed by multiples of a fraction of the smallest
921 * cyclical delay time.
923 * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
925 for(index = 0;index < 3;index++)
927 length = (DECO_FRACTION * aluPow(DECO_MULTIPLIER, (ALfloat)index)) *
928 LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));
929 State->DecoTap[index] = (ALuint)(length * frequency);
933 // Update the late reverb gains, line lengths, and line coefficients.
934 static ALvoid UpdateLateLines(ALfloat reverbGain, ALfloat lateGain, ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)
936 ALfloat length;
937 ALuint index;
939 /* Calculate the late reverb gain (from the master effect gain, and late
940 * reverb gain parameters). Since the output is tapped prior to the
941 * application of the next delay line coefficients, this gain needs to be
942 * attenuated by the 'x' mixing matrix coefficient as well.
944 State->Late.Gain = reverbGain * lateGain * xMix;
946 /* To compensate for changes in modal density and decay time of the late
947 * reverb signal, the input is attenuated based on the maximal energy of
948 * the outgoing signal. This approximation is used to keep the apparent
949 * energy of the signal equal for all ranges of density and decay time.
951 * The average length of the cyclcical delay lines is used to calculate
952 * the attenuation coefficient.
954 length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
955 LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;
956 length *= 1.0f + (density * LATE_LINE_MULTIPLIER);
957 State->Late.DensityGain = CalcDensityGain(CalcDecayCoeff(length,
958 decayTime));
960 // Calculate the all-pass feed-back and feed-forward coefficient.
961 State->Late.ApFeedCoeff = 0.5f * aluPow(diffusion, 2.0f);
963 for(index = 0;index < 4;index++)
965 // Calculate the gain (coefficient) for each all-pass line.
966 State->Late.ApCoeff[index] = CalcDecayCoeff(ALLPASS_LINE_LENGTH[index],
967 decayTime);
969 // Calculate the length (in seconds) of each cyclical delay line.
970 length = LATE_LINE_LENGTH[index] * (1.0f + (density *
971 LATE_LINE_MULTIPLIER));
973 // Calculate the delay offset for each cyclical delay line.
974 State->Late.Offset[index] = (ALuint)(length * frequency);
976 // Calculate the gain (coefficient) for each cyclical line.
977 State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);
979 // Calculate the damping coefficient for each low-pass filter.
980 State->Late.LpCoeff[index] =
981 CalcDampingCoeff(hfRatio, length, decayTime,
982 State->Late.Coeff[index], cw);
984 // Attenuate the cyclical line coefficients by the mixing coefficient
985 // (x).
986 State->Late.Coeff[index] *= xMix;
990 // Update the echo gain, line offset, line coefficients, and mixing
991 // coefficients.
992 static ALvoid UpdateEchoLine(ALfloat reverbGain, ALfloat lateGain, ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)
994 // Update the offset and coefficient for the echo delay line.
995 State->Echo.Offset = (ALuint)(echoTime * frequency);
997 // Calculate the decay coefficient for the echo line.
998 State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);
1000 // Calculate the energy-based attenuation coefficient for the echo delay
1001 // line.
1002 State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);
1004 // Calculate the echo all-pass feed coefficient.
1005 State->Echo.ApFeedCoeff = 0.5f * aluPow(diffusion, 2.0f);
1007 // Calculate the echo all-pass attenuation coefficient.
1008 State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);
1010 // Calculate the damping coefficient for each low-pass filter.
1011 State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,
1012 State->Echo.Coeff, cw);
1014 /* Calculate the echo mixing coefficients. The first is applied to the
1015 * echo itself. The second is used to attenuate the late reverb when
1016 * echo depth is high and diffusion is low, so the echo is slightly
1017 * stronger than the decorrelated echos in the reverb tail.
1019 State->Echo.MixCoeff[0] = reverbGain * lateGain * echoDepth;
1020 State->Echo.MixCoeff[1] = 1.0f - (echoDepth * 0.5f * (1.0f - diffusion));
1023 // Update the early and late 3D panning gains.
1024 static ALvoid Update3DPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALverbState *State)
1026 ALfloat earlyPan[3] = { ReflectionsPan[0], ReflectionsPan[1],
1027 ReflectionsPan[2] };
1028 ALfloat latePan[3] = { LateReverbPan[0], LateReverbPan[1],
1029 LateReverbPan[2] };
1030 const ALfloat *speakerGain;
1031 ALfloat ambientGain;
1032 ALfloat dirGain;
1033 ALfloat length;
1034 ALuint index;
1035 ALint pos;
1037 Gain *= ReverbBoost;
1039 // Attenuate non-directional reverb according to the number of channels
1040 ambientGain = aluSqrt(2.0f/Device->NumChan);
1042 // Calculate the 3D-panning gains for the early reflections and late
1043 // reverb.
1044 length = earlyPan[0]*earlyPan[0] + earlyPan[1]*earlyPan[1] + earlyPan[2]*earlyPan[2];
1045 if(length > 1.0f)
1047 length = 1.0f / aluSqrt(length);
1048 earlyPan[0] *= length;
1049 earlyPan[1] *= length;
1050 earlyPan[2] *= length;
1052 length = latePan[0]*latePan[0] + latePan[1]*latePan[1] + latePan[2]*latePan[2];
1053 if(length > 1.0f)
1055 length = 1.0f / aluSqrt(length);
1056 latePan[0] *= length;
1057 latePan[1] *= length;
1058 latePan[2] *= length;
1061 /* This code applies directional reverb just like the mixer applies
1062 * directional sources. It diffuses the sound toward all speakers as the
1063 * magnitude of the panning vector drops, which is only a rough
1064 * approximation of the expansion of sound across the speakers from the
1065 * panning direction.
1067 pos = aluCart2LUTpos(earlyPan[2], earlyPan[0]);
1068 speakerGain = Device->PanningLUT[pos];
1069 dirGain = aluSqrt((earlyPan[0] * earlyPan[0]) + (earlyPan[2] * earlyPan[2]));
1071 for(index = 0;index < MAXCHANNELS;index++)
1072 State->Early.PanGain[index] = 0.0f;
1073 for(index = 0;index < Device->NumChan;index++)
1075 enum Channel chan = Device->Speaker2Chan[index];
1076 State->Early.PanGain[chan] = lerp(ambientGain, speakerGain[chan], dirGain) * Gain;
1080 pos = aluCart2LUTpos(latePan[2], latePan[0]);
1081 speakerGain = Device->PanningLUT[pos];
1082 dirGain = aluSqrt((latePan[0] * latePan[0]) + (latePan[2] * latePan[2]));
1084 for(index = 0;index < MAXCHANNELS;index++)
1085 State->Late.PanGain[index] = 0.0f;
1086 for(index = 0;index < Device->NumChan;index++)
1088 enum Channel chan = Device->Speaker2Chan[index];
1089 State->Late.PanGain[chan] = lerp(ambientGain, speakerGain[chan], dirGain) * Gain;
1093 // This updates the EAX reverb state. This is called any time the EAX reverb
1094 // effect is loaded into a slot.
1095 static ALvoid ReverbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffectslot *Slot)
1097 ALverbState *State = (ALverbState*)effect;
1098 ALuint frequency = Context->Device->Frequency;
1099 ALboolean isEAX = AL_FALSE;
1100 ALfloat cw, x, y, hfRatio;
1102 if(Slot->effect.type == AL_EFFECT_EAXREVERB && !EmulateEAXReverb)
1104 State->state.Process = EAXVerbProcess;
1105 isEAX = AL_TRUE;
1107 else if(Slot->effect.type == AL_EFFECT_REVERB || EmulateEAXReverb)
1109 State->state.Process = VerbProcess;
1110 isEAX = AL_FALSE;
1113 // Calculate the master low-pass filter (from the master effect HF gain).
1114 if(isEAX) cw = CalcI3DL2HFreq(Slot->effect.Reverb.HFReference, frequency);
1115 else cw = CalcI3DL2HFreq(LOWPASSFREQCUTOFF, frequency);
1116 // This is done with 2 chained 1-pole filters, so no need to square g.
1117 State->LpFilter.coeff = lpCoeffCalc(Slot->effect.Reverb.GainHF, cw);
1119 if(isEAX)
1121 // Update the modulator line.
1122 UpdateModulator(Slot->effect.Reverb.ModulationTime,
1123 Slot->effect.Reverb.ModulationDepth,
1124 frequency, State);
1127 // Update the initial effect delay.
1128 UpdateDelayLine(Slot->effect.Reverb.ReflectionsDelay,
1129 Slot->effect.Reverb.LateReverbDelay,
1130 frequency, State);
1132 // Update the early lines.
1133 UpdateEarlyLines(Slot->effect.Reverb.Gain,
1134 Slot->effect.Reverb.ReflectionsGain,
1135 Slot->effect.Reverb.LateReverbDelay, State);
1137 // Update the decorrelator.
1138 UpdateDecorrelator(Slot->effect.Reverb.Density, frequency, State);
1140 // Get the mixing matrix coefficients (x and y).
1141 CalcMatrixCoeffs(Slot->effect.Reverb.Diffusion, &x, &y);
1142 // Then divide x into y to simplify the matrix calculation.
1143 State->Late.MixCoeff = y / x;
1145 // If the HF limit parameter is flagged, calculate an appropriate limit
1146 // based on the air absorption parameter.
1147 hfRatio = Slot->effect.Reverb.DecayHFRatio;
1148 if(Slot->effect.Reverb.DecayHFLimit &&
1149 Slot->effect.Reverb.AirAbsorptionGainHF < 1.0f)
1150 hfRatio = CalcLimitedHfRatio(hfRatio,
1151 Slot->effect.Reverb.AirAbsorptionGainHF,
1152 Slot->effect.Reverb.DecayTime);
1154 // Update the late lines.
1155 UpdateLateLines(Slot->effect.Reverb.Gain, Slot->effect.Reverb.LateReverbGain,
1156 x, Slot->effect.Reverb.Density, Slot->effect.Reverb.DecayTime,
1157 Slot->effect.Reverb.Diffusion, hfRatio, cw, frequency, State);
1159 if(isEAX)
1161 // Update the echo line.
1162 UpdateEchoLine(Slot->effect.Reverb.Gain, Slot->effect.Reverb.LateReverbGain,
1163 Slot->effect.Reverb.EchoTime, Slot->effect.Reverb.DecayTime,
1164 Slot->effect.Reverb.Diffusion, Slot->effect.Reverb.EchoDepth,
1165 hfRatio, cw, frequency, State);
1167 // Update early and late 3D panning.
1168 Update3DPanning(Context->Device, Slot->effect.Reverb.ReflectionsPan,
1169 Slot->effect.Reverb.LateReverbPan, Slot->Gain, State);
1171 else
1173 ALCdevice *Device = Context->Device;
1174 ALfloat gain = Slot->Gain;
1175 ALuint index;
1177 /* Update channel gains */
1178 gain *= aluSqrt(2.0f/Device->NumChan) * ReverbBoost;
1179 for(index = 0;index < MAXCHANNELS;index++)
1180 State->Gain[index] = 0.0f;
1181 for(index = 0;index < Device->NumChan;index++)
1183 enum Channel chan = Device->Speaker2Chan[index];
1184 State->Gain[chan] = gain;
1189 // This destroys the reverb state. It should be called only when the effect
1190 // slot has a different (or no) effect loaded over the reverb effect.
1191 static ALvoid ReverbDestroy(ALeffectState *effect)
1193 ALverbState *State = (ALverbState*)effect;
1194 if(State)
1196 free(State->SampleBuffer);
1197 State->SampleBuffer = NULL;
1198 free(State);
1202 // This creates the reverb state. It should be called only when the reverb
1203 // effect is loaded into a slot that doesn't already have a reverb effect.
1204 ALeffectState *ReverbCreate(void)
1206 ALverbState *State = NULL;
1207 ALuint index;
1209 State = malloc(sizeof(ALverbState));
1210 if(!State)
1211 return NULL;
1213 State->state.Destroy = ReverbDestroy;
1214 State->state.DeviceUpdate = ReverbDeviceUpdate;
1215 State->state.Update = ReverbUpdate;
1216 State->state.Process = VerbProcess;
1218 State->TotalSamples = 0;
1219 State->SampleBuffer = NULL;
1221 State->LpFilter.coeff = 0.0f;
1222 State->LpFilter.history[0] = 0.0f;
1223 State->LpFilter.history[1] = 0.0f;
1225 State->Mod.Delay.Mask = 0;
1226 State->Mod.Delay.Line = NULL;
1227 State->Mod.Index = 0;
1228 State->Mod.Range = 1;
1229 State->Mod.Depth = 0.0f;
1230 State->Mod.Coeff = 0.0f;
1231 State->Mod.Filter = 0.0f;
1233 State->Delay.Mask = 0;
1234 State->Delay.Line = NULL;
1235 State->DelayTap[0] = 0;
1236 State->DelayTap[1] = 0;
1238 State->Early.Gain = 0.0f;
1239 for(index = 0;index < 4;index++)
1241 State->Early.Coeff[index] = 0.0f;
1242 State->Early.Delay[index].Mask = 0;
1243 State->Early.Delay[index].Line = NULL;
1244 State->Early.Offset[index] = 0;
1247 State->Decorrelator.Mask = 0;
1248 State->Decorrelator.Line = NULL;
1249 State->DecoTap[0] = 0;
1250 State->DecoTap[1] = 0;
1251 State->DecoTap[2] = 0;
1253 State->Late.Gain = 0.0f;
1254 State->Late.DensityGain = 0.0f;
1255 State->Late.ApFeedCoeff = 0.0f;
1256 State->Late.MixCoeff = 0.0f;
1257 for(index = 0;index < 4;index++)
1259 State->Late.ApCoeff[index] = 0.0f;
1260 State->Late.ApDelay[index].Mask = 0;
1261 State->Late.ApDelay[index].Line = NULL;
1262 State->Late.ApOffset[index] = 0;
1264 State->Late.Coeff[index] = 0.0f;
1265 State->Late.Delay[index].Mask = 0;
1266 State->Late.Delay[index].Line = NULL;
1267 State->Late.Offset[index] = 0;
1269 State->Late.LpCoeff[index] = 0.0f;
1270 State->Late.LpSample[index] = 0.0f;
1273 for(index = 0;index < MAXCHANNELS;index++)
1275 State->Early.PanGain[index] = 0.0f;
1276 State->Late.PanGain[index] = 0.0f;
1279 State->Echo.DensityGain = 0.0f;
1280 State->Echo.Delay.Mask = 0;
1281 State->Echo.Delay.Line = NULL;
1282 State->Echo.ApDelay.Mask = 0;
1283 State->Echo.ApDelay.Line = NULL;
1284 State->Echo.Coeff = 0.0f;
1285 State->Echo.ApFeedCoeff = 0.0f;
1286 State->Echo.ApCoeff = 0.0f;
1287 State->Echo.Offset = 0;
1288 State->Echo.ApOffset = 0;
1289 State->Echo.LpCoeff = 0.0f;
1290 State->Echo.LpSample = 0.0f;
1291 State->Echo.MixCoeff[0] = 0.0f;
1292 State->Echo.MixCoeff[1] = 0.0f;
1294 State->Offset = 0;
1296 State->Gain = State->Late.PanGain;
1298 return &State->state;