Fix alsoftrc configuration sample comments
[openal-soft.git] / Alc / alcReverb.c
blob0a65920f03f69ea9902ae049cfb9ccd27c7eb857
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 <math.h>
24 #include <stdlib.h>
26 #include "AL/al.h"
27 #include "AL/alc.h"
28 #include "alMain.h"
29 #include "alAuxEffectSlot.h"
30 #include "alEffect.h"
31 #include "alReverb.h"
33 #ifdef HAVE_SQRTF
34 #define aluSqrt(x) ((ALfloat)sqrtf((float)(x)))
35 #else
36 #define aluSqrt(x) ((ALfloat)sqrt((double)(x)))
37 #endif
39 // fixes for mingw32.
40 #if defined(max) && !defined(__max)
41 #define __max max
42 #endif
43 #if defined(min) && !defined(__min)
44 #define __min min
45 #endif
47 typedef struct DelayLine
49 // The delay lines use sample lengths that are powers of 2 to allow
50 // bitmasking instead of modulus wrapping.
51 ALuint Mask;
52 ALfloat *Line;
53 } DelayLine;
55 struct ALverbState
57 // All delay lines are allocated as a single buffer to reduce memory
58 // fragmentation and management code.
59 ALfloat *SampleBuffer;
60 // Master effect gain.
61 ALfloat Gain;
62 // Initial effect delay and decorrelation.
63 DelayLine Delay;
64 // The tap points for the initial delay. First tap goes to early
65 // reflections, the last four decorrelate to late reverb.
66 ALuint Tap[5];
67 struct {
68 // Gain for early reflections.
69 ALfloat Gain;
70 // Early reflections are done with 4 delay lines.
71 ALfloat Coeff[4];
72 DelayLine Delay[4];
73 ALuint Offset[4];
74 } Early;
75 struct {
76 // Gain for late reverb.
77 ALfloat Gain;
78 // Attenuation to compensate for modal density and decay rate.
79 ALfloat DensityGain;
80 // The feed-back and feed-forward all-pass coefficient.
81 ALfloat ApFeedCoeff;
82 // Mixing matrix coefficient.
83 ALfloat MixCoeff;
84 // Late reverb has 4 parallel all-pass filters.
85 ALfloat ApCoeff[4];
86 DelayLine ApDelay[4];
87 ALuint ApOffset[4];
88 // In addition to 4 cyclical delay lines.
89 ALfloat Coeff[4];
90 DelayLine Delay[4];
91 ALuint Offset[4];
92 // The cyclical delay lines are low-pass filtered.
93 ALfloat LpCoeff[4][2];
94 ALfloat LpSample[4];
95 } Late;
96 // The current read offset for all delay lines.
97 ALuint Offset;
100 // All delay line lengths are specified in seconds.
102 // The lengths of the early delay lines.
103 static const ALfloat EARLY_LINE_LENGTH[4] =
105 0.0015f, 0.0045f, 0.0135f, 0.0405f
108 // The lengths of the late all-pass delay lines.
109 static const ALfloat ALLPASS_LINE_LENGTH[4] =
111 0.0151f, 0.0167f, 0.0183f, 0.0200f,
114 // The lengths of the late cyclical delay lines.
115 static const ALfloat LATE_LINE_LENGTH[4] =
117 0.0211f, 0.0311f, 0.0461f, 0.0680f
120 // The late cyclical delay lines have a variable length dependent on the
121 // effect's density parameter (inverted for some reason) and this multiplier.
122 static const ALfloat LATE_LINE_MULTIPLIER = 4.0f;
124 // Input into the late reverb is decorrelated between four channels. Their
125 // timings are dependent on a fraction and multiplier. See VerbUpdate() for
126 // the calculations involved.
127 static const ALfloat DECO_FRACTION = 1.0f / 32.0f;
128 static const ALfloat DECO_MULTIPLIER = 2.0f;
130 // The maximum length of initial delay for the master delay line (a sum of
131 // the maximum early reflection and late reverb delays).
132 static const ALfloat MASTER_LINE_LENGTH = 0.3f + 0.1f;
134 // Find the next power of 2. Actually, this will return the input value if
135 // it is already a power of 2.
136 static ALuint NextPowerOf2(ALuint value)
138 ALuint powerOf2 = 1;
140 if(value)
142 value--;
143 while(value)
145 value >>= 1;
146 powerOf2 <<= 1;
149 return powerOf2;
152 // Basic delay line input/output routines.
153 static __inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
155 return Delay->Line[offset&Delay->Mask];
158 static __inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
160 Delay->Line[offset&Delay->Mask] = in;
163 // Delay line output routine for early reflections.
164 static __inline ALfloat EarlyDelayLineOut(ALverbState *State, ALuint index)
166 return State->Early.Coeff[index] *
167 DelayLineOut(&State->Early.Delay[index],
168 State->Offset - State->Early.Offset[index]);
171 // Given an input sample, this function produces stereo output for early
172 // reflections.
173 static __inline ALvoid EarlyReflection(ALverbState *State, ALfloat in, ALfloat *out)
175 ALfloat d[4], v, f[4];
177 // Obtain the decayed results of each early delay line.
178 d[0] = EarlyDelayLineOut(State, 0);
179 d[1] = EarlyDelayLineOut(State, 1);
180 d[2] = EarlyDelayLineOut(State, 2);
181 d[3] = EarlyDelayLineOut(State, 3);
183 /* The following uses a lossless scattering junction from waveguide
184 * theory. It actually amounts to a householder mixing matrix, which
185 * will produce a maximally diffuse response, and means this can probably
186 * be considered a simple feedback delay network (FDN).
188 * ---
190 * v = 2/N / d_i
191 * ---
192 * i=1
194 v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
195 // The junction is loaded with the input here.
196 v += in;
198 // Calculate the feed values for the delay lines.
199 f[0] = v - d[0];
200 f[1] = v - d[1];
201 f[2] = v - d[2];
202 f[3] = v - d[3];
204 // Refeed the delay lines.
205 DelayLineIn(&State->Early.Delay[0], State->Offset, f[0]);
206 DelayLineIn(&State->Early.Delay[1], State->Offset, f[1]);
207 DelayLineIn(&State->Early.Delay[2], State->Offset, f[2]);
208 DelayLineIn(&State->Early.Delay[3], State->Offset, f[3]);
210 // To decorrelate the output for stereo separation, the two outputs are
211 // obtained from the inner delay lines.
212 // Output is instant by using the inputs to them instead of taking the
213 // result of the two delay lines directly (f[0] and f[3] instead of d[1]
214 // and d[2]).
215 out[0] = State->Early.Gain * f[0];
216 out[1] = State->Early.Gain * f[3];
219 // All-pass input/output routine for late reverb.
220 static __inline ALfloat LateAllPassInOut(ALverbState *State, ALuint index, ALfloat in)
222 ALfloat out;
224 out = State->Late.ApCoeff[index] *
225 DelayLineOut(&State->Late.ApDelay[index],
226 State->Offset - State->Late.ApOffset[index]);
227 out -= (State->Late.ApFeedCoeff * in);
228 DelayLineIn(&State->Late.ApDelay[index], State->Offset,
229 (State->Late.ApFeedCoeff * out) + in);
230 return out;
233 // Delay line output routine for late reverb.
234 static __inline ALfloat LateDelayLineOut(ALverbState *State, ALuint index)
236 return State->Late.Coeff[index] *
237 DelayLineOut(&State->Late.Delay[index],
238 State->Offset - State->Late.Offset[index]);
241 // Low-pass filter input/output routine for late reverb.
242 static __inline ALfloat LateLowPassInOut(ALverbState *State, ALuint index, ALfloat in)
244 State->Late.LpSample[index] = (State->Late.LpCoeff[index][0] * in) +
245 (State->Late.LpCoeff[index][1] * State->Late.LpSample[index]);
246 return State->Late.LpSample[index];
249 // Given four decorrelated input samples, this function produces stereo
250 // output for late reverb.
251 static __inline ALvoid LateReverb(ALverbState *State, ALfloat *in, ALfloat *out)
253 ALfloat d[4], f[4];
255 // Obtain the decayed results of the cyclical delay lines, and add the
256 // corresponding input channels attenuated by density. Then pass the
257 // results through the low-pass filters.
258 d[0] = LateLowPassInOut(State, 0, (State->Late.DensityGain * in[0]) +
259 LateDelayLineOut(State, 0));
260 d[1] = LateLowPassInOut(State, 1, (State->Late.DensityGain * in[1]) +
261 LateDelayLineOut(State, 1));
262 d[2] = LateLowPassInOut(State, 2, (State->Late.DensityGain * in[2]) +
263 LateDelayLineOut(State, 2));
264 d[3] = LateLowPassInOut(State, 3, (State->Late.DensityGain * in[3]) +
265 LateDelayLineOut(State, 3));
267 // To help increase diffusion, run each line through an all-pass filter.
268 // The order of the all-pass filters is selected so that the shortest
269 // all-pass filter will feed the shortest delay line.
270 d[0] = LateAllPassInOut(State, 1, d[0]);
271 d[1] = LateAllPassInOut(State, 3, d[1]);
272 d[2] = LateAllPassInOut(State, 0, d[2]);
273 d[3] = LateAllPassInOut(State, 2, d[3]);
275 /* Late reverb is done with a modified feedback delay network (FDN)
276 * topology. Four input lines are each fed through their own all-pass
277 * filter and then into the mixing matrix. The four outputs of the
278 * mixing matrix are then cycled back to the inputs. Each output feeds
279 * a different input to form a circlular feed cycle.
281 * The mixing matrix used is a 4D skew-symmetric rotation matrix derived
282 * using a single unitary rotational parameter:
284 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
285 * [ -a, d, c, -b ]
286 * [ -b, -c, d, a ]
287 * [ -c, b, -a, d ]
289 * The rotation is constructed from the effect's diffusion parameter,
290 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
291 * with differing signs, and d is the coefficient x. The matrix is thus:
293 * [ x, y, -y, y ] x = 1 - (0.5 diffusion^3)
294 * [ -y, x, y, y ] y = sqrt((1 - x^2) / 3)
295 * [ y, -y, x, y ]
296 * [ -y, -y, -y, x ]
298 * To reduce the number of multiplies, the x coefficient is applied with
299 * the cyclical delay line coefficients. Thus only the y coefficient is
300 * applied when mixing, and is modified to be: y / x.
302 f[0] = d[0] + (State->Late.MixCoeff * ( d[1] - d[2] + d[3]));
303 f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
304 f[2] = d[2] + (State->Late.MixCoeff * ( d[0] - d[1] + d[3]));
305 f[3] = d[3] + (State->Late.MixCoeff * (-d[0] - d[1] - d[2]));
307 // Output is tapped at the input to the shortest two cyclical delay
308 // lines, attenuated by the late reverb gain (which is attenuated by the
309 // mixing coefficient x).
310 out[0] = State->Late.Gain * f[0];
311 out[1] = State->Late.Gain * f[1];
313 // The delay lines are fed circularly in the order:
314 // 0 -> 1 -> 3 -> 2 -> 0 ...
315 DelayLineIn(&State->Late.Delay[0], State->Offset, f[2]);
316 DelayLineIn(&State->Late.Delay[1], State->Offset, f[0]);
317 DelayLineIn(&State->Late.Delay[2], State->Offset, f[3]);
318 DelayLineIn(&State->Late.Delay[3], State->Offset, f[1]);
321 // This creates the reverb state. It should be called only when the reverb
322 // effect is loaded into a slot that doesn't already have a reverb effect.
323 ALverbState *VerbCreate(ALCcontext *Context)
325 ALverbState *State = NULL;
326 ALuint samples, length[13], totalLength, index;
328 State = malloc(sizeof(ALverbState));
329 if(!State)
330 return NULL;
332 // All line lengths are powers of 2, calculated from their lengths, with
333 // an additional sample in case of rounding errors.
335 // See VerbUpdate() for an explanation of the additional calculation
336 // added to the master line length.
337 samples = (ALuint)
338 ((MASTER_LINE_LENGTH +
339 (LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER) *
340 (DECO_FRACTION * ((DECO_MULTIPLIER * DECO_MULTIPLIER *
341 DECO_MULTIPLIER) - 1.0f)))) *
342 Context->Frequency) + 1;
343 length[0] = NextPowerOf2(samples);
344 totalLength = length[0];
345 for(index = 0;index < 4;index++)
347 samples = (ALuint)(EARLY_LINE_LENGTH[index] * Context->Frequency) + 1;
348 length[1 + index] = NextPowerOf2(samples);
349 totalLength += length[1 + index];
351 for(index = 0;index < 4;index++)
353 samples = (ALuint)(ALLPASS_LINE_LENGTH[index] * Context->Frequency) + 1;
354 length[5 + index] = NextPowerOf2(samples);
355 totalLength += length[5 + index];
357 for(index = 0;index < 4;index++)
359 samples = (ALuint)(LATE_LINE_LENGTH[index] *
360 (1.0f + LATE_LINE_MULTIPLIER) * Context->Frequency) + 1;
361 length[9 + index] = NextPowerOf2(samples);
362 totalLength += length[9 + index];
365 // All lines share a single sample buffer.
366 State->SampleBuffer = malloc(totalLength * sizeof(ALfloat));
367 if(!State->SampleBuffer)
369 free(State);
370 return NULL;
372 for(index = 0; index < totalLength;index++)
373 State->SampleBuffer[index] = 0.0f;
375 // Each one has its mask and start address calculated one time.
376 State->Gain = 0.0f;
377 State->Delay.Mask = length[0] - 1;
378 State->Delay.Line = &State->SampleBuffer[0];
379 totalLength = length[0];
381 State->Tap[0] = 0;
382 State->Tap[1] = 0;
383 State->Tap[2] = 0;
384 State->Tap[3] = 0;
385 State->Tap[4] = 0;
387 State->Early.Gain = 0.0f;
388 for(index = 0;index < 4;index++)
390 State->Early.Coeff[index] = 0.0f;
391 State->Early.Delay[index].Mask = length[1 + index] - 1;
392 State->Early.Delay[index].Line = &State->SampleBuffer[totalLength];
393 totalLength += length[1 + index];
395 // The early delay lines have their read offsets calculated once.
396 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *
397 Context->Frequency);
400 State->Late.Gain = 0.0f;
401 State->Late.DensityGain = 0.0f;
402 State->Late.ApFeedCoeff = 0.0f;
403 State->Late.MixCoeff = 0.0f;
405 for(index = 0;index < 4;index++)
407 State->Late.ApCoeff[index] = 0.0f;
408 State->Late.ApDelay[index].Mask = length[5 + index] - 1;
409 State->Late.ApDelay[index].Line = &State->SampleBuffer[totalLength];
410 totalLength += length[5 + index];
412 // The late all-pass lines have their read offsets calculated once.
413 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *
414 Context->Frequency);
417 for(index = 0;index < 4;index++)
419 State->Late.Coeff[index] = 0.0f;
420 State->Late.Delay[index].Mask = length[9 + index] - 1;
421 State->Late.Delay[index].Line = &State->SampleBuffer[totalLength];
422 totalLength += length[9 + index];
424 State->Late.Offset[index] = 0;
426 State->Late.LpCoeff[index][0] = 0.0f;
427 State->Late.LpCoeff[index][1] = 0.0f;
428 State->Late.LpSample[index] = 0.0f;
431 State->Offset = 0;
432 return State;
435 // This destroys the reverb state. It should be called only when the effect
436 // slot has a different (or no) effect loaded over the reverb effect.
437 ALvoid VerbDestroy(ALverbState *State)
439 if(State)
441 free(State->SampleBuffer);
442 State->SampleBuffer = NULL;
443 free(State);
447 // This updates the reverb state. This is called any time the reverb effect
448 // is loaded into a slot.
449 ALvoid VerbUpdate(ALCcontext *Context, ALeffectslot *Slot, ALeffect *Effect)
451 ALverbState *State = Slot->ReverbState;
452 ALuint index;
453 ALfloat length, mixCoeff, cw, g, lpCoeff;
454 ALfloat hfRatio = Effect->Reverb.DecayHFRatio;
456 // Calculate the master gain (from the slot and master effect gain).
457 State->Gain = Slot->Gain * Effect->Reverb.Gain;
459 // Calculate the initial delay taps.
460 length = Effect->Reverb.ReflectionsDelay;
461 State->Tap[0] = (ALuint)(length * Context->Frequency);
463 length += Effect->Reverb.LateReverbDelay;
465 /* The four inputs to the late reverb are decorrelated to smooth the
466 * initial reverb and reduce harsh echos. The timings are calculated as
467 * multiples of a fraction of the smallest cyclical delay time. This
468 * result is then adjusted so that the first tap occurs immediately (all
469 * taps are reduced by the shortest fraction).
471 * offset[index] = ((FRACTION MULTIPLIER^index) - 1) delay
473 for(index = 0;index < 4;index++)
475 length += LATE_LINE_LENGTH[0] *
476 (1.0f + (Effect->Reverb.Density * LATE_LINE_MULTIPLIER)) *
477 (DECO_FRACTION * (pow(DECO_MULTIPLIER, (ALfloat)index) - 1.0f));
478 State->Tap[1 + index] = (ALuint)(length * Context->Frequency);
481 // Set the early reflections gain.
482 State->Early.Gain = Effect->Reverb.ReflectionsGain;
484 // Calculate the gain (coefficient) for each early delay line.
485 for(index = 0;index < 4;index++)
486 State->Early.Coeff[index] = pow(10.0f, EARLY_LINE_LENGTH[index] /
487 Effect->Reverb.LateReverbDelay *
488 -60.0f / 20.0f);
490 // Calculate the first mixing matrix coefficient (x).
491 mixCoeff = 1.0f - (0.5f * pow(Effect->Reverb.Diffusion, 3.0f));
493 // Set the late reverb gain. Since the output is tapped prior to the
494 // application of the delay line coefficients, this gain needs to be
495 // attenuated by the mix coefficient from above.
496 State->Late.Gain = Effect->Reverb.LateReverbGain * mixCoeff;
498 /* To compensate for changes in modal density and decay time of the late
499 * reverb signal, the input is attenuated based on the maximal energy of
500 * the outgoing signal. This is calculated as the ratio between a
501 * reference value and the current approximation of energy for the output
502 * signal.
504 * Reverb output matches exponential decay of the form Sum(a^n), where a
505 * is the attenuation coefficient, and n is the sample ranging from 0 to
506 * infinity. The signal energy can thus be approximated using the area
507 * under this curve, calculated as: 1 / (1 - a).
509 * The reference energy is calculated from a signal at the lowest (effect
510 * at 1.0) density with a decay time of one second.
512 * The coefficient is calculated as the average length of the cyclical
513 * delay lines. This produces a better result than calculating the gain
514 * for each line individually (most likely a side effect of diffusion).
516 * The final result is the square root of the ratio bound to a maximum
517 * value of 1 (no amplification) and attenuated by 1 / sqrt(2) to
518 * compensate for the four decorrelated inputs.
520 length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
521 LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]);
522 g = length * (1.0f + LATE_LINE_MULTIPLIER) * 0.25f;
523 g = pow(10.0f, g * -60.0f / 20.0f);
524 g = 1.0f / (1.0f - g);
525 length *= 1.0f + (Effect->Reverb.Density * LATE_LINE_MULTIPLIER) * 0.25f;
526 length = pow(10.0f, length / Effect->Reverb.DecayTime * -60.0f / 20.0f);
527 length = 1.0f / (1.0f - length);
528 State->Late.DensityGain = 0.707106f * __min(aluSqrt(g / length), 1.0f);
530 // Calculate the all-pass feed-back and feed-forward coefficient.
531 State->Late.ApFeedCoeff = 0.6f * pow(Effect->Reverb.Diffusion, 3.0f);
533 // Calculate the mixing matrix coefficient (y / x).
534 g = aluSqrt((1.0f - (mixCoeff * mixCoeff)) / 3.0f);
535 State->Late.MixCoeff = g / mixCoeff;
537 for(index = 0;index < 4;index++)
539 // Calculate the gain (coefficient) for each all-pass line.
540 State->Late.ApCoeff[index] = pow(10.0f, ALLPASS_LINE_LENGTH[index] /
541 Effect->Reverb.DecayTime *
542 -60.0f / 20.0f);
545 // If the HF limit parameter is flagged, calculate an appropriate limit
546 // based on the air absorption parameter.
547 if(Effect->Reverb.DecayHFLimit && Effect->Reverb.AirAbsorptionGainHF < 1.0f)
549 ALfloat limitRatio;
551 // For each of the cyclical delays, find the attenuation due to air
552 // absorption in dB (converting delay time to meters using the speed
553 // of sound). Then reversing the decay equation, solve for HF ratio.
554 // The delay length is cancelled out of the equation, so it can be
555 // calculated once for all lines.
556 limitRatio = 1.0f / (log10(Effect->Reverb.AirAbsorptionGainHF) *
557 SPEEDOFSOUNDMETRESPERSEC *
558 Effect->Reverb.DecayTime / -60.0f * 20.0f);
559 // Need to limit the result to a minimum of 0.1, just like the HF
560 // ratio parameter.
561 limitRatio = __max(limitRatio, 0.1f);
563 // Using the limit calculated above, apply the upper bound to the
564 // HF ratio.
565 hfRatio = __min(hfRatio, limitRatio);
568 // Calculate the filter frequency for low-pass or high-pass depending on
569 // whether the HF ratio is above 1.
570 cw = 2.0f * M_PI * LOWPASSFREQCUTOFF / Context->Frequency;
571 if(hfRatio > 1.0f)
572 cw = M_PI - cw;
573 cw = cos(cw);
575 for(index = 0;index < 4;index++)
577 // Calculate the length (in seconds) of each cyclical delay line.
578 length = LATE_LINE_LENGTH[index] * (1.0f + (Effect->Reverb.Density *
579 LATE_LINE_MULTIPLIER));
580 // Calculate the delay offset for the cyclical delay lines.
581 State->Late.Offset[index] = (ALuint)(length * Context->Frequency);
583 // Calculate the gain (coefficient) for each cyclical line.
584 State->Late.Coeff[index] = pow(10.0f, length / Effect->Reverb.DecayTime *
585 -60.0f / 20.0f);
587 // Calculate the decay equation for each low-pass filter.
588 g = pow(10.0f, length / (Effect->Reverb.DecayTime * hfRatio) *
589 -60.0f / 20.0f);
590 if (hfRatio > 1.0f)
591 g = State->Late.Coeff[index] / g;
592 else
593 g = g / State->Late.Coeff[index];
594 g = __max(g, 0.1f);
595 g *= g;
597 // Calculate the gain (coefficient) for each low-pass filter.
598 lpCoeff = 0.0f;
599 if(g < 0.9999f) // 1-epsilon
600 lpCoeff = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
602 // Very low decay times will produce minimal output, so apply an
603 // upper bound to the coefficient.
604 lpCoeff = __min(lpCoeff, 0.98f);
606 // Calculate the filter coefficients for high-pass or low-pass
607 // dependent on HF ratio being above 1.
608 if(hfRatio > 1.0f) {
609 State->Late.LpCoeff[index][0] = 1.0f + lpCoeff;
610 State->Late.LpCoeff[index][1] = -lpCoeff;
611 } else {
612 State->Late.LpCoeff[index][0] = 1.0f - lpCoeff;
613 State->Late.LpCoeff[index][1] = lpCoeff;
616 // Attenuate the cyclical line coefficients by the mixing coefficient
617 // (x).
618 State->Late.Coeff[index] *= mixCoeff;
622 // This processes the reverb state, given the input samples and an output
623 // buffer.
624 ALvoid VerbProcess(ALverbState *State, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
626 ALuint index;
627 ALfloat in[4], early[2], late[2], out[2];
629 for(index = 0;index < SamplesToDo;index++)
631 // Feed the initial delay line.
632 DelayLineIn(&State->Delay, State->Offset, SamplesIn[index]);
634 // Calculate the early reflection from the first delay tap.
635 in[0] = DelayLineOut(&State->Delay, State->Offset - State->Tap[0]);
636 EarlyReflection(State, in[0], early);
638 // Calculate the late reverb from the last four delay taps.
639 in[0] = DelayLineOut(&State->Delay, State->Offset - State->Tap[1]);
640 in[1] = DelayLineOut(&State->Delay, State->Offset - State->Tap[2]);
641 in[2] = DelayLineOut(&State->Delay, State->Offset - State->Tap[3]);
642 in[3] = DelayLineOut(&State->Delay, State->Offset - State->Tap[4]);
643 LateReverb(State, in, late);
645 // Mix early reflections and late reverb.
646 out[0] = State->Gain * (early[0] + late[0]);
647 out[1] = State->Gain * (early[1] + late[1]);
649 // Step all delays forward one sample.
650 State->Offset++;
652 // Output the results.
653 SamplesOut[index][FRONT_LEFT] += out[0];
654 SamplesOut[index][FRONT_RIGHT] += out[1];
655 SamplesOut[index][SIDE_LEFT] += out[0];
656 SamplesOut[index][SIDE_RIGHT] += out[1];
657 SamplesOut[index][BACK_LEFT] += out[0];
658 SamplesOut[index][BACK_RIGHT] += out[1];