zenilib: zenilib/jni/external/openal-soft/Alc/alcModulator.c Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
alcModulator.c
Go to the documentation of this file.
1
21 #include "config.h"
22
23 #include <math.h>
24 #include <stdlib.h>
25
26 #include "alMain.h"
27 #include "alFilter.h"
28 #include "alAuxEffectSlot.h"
29 #include "alError.h"
30 #include "alu.h"
31
32
33 typedef struct ALmodulatorState {
34 // Must be first in all effects!
35 ALeffectState state;
36
37 enum {
38 SINUSOID,
39 SAWTOOTH,
40 SQUARE
41 } Waveform;
42
43 ALuint index;
44 ALuint step;
45
46 ALfloat Gain[MaxChannels];
47
48 FILTER iirFilter;
49 ALfloat history[1];
50 } ALmodulatorState;
51
52 #define WAVEFORM_FRACBITS 16
53 #define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
54 #define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
55
56 static __inline ALfloat Sin(ALuint index)
57 {
58 return sinf(index * (F_PI*2.0f / WAVEFORM_FRACONE));
59 }
60
61 static __inline ALfloat Saw(ALuint index)
62 {
63 return index*(2.0f/WAVEFORM_FRACONE) - 1.0f;
64 }
65
66 static __inline ALfloat Square(ALuint index)
67 {
68 return ((index>>(WAVEFORM_FRACBITS-1))&1)*2.0f - 1.0f;
69 }
70
71
72 static __inline ALfloat hpFilter1P(FILTER *iir, ALuint offset, ALfloat input)
73 {
74 ALfloat *history = &iir->history[offset];
75 ALfloat a = iir->coeff;
76 ALfloat output = input;
77
78 output = output + (history[0]-output)*a;
79 history[0] = output;
80
81 return input - output;
82 }
83
84
85 #define DECL_TEMPLATE(func) \
86 static void Process##func(ALmodulatorState *state, ALuint SamplesToDo, \
87 const ALfloat *RESTRICT SamplesIn, \
88 ALfloat (*RESTRICT SamplesOut)[BUFFERSIZE]) \
89 { \
90 const ALuint step = state->step; \
91 ALuint index = state->index; \
92 ALfloat samp; \
93 ALuint i, k; \
94 \
95 for(i = 0;i < SamplesToDo;i++) \
96 { \
97 samp = SamplesIn[i]; \
98 \
99 index += step; \
100 index &= WAVEFORM_FRACMASK; \
101 samp *= func(index); \
102 \
103 samp = hpFilter1P(&state->iirFilter, 0, samp); \
104 \
105 for(k = 0;k < MaxChannels;k++) \
106 SamplesOut[k][i] += state->Gain[k] * samp; \
107 } \
108 state->index = index; \
109 }
110
111 DECL_TEMPLATE(Sin)
112 DECL_TEMPLATE(Saw)
113 DECL_TEMPLATE(Square)
114
115 #undef DECL_TEMPLATE
116
117
118 static ALvoid ModulatorDestroy(ALeffectState *effect)
119 {
120 ALmodulatorState *state = (ALmodulatorState*)effect;
121 free(state);
122 }
123
124 static ALboolean ModulatorDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
125 {
126 return AL_TRUE;
127 (void)effect;
128 (void)Device;
129 }
130
131 static ALvoid ModulatorUpdate(ALeffectState *effect, ALCdevice *Device, const ALeffectslot *Slot)
132 {
133 ALmodulatorState *state = (ALmodulatorState*)effect;
134 ALfloat gain, cw, a = 0.0f;
135 ALuint index;
136
137 if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SINUSOID)
138 state->Waveform = SINUSOID;
139 else if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SAWTOOTH)
140 state->Waveform = SAWTOOTH;
141 else if(Slot->effect.Modulator.Waveform == AL_RING_MODULATOR_SQUARE)
142 state->Waveform = SQUARE;
143
144 state->step = fastf2u(Slot->effect.Modulator.Frequency*WAVEFORM_FRACONE /
145 Device->Frequency);
146 if(state->step == 0) state->step = 1;
147
148 cw = cosf(F_PI*2.0f * Slot->effect.Modulator.HighPassCutoff /
149 Device->Frequency);
150 a = (2.0f-cw) - sqrtf(powf(2.0f-cw, 2.0f) - 1.0f);
151 state->iirFilter.coeff = a;
152
153 gain = sqrtf(1.0f/Device->NumChan);
154 gain *= Slot->Gain;
155 for(index = 0;index < MaxChannels;index++)
156 state->Gain[index] = 0.0f;
157 for(index = 0;index < Device->NumChan;index++)
158 {
159 enum Channel chan = Device->Speaker2Chan[index];
160 state->Gain[chan] = gain;
161 }
162 }
163
164 static ALvoid ModulatorProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *RESTRICT SamplesIn, ALfloat (*RESTRICT SamplesOut)[BUFFERSIZE])
165 {
166 ALmodulatorState *state = (ALmodulatorState*)effect;
167
168 switch(state->Waveform)
169 {
170 case SINUSOID:
171 ProcessSin(state, SamplesToDo, SamplesIn, SamplesOut);
172 break;
173
174 case SAWTOOTH:
175 ProcessSaw(state, SamplesToDo, SamplesIn, SamplesOut);
176 break;
177
178 case SQUARE:
179 ProcessSquare(state, SamplesToDo, SamplesIn, SamplesOut);
180 break;
181 }
182 }
183
184 ALeffectState *ModulatorCreate(void)
185 {
186 ALmodulatorState *state;
187
188 state = malloc(sizeof(*state));
189 if(!state)
190 return NULL;
191
192 state->state.Destroy = ModulatorDestroy;
193 state->state.DeviceUpdate = ModulatorDeviceUpdate;
194 state->state.Update = ModulatorUpdate;
195 state->state.Process = ModulatorProcess;
196
197 state->index = 0;
198 state->step = 1;
199
200 state->iirFilter.coeff = 0.0f;
201 state->iirFilter.history[0] = 0.0f;
202
203 return &state->state;
204 }
AL_RING_MODULATOR_SAWTOOTH
#define AL_RING_MODULATOR_SAWTOOTH
Definition: efx.h:654
FILTER::coeff
ALfloat coeff
Definition: alFilter.h:13
F_PI
#define F_PI
Definition: alu.h:16
ALvoid
void ALvoid
Definition: al.h:74
ModulatorProcess
static ALvoid ModulatorProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *RESTRICT SamplesIn, ALfloat(*RESTRICT SamplesOut)[BUFFERSIZE])
Definition: alcModulator.c:164
FILTER::history
ALfloat history[0]
Definition: alFilter.h:15
ALeffect::Frequency
ALfloat Frequency
Definition: alEffect.h:69
void
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1824
AL_TRUE
#define AL_TRUE
Definition: al.h:86
hpFilter1P
static __inline ALfloat hpFilter1P(FILTER *iir, ALuint offset, ALfloat input)
Definition: alcModulator.c:72
ModulatorDeviceUpdate
static ALboolean ModulatorDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
Definition: alcModulator.c:124
AL_RING_MODULATOR_SINUSOID
#define AL_RING_MODULATOR_SINUSOID
Definition: efx.h:653
NULL
#define NULL
Definition: ftobjs.h:61
f
GLclampf f
Definition: glew.h:3390
ModulatorCreate
ALeffectState * ModulatorCreate(void)
Definition: alcModulator.c:184
alu.h
free
SDL_EventEntry * free
Definition: SDL_events.c:80
ALCdevice_struct::Frequency
ALuint Frequency
Definition: alMain.h:569
Sin
static __inline ALfloat Sin(ALuint index)
Definition: alcModulator.c:56
ALCdevice_struct
Definition: alMain.h:560
ALeffectState
Definition: alAuxEffectSlot.h:11
a
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:8736
alError.h
alFilter.h
ALeffect::Modulator
struct ALeffect::@54 Modulator
ALfloat
float ALfloat
Definition: al.h:68
Square
static __inline ALfloat Square(ALuint index)
Definition: alcModulator.c:66
Saw
static __inline ALfloat Saw(ALuint index)
Definition: alcModulator.c:61
DECL_TEMPLATE
#define DECL_TEMPLATE(func)
Definition: alcModulator.c:85
ALeffect::HighPassCutoff
ALfloat HighPassCutoff
Definition: alEffect.h:70
ALCdevice_struct::NumChan
ALuint NumChan
Definition: alMain.h:611
ALCdevice_struct::Speaker2Chan
enum Channel Speaker2Chan[MaxChannels]
Definition: alMain.h:609
alAuxEffectSlot.h
ALeffectslot::effect
ALeffect effect
Definition: alAuxEffectSlot.h:21
ALeffectslot
Definition: alAuxEffectSlot.h:19
alMain.h
FILTER
Definition: alFilter.h:12
fastf2u
static __inline ALuint fastf2u(ALfloat f)
Definition: alMain.h:379
index
GLuint index
Definition: glew.h:1800
ALeffectslot::Gain
volatile ALfloat Gain
Definition: alAuxEffectSlot.h:23
ALuint
unsigned int ALuint
Definition: al.h:59
WAVEFORM_FRACONE
#define WAVEFORM_FRACONE
Definition: alcModulator.c:53
WAVEFORM_FRACBITS
#define WAVEFORM_FRACBITS
Definition: alcModulator.c:52
malloc
#define malloc
Definition: SDL_malloc.c:635
ModulatorDestroy
static ALvoid ModulatorDestroy(ALeffectState *effect)
Definition: alcModulator.c:118
input
GLenum GLenum GLenum input
Definition: glew.h:12631
offset
GLintptr offset
Definition: glew.h:1668
sort_controllers.output
tuple output
Definition: sort_controllers.py:10
ALboolean
char ALboolean
Definition: al.h:38
ALmodulatorState
struct ALmodulatorState ALmodulatorState
AL_RING_MODULATOR_SQUARE
#define AL_RING_MODULATOR_SQUARE
Definition: efx.h:655
BUFFERSIZE
#define BUFFERSIZE
Definition: alMain.h:556
ModulatorUpdate
static ALvoid ModulatorUpdate(ALeffectState *effect, ALCdevice *Device, const ALeffectslot *Slot)
Definition: alcModulator.c:131
Channel
Channel
Definition: alMain.h:480
ALeffect::Waveform
ALint Waveform
Definition: alEffect.h:71
MaxChannels
Definition: alMain.h:491