Rename LOWPASSFREQCUTOFF to LOWPASSFREQREF
[openal-soft/android.git] / Alc / alcModulator.c
blobd955f10380460759ed442a92807c0bee637168cc
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"
33 typedef struct ALmodulatorState {
34 // Must be first in all effects!
35 ALeffectState state;
37 enum {
38 SINUSOID,
39 SAWTOOTH,
40 SQUARE
41 } Waveform;
43 ALuint index;
44 ALuint step;
46 ALfloat Gain[MAXCHANNELS];
48 FILTER iirFilter;
49 ALfloat history[1];
50 } ALmodulatorState;
52 #define WAVEFORM_FRACBITS 16
53 #define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
54 #define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
56 static __inline ALfloat Sin(ALuint index)
58 return aluSin(index * (F_PI*2.0f / WAVEFORM_FRACONE));
61 static __inline ALfloat Saw(ALuint index)
63 return index*(2.0f/WAVEFORM_FRACONE) - 1.0f;
66 static __inline ALfloat Square(ALuint index)
68 return ((index>>(WAVEFORM_FRACBITS-1))&1)*2.0f - 1.0f;
72 static __inline ALfloat hpFilter1P(FILTER *iir, ALuint offset, ALfloat input)
74 ALfloat *history = &iir->history[offset];
75 ALfloat a = iir->coeff;
76 ALfloat output = input;
78 output = output + (history[0]-output)*a;
79 history[0] = output;
81 return input - output;
85 #define DECL_TEMPLATE(func) \
86 static void Process##func(ALmodulatorState *state, ALuint SamplesToDo, \
87 const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS]) \
88 { \
89 const ALuint step = state->step; \
90 ALuint index = state->index; \
91 ALfloat samp; \
92 ALuint i; \
94 for(i = 0;i < SamplesToDo;i++) \
95 { \
96 samp = SamplesIn[i]; \
98 index += step; \
99 index &= WAVEFORM_FRACMASK; \
100 samp *= func(index); \
102 samp = hpFilter1P(&state->iirFilter, 0, samp); \
104 SamplesOut[i][FRONT_LEFT] += state->Gain[FRONT_LEFT] * samp; \
105 SamplesOut[i][FRONT_RIGHT] += state->Gain[FRONT_RIGHT] * samp; \
106 SamplesOut[i][FRONT_CENTER] += state->Gain[FRONT_CENTER] * samp; \
107 SamplesOut[i][SIDE_LEFT] += state->Gain[SIDE_LEFT] * samp; \
108 SamplesOut[i][SIDE_RIGHT] += state->Gain[SIDE_RIGHT] * samp; \
109 SamplesOut[i][BACK_LEFT] += state->Gain[BACK_LEFT] * samp; \
110 SamplesOut[i][BACK_RIGHT] += state->Gain[BACK_RIGHT] * samp; \
111 SamplesOut[i][BACK_CENTER] += state->Gain[BACK_CENTER] * samp; \
113 state->index = index; \
116 DECL_TEMPLATE(Sin)
117 DECL_TEMPLATE(Saw)
118 DECL_TEMPLATE(Square)
120 #undef DECL_TEMPLATE
123 static ALvoid ModulatorDestroy(ALeffectState *effect)
125 ALmodulatorState *state = (ALmodulatorState*)effect;
126 free(state);
129 static ALboolean ModulatorDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
131 return AL_TRUE;
132 (void)effect;
133 (void)Device;
136 static ALvoid ModulatorUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffectslot *Slot)
138 ALmodulatorState *state = (ALmodulatorState*)effect;
139 ALCdevice *Device = Context->Device;
140 ALfloat gain, cw, a = 0.0f;
141 ALuint index;
143 if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SINUSOID)
144 state->Waveform = SINUSOID;
145 else if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SAWTOOTH)
146 state->Waveform = SAWTOOTH;
147 else if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SQUARE)
148 state->Waveform = SQUARE;
150 state->step = fastf2u(Slot->effect.Modulator.Frequency*WAVEFORM_FRACONE /
151 Device->Frequency);
152 if(state->step == 0) state->step = 1;
154 cw = aluCos(F_PI*2.0f * Slot->effect.Modulator.HighPassCutoff /
155 Device->Frequency);
156 a = (2.0f-cw) - aluSqrt(aluPow(2.0f-cw, 2.0f) - 1.0f);
157 state->iirFilter.coeff = a;
159 gain = Slot->Gain;
160 for(index = 0;index < MAXCHANNELS;index++)
161 state->Gain[index] = 0.0f;
162 for(index = 0;index < Device->NumChan;index++)
164 enum Channel chan = Device->Speaker2Chan[index];
165 state->Gain[chan] = gain;
169 static ALvoid ModulatorProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
171 ALmodulatorState *state = (ALmodulatorState*)effect;
173 switch(state->Waveform)
175 case SINUSOID:
176 ProcessSin(state, SamplesToDo, SamplesIn, SamplesOut);
177 break;
179 case SAWTOOTH:
180 ProcessSaw(state, SamplesToDo, SamplesIn, SamplesOut);
181 break;
183 case SQUARE:
184 ProcessSquare(state, SamplesToDo, SamplesIn, SamplesOut);
185 break;
189 ALeffectState *ModulatorCreate(void)
191 ALmodulatorState *state;
193 state = malloc(sizeof(*state));
194 if(!state)
195 return NULL;
197 state->state.Destroy = ModulatorDestroy;
198 state->state.DeviceUpdate = ModulatorDeviceUpdate;
199 state->state.Update = ModulatorUpdate;
200 state->state.Process = ModulatorProcess;
202 state->index = 0;
203 state->step = 1;
205 state->iirFilter.coeff = 0.0f;
206 state->iirFilter.history[0] = 0.0f;
208 return &state->state;