Use a boolean check instead of a function pointer
[openal-soft.git] / alc / effects / equalizer.cpp
blob67ad67b05471762c5d9732b8be0b60235db7c00f
1 /**
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
21 #include "config.h"
23 #include <algorithm>
24 #include <array>
25 #include <cstdlib>
26 #include <functional>
27 #include <iterator>
28 #include <utility>
30 #include "alc/effects/base.h"
31 #include "almalloc.h"
32 #include "alspan.h"
33 #include "core/ambidefs.h"
34 #include "core/bufferline.h"
35 #include "core/context.h"
36 #include "core/devformat.h"
37 #include "core/device.h"
38 #include "core/effectslot.h"
39 #include "core/filters/biquad.h"
40 #include "core/mixer.h"
41 #include "intrusive_ptr.h"
44 namespace {
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 *
56 * gains above 1.0. *
57 * *
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 *
62 * *
63 * | | | | *
64 * | | | | *
65 * B -----+ /--+--\ /--+--\ +----- *
66 * O |\ | | | | | | /| *
67 * O | \ - | - - | - / | *
68 * S + | \ | | | | | | / | *
69 * T | | | | | | | | | | *
70 * ---------+---------------+------------------+---------------+-------- *
71 * C | | | | | | | | | | *
72 * U - | / | | | | | | \ | *
73 * T | / - | - - | - \ | *
74 * O |/ | | | | | | \| *
75 * F -----+ \--+--/ \--+--/ +----- *
76 * F | | | | *
77 * | | | | *
78 * *
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. *
82 * *
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 {
89 struct {
90 /* Effect parameters */
91 BiquadFilter filter[4];
93 /* Effect gains for each channel */
94 float CurrentGains[MAX_OUTPUT_CHANNELS]{};
95 float TargetGains[MAX_OUTPUT_CHANNELS]{};
96 } mChans[MaxAmbiChannels];
98 FloatBufferLine mSampleBuffer{};
101 void deviceUpdate(const DeviceBase *device, const Buffer &buffer) override;
102 void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
103 const EffectTarget target) override;
104 void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
105 const al::span<FloatBufferLine> samplesOut) override;
107 DEF_NEWDEL(EqualizerState)
110 void EqualizerState::deviceUpdate(const DeviceBase*, const Buffer&)
112 for(auto &e : mChans)
114 std::for_each(std::begin(e.filter), std::end(e.filter), std::mem_fn(&BiquadFilter::clear));
115 std::fill(std::begin(e.CurrentGains), std::end(e.CurrentGains), 0.0f);
119 void EqualizerState::update(const ContextBase *context, const EffectSlot *slot,
120 const EffectProps *props, const EffectTarget target)
122 const DeviceBase *device{context->mDevice};
123 auto frequency = static_cast<float>(device->Frequency);
124 float gain, f0norm;
126 /* Calculate coefficients for the each type of filter. Note that the shelf
127 * and peaking filters' gain is for the centerpoint of the transition band,
128 * while the effect property gains are for the shelf/peak itself. So the
129 * property gains need their dB halved (sqrt of linear gain) for the
130 * shelf/peak to reach the provided gain.
132 gain = std::sqrt(props->Equalizer.LowGain);
133 f0norm = props->Equalizer.LowCutoff / frequency;
134 mChans[0].filter[0].setParamsFromSlope(BiquadType::LowShelf, f0norm, gain, 0.75f);
136 gain = std::sqrt(props->Equalizer.Mid1Gain);
137 f0norm = props->Equalizer.Mid1Center / frequency;
138 mChans[0].filter[1].setParamsFromBandwidth(BiquadType::Peaking, f0norm, gain,
139 props->Equalizer.Mid1Width);
141 gain = std::sqrt(props->Equalizer.Mid2Gain);
142 f0norm = props->Equalizer.Mid2Center / frequency;
143 mChans[0].filter[2].setParamsFromBandwidth(BiquadType::Peaking, f0norm, gain,
144 props->Equalizer.Mid2Width);
146 gain = std::sqrt(props->Equalizer.HighGain);
147 f0norm = props->Equalizer.HighCutoff / frequency;
148 mChans[0].filter[3].setParamsFromSlope(BiquadType::HighShelf, f0norm, gain, 0.75f);
150 /* Copy the filter coefficients for the other input channels. */
151 for(size_t i{1u};i < slot->Wet.Buffer.size();++i)
153 mChans[i].filter[0].copyParamsFrom(mChans[0].filter[0]);
154 mChans[i].filter[1].copyParamsFrom(mChans[0].filter[1]);
155 mChans[i].filter[2].copyParamsFrom(mChans[0].filter[2]);
156 mChans[i].filter[3].copyParamsFrom(mChans[0].filter[3]);
159 mOutTarget = target.Main->Buffer;
160 auto set_gains = [slot,target](auto &chan, al::span<const float,MaxAmbiChannels> coeffs)
161 { ComputePanGains(target.Main, coeffs.data(), slot->Gain, chan.TargetGains); };
162 SetAmbiPanIdentity(std::begin(mChans), slot->Wet.Buffer.size(), set_gains);
165 void EqualizerState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
167 const al::span<float> buffer{mSampleBuffer.data(), samplesToDo};
168 auto chan = std::addressof(mChans[0]);
169 for(const auto &input : samplesIn)
171 const al::span<const float> inbuf{input.data(), samplesToDo};
172 DualBiquad{chan->filter[0], chan->filter[1]}.process(inbuf, buffer.begin());
173 DualBiquad{chan->filter[2], chan->filter[3]}.process(buffer, buffer.begin());
175 MixSamples(buffer, samplesOut, chan->CurrentGains, chan->TargetGains, samplesToDo, 0u);
176 ++chan;
181 struct EqualizerStateFactory final : public EffectStateFactory {
182 al::intrusive_ptr<EffectState> create() override
183 { return al::intrusive_ptr<EffectState>{new EqualizerState{}}; }
186 } // namespace
188 EffectStateFactory *EqualizerStateFactory_getFactory()
190 static EqualizerStateFactory EqualizerFactory{};
191 return &EqualizerFactory;