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
28 #include "alAuxEffectSlot.h"
33 typedef struct ALechoState
{
34 // Must be first in all effects!
37 ALfloat
*SampleBuffer
;
40 // The echo is two tap. The delay is the number of samples from before the
46 /* The panning gains for the two taps */
47 ALfloat Gain
[2][MaxChannels
];
55 static ALvoid
EchoDestroy(ALeffectState
*effect
)
57 ALechoState
*state
= (ALechoState
*)effect
;
60 free(state
->SampleBuffer
);
61 state
->SampleBuffer
= NULL
;
66 static ALboolean
EchoDeviceUpdate(ALeffectState
*effect
, ALCdevice
*Device
)
68 ALechoState
*state
= (ALechoState
*)effect
;
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
);
77 if(maxlen
!= state
->BufferLength
)
81 temp
= realloc(state
->SampleBuffer
, maxlen
* sizeof(ALfloat
));
84 state
->SampleBuffer
= temp
;
85 state
->BufferLength
= maxlen
;
87 for(i
= 0;i
< state
->BufferLength
;i
++)
88 state
->SampleBuffer
[i
] = 0.0f
;
93 static ALvoid
EchoUpdate(ALeffectState
*effect
, ALCdevice
*Device
, const ALeffectslot
*Slot
)
95 ALechoState
*state
= (ALechoState
*)effect
;
96 ALuint frequency
= Device
->Frequency
;
97 ALfloat lrpan
, cw
, g
, gain
;
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
;
105 lrpan
= Slot
->effect
.Echo
.Spread
;
107 state
->FeedGain
= Slot
->effect
.Echo
.Feedback
;
109 cw
= cosf(F_PI
*2.0f
* LOWPASSFREQREF
/ frequency
);
110 g
= 1.0f
- Slot
->effect
.Echo
.Damping
;
111 state
->iirFilter
.coeff
= lpCoeffCalc(g
, cw
);
114 for(i
= 0;i
< MaxChannels
;i
++)
116 state
->Gain
[0][i
] = 0.0f
;
117 state
->Gain
[1][i
] = 0.0f
;
120 dirGain
= fabsf(lrpan
);
122 /* First tap panning */
123 ComputeAngleGains(Device
, atan2f(-lrpan
, 0.0f
), (1.0f
-dirGain
)*F_PI
, gain
, state
->Gain
[0]);
125 /* Second tap panning */
126 ComputeAngleGains(Device
, atan2f(+lrpan
, 0.0f
), (1.0f
-dirGain
)*F_PI
, gain
, state
->Gain
[1]);
129 static ALvoid
EchoProcess(ALeffectState
*effect
, ALuint SamplesToDo
, const ALfloat
*RESTRICT SamplesIn
, ALfloat (*RESTRICT SamplesOut
)[BUFFERSIZE
])
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
;
139 for(i
= 0;i
< SamplesToDo
;i
++,offset
++)
142 smp
= state
->SampleBuffer
[(offset
-tap1
) & mask
];
143 for(k
= 0;k
< MaxChannels
;k
++)
144 SamplesOut
[k
][i
] += smp
* state
->Gain
[0][k
];
147 smp
= state
->SampleBuffer
[(offset
-tap2
) & mask
];
148 for(k
= 0;k
< MaxChannels
;k
++)
149 SamplesOut
[k
][i
] += smp
* state
->Gain
[1][k
];
151 // Apply damping and feedback gain to the second tap, and mix in the
153 smp
= lpFilter2P(&state
->iirFilter
, 0, smp
+SamplesIn
[i
]);
154 state
->SampleBuffer
[offset
&mask
] = smp
* state
->FeedGain
;
156 state
->Offset
= offset
;
159 ALeffectState
*EchoCreate(void)
163 state
= malloc(sizeof(*state
));
167 state
->state
.Destroy
= EchoDestroy
;
168 state
->state
.DeviceUpdate
= EchoDeviceUpdate
;
169 state
->state
.Update
= EchoUpdate
;
170 state
->state
.Process
= EchoProcess
;
172 state
->BufferLength
= 0;
173 state
->SampleBuffer
= NULL
;
175 state
->Tap
[0].delay
= 0;
176 state
->Tap
[1].delay
= 0;
179 state
->iirFilter
.coeff
= 0.0f
;
180 state
->iirFilter
.history
[0] = 0.0f
;
181 state
->iirFilter
.history
[1] = 0.0f
;
183 return &state
->state
;