2 * OpenAL cross platform audio library
3 * Copyright (C) 2013 by Mike Gorchak
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.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
27 #include "alcontext.h"
28 #include "alAuxEffectSlot.h"
31 #include "filters/defs.h"
35 /* The document "Effects Extension Guide.pdf" says that low and high *
36 * frequencies are cutoff frequencies. This is not fully correct, they *
37 * are corner frequencies for low and high shelf filters. If they were *
38 * just cutoff frequencies, there would be no need in cutoff frequency *
39 * gains, which are present. Documentation for "Creative Proteus X2" *
40 * software describes 4-band equalizer functionality in a much better *
41 * way. This equalizer seems to be a predecessor of OpenAL 4-band *
42 * equalizer. With low and high shelf filters we are able to cutoff *
43 * frequencies below and/or above corner frequencies using attenuation *
44 * gains (below 1.0) and amplify all low and/or high frequencies using *
47 * Low-shelf Low Mid Band High Mid Band High-shelf *
48 * corner center center corner *
49 * frequency frequency frequency frequency *
50 * 50Hz..800Hz 200Hz..3000Hz 1000Hz..8000Hz 4000Hz..16000Hz *
54 * B -----+ /--+--\ /--+--\ +----- *
55 * O |\ | | | | | | /| *
56 * O | \ - | - - | - / | *
57 * S + | \ | | | | | | / | *
58 * T | | | | | | | | | | *
59 * ---------+---------------+------------------+---------------+-------- *
60 * C | | | | | | | | | | *
61 * U - | / | | | | | | \ | *
62 * T | / - | - - | - \ | *
63 * O |/ | | | | | | \| *
64 * F -----+ \--+--/ \--+--/ +----- *
68 * Gains vary from 0.126 up to 7.943, which means from -18dB attenuation *
69 * up to +18dB amplification. Band width varies from 0.01 up to 1.0 in *
70 * octaves for two mid bands. *
72 * Implementation is based on the "Cookbook formulae for audio EQ biquad *
73 * filter coefficients" by Robert Bristow-Johnson *
74 * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt */
77 struct ALequalizerState final
: public ALeffectState
{
79 /* Effect parameters */
80 BiquadFilter filter
[4];
82 /* Effect gains for each channel */
83 ALfloat CurrentGains
[MAX_OUTPUT_CHANNELS
];
84 ALfloat TargetGains
[MAX_OUTPUT_CHANNELS
];
85 } Chans
[MAX_EFFECT_CHANNELS
];
87 ALfloat SampleBuffer
[MAX_EFFECT_CHANNELS
][BUFFERSIZE
];
90 static ALvoid
ALequalizerState_Destruct(ALequalizerState
*state
);
91 static ALboolean
ALequalizerState_deviceUpdate(ALequalizerState
*state
, ALCdevice
*device
);
92 static ALvoid
ALequalizerState_update(ALequalizerState
*state
, const ALCcontext
*context
, const ALeffectslot
*slot
, const ALeffectProps
*props
);
93 static ALvoid
ALequalizerState_process(ALequalizerState
*state
, ALsizei SamplesToDo
, const ALfloat (*RESTRICT SamplesIn
)[BUFFERSIZE
], ALfloat (*RESTRICT SamplesOut
)[BUFFERSIZE
], ALsizei NumChannels
);
94 DECLARE_DEFAULT_ALLOCATORS(ALequalizerState
)
96 DEFINE_ALEFFECTSTATE_VTABLE(ALequalizerState
);
99 static void ALequalizerState_Construct(ALequalizerState
*state
)
101 new (state
) ALequalizerState
{};
102 ALeffectState_Construct(STATIC_CAST(ALeffectState
, state
));
103 SET_VTABLE2(ALequalizerState
, ALeffectState
, state
);
106 static ALvoid
ALequalizerState_Destruct(ALequalizerState
*state
)
108 ALeffectState_Destruct(STATIC_CAST(ALeffectState
,state
));
109 state
->~ALequalizerState();
112 static ALboolean
ALequalizerState_deviceUpdate(ALequalizerState
*state
, ALCdevice
*UNUSED(device
))
116 for(i
= 0; i
< MAX_EFFECT_CHANNELS
;i
++)
119 BiquadFilter_clear(&state
->Chans
[i
].filter
[j
]);
120 for(j
= 0;j
< MAX_OUTPUT_CHANNELS
;j
++)
121 state
->Chans
[i
].CurrentGains
[j
] = 0.0f
;
126 static ALvoid
ALequalizerState_update(ALequalizerState
*state
, const ALCcontext
*context
, const ALeffectslot
*slot
, const ALeffectProps
*props
)
128 const ALCdevice
*device
= context
->Device
;
129 ALfloat frequency
= (ALfloat
)device
->Frequency
;
130 ALfloat gain
, f0norm
;
133 /* Calculate coefficients for the each type of filter. Note that the shelf
134 * filters' gain is for the reference frequency, which is the centerpoint
135 * of the transition band.
137 gain
= maxf(sqrtf(props
->Equalizer
.LowGain
), 0.0625f
); /* Limit -24dB */
138 f0norm
= props
->Equalizer
.LowCutoff
/frequency
;
139 BiquadFilter_setParams(&state
->Chans
[0].filter
[0], BiquadType::LowShelf
,
140 gain
, f0norm
, calc_rcpQ_from_slope(gain
, 0.75f
)
143 gain
= maxf(props
->Equalizer
.Mid1Gain
, 0.0625f
);
144 f0norm
= props
->Equalizer
.Mid1Center
/frequency
;
145 BiquadFilter_setParams(&state
->Chans
[0].filter
[1], BiquadType::Peaking
,
146 gain
, f0norm
, calc_rcpQ_from_bandwidth(
147 f0norm
, props
->Equalizer
.Mid1Width
151 gain
= maxf(props
->Equalizer
.Mid2Gain
, 0.0625f
);
152 f0norm
= props
->Equalizer
.Mid2Center
/frequency
;
153 BiquadFilter_setParams(&state
->Chans
[0].filter
[2], BiquadType::Peaking
,
154 gain
, f0norm
, calc_rcpQ_from_bandwidth(
155 f0norm
, props
->Equalizer
.Mid2Width
159 gain
= maxf(sqrtf(props
->Equalizer
.HighGain
), 0.0625f
);
160 f0norm
= props
->Equalizer
.HighCutoff
/frequency
;
161 BiquadFilter_setParams(&state
->Chans
[0].filter
[3], BiquadType::HighShelf
,
162 gain
, f0norm
, calc_rcpQ_from_slope(gain
, 0.75f
)
165 /* Copy the filter coefficients for the other input channels. */
166 for(i
= 1;i
< MAX_EFFECT_CHANNELS
;i
++)
168 BiquadFilter_copyParams(&state
->Chans
[i
].filter
[0], &state
->Chans
[0].filter
[0]);
169 BiquadFilter_copyParams(&state
->Chans
[i
].filter
[1], &state
->Chans
[0].filter
[1]);
170 BiquadFilter_copyParams(&state
->Chans
[i
].filter
[2], &state
->Chans
[0].filter
[2]);
171 BiquadFilter_copyParams(&state
->Chans
[i
].filter
[3], &state
->Chans
[0].filter
[3]);
174 STATIC_CAST(ALeffectState
,state
)->OutBuffer
= device
->FOAOut
.Buffer
;
175 STATIC_CAST(ALeffectState
,state
)->OutChannels
= device
->FOAOut
.NumChannels
;
176 for(i
= 0;i
< MAX_EFFECT_CHANNELS
;i
++)
177 ComputePanGains(&device
->FOAOut
, aluMatrixf::Identity
.m
[i
], slot
->Params
.Gain
,
178 state
->Chans
[i
].TargetGains
);
181 static ALvoid
ALequalizerState_process(ALequalizerState
*state
, ALsizei SamplesToDo
, const ALfloat (*RESTRICT SamplesIn
)[BUFFERSIZE
], ALfloat (*RESTRICT SamplesOut
)[BUFFERSIZE
], ALsizei NumChannels
)
183 ALfloat (*RESTRICT temps
)[BUFFERSIZE
] = state
->SampleBuffer
;
186 for(c
= 0;c
< MAX_EFFECT_CHANNELS
;c
++)
188 BiquadFilter_process(&state
->Chans
[c
].filter
[0], temps
[0], SamplesIn
[c
], SamplesToDo
);
189 BiquadFilter_process(&state
->Chans
[c
].filter
[1], temps
[1], temps
[0], SamplesToDo
);
190 BiquadFilter_process(&state
->Chans
[c
].filter
[2], temps
[2], temps
[1], SamplesToDo
);
191 BiquadFilter_process(&state
->Chans
[c
].filter
[3], temps
[3], temps
[2], SamplesToDo
);
193 MixSamples(temps
[3], NumChannels
, SamplesOut
,
194 state
->Chans
[c
].CurrentGains
, state
->Chans
[c
].TargetGains
,
195 SamplesToDo
, 0, SamplesToDo
201 struct EqualizerStateFactory final
: public EffectStateFactory
{
202 ALeffectState
*create() override
;
205 ALeffectState
*EqualizerStateFactory::create()
207 ALequalizerState
*state
;
208 NEW_OBJ0(state
, ALequalizerState
)();
212 EffectStateFactory
*EqualizerStateFactory_getFactory(void)
214 static EqualizerStateFactory EqualizerFactory
{};
215 return &EqualizerFactory
;
219 void ALequalizer_setParami(ALeffect
*UNUSED(effect
), ALCcontext
*context
, ALenum param
, ALint
UNUSED(val
))
220 { alSetError(context
, AL_INVALID_ENUM
, "Invalid equalizer integer property 0x%04x", param
); }
221 void ALequalizer_setParamiv(ALeffect
*UNUSED(effect
), ALCcontext
*context
, ALenum param
, const ALint
*UNUSED(vals
))
222 { alSetError(context
, AL_INVALID_ENUM
, "Invalid equalizer integer-vector property 0x%04x", param
); }
223 void ALequalizer_setParamf(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat val
)
225 ALeffectProps
*props
= &effect
->Props
;
228 case AL_EQUALIZER_LOW_GAIN
:
229 if(!(val
>= AL_EQUALIZER_MIN_LOW_GAIN
&& val
<= AL_EQUALIZER_MAX_LOW_GAIN
))
230 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Equalizer low-band gain out of range");
231 props
->Equalizer
.LowGain
= val
;
234 case AL_EQUALIZER_LOW_CUTOFF
:
235 if(!(val
>= AL_EQUALIZER_MIN_LOW_CUTOFF
&& val
<= AL_EQUALIZER_MAX_LOW_CUTOFF
))
236 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Equalizer low-band cutoff out of range");
237 props
->Equalizer
.LowCutoff
= val
;
240 case AL_EQUALIZER_MID1_GAIN
:
241 if(!(val
>= AL_EQUALIZER_MIN_MID1_GAIN
&& val
<= AL_EQUALIZER_MAX_MID1_GAIN
))
242 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Equalizer mid1-band gain out of range");
243 props
->Equalizer
.Mid1Gain
= val
;
246 case AL_EQUALIZER_MID1_CENTER
:
247 if(!(val
>= AL_EQUALIZER_MIN_MID1_CENTER
&& val
<= AL_EQUALIZER_MAX_MID1_CENTER
))
248 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Equalizer mid1-band center out of range");
249 props
->Equalizer
.Mid1Center
= val
;
252 case AL_EQUALIZER_MID1_WIDTH
:
253 if(!(val
>= AL_EQUALIZER_MIN_MID1_WIDTH
&& val
<= AL_EQUALIZER_MAX_MID1_WIDTH
))
254 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Equalizer mid1-band width out of range");
255 props
->Equalizer
.Mid1Width
= val
;
258 case AL_EQUALIZER_MID2_GAIN
:
259 if(!(val
>= AL_EQUALIZER_MIN_MID2_GAIN
&& val
<= AL_EQUALIZER_MAX_MID2_GAIN
))
260 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Equalizer mid2-band gain out of range");
261 props
->Equalizer
.Mid2Gain
= val
;
264 case AL_EQUALIZER_MID2_CENTER
:
265 if(!(val
>= AL_EQUALIZER_MIN_MID2_CENTER
&& val
<= AL_EQUALIZER_MAX_MID2_CENTER
))
266 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Equalizer mid2-band center out of range");
267 props
->Equalizer
.Mid2Center
= val
;
270 case AL_EQUALIZER_MID2_WIDTH
:
271 if(!(val
>= AL_EQUALIZER_MIN_MID2_WIDTH
&& val
<= AL_EQUALIZER_MAX_MID2_WIDTH
))
272 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Equalizer mid2-band width out of range");
273 props
->Equalizer
.Mid2Width
= val
;
276 case AL_EQUALIZER_HIGH_GAIN
:
277 if(!(val
>= AL_EQUALIZER_MIN_HIGH_GAIN
&& val
<= AL_EQUALIZER_MAX_HIGH_GAIN
))
278 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Equalizer high-band gain out of range");
279 props
->Equalizer
.HighGain
= val
;
282 case AL_EQUALIZER_HIGH_CUTOFF
:
283 if(!(val
>= AL_EQUALIZER_MIN_HIGH_CUTOFF
&& val
<= AL_EQUALIZER_MAX_HIGH_CUTOFF
))
284 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Equalizer high-band cutoff out of range");
285 props
->Equalizer
.HighCutoff
= val
;
289 alSetError(context
, AL_INVALID_ENUM
, "Invalid equalizer float property 0x%04x", param
);
292 void ALequalizer_setParamfv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALfloat
*vals
)
293 { ALequalizer_setParamf(effect
, context
, param
, vals
[0]); }
295 void ALequalizer_getParami(const ALeffect
*UNUSED(effect
), ALCcontext
*context
, ALenum param
, ALint
*UNUSED(val
))
296 { alSetError(context
, AL_INVALID_ENUM
, "Invalid equalizer integer property 0x%04x", param
); }
297 void ALequalizer_getParamiv(const ALeffect
*UNUSED(effect
), ALCcontext
*context
, ALenum param
, ALint
*UNUSED(vals
))
298 { alSetError(context
, AL_INVALID_ENUM
, "Invalid equalizer integer-vector property 0x%04x", param
); }
299 void ALequalizer_getParamf(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*val
)
301 const ALeffectProps
*props
= &effect
->Props
;
304 case AL_EQUALIZER_LOW_GAIN
:
305 *val
= props
->Equalizer
.LowGain
;
308 case AL_EQUALIZER_LOW_CUTOFF
:
309 *val
= props
->Equalizer
.LowCutoff
;
312 case AL_EQUALIZER_MID1_GAIN
:
313 *val
= props
->Equalizer
.Mid1Gain
;
316 case AL_EQUALIZER_MID1_CENTER
:
317 *val
= props
->Equalizer
.Mid1Center
;
320 case AL_EQUALIZER_MID1_WIDTH
:
321 *val
= props
->Equalizer
.Mid1Width
;
324 case AL_EQUALIZER_MID2_GAIN
:
325 *val
= props
->Equalizer
.Mid2Gain
;
328 case AL_EQUALIZER_MID2_CENTER
:
329 *val
= props
->Equalizer
.Mid2Center
;
332 case AL_EQUALIZER_MID2_WIDTH
:
333 *val
= props
->Equalizer
.Mid2Width
;
336 case AL_EQUALIZER_HIGH_GAIN
:
337 *val
= props
->Equalizer
.HighGain
;
340 case AL_EQUALIZER_HIGH_CUTOFF
:
341 *val
= props
->Equalizer
.HighCutoff
;
345 alSetError(context
, AL_INVALID_ENUM
, "Invalid equalizer float property 0x%04x", param
);
348 void ALequalizer_getParamfv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*vals
)
349 { ALequalizer_getParamf(effect
, context
, param
, vals
); }
351 DEFINE_ALEFFECT_VTABLE(ALequalizer
);