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 LR gains for the first tap. The second tap uses the reverse
58 static ALvoid
EchoDestroy(ALeffectState
*effect
)
60 ALechoState
*state
= (ALechoState
*)effect
;
63 free(state
->SampleBuffer
);
64 state
->SampleBuffer
= NULL
;
69 static ALboolean
EchoDeviceUpdate(ALeffectState
*effect
, ALCdevice
*Device
)
71 ALechoState
*state
= (ALechoState
*)effect
;
74 // Use the next power of 2 for the buffer length, so the tap offsets can be
75 // wrapped using a mask instead of a modulo
76 maxlen
= (ALuint
)(AL_ECHO_MAX_DELAY
* Device
->Frequency
) + 1;
77 maxlen
+= (ALuint
)(AL_ECHO_MAX_LRDELAY
* Device
->Frequency
) + 1;
78 maxlen
= NextPowerOf2(maxlen
);
80 if(maxlen
!= state
->BufferLength
)
84 temp
= realloc(state
->SampleBuffer
, maxlen
* sizeof(ALfloat
));
87 state
->SampleBuffer
= temp
;
88 state
->BufferLength
= maxlen
;
90 for(i
= 0;i
< state
->BufferLength
;i
++)
91 state
->SampleBuffer
[i
] = 0.0f
;
93 state
->Scale
= aluSqrt(Device
->NumChan
/ 6.0f
);
94 state
->Scale
= __min(state
->Scale
, 1.0f
);
99 static ALvoid
EchoUpdate(ALeffectState
*effect
, ALCcontext
*Context
, const ALeffect
*Effect
)
101 ALechoState
*state
= (ALechoState
*)effect
;
102 ALuint frequency
= Context
->Device
->Frequency
;
103 ALfloat lrpan
, cw
, a
, g
;
105 state
->Tap
[0].delay
= (ALuint
)(Effect
->Echo
.Delay
* frequency
) + 1;
106 state
->Tap
[1].delay
= (ALuint
)(Effect
->Echo
.LRDelay
* frequency
);
107 state
->Tap
[1].delay
+= state
->Tap
[0].delay
;
109 lrpan
= Effect
->Echo
.Spread
*0.5f
+ 0.5f
;
110 state
->GainL
= aluSqrt( lrpan
);
111 state
->GainR
= aluSqrt(1.0f
-lrpan
);
113 state
->FeedGain
= Effect
->Echo
.Feedback
;
115 cw
= cos(2.0*M_PI
* LOWPASSFREQCUTOFF
/ frequency
);
116 g
= 1.0f
- Effect
->Echo
.Damping
;
118 if(g
< 0.9999f
) // 1-epsilon
119 a
= (1 - g
*cw
- aluSqrt(2*g
*(1-cw
) - g
*g
*(1 - cw
*cw
))) / (1 - g
);
120 state
->iirFilter
.coeff
= a
;
123 static ALvoid
EchoProcess(ALeffectState
*effect
, const ALeffectslot
*Slot
, ALuint SamplesToDo
, const ALfloat
*SamplesIn
, ALfloat (*SamplesOut
)[OUTPUTCHANNELS
])
125 ALechoState
*state
= (ALechoState
*)effect
;
126 const ALuint mask
= state
->BufferLength
-1;
127 const ALuint tap1
= state
->Tap
[0].delay
;
128 const ALuint tap2
= state
->Tap
[1].delay
;
129 ALuint offset
= state
->Offset
;
130 const ALfloat gain
= Slot
->Gain
* state
->Scale
;
131 ALfloat samp
[2], smp
;
134 for(i
= 0;i
< SamplesToDo
;i
++,offset
++)
137 smp
= state
->SampleBuffer
[(offset
-tap1
) & mask
];
138 samp
[0] = smp
* state
->GainL
;
139 samp
[1] = smp
* state
->GainR
;
140 // Sample second tap. Reverse LR panning
141 smp
= state
->SampleBuffer
[(offset
-tap2
) & mask
];
142 samp
[0] += smp
* state
->GainR
;
143 samp
[1] += smp
* state
->GainL
;
145 // Apply damping and feedback gain to the second tap, and mix in the
147 smp
= lpFilter2P(&state
->iirFilter
, 0, smp
+SamplesIn
[i
]);
148 state
->SampleBuffer
[offset
&mask
] = smp
* state
->FeedGain
;
154 SamplesOut
[i
][FRONT_LEFT
] += samp
[0];
155 SamplesOut
[i
][FRONT_RIGHT
] += samp
[1];
156 SamplesOut
[i
][SIDE_LEFT
] += samp
[0];
157 SamplesOut
[i
][SIDE_RIGHT
] += samp
[1];
158 SamplesOut
[i
][BACK_LEFT
] += samp
[0];
159 SamplesOut
[i
][BACK_RIGHT
] += samp
[1];
161 state
->Offset
= offset
;
164 ALeffectState
*EchoCreate(void)
168 state
= malloc(sizeof(*state
));
172 state
->state
.Destroy
= EchoDestroy
;
173 state
->state
.DeviceUpdate
= EchoDeviceUpdate
;
174 state
->state
.Update
= EchoUpdate
;
175 state
->state
.Process
= EchoProcess
;
177 state
->BufferLength
= 0;
178 state
->SampleBuffer
= NULL
;
180 state
->Tap
[0].delay
= 0;
181 state
->Tap
[1].delay
= 0;
188 state
->iirFilter
.coeff
= 0.0f
;
189 state
->iirFilter
.history
[0] = 0.0f
;
190 state
->iirFilter
.history
[1] = 0.0f
;
192 return &state
->state
;