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
30 #include "alc/effects/base.h"
32 #include "core/ambidefs.h"
33 #include "core/bufferline.h"
34 #include "core/context.h"
35 #include "core/device.h"
36 #include "core/effects/base.h"
37 #include "core/effectslot.h"
38 #include "core/filters/biquad.h"
39 #include "core/mixer.h"
40 #include "intrusive_ptr.h"
46 /* The document "Effects Extension Guide.pdf" says that low and high *
47 * frequencies are cutoff frequencies. This is not fully correct, they *
48 * are corner frequencies for low and high shelf filters. If they were *
49 * just cutoff frequencies, there would be no need in cutoff frequency *
50 * gains, which are present. Documentation for "Creative Proteus X2" *
51 * software describes 4-band equalizer functionality in a much better *
52 * way. This equalizer seems to be a predecessor of OpenAL 4-band *
53 * equalizer. With low and high shelf filters we are able to cutoff *
54 * frequencies below and/or above corner frequencies using attenuation *
55 * gains (below 1.0) and amplify all low and/or high frequencies using *
58 * Low-shelf Low Mid Band High Mid Band High-shelf *
59 * corner center center corner *
60 * frequency frequency frequency frequency *
61 * 50Hz..800Hz 200Hz..3000Hz 1000Hz..8000Hz 4000Hz..16000Hz *
65 * B -----+ /--+--\ /--+--\ +----- *
66 * O |\ | | | | | | /| *
67 * O | \ - | - - | - / | *
68 * S + | \ | | | | | | / | *
69 * T | | | | | | | | | | *
70 * ---------+---------------+------------------+---------------+-------- *
71 * C | | | | | | | | | | *
72 * U - | / | | | | | | \ | *
73 * T | / - | - - | - \ | *
74 * O |/ | | | | | | \| *
75 * F -----+ \--+--/ \--+--/ +----- *
79 * Gains vary from 0.126 up to 7.943, which means from -18dB attenuation *
80 * up to +18dB amplification. Band width varies from 0.01 up to 1.0 in *
81 * octaves for two mid bands. *
83 * Implementation is based on the "Cookbook formulae for audio EQ biquad *
84 * filter coefficients" by Robert Bristow-Johnson *
85 * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt */
88 struct EqualizerState final
: public EffectState
{
90 uint mTargetChannel
{InvalidChannelIndex
};
92 /* Effect parameters */
93 std::array
<BiquadFilter
,4> mFilter
;
95 /* Effect gains for each channel */
99 std::array
<OutParams
,MaxAmbiChannels
> mChans
;
101 alignas(16) FloatBufferLine mSampleBuffer
{};
104 void deviceUpdate(const DeviceBase
*device
, const BufferStorage
*buffer
) override
;
105 void update(const ContextBase
*context
, const EffectSlot
*slot
, const EffectProps
*props
,
106 const EffectTarget target
) override
;
107 void process(const size_t samplesToDo
, const al::span
<const FloatBufferLine
> samplesIn
,
108 const al::span
<FloatBufferLine
> samplesOut
) override
;
111 void EqualizerState::deviceUpdate(const DeviceBase
*, const BufferStorage
*)
113 for(auto &e
: mChans
)
115 e
.mTargetChannel
= InvalidChannelIndex
;
116 std::for_each(e
.mFilter
.begin(), e
.mFilter
.end(), std::mem_fn(&BiquadFilter::clear
));
117 e
.mCurrentGain
= 0.0f
;
121 void EqualizerState::update(const ContextBase
*context
, const EffectSlot
*slot
,
122 const EffectProps
*props_
, const EffectTarget target
)
124 auto &props
= std::get
<EqualizerProps
>(*props_
);
125 const DeviceBase
*device
{context
->mDevice
};
126 auto frequency
= static_cast<float>(device
->Frequency
);
128 /* Calculate coefficients for the each type of filter. Note that the shelf
129 * and peaking filters' gain is for the centerpoint of the transition band,
130 * while the effect property gains are for the shelf/peak itself. So the
131 * property gains need their dB halved (sqrt of linear gain) for the
132 * shelf/peak to reach the provided gain.
134 float gain
{std::sqrt(props
.LowGain
)};
135 float f0norm
{props
.LowCutoff
/ frequency
};
136 mChans
[0].mFilter
[0].setParamsFromSlope(BiquadType::LowShelf
, f0norm
, gain
, 0.75f
);
138 gain
= std::sqrt(props
.Mid1Gain
);
139 f0norm
= props
.Mid1Center
/ frequency
;
140 mChans
[0].mFilter
[1].setParamsFromBandwidth(BiquadType::Peaking
, f0norm
, gain
,
143 gain
= std::sqrt(props
.Mid2Gain
);
144 f0norm
= props
.Mid2Center
/ frequency
;
145 mChans
[0].mFilter
[2].setParamsFromBandwidth(BiquadType::Peaking
, f0norm
, gain
,
148 gain
= std::sqrt(props
.HighGain
);
149 f0norm
= props
.HighCutoff
/ frequency
;
150 mChans
[0].mFilter
[3].setParamsFromSlope(BiquadType::HighShelf
, f0norm
, gain
, 0.75f
);
152 /* Copy the filter coefficients for the other input channels. */
153 for(size_t i
{1u};i
< slot
->Wet
.Buffer
.size();++i
)
155 mChans
[i
].mFilter
[0].copyParamsFrom(mChans
[0].mFilter
[0]);
156 mChans
[i
].mFilter
[1].copyParamsFrom(mChans
[0].mFilter
[1]);
157 mChans
[i
].mFilter
[2].copyParamsFrom(mChans
[0].mFilter
[2]);
158 mChans
[i
].mFilter
[3].copyParamsFrom(mChans
[0].mFilter
[3]);
161 mOutTarget
= target
.Main
->Buffer
;
162 auto set_channel
= [this](size_t idx
, uint outchan
, float outgain
)
164 mChans
[idx
].mTargetChannel
= outchan
;
165 mChans
[idx
].mTargetGain
= outgain
;
167 target
.Main
->setAmbiMixParams(slot
->Wet
, slot
->Gain
, set_channel
);
170 void EqualizerState::process(const size_t samplesToDo
, const al::span
<const FloatBufferLine
> samplesIn
, const al::span
<FloatBufferLine
> samplesOut
)
172 const auto buffer
= al::span
{mSampleBuffer
}.first(samplesToDo
);
173 auto chan
= mChans
.begin();
174 for(const auto &input
: samplesIn
)
176 if(const size_t outidx
{chan
->mTargetChannel
}; outidx
!= InvalidChannelIndex
)
178 const auto inbuf
= al::span
{input
}.first(samplesToDo
);
179 DualBiquad
{chan
->mFilter
[0], chan
->mFilter
[1]}.process(inbuf
, buffer
);
180 DualBiquad
{chan
->mFilter
[2], chan
->mFilter
[3]}.process(buffer
, buffer
);
182 MixSamples(buffer
, samplesOut
[outidx
], chan
->mCurrentGain
, chan
->mTargetGain
,
190 struct EqualizerStateFactory final
: public EffectStateFactory
{
191 al::intrusive_ptr
<EffectState
> create() override
192 { return al::intrusive_ptr
<EffectState
>{new EqualizerState
{}}; }
197 EffectStateFactory
*EqualizerStateFactory_getFactory()
199 static EqualizerStateFactory EqualizerFactory
{};
200 return &EqualizerFactory
;