Mercurial > audio-send
view Alc/alcReverb.c @ 11:17ace5a879b2 hardcoded_but_working
fixed JNI include problem
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 28 Oct 2011 00:27:59 -0700 |
parents | f9476ff7637e |
children |
line wrap: on
line source
1 /**2 * Reverb for the OpenAL cross platform audio library3 * Copyright (C) 2008-2009 by Christopher Fitzgerald.4 * This library is free software; you can redistribute it and/or5 * modify it under the terms of the GNU Library General Public6 * License as published by the Free Software Foundation; either7 * version 2 of the License, or (at your option) any later version.8 *9 * This library is distributed in the hope that it will be useful,10 * but WITHOUT ANY WARRANTY; without even the implied warranty of11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12 * Library General Public License for more details.13 *14 * You should have received a copy of the GNU Library General Public15 * License along with this library; if not, write to the16 * 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.html19 */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 DelayLine36 {37 // The delay lines use sample lengths that are powers of 2 to allow the38 // 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 memory48 // 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-wrapped58 // 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 early69 // 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 the79 // 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 the85 // 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 of91 // 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 the109 // EAX path).110 ALfloat PanGain[MAXCHANNELS];111 } Late;112 struct {113 // Attenuation to compensate for the modal density and decay rate of114 // 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 from134 // Late.PanGain)135 ALfloat *Gain;136 } ALverbState;138 /* This is a user config option for modifying the overall output of the reverb139 * effect.140 */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 controlled147 * by the modulation depth. The current value of 0.1 will allow it to swing148 * from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the149 * sampler to stall on the downswing, and above 1 it will cause it to sample150 * backwards.151 */152 static const ALfloat MODULATION_DEPTH_COEFF = 0.1f;154 /* A filter is used to avoid the terrible distortion caused by changing155 * modulation time and/or depth. To be consistent across different sample156 * rates, the coefficient must be raised to a constant divided by the sample157 * rate: coeff^(constant / rate).158 */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 off163 // 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. Their167 // timings are dependent on a fraction and multiplier. See the168 // 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] =176 {177 0.0015f, 0.0045f, 0.0135f, 0.0405f178 };180 // The lengths of the late all-pass delay lines.181 static const ALfloat ALLPASS_LINE_LENGTH[4] =182 {183 0.0151f, 0.0167f, 0.0183f, 0.0200f,184 };186 // The lengths of the late cyclical delay lines.187 static const ALfloat LATE_LINE_LENGTH[4] =188 {189 0.0211f, 0.0311f, 0.0461f, 0.0680f190 };192 // The late cyclical delay lines have a variable length dependent on the193 // effect's density parameter (inverted for some reason) and this multiplier.194 static const ALfloat LATE_LINE_MULTIPLIER = 4.0f;196 // Calculate the length of a delay line and store its mask and offset.197 static ALuint CalcLineLength(ALfloat length, ALintptrEXT offset, ALuint frequency, DelayLine *Delay)198 {199 ALuint samples;201 // All line lengths are powers of 2, calculated from their lengths, with202 // an additional sample in case of rounding errors.203 samples = NextPowerOf2((ALuint)(length * frequency) + 1);204 // All lines share a single sample buffer.205 Delay->Mask = samples - 1;206 Delay->Line = (ALfloat*)offset;207 // Return the sample count for accumulation.208 return samples;209 }211 // Given the allocated sample buffer, this function updates each delay line212 // offset.213 static __inline ALvoid RealizeLineOffset(ALfloat * sampleBuffer, DelayLine *Delay)214 {215 Delay->Line = &sampleBuffer[(ALintptrEXT)Delay->Line];216 }218 /* Calculates the delay line metrics and allocates the shared sample buffer219 * for all lines given a flag indicating whether or not to allocate the EAX-220 * related delays (eaxFlag) and the sample rate (frequency). If an221 * allocation failure occurs, it returns AL_FALSE.222 */223 static ALboolean AllocLines(ALboolean eaxFlag, ALuint frequency, ALverbState *State)224 {225 ALuint totalSamples, index;226 ALfloat length;227 ALfloat *newBuffer = NULL;229 // All delay line lengths are calculated to accomodate the full range of230 // lengths given their respective paramters.231 totalSamples = 0;232 if(eaxFlag)233 {234 /* The modulator's line length is calculated from the maximum235 * modulation time and depth coefficient, and halfed for the low-to-236 * high frequency swing. An additional sample is added to keep it237 * stable when there is no modulation.238 */239 length = (AL_EAXREVERB_MAX_MODULATION_TIME * MODULATION_DEPTH_COEFF /240 2.0f) + (1.0f / frequency);241 totalSamples += CalcLineLength(length, totalSamples, frequency,242 &State->Mod.Delay);243 }245 // The initial delay is the sum of the reflections and late reverb246 // delays.247 if(eaxFlag)248 length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY +249 AL_EAXREVERB_MAX_LATE_REVERB_DELAY;250 else251 length = AL_REVERB_MAX_REFLECTIONS_DELAY +252 AL_REVERB_MAX_LATE_REVERB_DELAY;253 totalSamples += CalcLineLength(length, totalSamples, frequency,254 &State->Delay);256 // The early reflection lines.257 for(index = 0;index < 4;index++)258 totalSamples += CalcLineLength(EARLY_LINE_LENGTH[index], totalSamples,259 frequency, &State->Early.Delay[index]);261 // The decorrelator line is calculated from the lowest reverb density (a262 // parameter value of 1).263 length = (DECO_FRACTION * DECO_MULTIPLIER * DECO_MULTIPLIER) *264 LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER);265 totalSamples += CalcLineLength(length, totalSamples, frequency,266 &State->Decorrelator);268 // The late all-pass lines.269 for(index = 0;index < 4;index++)270 totalSamples += CalcLineLength(ALLPASS_LINE_LENGTH[index], totalSamples,271 frequency, &State->Late.ApDelay[index]);273 // The late delay lines are calculated from the lowest reverb density.274 for(index = 0;index < 4;index++)275 {276 length = LATE_LINE_LENGTH[index] * (1.0f + LATE_LINE_MULTIPLIER);277 totalSamples += CalcLineLength(length, totalSamples, frequency,278 &State->Late.Delay[index]);279 }281 if(eaxFlag)282 {283 // The echo all-pass and delay lines.284 totalSamples += CalcLineLength(ECHO_ALLPASS_LENGTH, totalSamples,285 frequency, &State->Echo.ApDelay);286 totalSamples += CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME, totalSamples,287 frequency, &State->Echo.Delay);288 }290 if(totalSamples != State->TotalSamples)291 {292 newBuffer = realloc(State->SampleBuffer, sizeof(ALfloat) * totalSamples);293 if(newBuffer == NULL)294 return AL_FALSE;295 State->SampleBuffer = newBuffer;296 State->TotalSamples = totalSamples;297 }299 // Update all delays to reflect the new sample buffer.300 RealizeLineOffset(State->SampleBuffer, &State->Delay);301 RealizeLineOffset(State->SampleBuffer, &State->Decorrelator);302 for(index = 0;index < 4;index++)303 {304 RealizeLineOffset(State->SampleBuffer, &State->Early.Delay[index]);305 RealizeLineOffset(State->SampleBuffer, &State->Late.ApDelay[index]);306 RealizeLineOffset(State->SampleBuffer, &State->Late.Delay[index]);307 }308 if(eaxFlag)309 {310 RealizeLineOffset(State->SampleBuffer, &State->Mod.Delay);311 RealizeLineOffset(State->SampleBuffer, &State->Echo.ApDelay);312 RealizeLineOffset(State->SampleBuffer, &State->Echo.Delay);313 }315 // Clear the sample buffer.316 for(index = 0;index < State->TotalSamples;index++)317 State->SampleBuffer[index] = 0.0f;319 return AL_TRUE;320 }322 // Calculate a decay coefficient given the length of each cycle and the time323 // until the decay reaches -60 dB.324 static __inline ALfloat CalcDecayCoeff(ALfloat length, ALfloat decayTime)325 {326 return aluPow(0.001f/*-60 dB*/, length/decayTime);327 }329 // Calculate a decay length from a coefficient and the time until the decay330 // reaches -60 dB.331 static __inline ALfloat CalcDecayLength(ALfloat coeff, ALfloat decayTime)332 {333 return log10(coeff) * decayTime / -3.0f/*log10(0.001)*/;334 }336 // Calculate the high frequency parameter for the I3DL2 coefficient337 // calculation.338 static __inline ALfloat CalcI3DL2HFreq(ALfloat hfRef, ALuint frequency)339 {340 return cos(2.0f * M_PI * hfRef / frequency);341 }343 // Calculate an attenuation to be applied to the input of any echo models to344 // compensate for modal density and decay time.345 static __inline ALfloat CalcDensityGain(ALfloat a)346 {347 /* The energy of a signal can be obtained by finding the area under the348 * squared signal. This takes the form of Sum(x_n^2), where x is the349 * amplitude for the sample n.350 *351 * Decaying feedback matches exponential decay of the form Sum(a^n),352 * where a is the attenuation coefficient, and n is the sample. The area353 * under this decay curve can be calculated as: 1 / (1 - a).354 *355 * Modifying the above equation to find the squared area under the curve356 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be357 * calculated by inverting the square root of this approximation,358 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).359 */360 return aluSqrt(1.0f - (a * a));361 }363 // Calculate the mixing matrix coefficients given a diffusion factor.364 static __inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)365 {366 ALfloat n, t;368 // The matrix is of order 4, so n is sqrt (4 - 1).369 n = aluSqrt(3.0f);370 t = diffusion * atan(n);372 // Calculate the first mixing matrix coefficient.373 *x = cos(t);374 // Calculate the second mixing matrix coefficient.375 *y = sin(t) / n;376 }378 // Calculate the limited HF ratio for use with the late reverb low-pass379 // filters.380 static ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)381 {382 ALfloat limitRatio;384 /* Find the attenuation due to air absorption in dB (converting delay385 * time to meters using the speed of sound). Then reversing the decay386 * equation, solve for HF ratio. The delay length is cancelled out of387 * the equation, so it can be calculated once for all lines.388 */389 limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *390 SPEEDOFSOUNDMETRESPERSEC);391 /* Using the limit calculated above, apply the upper bound to the HF392 * ratio. Also need to limit the result to a minimum of 0.1, just like the393 * HF ratio parameter. */394 return clampf(limitRatio, 0.1f, hfRatio);395 }397 // Calculate the coefficient for a HF (and eventually LF) decay damping398 // filter.399 static __inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)400 {401 ALfloat coeff, g;403 // Eventually this should boost the high frequencies when the ratio404 // exceeds 1.405 coeff = 0.0f;406 if (hfRatio < 1.0f)407 {408 // Calculate the low-pass coefficient by dividing the HF decay409 // coefficient by the full decay coefficient.410 g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;412 // Damping is done with a 1-pole filter, so g needs to be squared.413 g *= g;414 coeff = lpCoeffCalc(g, cw);416 // Very low decay times will produce minimal output, so apply an417 // upper bound to the coefficient.418 coeff = minf(coeff, 0.98f);419 }420 return coeff;421 }423 // Update the EAX modulation index, range, and depth. Keep in mind that this424 // kind of vibrato is additive and not multiplicative as one may expect. The425 // downswing will sound stronger than the upswing.426 static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALverbState *State)427 {428 ALfloat length;430 /* Modulation is calculated in two parts.431 *432 * The modulation time effects the sinus applied to the change in433 * frequency. An index out of the current time range (both in samples)434 * is incremented each sample. The range is bound to a reasonable435 * minimum (1 sample) and when the timing changes, the index is rescaled436 * to the new range (to keep the sinus consistent).437 */438 length = modTime * frequency;439 if (length >= 1.0f) {440 State->Mod.Index = (ALuint)(State->Mod.Index * length /441 State->Mod.Range);442 State->Mod.Range = (ALuint)length;443 } else {444 State->Mod.Index = 0;445 State->Mod.Range = 1;446 }448 /* The modulation depth effects the amount of frequency change over the449 * range of the sinus. It needs to be scaled by the modulation time so450 * that a given depth produces a consistent change in frequency over all451 * ranges of time. Since the depth is applied to a sinus value, it needs452 * to be halfed once for the sinus range and again for the sinus swing453 * in time (half of it is spent decreasing the frequency, half is spent454 * increasing it).455 */456 State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /457 2.0f * frequency;458 }460 // Update the offsets for the initial effect delay line.461 static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALverbState *State)462 {463 // Calculate the initial delay taps.464 State->DelayTap[0] = (ALuint)(earlyDelay * frequency);465 State->DelayTap[1] = (ALuint)((earlyDelay + lateDelay) * frequency);466 }468 // Update the early reflections gain and line coefficients.469 static ALvoid UpdateEarlyLines(ALfloat reverbGain, ALfloat earlyGain, ALfloat lateDelay, ALverbState *State)470 {471 ALuint index;473 // Calculate the early reflections gain (from the master effect gain, and474 // reflections gain parameters) with a constant attenuation of 0.5.475 State->Early.Gain = 0.5f * reverbGain * earlyGain;477 // Calculate the gain (coefficient) for each early delay line using the478 // late delay time. This expands the early reflections to the start of479 // the late reverb.480 for(index = 0;index < 4;index++)481 State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],482 lateDelay);483 }485 // Update the offsets for the decorrelator line.486 static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALverbState *State)487 {488 ALuint index;489 ALfloat length;491 /* The late reverb inputs are decorrelated to smooth the reverb tail and492 * reduce harsh echos. The first tap occurs immediately, while the493 * remaining taps are delayed by multiples of a fraction of the smallest494 * cyclical delay time.495 *496 * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay497 */498 for(index = 0;index < 3;index++)499 {500 length = (DECO_FRACTION * aluPow(DECO_MULTIPLIER, (ALfloat)index)) *501 LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));502 State->DecoTap[index] = (ALuint)(length * frequency);503 }504 }506 // Update the late reverb gains, line lengths, and line coefficients.507 static ALvoid UpdateLateLines(ALfloat reverbGain, ALfloat lateGain, ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)508 {509 ALfloat length;510 ALuint index;512 /* Calculate the late reverb gain (from the master effect gain, and late513 * reverb gain parameters). Since the output is tapped prior to the514 * application of the next delay line coefficients, this gain needs to be515 * attenuated by the 'x' mixing matrix coefficient as well.516 */517 State->Late.Gain = reverbGain * lateGain * xMix;519 /* To compensate for changes in modal density and decay time of the late520 * reverb signal, the input is attenuated based on the maximal energy of521 * the outgoing signal. This approximation is used to keep the apparent522 * energy of the signal equal for all ranges of density and decay time.523 *524 * The average length of the cyclcical delay lines is used to calculate525 * the attenuation coefficient.526 */527 length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +528 LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;529 length *= 1.0f + (density * LATE_LINE_MULTIPLIER);530 State->Late.DensityGain = CalcDensityGain(CalcDecayCoeff(length,531 decayTime));533 // Calculate the all-pass feed-back and feed-forward coefficient.534 State->Late.ApFeedCoeff = 0.5f * aluPow(diffusion, 2.0f);536 for(index = 0;index < 4;index++)537 {538 // Calculate the gain (coefficient) for each all-pass line.539 State->Late.ApCoeff[index] = CalcDecayCoeff(ALLPASS_LINE_LENGTH[index],540 decayTime);542 // Calculate the length (in seconds) of each cyclical delay line.543 length = LATE_LINE_LENGTH[index] * (1.0f + (density *544 LATE_LINE_MULTIPLIER));546 // Calculate the delay offset for each cyclical delay line.547 State->Late.Offset[index] = (ALuint)(length * frequency);549 // Calculate the gain (coefficient) for each cyclical line.550 State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);552 // Calculate the damping coefficient for each low-pass filter.553 State->Late.LpCoeff[index] =554 CalcDampingCoeff(hfRatio, length, decayTime,555 State->Late.Coeff[index], cw);557 // Attenuate the cyclical line coefficients by the mixing coefficient558 // (x).559 State->Late.Coeff[index] *= xMix;560 }561 }563 // Update the echo gain, line offset, line coefficients, and mixing564 // coefficients.565 static ALvoid UpdateEchoLine(ALfloat reverbGain, ALfloat lateGain, ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)566 {567 // Update the offset and coefficient for the echo delay line.568 State->Echo.Offset = (ALuint)(echoTime * frequency);570 // Calculate the decay coefficient for the echo line.571 State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);573 // Calculate the energy-based attenuation coefficient for the echo delay574 // line.575 State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);577 // Calculate the echo all-pass feed coefficient.578 State->Echo.ApFeedCoeff = 0.5f * aluPow(diffusion, 2.0f);580 // Calculate the echo all-pass attenuation coefficient.581 State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);583 // Calculate the damping coefficient for each low-pass filter.584 State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,585 State->Echo.Coeff, cw);587 /* Calculate the echo mixing coefficients. The first is applied to the588 * echo itself. The second is used to attenuate the late reverb when589 * echo depth is high and diffusion is low, so the echo is slightly590 * stronger than the decorrelated echos in the reverb tail.591 */592 State->Echo.MixCoeff[0] = reverbGain * lateGain * echoDepth;593 State->Echo.MixCoeff[1] = 1.0f - (echoDepth * 0.5f * (1.0f - diffusion));594 }596 // Update the early and late 3D panning gains.597 static ALvoid Update3DPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALverbState *State)598 {599 ALfloat earlyPan[3] = { ReflectionsPan[0], ReflectionsPan[1],600 ReflectionsPan[2] };601 ALfloat latePan[3] = { LateReverbPan[0], LateReverbPan[1],602 LateReverbPan[2] };603 const ALfloat *speakerGain;604 ALfloat ambientGain;605 ALfloat dirGain;606 ALfloat length;607 ALuint index;608 ALint pos;610 Gain *= ReverbBoost;612 // Attenuate non-directional reverb according to the number of channels613 ambientGain = aluSqrt(2.0f/Device->NumChan);615 // Calculate the 3D-panning gains for the early reflections and late616 // reverb.617 length = earlyPan[0]*earlyPan[0] + earlyPan[1]*earlyPan[1] + earlyPan[2]*earlyPan[2];618 if(length > 1.0f)619 {620 length = 1.0f / aluSqrt(length);621 earlyPan[0] *= length;622 earlyPan[1] *= length;623 earlyPan[2] *= length;624 }625 length = latePan[0]*latePan[0] + latePan[1]*latePan[1] + latePan[2]*latePan[2];626 if(length > 1.0f)627 {628 length = 1.0f / aluSqrt(length);629 latePan[0] *= length;630 latePan[1] *= length;631 latePan[2] *= length;632 }634 /* This code applies directional reverb just like the mixer applies635 * directional sources. It diffuses the sound toward all speakers as the636 * magnitude of the panning vector drops, which is only a rough637 * approximation of the expansion of sound across the speakers from the638 * panning direction.639 */640 pos = aluCart2LUTpos(earlyPan[2], earlyPan[0]);641 speakerGain = Device->PanningLUT[pos];642 dirGain = aluSqrt((earlyPan[0] * earlyPan[0]) + (earlyPan[2] * earlyPan[2]));644 for(index = 0;index < MAXCHANNELS;index++)645 State->Early.PanGain[index] = 0.0f;646 for(index = 0;index < Device->NumChan;index++)647 {648 enum Channel chan = Device->Speaker2Chan[index];649 State->Early.PanGain[chan] = lerp(ambientGain, speakerGain[chan], dirGain) * Gain;650 }653 pos = aluCart2LUTpos(latePan[2], latePan[0]);654 speakerGain = Device->PanningLUT[pos];655 dirGain = aluSqrt((latePan[0] * latePan[0]) + (latePan[2] * latePan[2]));657 for(index = 0;index < MAXCHANNELS;index++)658 State->Late.PanGain[index] = 0.0f;659 for(index = 0;index < Device->NumChan;index++)660 {661 enum Channel chan = Device->Speaker2Chan[index];662 State->Late.PanGain[chan] = lerp(ambientGain, speakerGain[chan], dirGain) * Gain;663 }664 }666 // Basic delay line input/output routines.667 static __inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)668 {669 return Delay->Line[offset&Delay->Mask];670 }672 static __inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)673 {674 Delay->Line[offset&Delay->Mask] = in;675 }677 // Attenuated delay line output routine.678 static __inline ALfloat AttenuatedDelayLineOut(DelayLine *Delay, ALuint offset, ALfloat coeff)679 {680 return coeff * Delay->Line[offset&Delay->Mask];681 }683 // Basic attenuated all-pass input/output routine.684 static __inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)685 {686 ALfloat out, feed;688 out = DelayLineOut(Delay, outOffset);689 feed = feedCoeff * in;690 DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);692 // The time-based attenuation is only applied to the delay output to693 // keep it from affecting the feed-back path (which is already controlled694 // by the all-pass feed coefficient).695 return (coeff * out) - feed;696 }698 // Given an input sample, this function produces modulation for the late699 // reverb.700 static __inline ALfloat EAXModulation(ALverbState *State, ALfloat in)701 {702 ALfloat sinus, frac;703 ALuint offset;704 ALfloat out0, out1;706 // Calculate the sinus rythm (dependent on modulation time and the707 // sampling rate). The center of the sinus is moved to reduce the delay708 // of the effect when the time or depth are low.709 sinus = 1.0f - cos(2.0f * M_PI * State->Mod.Index / State->Mod.Range);711 // The depth determines the range over which to read the input samples712 // from, so it must be filtered to reduce the distortion caused by even713 // small parameter changes.714 State->Mod.Filter = lerp(State->Mod.Filter, State->Mod.Depth,715 State->Mod.Coeff);717 // Calculate the read offset and fraction between it and the next sample.718 frac = (1.0f + (State->Mod.Filter * sinus));719 offset = (ALuint)frac;720 frac -= offset;722 // Get the two samples crossed by the offset, and feed the delay line723 // with the next input sample.724 out0 = DelayLineOut(&State->Mod.Delay, State->Offset - offset);725 out1 = DelayLineOut(&State->Mod.Delay, State->Offset - offset - 1);726 DelayLineIn(&State->Mod.Delay, State->Offset, in);728 // Step the modulation index forward, keeping it bound to its range.729 State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;731 // The output is obtained by linearly interpolating the two samples that732 // were acquired above.733 return lerp(out0, out1, frac);734 }736 // Delay line output routine for early reflections.737 static __inline ALfloat EarlyDelayLineOut(ALverbState *State, ALuint index)738 {739 return AttenuatedDelayLineOut(&State->Early.Delay[index],740 State->Offset - State->Early.Offset[index],741 State->Early.Coeff[index]);742 }744 // Given an input sample, this function produces four-channel output for the745 // early reflections.746 static __inline ALvoid EarlyReflection(ALverbState *State, ALfloat in, ALfloat *out)747 {748 ALfloat d[4], v, f[4];750 // Obtain the decayed results of each early delay line.751 d[0] = EarlyDelayLineOut(State, 0);752 d[1] = EarlyDelayLineOut(State, 1);753 d[2] = EarlyDelayLineOut(State, 2);754 d[3] = EarlyDelayLineOut(State, 3);756 /* The following uses a lossless scattering junction from waveguide757 * theory. It actually amounts to a householder mixing matrix, which758 * will produce a maximally diffuse response, and means this can probably759 * be considered a simple feed-back delay network (FDN).760 * N761 * ---762 * \763 * v = 2/N / d_i764 * ---765 * i=1766 */767 v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;768 // The junction is loaded with the input here.769 v += in;771 // Calculate the feed values for the delay lines.772 f[0] = v - d[0];773 f[1] = v - d[1];774 f[2] = v - d[2];775 f[3] = v - d[3];777 // Re-feed the delay lines.778 DelayLineIn(&State->Early.Delay[0], State->Offset, f[0]);779 DelayLineIn(&State->Early.Delay[1], State->Offset, f[1]);780 DelayLineIn(&State->Early.Delay[2], State->Offset, f[2]);781 DelayLineIn(&State->Early.Delay[3], State->Offset, f[3]);783 // Output the results of the junction for all four channels.784 out[0] = State->Early.Gain * f[0];785 out[1] = State->Early.Gain * f[1];786 out[2] = State->Early.Gain * f[2];787 out[3] = State->Early.Gain * f[3];788 }790 // All-pass input/output routine for late reverb.791 static __inline ALfloat LateAllPassInOut(ALverbState *State, ALuint index, ALfloat in)792 {793 return AllpassInOut(&State->Late.ApDelay[index],794 State->Offset - State->Late.ApOffset[index],795 State->Offset, in, State->Late.ApFeedCoeff,796 State->Late.ApCoeff[index]);797 }799 // Delay line output routine for late reverb.800 static __inline ALfloat LateDelayLineOut(ALverbState *State, ALuint index)801 {802 return AttenuatedDelayLineOut(&State->Late.Delay[index],803 State->Offset - State->Late.Offset[index],804 State->Late.Coeff[index]);805 }807 // Low-pass filter input/output routine for late reverb.808 static __inline ALfloat LateLowPassInOut(ALverbState *State, ALuint index, ALfloat in)809 {810 in = lerp(in, State->Late.LpSample[index], State->Late.LpCoeff[index]);811 State->Late.LpSample[index] = in;812 return in;813 }815 // Given four decorrelated input samples, this function produces four-channel816 // output for the late reverb.817 static __inline ALvoid LateReverb(ALverbState *State, ALfloat *in, ALfloat *out)818 {819 ALfloat d[4], f[4];821 // Obtain the decayed results of the cyclical delay lines, and add the822 // corresponding input channels. Then pass the results through the823 // low-pass filters.825 // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and back826 // to 0.827 d[0] = LateLowPassInOut(State, 2, in[2] + LateDelayLineOut(State, 2));828 d[1] = LateLowPassInOut(State, 0, in[0] + LateDelayLineOut(State, 0));829 d[2] = LateLowPassInOut(State, 3, in[3] + LateDelayLineOut(State, 3));830 d[3] = LateLowPassInOut(State, 1, in[1] + LateDelayLineOut(State, 1));832 // To help increase diffusion, run each line through an all-pass filter.833 // When there is no diffusion, the shortest all-pass filter will feed the834 // shortest delay line.835 d[0] = LateAllPassInOut(State, 0, d[0]);836 d[1] = LateAllPassInOut(State, 1, d[1]);837 d[2] = LateAllPassInOut(State, 2, d[2]);838 d[3] = LateAllPassInOut(State, 3, d[3]);840 /* Late reverb is done with a modified feed-back delay network (FDN)841 * topology. Four input lines are each fed through their own all-pass842 * filter and then into the mixing matrix. The four outputs of the843 * mixing matrix are then cycled back to the inputs. Each output feeds844 * a different input to form a circlular feed cycle.845 *846 * The mixing matrix used is a 4D skew-symmetric rotation matrix derived847 * using a single unitary rotational parameter:848 *849 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2850 * [ -a, d, c, -b ]851 * [ -b, -c, d, a ]852 * [ -c, b, -a, d ]853 *854 * The rotation is constructed from the effect's diffusion parameter,855 * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y856 * with differing signs, and d is the coefficient x. The matrix is thus:857 *858 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)859 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)860 * [ y, -y, x, y ] x = cos(t)861 * [ -y, -y, -y, x ] y = sin(t) / n862 *863 * To reduce the number of multiplies, the x coefficient is applied with864 * the cyclical delay line coefficients. Thus only the y coefficient is865 * applied when mixing, and is modified to be: y / x.866 */867 f[0] = d[0] + (State->Late.MixCoeff * ( d[1] + -d[2] + d[3]));868 f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));869 f[2] = d[2] + (State->Late.MixCoeff * ( d[0] + -d[1] + d[3]));870 f[3] = d[3] + (State->Late.MixCoeff * (-d[0] + -d[1] + -d[2] ));872 // Output the results of the matrix for all four channels, attenuated by873 // the late reverb gain (which is attenuated by the 'x' mix coefficient).874 out[0] = State->Late.Gain * f[0];875 out[1] = State->Late.Gain * f[1];876 out[2] = State->Late.Gain * f[2];877 out[3] = State->Late.Gain * f[3];879 // Re-feed the cyclical delay lines.880 DelayLineIn(&State->Late.Delay[0], State->Offset, f[0]);881 DelayLineIn(&State->Late.Delay[1], State->Offset, f[1]);882 DelayLineIn(&State->Late.Delay[2], State->Offset, f[2]);883 DelayLineIn(&State->Late.Delay[3], State->Offset, f[3]);884 }886 // Given an input sample, this function mixes echo into the four-channel late887 // reverb.888 static __inline ALvoid EAXEcho(ALverbState *State, ALfloat in, ALfloat *late)889 {890 ALfloat out, feed;892 // Get the latest attenuated echo sample for output.893 feed = AttenuatedDelayLineOut(&State->Echo.Delay,894 State->Offset - State->Echo.Offset,895 State->Echo.Coeff);897 // Mix the output into the late reverb channels.898 out = State->Echo.MixCoeff[0] * feed;899 late[0] = (State->Echo.MixCoeff[1] * late[0]) + out;900 late[1] = (State->Echo.MixCoeff[1] * late[1]) + out;901 late[2] = (State->Echo.MixCoeff[1] * late[2]) + out;902 late[3] = (State->Echo.MixCoeff[1] * late[3]) + out;904 // Mix the energy-attenuated input with the output and pass it through905 // the echo low-pass filter.906 feed += State->Echo.DensityGain * in;907 feed = lerp(feed, State->Echo.LpSample, State->Echo.LpCoeff);908 State->Echo.LpSample = feed;910 // Then the echo all-pass filter.911 feed = AllpassInOut(&State->Echo.ApDelay,912 State->Offset - State->Echo.ApOffset,913 State->Offset, feed, State->Echo.ApFeedCoeff,914 State->Echo.ApCoeff);916 // Feed the delay with the mixed and filtered sample.917 DelayLineIn(&State->Echo.Delay, State->Offset, feed);918 }920 // Perform the non-EAX reverb pass on a given input sample, resulting in921 // four-channel output.922 static __inline ALvoid VerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)923 {924 ALfloat feed, taps[4];926 // Low-pass filter the incoming sample.927 in = lpFilter2P(&State->LpFilter, 0, in);929 // Feed the initial delay line.930 DelayLineIn(&State->Delay, State->Offset, in);932 // Calculate the early reflection from the first delay tap.933 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);934 EarlyReflection(State, in, early);936 // Feed the decorrelator from the energy-attenuated output of the second937 // delay tap.938 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);939 feed = in * State->Late.DensityGain;940 DelayLineIn(&State->Decorrelator, State->Offset, feed);942 // Calculate the late reverb from the decorrelator taps.943 taps[0] = feed;944 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);945 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);946 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);947 LateReverb(State, taps, late);949 // Step all delays forward one sample.950 State->Offset++;951 }953 // Perform the EAX reverb pass on a given input sample, resulting in four-954 // channel output.955 static __inline ALvoid EAXVerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)956 {957 ALfloat feed, taps[4];959 // Low-pass filter the incoming sample.960 in = lpFilter2P(&State->LpFilter, 0, in);962 // Perform any modulation on the input.963 in = EAXModulation(State, in);965 // Feed the initial delay line.966 DelayLineIn(&State->Delay, State->Offset, in);968 // Calculate the early reflection from the first delay tap.969 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);970 EarlyReflection(State, in, early);972 // Feed the decorrelator from the energy-attenuated output of the second973 // delay tap.974 in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);975 feed = in * State->Late.DensityGain;976 DelayLineIn(&State->Decorrelator, State->Offset, feed);978 // Calculate the late reverb from the decorrelator taps.979 taps[0] = feed;980 taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);981 taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);982 taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);983 LateReverb(State, taps, late);985 // Calculate and mix in any echo.986 EAXEcho(State, in, late);988 // Step all delays forward one sample.989 State->Offset++;990 }992 // This destroys the reverb state. It should be called only when the effect993 // slot has a different (or no) effect loaded over the reverb effect.994 static ALvoid VerbDestroy(ALeffectState *effect)995 {996 ALverbState *State = (ALverbState*)effect;997 if(State)998 {999 free(State->SampleBuffer);1000 State->SampleBuffer = NULL;1001 free(State);1002 }1003 }1005 // This updates the device-dependant reverb state. This is called on1006 // initialization and any time the device parameters (eg. playback frequency,1007 // or format) have been changed.1008 static ALboolean VerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)1009 {1010 ALverbState *State = (ALverbState*)effect;1011 ALuint frequency = Device->Frequency;1012 ALuint index;1014 // Allocate the delay lines.1015 if(!AllocLines(AL_FALSE, frequency, State))1016 return AL_FALSE;1018 // The early reflection and late all-pass filter line lengths are static,1019 // so their offsets only need to be calculated once.1020 for(index = 0;index < 4;index++)1021 {1022 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *1023 frequency);1024 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *1025 frequency);1026 }1028 return AL_TRUE;1029 }1031 // This updates the device-dependant EAX reverb state. This is called on1032 // initialization and any time the device parameters (eg. playback frequency,1033 // format) have been changed.1034 static ALboolean EAXVerbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)1035 {1036 ALverbState *State = (ALverbState*)effect;1037 ALuint frequency = Device->Frequency, index;1039 // Allocate the delay lines.1040 if(!AllocLines(AL_TRUE, frequency, State))1041 return AL_FALSE;1043 // Calculate the modulation filter coefficient. Notice that the exponent1044 // is calculated given the current sample rate. This ensures that the1045 // resulting filter response over time is consistent across all sample1046 // rates.1047 State->Mod.Coeff = aluPow(MODULATION_FILTER_COEFF,1048 MODULATION_FILTER_CONST / frequency);1050 // The early reflection and late all-pass filter line lengths are static,1051 // so their offsets only need to be calculated once.1052 for(index = 0;index < 4;index++)1053 {1054 State->Early.Offset[index] = (ALuint)(EARLY_LINE_LENGTH[index] *1055 frequency);1056 State->Late.ApOffset[index] = (ALuint)(ALLPASS_LINE_LENGTH[index] *1057 frequency);1058 }1060 // The echo all-pass filter line length is static, so its offset only1061 // needs to be calculated once.1062 State->Echo.ApOffset = (ALuint)(ECHO_ALLPASS_LENGTH * frequency);1064 return AL_TRUE;1065 }1067 // This updates the reverb state. This is called any time the reverb effect1068 // is loaded into a slot.1069 static ALvoid VerbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffectslot *Slot)1070 {1071 ALverbState *State = (ALverbState*)effect;1072 ALCdevice *Device = Context->Device;1073 ALuint frequency = Device->Frequency;1074 ALfloat cw, x, y, hfRatio, gain;1075 ALuint index;1077 // Calculate the master low-pass filter (from the master effect HF gain).1078 cw = CalcI3DL2HFreq(Slot->effect.Params.Reverb.HFReference, frequency);1079 // This is done with 2 chained 1-pole filters, so no need to square g.1080 State->LpFilter.coeff = lpCoeffCalc(Slot->effect.Params.Reverb.GainHF, cw);1082 // Update the initial effect delay.1083 UpdateDelayLine(Slot->effect.Params.Reverb.ReflectionsDelay,1084 Slot->effect.Params.Reverb.LateReverbDelay,1085 frequency, State);1087 // Update the early lines.1088 UpdateEarlyLines(Slot->effect.Params.Reverb.Gain,1089 Slot->effect.Params.Reverb.ReflectionsGain,1090 Slot->effect.Params.Reverb.LateReverbDelay, State);1092 // Update the decorrelator.1093 UpdateDecorrelator(Slot->effect.Params.Reverb.Density, frequency, State);1095 // Get the mixing matrix coefficients (x and y).1096 CalcMatrixCoeffs(Slot->effect.Params.Reverb.Diffusion, &x, &y);1097 // Then divide x into y to simplify the matrix calculation.1098 State->Late.MixCoeff = y / x;1100 // If the HF limit parameter is flagged, calculate an appropriate limit1101 // based on the air absorption parameter.1102 hfRatio = Slot->effect.Params.Reverb.DecayHFRatio;1103 if(Slot->effect.Params.Reverb.DecayHFLimit &&1104 Slot->effect.Params.Reverb.AirAbsorptionGainHF < 1.0f)1105 hfRatio = CalcLimitedHfRatio(hfRatio,1106 Slot->effect.Params.Reverb.AirAbsorptionGainHF,1107 Slot->effect.Params.Reverb.DecayTime);1109 // Update the late lines.1110 UpdateLateLines(Slot->effect.Params.Reverb.Gain, Slot->effect.Params.Reverb.LateReverbGain,1111 x, Slot->effect.Params.Reverb.Density, Slot->effect.Params.Reverb.DecayTime,1112 Slot->effect.Params.Reverb.Diffusion, hfRatio, cw, frequency, State);1114 // Update channel gains1115 gain = Slot->Gain;1116 gain *= aluSqrt(2.0f/Device->NumChan);1117 gain *= ReverbBoost;1118 for(index = 0;index < MAXCHANNELS;index++)1119 State->Gain[index] = 0.0f;1120 for(index = 0;index < Device->NumChan;index++)1121 {1122 enum Channel chan = Device->Speaker2Chan[index];1123 State->Gain[chan] = gain;1124 }1125 }1127 // This updates the EAX reverb state. This is called any time the EAX reverb1128 // effect is loaded into a slot.1129 static ALvoid EAXVerbUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffectslot *Slot)1130 {1131 ALverbState *State = (ALverbState*)effect;1132 ALuint frequency = Context->Device->Frequency;1133 ALfloat cw, x, y, hfRatio;1135 // Calculate the master low-pass filter (from the master effect HF gain).1136 cw = CalcI3DL2HFreq(Slot->effect.Params.Reverb.HFReference, frequency);1137 // This is done with 2 chained 1-pole filters, so no need to square g.1138 State->LpFilter.coeff = lpCoeffCalc(Slot->effect.Params.Reverb.GainHF, cw);1140 // Update the modulator line.1141 UpdateModulator(Slot->effect.Params.Reverb.ModulationTime,1142 Slot->effect.Params.Reverb.ModulationDepth,1143 frequency, State);1145 // Update the initial effect delay.1146 UpdateDelayLine(Slot->effect.Params.Reverb.ReflectionsDelay,1147 Slot->effect.Params.Reverb.LateReverbDelay,1148 frequency, State);1150 // Update the early lines.1151 UpdateEarlyLines(Slot->effect.Params.Reverb.Gain,1152 Slot->effect.Params.Reverb.ReflectionsGain,1153 Slot->effect.Params.Reverb.LateReverbDelay, State);1155 // Update the decorrelator.1156 UpdateDecorrelator(Slot->effect.Params.Reverb.Density, frequency, State);1158 // Get the mixing matrix coefficients (x and y).1159 CalcMatrixCoeffs(Slot->effect.Params.Reverb.Diffusion, &x, &y);1160 // Then divide x into y to simplify the matrix calculation.1161 State->Late.MixCoeff = y / x;1163 // If the HF limit parameter is flagged, calculate an appropriate limit1164 // based on the air absorption parameter.1165 hfRatio = Slot->effect.Params.Reverb.DecayHFRatio;1166 if(Slot->effect.Params.Reverb.DecayHFLimit &&1167 Slot->effect.Params.Reverb.AirAbsorptionGainHF < 1.0f)1168 hfRatio = CalcLimitedHfRatio(hfRatio,1169 Slot->effect.Params.Reverb.AirAbsorptionGainHF,1170 Slot->effect.Params.Reverb.DecayTime);1172 // Update the late lines.1173 UpdateLateLines(Slot->effect.Params.Reverb.Gain, Slot->effect.Params.Reverb.LateReverbGain,1174 x, Slot->effect.Params.Reverb.Density, Slot->effect.Params.Reverb.DecayTime,1175 Slot->effect.Params.Reverb.Diffusion, hfRatio, cw, frequency, State);1177 // Update the echo line.1178 UpdateEchoLine(Slot->effect.Params.Reverb.Gain, Slot->effect.Params.Reverb.LateReverbGain,1179 Slot->effect.Params.Reverb.EchoTime, Slot->effect.Params.Reverb.DecayTime,1180 Slot->effect.Params.Reverb.Diffusion, Slot->effect.Params.Reverb.EchoDepth,1181 hfRatio, cw, frequency, State);1183 // Update early and late 3D panning.1184 Update3DPanning(Context->Device, Slot->effect.Params.Reverb.ReflectionsPan,1185 Slot->effect.Params.Reverb.LateReverbPan, Slot->Gain, State);1186 }1188 // This processes the reverb state, given the input samples and an output1189 // buffer.1190 static ALvoid VerbProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])1191 {1192 ALverbState *State = (ALverbState*)effect;1193 ALuint index;1194 ALfloat early[4], late[4], out[4];1195 const ALfloat *panGain = State->Gain;1196 (void)Slot;1198 for(index = 0;index < SamplesToDo;index++)1199 {1200 // Process reverb for this sample.1201 VerbPass(State, SamplesIn[index], early, late);1203 // Mix early reflections and late reverb.1204 out[0] = (early[0] + late[0]);1205 out[1] = (early[1] + late[1]);1206 out[2] = (early[2] + late[2]);1207 out[3] = (early[3] + late[3]);1209 // Output the results.1210 SamplesOut[index][FRONT_LEFT] += panGain[FRONT_LEFT] * out[0];1211 SamplesOut[index][FRONT_RIGHT] += panGain[FRONT_RIGHT] * out[1];1212 SamplesOut[index][FRONT_CENTER] += panGain[FRONT_CENTER] * out[3];1213 SamplesOut[index][SIDE_LEFT] += panGain[SIDE_LEFT] * out[0];1214 SamplesOut[index][SIDE_RIGHT] += panGain[SIDE_RIGHT] * out[1];1215 SamplesOut[index][BACK_LEFT] += panGain[BACK_LEFT] * out[0];1216 SamplesOut[index][BACK_RIGHT] += panGain[BACK_RIGHT] * out[1];1217 SamplesOut[index][BACK_CENTER] += panGain[BACK_CENTER] * out[2];1218 }1219 }1221 // This processes the EAX reverb state, given the input samples and an output1222 // buffer.1223 static ALvoid EAXVerbProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])1224 {1225 ALverbState *State = (ALverbState*)effect;1226 ALuint index;1227 ALfloat early[4], late[4];1228 (void)Slot;1230 for(index = 0;index < SamplesToDo;index++)1231 {1232 // Process reverb for this sample.1233 EAXVerbPass(State, SamplesIn[index], early, late);1235 // Unfortunately, while the number and configuration of gains for1236 // panning adjust according to MAXCHANNELS, the output from the1237 // reverb engine is not so scalable.1238 SamplesOut[index][FRONT_LEFT] +=1239 (State->Early.PanGain[FRONT_LEFT]*early[0] +1240 State->Late.PanGain[FRONT_LEFT]*late[0]);1241 SamplesOut[index][FRONT_RIGHT] +=1242 (State->Early.PanGain[FRONT_RIGHT]*early[1] +1243 State->Late.PanGain[FRONT_RIGHT]*late[1]);1244 SamplesOut[index][FRONT_CENTER] +=1245 (State->Early.PanGain[FRONT_CENTER]*early[3] +1246 State->Late.PanGain[FRONT_CENTER]*late[3]);1247 SamplesOut[index][SIDE_LEFT] +=1248 (State->Early.PanGain[SIDE_LEFT]*early[0] +1249 State->Late.PanGain[SIDE_LEFT]*late[0]);1250 SamplesOut[index][SIDE_RIGHT] +=1251 (State->Early.PanGain[SIDE_RIGHT]*early[1] +1252 State->Late.PanGain[SIDE_RIGHT]*late[1]);1253 SamplesOut[index][BACK_LEFT] +=1254 (State->Early.PanGain[BACK_LEFT]*early[0] +1255 State->Late.PanGain[BACK_LEFT]*late[0]);1256 SamplesOut[index][BACK_RIGHT] +=1257 (State->Early.PanGain[BACK_RIGHT]*early[1] +1258 State->Late.PanGain[BACK_RIGHT]*late[1]);1259 SamplesOut[index][BACK_CENTER] +=1260 (State->Early.PanGain[BACK_CENTER]*early[2] +1261 State->Late.PanGain[BACK_CENTER]*late[2]);1262 }1263 }1265 // This creates the reverb state. It should be called only when the reverb1266 // effect is loaded into a slot that doesn't already have a reverb effect.1267 ALeffectState *VerbCreate(void)1268 {1269 ALverbState *State = NULL;1270 ALuint index;1272 State = malloc(sizeof(ALverbState));1273 if(!State)1274 return NULL;1276 State->state.Destroy = VerbDestroy;1277 State->state.DeviceUpdate = VerbDeviceUpdate;1278 State->state.Update = VerbUpdate;1279 State->state.Process = VerbProcess;1281 State->TotalSamples = 0;1282 State->SampleBuffer = NULL;1284 State->LpFilter.coeff = 0.0f;1285 State->LpFilter.history[0] = 0.0f;1286 State->LpFilter.history[1] = 0.0f;1288 State->Mod.Delay.Mask = 0;1289 State->Mod.Delay.Line = NULL;1290 State->Mod.Index = 0;1291 State->Mod.Range = 1;1292 State->Mod.Depth = 0.0f;1293 State->Mod.Coeff = 0.0f;1294 State->Mod.Filter = 0.0f;1296 State->Delay.Mask = 0;1297 State->Delay.Line = NULL;1298 State->DelayTap[0] = 0;1299 State->DelayTap[1] = 0;1301 State->Early.Gain = 0.0f;1302 for(index = 0;index < 4;index++)1303 {1304 State->Early.Coeff[index] = 0.0f;1305 State->Early.Delay[index].Mask = 0;1306 State->Early.Delay[index].Line = NULL;1307 State->Early.Offset[index] = 0;1308 }1310 State->Decorrelator.Mask = 0;1311 State->Decorrelator.Line = NULL;1312 State->DecoTap[0] = 0;1313 State->DecoTap[1] = 0;1314 State->DecoTap[2] = 0;1316 State->Late.Gain = 0.0f;1317 State->Late.DensityGain = 0.0f;1318 State->Late.ApFeedCoeff = 0.0f;1319 State->Late.MixCoeff = 0.0f;1320 for(index = 0;index < 4;index++)1321 {1322 State->Late.ApCoeff[index] = 0.0f;1323 State->Late.ApDelay[index].Mask = 0;1324 State->Late.ApDelay[index].Line = NULL;1325 State->Late.ApOffset[index] = 0;1327 State->Late.Coeff[index] = 0.0f;1328 State->Late.Delay[index].Mask = 0;1329 State->Late.Delay[index].Line = NULL;1330 State->Late.Offset[index] = 0;1332 State->Late.LpCoeff[index] = 0.0f;1333 State->Late.LpSample[index] = 0.0f;1334 }1336 for(index = 0;index < MAXCHANNELS;index++)1337 {1338 State->Early.PanGain[index] = 0.0f;1339 State->Late.PanGain[index] = 0.0f;1340 }1342 State->Echo.DensityGain = 0.0f;1343 State->Echo.Delay.Mask = 0;1344 State->Echo.Delay.Line = NULL;1345 State->Echo.ApDelay.Mask = 0;1346 State->Echo.ApDelay.Line = NULL;1347 State->Echo.Coeff = 0.0f;1348 State->Echo.ApFeedCoeff = 0.0f;1349 State->Echo.ApCoeff = 0.0f;1350 State->Echo.Offset = 0;1351 State->Echo.ApOffset = 0;1352 State->Echo.LpCoeff = 0.0f;1353 State->Echo.LpSample = 0.0f;1354 State->Echo.MixCoeff[0] = 0.0f;1355 State->Echo.MixCoeff[1] = 0.0f;1357 State->Offset = 0;1359 State->Gain = State->Late.PanGain;1361 return &State->state;1362 }1364 ALeffectState *EAXVerbCreate(void)1365 {1366 ALeffectState *State = VerbCreate();1367 if(State && EmulateEAXReverb == AL_FALSE)1368 {1369 State->DeviceUpdate = EAXVerbDeviceUpdate;1370 State->Update = EAXVerbUpdate;1371 State->Process = EAXVerbProcess;1372 }1373 return State;1374 }