Don't needlessly expose a variable for the backends
[openal-soft/android/lowlatency.git] / Alc / alcModulator.c
blob0578ee1c1c86305440260dbe13a822b5d88d46f2
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 Scale;
48 FILTER iirFilter;
49 ALfloat history[1];
50 } ALmodulatorState;
52 #define WAVEFORM_FRACBITS 16
53 #define WAVEFORM_FRACMASK ((1<<WAVEFORM_FRACBITS)-1)
55 static __inline ALfloat sin_func(ALuint index)
57 return sin(index / (double)(1<<WAVEFORM_FRACBITS) * M_PI * 2.0f);
60 static __inline ALfloat saw_func(ALuint index)
62 return index*2.0f/(1<<WAVEFORM_FRACBITS) - 1.0f;
65 static __inline ALfloat square_func(ALuint index)
67 return ((index>>(WAVEFORM_FRACBITS-1))&1) ? -1.0f : 1.0f;
71 static __inline ALfloat hpFilter1P(FILTER *iir, ALuint offset, ALfloat input)
73 ALfloat *history = &iir->history[offset];
74 ALfloat a = iir->coeff;
75 ALfloat output = input;
77 output = output + (history[0]-output)*a;
78 history[0] = output;
80 return input - output;
84 static ALvoid ModulatorDestroy(ALeffectState *effect)
86 ALmodulatorState *state = (ALmodulatorState*)effect;
87 free(state);
90 static ALboolean ModulatorDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
92 ALmodulatorState *state = (ALmodulatorState*)effect;
94 state->Scale = aluSqrt(Device->NumChan / 8.0f);
96 return AL_TRUE;
99 static ALvoid ModulatorUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
101 ALmodulatorState *state = (ALmodulatorState*)effect;
102 ALfloat cw, a = 0.0f;
104 if(Effect->Modulator.Waveform == AL_RING_MODULATOR_SINUSOID)
105 state->Waveform = SINUSOID;
106 else if(Effect->Modulator.Waveform == AL_RING_MODULATOR_SAWTOOTH)
107 state->Waveform = SAWTOOTH;
108 else if(Effect->Modulator.Waveform == AL_RING_MODULATOR_SQUARE)
109 state->Waveform = SQUARE;
111 state->step = Effect->Modulator.Frequency*(1<<WAVEFORM_FRACBITS) /
112 Context->Device->Frequency;
113 if(!state->step)
114 state->step = 1;
116 cw = cos(2.0*M_PI * Effect->Modulator.HighPassCutoff / Context->Device->Frequency);
117 a = (2.0f-cw) - aluSqrt(aluPow(2.0f-cw, 2.0f) - 1.0f);
118 state->iirFilter.coeff = a;
121 static ALvoid ModulatorProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
123 ALmodulatorState *state = (ALmodulatorState*)effect;
124 const ALfloat gain = Slot->Gain * state->Scale;
125 const ALuint step = state->step;
126 ALuint index = state->index;
127 ALfloat samp;
128 ALuint i;
130 switch(state->Waveform)
132 case SINUSOID:
133 for(i = 0;i < SamplesToDo;i++)
135 #define FILTER_OUT(func) do { \
136 samp = SamplesIn[i]; \
138 index += step; \
139 index &= WAVEFORM_FRACMASK; \
140 samp *= func(index); \
142 samp = hpFilter1P(&state->iirFilter, 0, samp); \
144 /* Apply slot gain */ \
145 samp *= gain; \
147 SamplesOut[i][FRONT_LEFT] += samp; \
148 SamplesOut[i][FRONT_RIGHT] += samp; \
149 SamplesOut[i][FRONT_CENTER] += samp; \
150 SamplesOut[i][SIDE_LEFT] += samp; \
151 SamplesOut[i][SIDE_RIGHT] += samp; \
152 SamplesOut[i][BACK_LEFT] += samp; \
153 SamplesOut[i][BACK_RIGHT] += samp; \
154 SamplesOut[i][BACK_CENTER] += samp; \
155 } while(0)
156 FILTER_OUT(sin_func);
158 break;
160 case SAWTOOTH:
161 for(i = 0;i < SamplesToDo;i++)
163 FILTER_OUT(saw_func);
165 break;
167 case SQUARE:
168 for(i = 0;i < SamplesToDo;i++)
170 FILTER_OUT(square_func);
171 #undef FILTER_OUT
173 break;
175 state->index = index;
178 ALeffectState *ModulatorCreate(void)
180 ALmodulatorState *state;
182 state = malloc(sizeof(*state));
183 if(!state)
184 return NULL;
186 state->state.Destroy = ModulatorDestroy;
187 state->state.DeviceUpdate = ModulatorDeviceUpdate;
188 state->state.Update = ModulatorUpdate;
189 state->state.Process = ModulatorProcess;
191 state->index = 0.0f;
192 state->step = 1.0f;
194 state->Scale = 1.0f;
196 state->iirFilter.coeff = 0.0f;
197 state->iirFilter.history[0] = 0.0f;
199 return &state->state;