Small fixups
[openal-soft.git] / Alc / alcEcho.c
blob0c0f19ed8f89af5fd96320d73d45e21034dbd55a
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 "alMain.h"
27 #include "alFilter.h"
28 #include "alAuxEffectSlot.h"
29 #include "alError.h"
30 #include "alu.h"
32 // Just a soft maximum. Being higher will cause EchoUpdate to reallocate the
33 // sample buffer which may cause an abort if realloc fails
34 #define MAX_ECHO_FREQ 192000
36 typedef struct ALechoState {
37 // Must be first in all effects!
38 ALeffectState state;
40 ALfloat *SampleBuffer;
41 ALuint BufferLength;
43 // The echo is two tap. The delay is the number of samples from before the
44 // current offset
45 struct {
46 ALuint delay;
47 } Tap[2];
48 ALuint Offset;
49 // The LR gains for the first tap. The second tap uses the reverse
50 ALfloat GainL;
51 ALfloat GainR;
53 ALfloat FeedGain;
55 FILTER iirFilter;
56 ALfloat history[2];
57 } ALechoState;
59 // Find the next power of 2. Actually, this will return the input value if
60 // it is already a power of 2.
61 static ALuint NextPowerOf2(ALuint value)
63 ALuint powerOf2 = 1;
65 if(value)
67 value--;
68 while(value)
70 value >>= 1;
71 powerOf2 <<= 1;
74 return powerOf2;
77 ALvoid EchoDestroy(ALeffectState *effect)
79 ALechoState *state = (ALechoState*)effect;
80 if(state)
82 free(state->SampleBuffer);
83 state->SampleBuffer = NULL;
84 free(state);
88 ALboolean EchoDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
90 ALechoState *state = (ALechoState*)effect;
91 ALuint maxlen, i;
93 // Use the next power of 2 for the buffer length, so the tap offsets can be
94 // wrapped using a mask instead of a modulo
95 maxlen = (ALuint)(AL_ECHO_MAX_DELAY * Device->Frequency);
96 maxlen += (ALuint)(AL_ECHO_MAX_LRDELAY * Device->Frequency);
97 maxlen = NextPowerOf2(maxlen+1);
99 if(maxlen != state->BufferLength)
101 void *temp;
103 temp = realloc(state->SampleBuffer, maxlen * sizeof(ALfloat));
104 if(!temp)
106 alSetError(AL_OUT_OF_MEMORY);
107 return AL_FALSE;
109 state->BufferLength = maxlen;
111 for(i = 0;i < state->BufferLength;i++)
112 state->SampleBuffer[i] = 0.0f;
114 return AL_TRUE;
117 ALvoid EchoUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
119 ALechoState *state = (ALechoState*)effect;
120 ALuint frequency = Context->Device->Frequency;
121 ALfloat lrpan, cw, a, g;
123 state->Tap[0].delay = (ALuint)(Effect->Echo.Delay * frequency);
124 state->Tap[1].delay = (ALuint)(Effect->Echo.LRDelay * frequency);
125 state->Tap[1].delay += state->Tap[0].delay;
127 lrpan = Effect->Echo.Spread*0.5f + 0.5f;
128 state->GainL = aluSqrt( lrpan);
129 state->GainR = aluSqrt(1.0f-lrpan);
131 state->FeedGain = Effect->Echo.Feedback;
133 cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / frequency);
134 g = 1.0f - Effect->Echo.Damping;
135 a = 0.0f;
136 if(g < 0.9999f) // 1-epsilon
137 a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
138 state->iirFilter.coeff = a;
141 ALvoid EchoProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
143 ALechoState *state = (ALechoState*)effect;
144 const ALuint mask = state->BufferLength-1;
145 const ALuint tap1 = state->Tap[0].delay;
146 const ALuint tap2 = state->Tap[1].delay;
147 ALuint offset = state->Offset;
148 const ALfloat gain = Slot->Gain;
149 ALfloat samp[2], smp;
150 ALuint i;
152 for(i = 0;i < SamplesToDo;i++,offset++)
154 // Sample first tap
155 smp = state->SampleBuffer[(offset-tap1) & mask];
156 samp[0] = smp * state->GainL;
157 samp[1] = smp * state->GainR;
158 // Sample second tap. Reverse LR panning
159 smp = state->SampleBuffer[(offset-tap2) & mask];
160 samp[0] += smp * state->GainR;
161 samp[1] += smp * state->GainL;
163 // Apply damping and feedback gain to the second tap, and mix in the
164 // new sample
165 smp = lpFilter2P(&state->iirFilter, 0, smp+SamplesIn[i]);
166 state->SampleBuffer[offset&mask] = smp * state->FeedGain;
168 // Apply slot gain
169 samp[0] *= gain;
170 samp[1] *= gain;
172 SamplesOut[i][FRONT_LEFT] += samp[0];
173 SamplesOut[i][FRONT_RIGHT] += samp[1];
174 SamplesOut[i][SIDE_LEFT] += samp[0];
175 SamplesOut[i][SIDE_RIGHT] += samp[1];
176 SamplesOut[i][BACK_LEFT] += samp[0];
177 SamplesOut[i][BACK_RIGHT] += samp[1];
179 state->Offset = offset;
182 ALeffectState *EchoCreate(void)
184 ALechoState *state;
186 state = malloc(sizeof(*state));
187 if(!state)
189 alSetError(AL_OUT_OF_MEMORY);
190 return NULL;
193 state->state.Destroy = EchoDestroy;
194 state->state.DeviceUpdate = EchoDeviceUpdate;
195 state->state.Update = EchoUpdate;
196 state->state.Process = EchoProcess;
198 state->BufferLength = 0;
199 state->SampleBuffer = NULL;
201 state->Tap[0].delay = 0;
202 state->Tap[1].delay = 0;
203 state->Offset = 0;
204 state->GainL = 0.0f;
205 state->GainR = 0.0f;
207 state->iirFilter.coeff = 0.0f;
208 state->iirFilter.history[0] = 0.0f;
209 state->iirFilter.history[1] = 0.0f;
211 return &state->state;