zenilib  0.5.3.0
 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];
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 
73 {
74  ALfloat *history = &iir->history[offset];
75  ALfloat a = iir->coeff;
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 
114 
115 #undef DECL_TEMPLATE
116 
117 
119 {
120  ALmodulatorState *state = (ALmodulatorState*)effect;
121  free(state);
122 }
123 
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 
138  state->Waveform = SINUSOID;
140  state->Waveform = SAWTOOTH;
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 
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 }
#define AL_RING_MODULATOR_SAWTOOTH
Definition: efx.h:654
ALfloat coeff
Definition: alFilter.h:13
#define F_PI
Definition: alu.h:16
void ALvoid
Definition: al.h:74
static ALvoid ModulatorProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *RESTRICT SamplesIn, ALfloat(*RESTRICT SamplesOut)[BUFFERSIZE])
Definition: alcModulator.c:164
ALfloat history[0]
Definition: alFilter.h:15
ALfloat Frequency
Definition: alEffect.h:69
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1824
#define AL_TRUE
Definition: al.h:86
static __inline ALfloat hpFilter1P(FILTER *iir, ALuint offset, ALfloat input)
Definition: alcModulator.c:72
static ALboolean ModulatorDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
Definition: alcModulator.c:124
#define AL_RING_MODULATOR_SINUSOID
Definition: efx.h:653
#define NULL
Definition: ftobjs.h:61
GLclampf f
Definition: glew.h:3390
ALeffectState * ModulatorCreate(void)
Definition: alcModulator.c:184
SDL_EventEntry * free
Definition: SDL_events.c:80
ALuint Frequency
Definition: alMain.h:569
static __inline ALfloat Sin(ALuint index)
Definition: alcModulator.c:56
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:8736
struct ALeffect::@54 Modulator
float ALfloat
Definition: al.h:68
static __inline ALfloat Square(ALuint index)
Definition: alcModulator.c:66
static __inline ALfloat Saw(ALuint index)
Definition: alcModulator.c:61
#define DECL_TEMPLATE(func)
Definition: alcModulator.c:85
ALfloat HighPassCutoff
Definition: alEffect.h:70
ALuint NumChan
Definition: alMain.h:611
enum Channel Speaker2Chan[MaxChannels]
Definition: alMain.h:609
ALeffect effect
static __inline ALuint fastf2u(ALfloat f)
Definition: alMain.h:379
GLuint index
Definition: glew.h:1800
volatile ALfloat Gain
unsigned int ALuint
Definition: al.h:59
#define WAVEFORM_FRACONE
Definition: alcModulator.c:53
#define WAVEFORM_FRACBITS
Definition: alcModulator.c:52
#define malloc
Definition: SDL_malloc.c:635
static ALvoid ModulatorDestroy(ALeffectState *effect)
Definition: alcModulator.c:118
GLenum GLenum GLenum input
Definition: glew.h:12631
GLintptr offset
Definition: glew.h:1668
char ALboolean
Definition: al.h:38
struct ALmodulatorState ALmodulatorState
#define AL_RING_MODULATOR_SQUARE
Definition: efx.h:655
#define BUFFERSIZE
Definition: alMain.h:556
static ALvoid ModulatorUpdate(ALeffectState *effect, ALCdevice *Device, const ALeffectslot *Slot)
Definition: alcModulator.c:131
Channel
Definition: alMain.h:480
ALint Waveform
Definition: alEffect.h:71