Make the filter functions global inline, and use it for echo
[openal-soft.git] / Alc / alcEcho.c
blobfeede2ba0b6dd6c24407bd05b948437773889313
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"
30 #include "alu.h"
32 struct ALechoState {
33 ALfloat *SampleBuffer;
34 ALuint BufferLength;
36 // The echo is two tap. The third tap is the offset to write the feedback
37 // and input sample to
38 struct {
39 ALuint offset;
40 } Tap[3];
41 // The LR gains for the first tap. The second tap uses the reverse
42 ALfloat GainL;
43 ALfloat GainR;
45 ALfloat FeedGain;
47 FILTER iirFilter;
48 ALfloat history[2];
51 // Find the next power of 2. Actually, this will return the input value if
52 // it is already a power of 2.
53 static ALuint NextPowerOf2(ALuint value)
55 ALuint powerOf2 = 1;
57 if(value)
59 value--;
60 while(value)
62 value >>= 1;
63 powerOf2 <<= 1;
66 return powerOf2;
69 ALechoState *EchoCreate(ALCcontext *Context)
71 ALechoState *state;
72 ALuint i, maxlen;
74 state = malloc(sizeof(*state));
75 if(!state)
76 return NULL;
78 maxlen = (ALuint)(AL_ECHO_MAX_DELAY * Context->Frequency);
79 maxlen += (ALuint)(AL_ECHO_MAX_LRDELAY * Context->Frequency);
81 // Use the next power of 2 for the buffer length, so the tap offsets can be
82 // wrapped using a mask instead of a modulo
83 state->BufferLength = NextPowerOf2(maxlen+1);
84 state->SampleBuffer = malloc(state->BufferLength * sizeof(ALfloat));
85 if(!state->SampleBuffer)
87 free(state);
88 return NULL;
91 for(i = 0;i < state->BufferLength;i++)
92 state->SampleBuffer[i] = 0.0f;
94 state->Tap[0].offset = 0;
95 state->Tap[1].offset = 0;
96 state->Tap[2].offset = 0;
97 state->GainL = 0.0f;
98 state->GainR = 0.0f;
100 for(i = 0;i < sizeof(state->history)/sizeof(state->history[0]);i++)
101 state->history[i] = 0.0f;
102 state->iirFilter.coeff = 0.0f;
104 return state;
107 ALvoid EchoDestroy(ALechoState *state)
109 if(state)
111 free(state->SampleBuffer);
112 state->SampleBuffer = NULL;
113 free(state);
117 ALvoid EchoUpdate(ALCcontext *Context, struct ALeffectslot *Slot, ALeffect *Effect)
119 ALechoState *state = Slot->EchoState;
120 ALuint newdelay1, newdelay2;
121 ALfloat lrpan, cw, a, g;
123 newdelay1 = (ALuint)(Effect->Echo.Delay * Context->Frequency);
124 newdelay2 = (ALuint)(Effect->Echo.LRDelay * Context->Frequency);
126 state->Tap[0].offset = (state->BufferLength - newdelay1 - 1 +
127 state->Tap[2].offset)%state->BufferLength;
128 state->Tap[1].offset = (state->BufferLength - newdelay1 - newdelay2 - 1 +
129 state->Tap[2].offset)%state->BufferLength;
131 lrpan = Effect->Echo.Spread*0.5f + 0.5f;
132 state->GainL = aluSqrt( lrpan);
133 state->GainR = aluSqrt(1.0f-lrpan);
135 state->FeedGain = Effect->Echo.Feedback;
137 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / Context->Frequency);
138 g = 1.0f - Effect->Echo.Damping;
139 a = 0.0f;
140 if(g < 0.9999f) // 1-epsilon
141 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
142 state->iirFilter.coeff = a;
145 ALvoid EchoProcess(ALechoState *state, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
147 const ALuint delay = state->BufferLength-1;
148 ALuint tap1off = state->Tap[0].offset;
149 ALuint tap2off = state->Tap[1].offset;
150 ALuint fboff = state->Tap[2].offset;
151 ALfloat samp[2];
152 ALuint i;
154 for(i = 0;i < SamplesToDo;i++)
156 // Apply damping
157 samp[0] = lpFilter2P(&state->iirFilter, 0, state->SampleBuffer[tap2off]+SamplesIn[i]);
159 // Apply feedback gain and mix in the new sample
160 state->SampleBuffer[fboff] = samp[0] * state->FeedGain;
162 tap1off = (tap1off+1) & delay;
163 tap2off = (tap2off+1) & delay;
164 fboff = (fboff+1) & delay;
166 // Sample first tap
167 samp[0] = state->SampleBuffer[tap1off]*state->GainL;
168 samp[1] = state->SampleBuffer[tap1off]*state->GainR;
169 // Sample second tap. Reverse LR panning
170 samp[0] += state->SampleBuffer[tap2off]*state->GainR;
171 samp[1] += state->SampleBuffer[tap2off]*state->GainL;
173 SamplesOut[i][FRONT_LEFT] += samp[0];
174 SamplesOut[i][FRONT_RIGHT] += samp[1];
175 SamplesOut[i][SIDE_LEFT] += samp[0];
176 SamplesOut[i][SIDE_RIGHT] += samp[1];
177 SamplesOut[i][BACK_LEFT] += samp[0];
178 SamplesOut[i][BACK_RIGHT] += samp[1];
181 state->Tap[0].offset = tap1off;
182 state->Tap[1].offset = tap2off;
183 state->Tap[2].offset = fboff;