Make the filter history buffer size flexible
[openal-soft/openal-hmr.git] / Alc / alcEcho.c
blobf6606bfa91b209591a86f7c08b147aefd0442936
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2009 by Chris Robinson.
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 "alFilter.h"
28 #include "alAuxEffectSlot.h"
29 #include "alEcho.h"
31 #ifdef HAVE_SQRTF
32 #define aluSqrt(x) ((ALfloat)sqrtf((float)(x)))
33 #else
34 #define aluSqrt(x) ((ALfloat)sqrt((double)(x)))
35 #endif
37 struct ALechoState {
38 ALfloat *SampleBuffer;
39 ALuint BufferLength;
41 // The echo is two tap. The third tap is the offset to write the feedback
42 // and input sample to
43 struct {
44 ALuint offset;
45 } Tap[3];
46 // The LR gains for the first tap. The second tap uses the reverse
47 ALfloat GainL;
48 ALfloat GainR;
50 ALfloat FeedGain;
52 FILTER iirFilter;
53 ALfloat history[2];
56 // Find the next power of 2. Actually, this will return the input value if
57 // it is already a power of 2.
58 static ALuint NextPowerOf2(ALuint value)
60 ALuint powerOf2 = 1;
62 if(value)
64 value--;
65 while(value)
67 value >>= 1;
68 powerOf2 <<= 1;
71 return powerOf2;
74 ALechoState *EchoCreate(ALCcontext *Context)
76 ALechoState *state;
77 ALuint i, maxlen;
79 state = malloc(sizeof(*state));
80 if(!state)
81 return NULL;
83 maxlen = (ALuint)(AL_ECHO_MAX_DELAY * Context->Frequency);
84 maxlen += (ALuint)(AL_ECHO_MAX_LRDELAY * Context->Frequency);
86 // Use the next power of 2 for the buffer length, so the tap offsets can be
87 // wrapped using a mask instead of a modulo
88 state->BufferLength = NextPowerOf2(maxlen+1);
89 state->SampleBuffer = malloc(state->BufferLength * sizeof(ALfloat));
90 if(!state->SampleBuffer)
92 free(state);
93 return NULL;
96 for(i = 0;i < state->BufferLength;i++)
97 state->SampleBuffer[i] = 0.0f;
99 state->Tap[0].offset = 0;
100 state->Tap[1].offset = 0;
101 state->Tap[2].offset = 0;
102 state->GainL = 0.0f;
103 state->GainR = 0.0f;
105 for(i = 0;i < sizeof(state->history)/sizeof(state->history[0]);i++)
106 state->history[i] = 0.0f;
107 state->iirFilter.coeff = 0.0f;
109 return state;
112 ALvoid EchoDestroy(ALechoState *state)
114 if(state)
116 free(state->SampleBuffer);
117 state->SampleBuffer = NULL;
118 free(state);
122 ALvoid EchoUpdate(ALCcontext *Context, struct ALeffectslot *Slot, ALeffect *Effect)
124 ALechoState *state = Slot->EchoState;
125 ALuint newdelay1, newdelay2;
126 ALfloat lrpan, cw, a, g;
128 newdelay1 = (ALuint)(Effect->Echo.Delay * Context->Frequency);
129 newdelay2 = (ALuint)(Effect->Echo.LRDelay * Context->Frequency);
131 state->Tap[0].offset = (state->BufferLength - newdelay1 - 1 +
132 state->Tap[2].offset)%state->BufferLength;
133 state->Tap[1].offset = (state->BufferLength - newdelay1 - newdelay2 - 1 +
134 state->Tap[2].offset)%state->BufferLength;
136 lrpan = Effect->Echo.Spread*0.5f + 0.5f;
137 state->GainL = aluSqrt( lrpan);
138 state->GainR = aluSqrt(1.0f-lrpan);
140 state->FeedGain = Effect->Echo.Feedback;
142 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / Context->Frequency);
143 g = 1.0f - Effect->Echo.Damping;
144 a = 0.0f;
145 if(g < 0.9999f) // 1-epsilon
146 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
147 state->iirFilter.coeff = a;
150 ALvoid EchoProcess(ALechoState *state, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
152 ALfloat *history = state->iirFilter.history;
153 const ALfloat a = state->iirFilter.coeff;
154 const ALuint delay = state->BufferLength-1;
155 ALuint tap1off = state->Tap[0].offset;
156 ALuint tap2off = state->Tap[1].offset;
157 ALuint fboff = state->Tap[2].offset;
158 ALfloat samp[2];
159 ALuint i;
161 for(i = 0;i < SamplesToDo;i++)
163 // Apply damping
164 samp[0] = state->SampleBuffer[tap2off] + SamplesIn[i];
166 samp[0] += (history[0]-samp[0]) * a;
167 history[0] = samp[0];
168 samp[0] += (history[1]-samp[0]) * a;
169 history[1] = samp[0];
171 // Apply feedback gain and mix in the new sample
172 state->SampleBuffer[fboff] = samp[0] * state->FeedGain;
174 tap1off = (tap1off+1) & delay;
175 tap2off = (tap2off+1) & delay;
176 fboff = (fboff+1) & delay;
178 // Sample first tap
179 samp[0] = state->SampleBuffer[tap1off]*state->GainL;
180 samp[1] = state->SampleBuffer[tap1off]*state->GainR;
181 // Sample second tap. Reverse LR panning
182 samp[0] += state->SampleBuffer[tap2off]*state->GainR;
183 samp[1] += state->SampleBuffer[tap2off]*state->GainL;
185 SamplesOut[i][FRONT_LEFT] += samp[0];
186 SamplesOut[i][FRONT_RIGHT] += samp[1];
187 SamplesOut[i][SIDE_LEFT] += samp[0];
188 SamplesOut[i][SIDE_RIGHT] += samp[1];
189 SamplesOut[i][BACK_LEFT] += samp[0];
190 SamplesOut[i][BACK_RIGHT] += samp[1];
193 state->Tap[0].offset = tap1off;
194 state->Tap[1].offset = tap2off;
195 state->Tap[2].offset = fboff;