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
29 #include "alcontext.h"
30 #include "alAuxEffectSlot.h"
33 #include "filters/biquad.h"
36 struct ALdistortionState final
: public EffectState
{
37 /* Effect gains for each channel */
38 ALfloat mGain
[MAX_OUTPUT_CHANNELS
]{};
40 /* Effect parameters */
41 BiquadFilter mLowpass
;
42 BiquadFilter mBandpass
;
43 ALfloat mAttenuation
{};
46 ALfloat mBuffer
[2][BUFFERSIZE
]{};
49 ALboolean
deviceUpdate(const ALCdevice
*device
) override
;
50 void update(const ALCcontext
*context
, const ALeffectslot
*slot
, const ALeffectProps
*props
, const EffectTarget target
) override
;
51 void process(ALsizei samplesToDo
, const ALfloat (*RESTRICT samplesIn
)[BUFFERSIZE
], ALfloat (*RESTRICT samplesOut
)[BUFFERSIZE
], ALsizei numChannels
) override
;
53 DEF_NEWDEL(ALdistortionState
)
56 ALboolean
ALdistortionState::deviceUpdate(const ALCdevice
*UNUSED(device
))
63 void ALdistortionState::update(const ALCcontext
*context
, const ALeffectslot
*slot
, const ALeffectProps
*props
, const EffectTarget target
)
65 const ALCdevice
*device
{context
->Device
};
67 /* Store waveshaper edge settings. */
69 minf(std::sin(al::MathDefs
<float>::Pi()*0.5f
* props
->Distortion
.Edge
), 0.99f
)};
70 mEdgeCoeff
= 2.0f
* edge
/ (1.0f
-edge
);
72 ALfloat cutoff
{props
->Distortion
.LowpassCutoff
};
73 /* Bandwidth value is constant in octaves. */
74 ALfloat bandwidth
{(cutoff
/ 2.0f
) / (cutoff
* 0.67f
)};
75 /* Multiply sampling frequency by the amount of oversampling done during
78 auto frequency
= static_cast<ALfloat
>(device
->Frequency
);
79 mLowpass
.setParams(BiquadType::LowPass
, 1.0f
, cutoff
/ (frequency
*4.0f
),
80 calc_rcpQ_from_bandwidth(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 calc_rcpQ_from_bandwidth(cutoff
/ (frequency
*4.0f
), bandwidth
)
90 ALfloat coeffs
[MAX_AMBI_COEFFS
];
91 CalcAngleCoeffs(0.0f
, 0.0f
, 0.0f
, coeffs
);
93 mOutBuffer
= target
.Main
->Buffer
;
94 mOutChannels
= target
.Main
->NumChannels
;
95 ComputePanGains(target
.Main
, coeffs
, slot
->Params
.Gain
*props
->Distortion
.Gain
, mGain
);
98 void ALdistortionState::process(ALsizei SamplesToDo
, const ALfloat (*RESTRICT SamplesIn
)[BUFFERSIZE
], ALfloat (*RESTRICT SamplesOut
)[BUFFERSIZE
], ALsizei NumChannels
)
100 ALfloat (*RESTRICT buffer
)[BUFFERSIZE
] = mBuffer
;
101 const ALfloat fc
= mEdgeCoeff
;
105 for(base
= 0;base
< SamplesToDo
;)
107 /* Perform 4x oversampling to avoid aliasing. Oversampling greatly
108 * improves distortion quality and allows to implement lowpass and
109 * bandpass filters using high frequencies, at which classic IIR
110 * filters became unstable.
112 ALsizei todo
= mini(BUFFERSIZE
, (SamplesToDo
-base
) * 4);
114 /* Fill oversample buffer using zero stuffing. Multiply the sample by
115 * the amount of oversampling to maintain the signal's power.
117 for(i
= 0;i
< todo
;i
++)
118 buffer
[0][i
] = !(i
&3) ? SamplesIn
[0][(i
>>2)+base
] * 4.0f
: 0.0f
;
120 /* First step, do lowpass filtering of original signal. Additionally
121 * perform buffer interpolation and lowpass cutoff for oversampling
122 * (which is fortunately first step of distortion). So combine three
123 * operations into the one.
125 mLowpass
.process(buffer
[1], buffer
[0], todo
);
127 /* Second step, do distortion using waveshaper function to emulate
128 * signal processing during tube overdriving. Three steps of
129 * waveshaping are intended to modify waveform without boost/clipping/
130 * attenuation process.
132 for(i
= 0;i
< todo
;i
++)
134 ALfloat smp
= buffer
[1][i
];
136 smp
= (1.0f
+ fc
) * smp
/(1.0f
+ fc
*fabsf(smp
));
137 smp
= (1.0f
+ fc
) * smp
/(1.0f
+ fc
*fabsf(smp
)) * -1.0f
;
138 smp
= (1.0f
+ fc
) * smp
/(1.0f
+ fc
*fabsf(smp
));
143 /* Third step, do bandpass filtering of distorted signal. */
144 mBandpass
.process(buffer
[1], buffer
[0], todo
);
147 for(k
= 0;k
< NumChannels
;k
++)
149 /* Fourth step, final, do attenuation and perform decimation,
150 * storing only one sample out of four.
152 ALfloat gain
= mGain
[k
];
153 if(!(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
))
156 for(i
= 0;i
< todo
;i
++)
157 SamplesOut
[k
][base
+i
] += gain
* buffer
[1][i
*4];
165 struct DistortionStateFactory final
: public EffectStateFactory
{
166 EffectState
*create() override
;
169 EffectState
*DistortionStateFactory::create()
170 { return new ALdistortionState
{}; }
172 EffectStateFactory
*DistortionStateFactory_getFactory()
174 static DistortionStateFactory DistortionFactory
{};
175 return &DistortionFactory
;
179 void ALdistortion_setParami(ALeffect
*UNUSED(effect
), ALCcontext
*context
, ALenum param
, ALint
UNUSED(val
))
180 { alSetError(context
, AL_INVALID_ENUM
, "Invalid distortion integer property 0x%04x", param
); }
181 void ALdistortion_setParamiv(ALeffect
*UNUSED(effect
), ALCcontext
*context
, ALenum param
, const ALint
*UNUSED(vals
))
182 { alSetError(context
, AL_INVALID_ENUM
, "Invalid distortion integer-vector property 0x%04x", param
); }
183 void ALdistortion_setParamf(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat val
)
185 ALeffectProps
*props
= &effect
->Props
;
188 case AL_DISTORTION_EDGE
:
189 if(!(val
>= AL_DISTORTION_MIN_EDGE
&& val
<= AL_DISTORTION_MAX_EDGE
))
190 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Distortion edge out of range");
191 props
->Distortion
.Edge
= val
;
194 case AL_DISTORTION_GAIN
:
195 if(!(val
>= AL_DISTORTION_MIN_GAIN
&& val
<= AL_DISTORTION_MAX_GAIN
))
196 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Distortion gain out of range");
197 props
->Distortion
.Gain
= val
;
200 case AL_DISTORTION_LOWPASS_CUTOFF
:
201 if(!(val
>= AL_DISTORTION_MIN_LOWPASS_CUTOFF
&& val
<= AL_DISTORTION_MAX_LOWPASS_CUTOFF
))
202 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Distortion low-pass cutoff out of range");
203 props
->Distortion
.LowpassCutoff
= val
;
206 case AL_DISTORTION_EQCENTER
:
207 if(!(val
>= AL_DISTORTION_MIN_EQCENTER
&& val
<= AL_DISTORTION_MAX_EQCENTER
))
208 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Distortion EQ center out of range");
209 props
->Distortion
.EQCenter
= val
;
212 case AL_DISTORTION_EQBANDWIDTH
:
213 if(!(val
>= AL_DISTORTION_MIN_EQBANDWIDTH
&& val
<= AL_DISTORTION_MAX_EQBANDWIDTH
))
214 SETERR_RETURN(context
, AL_INVALID_VALUE
,, "Distortion EQ bandwidth out of range");
215 props
->Distortion
.EQBandwidth
= val
;
219 alSetError(context
, AL_INVALID_ENUM
, "Invalid distortion float property 0x%04x",
223 void ALdistortion_setParamfv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALfloat
*vals
)
224 { ALdistortion_setParamf(effect
, context
, param
, vals
[0]); }
226 void ALdistortion_getParami(const ALeffect
*UNUSED(effect
), ALCcontext
*context
, ALenum param
, ALint
*UNUSED(val
))
227 { alSetError(context
, AL_INVALID_ENUM
, "Invalid distortion integer property 0x%04x", param
); }
228 void ALdistortion_getParamiv(const ALeffect
*UNUSED(effect
), ALCcontext
*context
, ALenum param
, ALint
*UNUSED(vals
))
229 { alSetError(context
, AL_INVALID_ENUM
, "Invalid distortion integer-vector property 0x%04x", param
); }
230 void ALdistortion_getParamf(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*val
)
232 const ALeffectProps
*props
= &effect
->Props
;
235 case AL_DISTORTION_EDGE
:
236 *val
= props
->Distortion
.Edge
;
239 case AL_DISTORTION_GAIN
:
240 *val
= props
->Distortion
.Gain
;
243 case AL_DISTORTION_LOWPASS_CUTOFF
:
244 *val
= props
->Distortion
.LowpassCutoff
;
247 case AL_DISTORTION_EQCENTER
:
248 *val
= props
->Distortion
.EQCenter
;
251 case AL_DISTORTION_EQBANDWIDTH
:
252 *val
= props
->Distortion
.EQBandwidth
;
256 alSetError(context
, AL_INVALID_ENUM
, "Invalid distortion float property 0x%04x",
260 void ALdistortion_getParamfv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*vals
)
261 { ALdistortion_getParamf(effect
, context
, param
, vals
); }
263 DEFINE_ALEFFECT_VTABLE(ALdistortion
);