Clean up some unnecessary includes
[openal-soft.git] / alc / effects / distortion.cpp
blob7dd43008445aa574d7a9faa921c4d70336caffd3
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 <cmath>
24 #include <cstdlib>
26 #include <cmath>
28 #include "al/auxeffectslot.h"
29 #include "alcmain.h"
30 #include "alcontext.h"
31 #include "alu.h"
32 #include "filters/biquad.h"
35 namespace {
37 struct DistortionState final : public EffectState {
38 /* Effect gains for each channel */
39 ALfloat mGain[MAX_OUTPUT_CHANNELS]{};
41 /* Effect parameters */
42 BiquadFilter mLowpass;
43 BiquadFilter mBandpass;
44 ALfloat mAttenuation{};
45 ALfloat mEdgeCoeff{};
47 ALfloat mBuffer[2][BUFFERSIZE]{};
50 ALboolean deviceUpdate(const ALCdevice *device) override;
51 void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
52 void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut) override;
54 DEF_NEWDEL(DistortionState)
57 ALboolean DistortionState::deviceUpdate(const ALCdevice*)
59 mLowpass.clear();
60 mBandpass.clear();
61 return AL_TRUE;
64 void DistortionState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
66 const ALCdevice *device{context->mDevice.get()};
68 /* Store waveshaper edge settings. */
69 const ALfloat edge{
70 minf(std::sin(al::MathDefs<float>::Pi()*0.5f * props->Distortion.Edge), 0.99f)};
71 mEdgeCoeff = 2.0f * edge / (1.0f-edge);
73 ALfloat cutoff{props->Distortion.LowpassCutoff};
74 /* Bandwidth value is constant in octaves. */
75 ALfloat bandwidth{(cutoff / 2.0f) / (cutoff * 0.67f)};
76 /* Multiply sampling frequency by the amount of oversampling done during
77 * processing.
79 auto frequency = static_cast<ALfloat>(device->Frequency);
80 mLowpass.setParams(BiquadType::LowPass, 1.0f, cutoff / (frequency*4.0f),
81 mLowpass.rcpQFromBandwidth(cutoff / (frequency*4.0f), bandwidth));
83 cutoff = props->Distortion.EQCenter;
84 /* Convert bandwidth in Hz to octaves. */
85 bandwidth = props->Distortion.EQBandwidth / (cutoff * 0.67f);
86 mBandpass.setParams(BiquadType::BandPass, 1.0f, cutoff / (frequency*4.0f),
87 mBandpass.rcpQFromBandwidth(cutoff / (frequency*4.0f), bandwidth));
89 ALfloat coeffs[MAX_AMBI_CHANNELS];
90 CalcDirectionCoeffs({0.0f, 0.0f, -1.0f}, 0.0f, coeffs);
92 mOutTarget = target.Main->Buffer;
93 ComputePanGains(target.Main, coeffs, slot->Params.Gain*props->Distortion.Gain, mGain);
96 void DistortionState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
98 const ALfloat fc{mEdgeCoeff};
99 for(size_t base{0u};base < samplesToDo;)
101 /* Perform 4x oversampling to avoid aliasing. Oversampling greatly
102 * improves distortion quality and allows to implement lowpass and
103 * bandpass filters using high frequencies, at which classic IIR
104 * filters became unstable.
106 size_t todo{minz(BUFFERSIZE, (samplesToDo-base) * 4)};
108 /* Fill oversample buffer using zero stuffing. Multiply the sample by
109 * the amount of oversampling to maintain the signal's power.
111 for(size_t i{0u};i < todo;i++)
112 mBuffer[0][i] = !(i&3) ? samplesIn[0][(i>>2)+base] * 4.0f : 0.0f;
114 /* First step, do lowpass filtering of original signal. Additionally
115 * perform buffer interpolation and lowpass cutoff for oversampling
116 * (which is fortunately first step of distortion). So combine three
117 * operations into the one.
119 mLowpass.process(mBuffer[1], mBuffer[0], todo);
121 /* Second step, do distortion using waveshaper function to emulate
122 * signal processing during tube overdriving. Three steps of
123 * waveshaping are intended to modify waveform without boost/clipping/
124 * attenuation process.
126 for(size_t i{0u};i < todo;i++)
128 ALfloat smp{mBuffer[1][i]};
130 smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
131 smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp)) * -1.0f;
132 smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
134 mBuffer[0][i] = smp;
137 /* Third step, do bandpass filtering of distorted signal. */
138 mBandpass.process(mBuffer[1], mBuffer[0], todo);
140 todo >>= 2;
141 const ALfloat *outgains{mGain};
142 for(FloatBufferLine &output : samplesOut)
144 /* Fourth step, final, do attenuation and perform decimation,
145 * storing only one sample out of four.
147 const ALfloat gain{*(outgains++)};
148 if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
149 continue;
151 for(size_t i{0u};i < todo;i++)
152 output[base+i] += gain * mBuffer[1][i*4];
155 base += todo;
160 void Distortion_setParami(EffectProps*, ALCcontext *context, ALenum param, ALint)
161 { context->setError(AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param); }
162 void Distortion_setParamiv(EffectProps*, ALCcontext *context, ALenum param, const ALint*)
163 { context->setError(AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x", param); }
164 void Distortion_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
166 switch(param)
168 case AL_DISTORTION_EDGE:
169 if(!(val >= AL_DISTORTION_MIN_EDGE && val <= AL_DISTORTION_MAX_EDGE))
170 SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion edge out of range");
171 props->Distortion.Edge = val;
172 break;
174 case AL_DISTORTION_GAIN:
175 if(!(val >= AL_DISTORTION_MIN_GAIN && val <= AL_DISTORTION_MAX_GAIN))
176 SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion gain out of range");
177 props->Distortion.Gain = val;
178 break;
180 case AL_DISTORTION_LOWPASS_CUTOFF:
181 if(!(val >= AL_DISTORTION_MIN_LOWPASS_CUTOFF && val <= AL_DISTORTION_MAX_LOWPASS_CUTOFF))
182 SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion low-pass cutoff out of range");
183 props->Distortion.LowpassCutoff = val;
184 break;
186 case AL_DISTORTION_EQCENTER:
187 if(!(val >= AL_DISTORTION_MIN_EQCENTER && val <= AL_DISTORTION_MAX_EQCENTER))
188 SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion EQ center out of range");
189 props->Distortion.EQCenter = val;
190 break;
192 case AL_DISTORTION_EQBANDWIDTH:
193 if(!(val >= AL_DISTORTION_MIN_EQBANDWIDTH && val <= AL_DISTORTION_MAX_EQBANDWIDTH))
194 SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion EQ bandwidth out of range");
195 props->Distortion.EQBandwidth = val;
196 break;
198 default:
199 context->setError(AL_INVALID_ENUM, "Invalid distortion float property 0x%04x", param);
202 void Distortion_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
203 { Distortion_setParamf(props, context, param, vals[0]); }
205 void Distortion_getParami(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
206 { context->setError(AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param); }
207 void Distortion_getParamiv(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
208 { context->setError(AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x", param); }
209 void Distortion_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
211 switch(param)
213 case AL_DISTORTION_EDGE:
214 *val = props->Distortion.Edge;
215 break;
217 case AL_DISTORTION_GAIN:
218 *val = props->Distortion.Gain;
219 break;
221 case AL_DISTORTION_LOWPASS_CUTOFF:
222 *val = props->Distortion.LowpassCutoff;
223 break;
225 case AL_DISTORTION_EQCENTER:
226 *val = props->Distortion.EQCenter;
227 break;
229 case AL_DISTORTION_EQBANDWIDTH:
230 *val = props->Distortion.EQBandwidth;
231 break;
233 default:
234 context->setError(AL_INVALID_ENUM, "Invalid distortion float property 0x%04x", param);
237 void Distortion_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
238 { Distortion_getParamf(props, context, param, vals); }
240 DEFINE_ALEFFECT_VTABLE(Distortion);
243 struct DistortionStateFactory final : public EffectStateFactory {
244 EffectState *create() override { return new DistortionState{}; }
245 EffectProps getDefaultProps() const noexcept override;
246 const EffectVtable *getEffectVtable() const noexcept override { return &Distortion_vtable; }
249 EffectProps DistortionStateFactory::getDefaultProps() const noexcept
251 EffectProps props{};
252 props.Distortion.Edge = AL_DISTORTION_DEFAULT_EDGE;
253 props.Distortion.Gain = AL_DISTORTION_DEFAULT_GAIN;
254 props.Distortion.LowpassCutoff = AL_DISTORTION_DEFAULT_LOWPASS_CUTOFF;
255 props.Distortion.EQCenter = AL_DISTORTION_DEFAULT_EQCENTER;
256 props.Distortion.EQBandwidth = AL_DISTORTION_DEFAULT_EQBANDWIDTH;
257 return props;
260 } // namespace
262 EffectStateFactory *DistortionStateFactory_getFactory()
264 static DistortionStateFactory DistortionFactory{};
265 return &DistortionFactory;