Allocate enough reverb panning gains
[openal-soft.git] / Alc / effects / chorus.c
blobe212cd756ceb9295f76c5e14b6962e17d7059ffa
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2013 by Mike Gorchak
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.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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"
33 enum ChorusWaveForm {
34 CWF_Triangle = AL_CHORUS_WAVEFORM_TRIANGLE,
35 CWF_Sinusoid = AL_CHORUS_WAVEFORM_SINUSOID
38 typedef struct ALchorusState {
39 DERIVE_FROM_TYPE(ALeffectState);
41 ALfloat *SampleBuffer[2];
42 ALuint BufferLength;
43 ALuint offset;
44 ALuint lfo_range;
45 ALfloat lfo_scale;
46 ALint lfo_disp;
48 /* Gains for left and right sides */
49 ALfloat Gain[2][MAX_OUTPUT_CHANNELS];
51 /* effect parameters */
52 enum ChorusWaveForm waveform;
53 ALint delay;
54 ALfloat depth;
55 ALfloat feedback;
56 } ALchorusState;
58 static ALvoid ALchorusState_Destruct(ALchorusState *state)
60 free(state->SampleBuffer[0]);
61 state->SampleBuffer[0] = NULL;
62 state->SampleBuffer[1] = NULL;
65 static ALboolean ALchorusState_deviceUpdate(ALchorusState *state, ALCdevice *Device)
67 ALuint maxlen;
68 ALuint it;
70 maxlen = fastf2u(AL_CHORUS_MAX_DELAY * 3.0f * Device->Frequency) + 1;
71 maxlen = NextPowerOf2(maxlen);
73 if(maxlen != state->BufferLength)
75 void *temp;
77 temp = realloc(state->SampleBuffer[0], maxlen * sizeof(ALfloat) * 2);
78 if(!temp) return AL_FALSE;
79 state->SampleBuffer[0] = temp;
80 state->SampleBuffer[1] = state->SampleBuffer[0] + maxlen;
82 state->BufferLength = maxlen;
85 for(it = 0;it < state->BufferLength;it++)
87 state->SampleBuffer[0][it] = 0.0f;
88 state->SampleBuffer[1][it] = 0.0f;
91 return AL_TRUE;
94 static ALvoid ALchorusState_update(ALchorusState *state, const ALCdevice *Device, const ALeffectslot *Slot)
96 ALfloat frequency = (ALfloat)Device->Frequency;
97 ALfloat coeffs[MAX_AMBI_COEFFS];
98 ALfloat rate;
99 ALint phase;
101 switch(Slot->EffectProps.Chorus.Waveform)
103 case AL_CHORUS_WAVEFORM_TRIANGLE:
104 state->waveform = CWF_Triangle;
105 break;
106 case AL_CHORUS_WAVEFORM_SINUSOID:
107 state->waveform = CWF_Sinusoid;
108 break;
110 state->depth = Slot->EffectProps.Chorus.Depth;
111 state->feedback = Slot->EffectProps.Chorus.Feedback;
112 state->delay = fastf2i(Slot->EffectProps.Chorus.Delay * frequency);
114 /* Gains for left and right sides */
115 CalcXYZCoeffs(-1.0f, 0.0f, 0.0f, coeffs);
116 ComputePanningGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels, coeffs, Slot->Gain, state->Gain[0]);
117 CalcXYZCoeffs( 1.0f, 0.0f, 0.0f, coeffs);
118 ComputePanningGains(Device->Dry.AmbiCoeffs, Device->Dry.NumChannels, coeffs, Slot->Gain, state->Gain[1]);
120 phase = Slot->EffectProps.Chorus.Phase;
121 rate = Slot->EffectProps.Chorus.Rate;
122 if(!(rate > 0.0f))
124 state->lfo_scale = 0.0f;
125 state->lfo_range = 1;
126 state->lfo_disp = 0;
128 else
130 /* Calculate LFO coefficient */
131 state->lfo_range = fastf2u(frequency/rate + 0.5f);
132 switch(state->waveform)
134 case CWF_Triangle:
135 state->lfo_scale = 4.0f / state->lfo_range;
136 break;
137 case CWF_Sinusoid:
138 state->lfo_scale = F_TAU / state->lfo_range;
139 break;
142 /* Calculate lfo phase displacement */
143 state->lfo_disp = fastf2i(state->lfo_range * (phase/360.0f));
147 static inline void Triangle(ALint *delay_left, ALint *delay_right, ALuint offset, const ALchorusState *state)
149 ALfloat lfo_value;
151 lfo_value = 2.0f - fabsf(2.0f - state->lfo_scale*(offset%state->lfo_range));
152 lfo_value *= state->depth * state->delay;
153 *delay_left = fastf2i(lfo_value) + state->delay;
155 offset += state->lfo_disp;
156 lfo_value = 2.0f - fabsf(2.0f - state->lfo_scale*(offset%state->lfo_range));
157 lfo_value *= state->depth * state->delay;
158 *delay_right = fastf2i(lfo_value) + state->delay;
161 static inline void Sinusoid(ALint *delay_left, ALint *delay_right, ALuint offset, const ALchorusState *state)
163 ALfloat lfo_value;
165 lfo_value = 1.0f + sinf(state->lfo_scale*(offset%state->lfo_range));
166 lfo_value *= state->depth * state->delay;
167 *delay_left = fastf2i(lfo_value) + state->delay;
169 offset += state->lfo_disp;
170 lfo_value = 1.0f + sinf(state->lfo_scale*(offset%state->lfo_range));
171 lfo_value *= state->depth * state->delay;
172 *delay_right = fastf2i(lfo_value) + state->delay;
175 #define DECL_TEMPLATE(Func) \
176 static void Process##Func(ALchorusState *state, const ALuint SamplesToDo, \
177 const ALfloat *restrict SamplesIn, ALfloat (*restrict out)[2]) \
179 const ALuint bufmask = state->BufferLength-1; \
180 ALfloat *restrict leftbuf = state->SampleBuffer[0]; \
181 ALfloat *restrict rightbuf = state->SampleBuffer[1]; \
182 ALuint offset = state->offset; \
183 const ALfloat feedback = state->feedback; \
184 ALuint it; \
186 for(it = 0;it < SamplesToDo;it++) \
188 ALint delay_left, delay_right; \
189 Func(&delay_left, &delay_right, offset, state); \
191 out[it][0] = leftbuf[(offset-delay_left)&bufmask]; \
192 leftbuf[offset&bufmask] = (out[it][0]+SamplesIn[it]) * feedback; \
194 out[it][1] = rightbuf[(offset-delay_right)&bufmask]; \
195 rightbuf[offset&bufmask] = (out[it][1]+SamplesIn[it]) * feedback; \
197 offset++; \
199 state->offset = offset; \
202 DECL_TEMPLATE(Triangle)
203 DECL_TEMPLATE(Sinusoid)
205 #undef DECL_TEMPLATE
207 static ALvoid ALchorusState_process(ALchorusState *state, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
209 ALuint it, kt;
210 ALuint base;
212 for(base = 0;base < SamplesToDo;)
214 ALfloat temps[128][2];
215 ALuint td = minu(128, SamplesToDo-base);
217 switch(state->waveform)
219 case CWF_Triangle:
220 ProcessTriangle(state, td, SamplesIn[0]+base, temps);
221 break;
222 case CWF_Sinusoid:
223 ProcessSinusoid(state, td, SamplesIn[0]+base, temps);
224 break;
227 for(kt = 0;kt < NumChannels;kt++)
229 ALfloat gain = state->Gain[0][kt];
230 if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
232 for(it = 0;it < td;it++)
233 SamplesOut[kt][it+base] += temps[it][0] * gain;
236 gain = state->Gain[1][kt];
237 if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
239 for(it = 0;it < td;it++)
240 SamplesOut[kt][it+base] += temps[it][1] * gain;
244 base += td;
248 DECLARE_DEFAULT_ALLOCATORS(ALchorusState)
250 DEFINE_ALEFFECTSTATE_VTABLE(ALchorusState);
253 typedef struct ALchorusStateFactory {
254 DERIVE_FROM_TYPE(ALeffectStateFactory);
255 } ALchorusStateFactory;
257 static ALeffectState *ALchorusStateFactory_create(ALchorusStateFactory *UNUSED(factory))
259 ALchorusState *state;
261 state = ALchorusState_New(sizeof(*state));
262 if(!state) return NULL;
263 SET_VTABLE2(ALchorusState, ALeffectState, state);
265 state->BufferLength = 0;
266 state->SampleBuffer[0] = NULL;
267 state->SampleBuffer[1] = NULL;
268 state->offset = 0;
269 state->lfo_range = 1;
270 state->waveform = CWF_Triangle;
272 return STATIC_CAST(ALeffectState, state);
275 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALchorusStateFactory);
278 ALeffectStateFactory *ALchorusStateFactory_getFactory(void)
280 static ALchorusStateFactory ChorusFactory = { { GET_VTABLE2(ALchorusStateFactory, ALeffectStateFactory) } };
282 return STATIC_CAST(ALeffectStateFactory, &ChorusFactory);
286 void ALchorus_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
288 ALeffectProps *props = &effect->Props;
289 switch(param)
291 case AL_CHORUS_WAVEFORM:
292 if(!(val >= AL_CHORUS_MIN_WAVEFORM && val <= AL_CHORUS_MAX_WAVEFORM))
293 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
294 props->Chorus.Waveform = val;
295 break;
297 case AL_CHORUS_PHASE:
298 if(!(val >= AL_CHORUS_MIN_PHASE && val <= AL_CHORUS_MAX_PHASE))
299 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
300 props->Chorus.Phase = val;
301 break;
303 default:
304 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
307 void ALchorus_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
309 ALchorus_setParami(effect, context, param, vals[0]);
311 void ALchorus_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
313 ALeffectProps *props = &effect->Props;
314 switch(param)
316 case AL_CHORUS_RATE:
317 if(!(val >= AL_CHORUS_MIN_RATE && val <= AL_CHORUS_MAX_RATE))
318 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
319 props->Chorus.Rate = val;
320 break;
322 case AL_CHORUS_DEPTH:
323 if(!(val >= AL_CHORUS_MIN_DEPTH && val <= AL_CHORUS_MAX_DEPTH))
324 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
325 props->Chorus.Depth = val;
326 break;
328 case AL_CHORUS_FEEDBACK:
329 if(!(val >= AL_CHORUS_MIN_FEEDBACK && val <= AL_CHORUS_MAX_FEEDBACK))
330 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
331 props->Chorus.Feedback = val;
332 break;
334 case AL_CHORUS_DELAY:
335 if(!(val >= AL_CHORUS_MIN_DELAY && val <= AL_CHORUS_MAX_DELAY))
336 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
337 props->Chorus.Delay = val;
338 break;
340 default:
341 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
344 void ALchorus_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
346 ALchorus_setParamf(effect, context, param, vals[0]);
349 void ALchorus_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
351 const ALeffectProps *props = &effect->Props;
352 switch(param)
354 case AL_CHORUS_WAVEFORM:
355 *val = props->Chorus.Waveform;
356 break;
358 case AL_CHORUS_PHASE:
359 *val = props->Chorus.Phase;
360 break;
362 default:
363 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
366 void ALchorus_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
368 ALchorus_getParami(effect, context, param, vals);
370 void ALchorus_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
372 const ALeffectProps *props = &effect->Props;
373 switch(param)
375 case AL_CHORUS_RATE:
376 *val = props->Chorus.Rate;
377 break;
379 case AL_CHORUS_DEPTH:
380 *val = props->Chorus.Depth;
381 break;
383 case AL_CHORUS_FEEDBACK:
384 *val = props->Chorus.Feedback;
385 break;
387 case AL_CHORUS_DELAY:
388 *val = props->Chorus.Delay;
389 break;
391 default:
392 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
395 void ALchorus_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
397 ALchorus_getParamf(effect, context, param, vals);
400 DEFINE_ALEFFECT_VTABLE(ALchorus);