zenilib: zenilib/jni/external/openal-soft/Alc/alcEcho.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
alcEcho.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 ALechoState {
34 // Must be first in all effects!
35 ALeffectState state;
36
37 ALfloat *SampleBuffer;
38 ALuint BufferLength;
39
40 // The echo is two tap. The delay is the number of samples from before the
41 // current offset
42 struct {
43 ALuint delay;
44 } Tap[2];
45 ALuint Offset;
46 /* The panning gains for the two taps */
47 ALfloat Gain[2][MaxChannels];
48
49 ALfloat FeedGain;
50
51 FILTER iirFilter;
52 ALfloat history[2];
53 } ALechoState;
54
55 static ALvoid EchoDestroy(ALeffectState *effect)
56 {
57 ALechoState *state = (ALechoState*)effect;
58 if(state)
59 {
60 free(state->SampleBuffer);
61 state->SampleBuffer = NULL;
62 free(state);
63 }
64 }
65
66 static ALboolean EchoDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
67 {
68 ALechoState *state = (ALechoState*)effect;
69 ALuint maxlen, i;
70
71 // Use the next power of 2 for the buffer length, so the tap offsets can be
72 // wrapped using a mask instead of a modulo
73 maxlen = fastf2u(AL_ECHO_MAX_DELAY * Device->Frequency) + 1;
74 maxlen += fastf2u(AL_ECHO_MAX_LRDELAY * Device->Frequency) + 1;
75 maxlen = NextPowerOf2(maxlen);
76
77 if(maxlen != state->BufferLength)
78 {
79 void *temp;
80
81 temp = realloc(state->SampleBuffer, maxlen * sizeof(ALfloat));
82 if(!temp)
83 return AL_FALSE;
84 state->SampleBuffer = temp;
85 state->BufferLength = maxlen;
86 }
87 for(i = 0;i < state->BufferLength;i++)
88 state->SampleBuffer[i] = 0.0f;
89
90 return AL_TRUE;
91 }
92
93 static ALvoid EchoUpdate(ALeffectState *effect, ALCdevice *Device, const ALeffectslot *Slot)
94 {
95 ALechoState *state = (ALechoState*)effect;
96 ALuint frequency = Device->Frequency;
97 ALfloat lrpan, cw, g, gain;
98 ALfloat dirGain;
99 ALuint i;
100
101 state->Tap[0].delay = fastf2u(Slot->effect.Echo.Delay * frequency) + 1;
102 state->Tap[1].delay = fastf2u(Slot->effect.Echo.LRDelay * frequency);
103 state->Tap[1].delay += state->Tap[0].delay;
104
105 lrpan = Slot->effect.Echo.Spread;
106
107 state->FeedGain = Slot->effect.Echo.Feedback;
108
109 cw = cosf(F_PI*2.0f * LOWPASSFREQREF / frequency);
110 g = 1.0f - Slot->effect.Echo.Damping;
111 state->iirFilter.coeff = lpCoeffCalc(g, cw);
112
113 gain = Slot->Gain;
114 for(i = 0;i < MaxChannels;i++)
115 {
116 state->Gain[0][i] = 0.0f;
117 state->Gain[1][i] = 0.0f;
118 }
119
120 dirGain = fabsf(lrpan);
121
122 /* First tap panning */
123 ComputeAngleGains(Device, atan2f(-lrpan, 0.0f), (1.0f-dirGain)*F_PI, gain, state->Gain[0]);
124
125 /* Second tap panning */
126 ComputeAngleGains(Device, atan2f(+lrpan, 0.0f), (1.0f-dirGain)*F_PI, gain, state->Gain[1]);
127 }
128
129 static ALvoid EchoProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *RESTRICT SamplesIn, ALfloat (*RESTRICT SamplesOut)[BUFFERSIZE])
130 {
131 ALechoState *state = (ALechoState*)effect;
132 const ALuint mask = state->BufferLength-1;
133 const ALuint tap1 = state->Tap[0].delay;
134 const ALuint tap2 = state->Tap[1].delay;
135 ALuint offset = state->Offset;
136 ALfloat smp;
137 ALuint i, k;
138
139 for(i = 0;i < SamplesToDo;i++,offset++)
140 {
141 /* First tap */
142 smp = state->SampleBuffer[(offset-tap1) & mask];
143 for(k = 0;k < MaxChannels;k++)
144 SamplesOut[k][i] += smp * state->Gain[0][k];
145
146 /* Second tap */
147 smp = state->SampleBuffer[(offset-tap2) & mask];
148 for(k = 0;k < MaxChannels;k++)
149 SamplesOut[k][i] += smp * state->Gain[1][k];
150
151 // Apply damping and feedback gain to the second tap, and mix in the
152 // new sample
153 smp = lpFilter2P(&state->iirFilter, 0, smp+SamplesIn[i]);
154 state->SampleBuffer[offset&mask] = smp * state->FeedGain;
155 }
156 state->Offset = offset;
157 }
158
159 ALeffectState *EchoCreate(void)
160 {
161 ALechoState *state;
162
163 state = malloc(sizeof(*state));
164 if(!state)
165 return NULL;
166
167 state->state.Destroy = EchoDestroy;
168 state->state.DeviceUpdate = EchoDeviceUpdate;
169 state->state.Update = EchoUpdate;
170 state->state.Process = EchoProcess;
171
172 state->BufferLength = 0;
173 state->SampleBuffer = NULL;
174
175 state->Tap[0].delay = 0;
176 state->Tap[1].delay = 0;
177 state->Offset = 0;
178
179 state->iirFilter.coeff = 0.0f;
180 state->iirFilter.history[0] = 0.0f;
181 state->iirFilter.history[1] = 0.0f;
182
183 return &state->state;
184 }
ALeffect::Spread
ALfloat Spread
Definition: alEffect.h:65
F_PI
#define F_PI
Definition: alu.h:16
ALvoid
void ALvoid
Definition: al.h:74
AL_TRUE
#define AL_TRUE
Definition: al.h:86
g
GLboolean GLboolean g
Definition: glew.h:8736
NULL
#define NULL
Definition: ftobjs.h:61
f
GLclampf f
Definition: glew.h:3390
k
int32_t k
Definition: e_log.c:102
lpCoeffCalc
ALfloat lpCoeffCalc(ALfloat g, ALfloat cw)
Definition: alFilter.c:329
alu.h
free
SDL_EventEntry * free
Definition: SDL_events.c:80
ALechoState
struct ALechoState ALechoState
ALCdevice_struct::Frequency
ALuint Frequency
Definition: alMain.h:569
ALCdevice_struct
Definition: alMain.h:560
ALeffectState
Definition: alAuxEffectSlot.h:11
AL_FALSE
#define AL_FALSE
Definition: al.h:83
EchoDestroy
static ALvoid EchoDestroy(ALeffectState *effect)
Definition: alcEcho.c:55
alError.h
alFilter.h
ALeffect::Echo
struct ALeffect::@53 Echo
ALfloat
float ALfloat
Definition: al.h:68
lpFilter2P
static __inline ALfloat lpFilter2P(FILTER *iir, ALuint offset, ALfloat input)
Definition: alFilter.h:21
ALeffect::Delay
ALfloat Delay
Definition: alEffect.h:59
alAuxEffectSlot.h
ALeffectslot::effect
ALeffect effect
Definition: alAuxEffectSlot.h:21
ALeffectslot
Definition: alAuxEffectSlot.h:19
alMain.h
ALeffect::LRDelay
ALfloat LRDelay
Definition: alEffect.h:60
FILTER
Definition: alFilter.h:12
fastf2u
static __inline ALuint fastf2u(ALfloat f)
Definition: alMain.h:379
ALeffect::Damping
ALfloat Damping
Definition: alEffect.h:62
realloc
#define realloc
Definition: SDL_malloc.c:637
ComputeAngleGains
ALvoid ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat hwidth, ALfloat ingain, ALfloat *gains)
Definition: panning.c:149
AL_ECHO_MAX_LRDELAY
#define AL_ECHO_MAX_LRDELAY
Definition: efx.h:515
ALeffect::Feedback
ALfloat Feedback
Definition: alEffect.h:63
ALeffectslot::Gain
volatile ALfloat Gain
Definition: alAuxEffectSlot.h:23
ALuint
unsigned int ALuint
Definition: al.h:59
EchoDeviceUpdate
static ALboolean EchoDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
Definition: alcEcho.c:66
EchoCreate
ALeffectState * EchoCreate(void)
Definition: alcEcho.c:159
AL_ECHO_MAX_DELAY
#define AL_ECHO_MAX_DELAY
Definition: efx.h:511
malloc
#define malloc
Definition: SDL_malloc.c:635
offset
GLintptr offset
Definition: glew.h:1668
LOWPASSFREQREF
#define LOWPASSFREQREF
Definition: alFilter.h:10
mask
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
Definition: gl2ext.h:961
NextPowerOf2
static size_t NextPowerOf2(size_t value)
Definition: alffmpeg.c:42
ALboolean
char ALboolean
Definition: al.h:38
EchoUpdate
static ALvoid EchoUpdate(ALeffectState *effect, ALCdevice *Device, const ALeffectslot *Slot)
Definition: alcEcho.c:93
i
int i
Definition: pngrutil.c:1377
EchoProcess
static ALvoid EchoProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *RESTRICT SamplesIn, ALfloat(*RESTRICT SamplesOut)[BUFFERSIZE])
Definition: alcEcho.c:129
BUFFERSIZE
#define BUFFERSIZE
Definition: alMain.h:556
MaxChannels
Definition: alMain.h:491