2013-05-18 01:33:01 -07:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 2013 by Mike Gorchak
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2014-08-18 14:11:03 +02:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2013-05-18 01:33:01 -07:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-01-09 19:42:40 +01:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
2013-05-18 01:33:01 -07:00
|
|
|
|
2018-12-24 13:29:36 -08:00
|
|
|
#include <cmath>
|
|
|
|
|
2013-05-18 01:33:01 -07:00
|
|
|
#include "alMain.h"
|
2018-11-17 23:02:27 -08:00
|
|
|
#include "alcontext.h"
|
2013-05-18 01:33:01 -07:00
|
|
|
#include "alAuxEffectSlot.h"
|
|
|
|
#include "alError.h"
|
|
|
|
#include "alu.h"
|
2018-12-25 10:28:02 -08:00
|
|
|
#include "filters/biquad.h"
|
2013-05-18 01:33:01 -07:00
|
|
|
|
2013-05-21 12:47:18 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
struct ALdistortionState final : public EffectState {
|
2013-05-18 01:33:01 -07:00
|
|
|
/* Effect gains for each channel */
|
2018-11-19 19:57:30 -08:00
|
|
|
ALfloat mGain[MAX_OUTPUT_CHANNELS]{};
|
2013-05-18 01:33:01 -07:00
|
|
|
|
|
|
|
/* Effect parameters */
|
2018-11-19 19:57:30 -08:00
|
|
|
BiquadFilter mLowpass;
|
|
|
|
BiquadFilter mBandpass;
|
|
|
|
ALfloat mAttenuation{};
|
|
|
|
ALfloat mEdgeCoeff{};
|
2018-01-16 08:49:15 -08:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
ALfloat mBuffer[2][BUFFERSIZE]{};
|
2016-08-25 03:42:43 -07:00
|
|
|
|
|
|
|
|
2018-12-22 18:43:34 -08:00
|
|
|
ALboolean deviceUpdate(const ALCdevice *device) override;
|
2018-12-24 13:29:36 -08:00
|
|
|
void update(const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props, const EffectTarget target) override;
|
2018-11-19 22:34:26 -08:00
|
|
|
void process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], ALsizei numChannels) override;
|
2016-08-25 03:42:43 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
DEF_NEWDEL(ALdistortionState)
|
|
|
|
};
|
2013-05-18 01:33:01 -07:00
|
|
|
|
2018-12-22 18:43:34 -08:00
|
|
|
ALboolean ALdistortionState::deviceUpdate(const ALCdevice *UNUSED(device))
|
2013-05-18 01:33:01 -07:00
|
|
|
{
|
2018-12-04 22:31:08 -08:00
|
|
|
mLowpass.clear();
|
|
|
|
mBandpass.clear();
|
2013-05-18 01:33:01 -07:00
|
|
|
return AL_TRUE;
|
|
|
|
}
|
|
|
|
|
2018-12-24 13:29:36 -08:00
|
|
|
void ALdistortionState::update(const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props, const EffectTarget target)
|
2013-05-18 01:33:01 -07:00
|
|
|
{
|
2018-12-24 13:29:36 -08:00
|
|
|
const ALCdevice *device{context->Device};
|
2013-05-18 01:33:01 -07:00
|
|
|
|
2016-12-23 12:37:48 -08:00
|
|
|
/* Store waveshaper edge settings. */
|
2019-01-06 04:16:51 -08:00
|
|
|
const ALfloat edge{
|
|
|
|
minf(std::sin(al::MathDefs<float>::Pi()*0.5f * props->Distortion.Edge), 0.99f)};
|
2018-11-19 22:34:26 -08:00
|
|
|
mEdgeCoeff = 2.0f * edge / (1.0f-edge);
|
2013-05-18 01:33:01 -07:00
|
|
|
|
2018-12-24 13:29:36 -08:00
|
|
|
ALfloat cutoff{props->Distortion.LowpassCutoff};
|
2016-12-23 12:37:48 -08:00
|
|
|
/* Bandwidth value is constant in octaves. */
|
2018-12-24 13:29:36 -08:00
|
|
|
ALfloat bandwidth{(cutoff / 2.0f) / (cutoff * 0.67f)};
|
2016-12-23 12:37:48 -08:00
|
|
|
/* Multiply sampling frequency by the amount of oversampling done during
|
|
|
|
* processing.
|
|
|
|
*/
|
2018-12-24 13:29:36 -08:00
|
|
|
auto frequency = static_cast<ALfloat>(device->Frequency);
|
2018-12-04 22:31:08 -08:00
|
|
|
mLowpass.setParams(BiquadType::LowPass, 1.0f, cutoff / (frequency*4.0f),
|
|
|
|
calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
|
2015-11-01 04:43:55 -08:00
|
|
|
);
|
2013-05-18 01:33:01 -07:00
|
|
|
|
2016-05-13 18:28:01 -07:00
|
|
|
cutoff = props->Distortion.EQCenter;
|
2016-12-23 12:37:48 -08:00
|
|
|
/* Convert bandwidth in Hz to octaves. */
|
2016-05-13 18:28:01 -07:00
|
|
|
bandwidth = props->Distortion.EQBandwidth / (cutoff * 0.67f);
|
2018-12-04 22:31:08 -08:00
|
|
|
mBandpass.setParams(BiquadType::BandPass, 1.0f, cutoff / (frequency*4.0f),
|
|
|
|
calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
|
2015-11-01 04:43:55 -08:00
|
|
|
);
|
2013-10-03 05:02:16 -07:00
|
|
|
|
2019-02-19 15:39:33 -08:00
|
|
|
ALfloat coeffs[MAX_AMBI_CHANNELS];
|
2018-02-17 18:16:39 -08:00
|
|
|
CalcAngleCoeffs(0.0f, 0.0f, 0.0f, coeffs);
|
2018-12-24 13:29:36 -08:00
|
|
|
|
|
|
|
mOutBuffer = target.Main->Buffer;
|
|
|
|
mOutChannels = target.Main->NumChannels;
|
|
|
|
ComputePanGains(target.Main, coeffs, slot->Params.Gain*props->Distortion.Gain, mGain);
|
2013-05-18 01:33:01 -07:00
|
|
|
}
|
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
void ALdistortionState::process(ALsizei SamplesToDo, const ALfloat (*RESTRICT SamplesIn)[BUFFERSIZE], ALfloat (*RESTRICT SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
|
2013-05-18 01:33:01 -07:00
|
|
|
{
|
2018-11-19 22:34:26 -08:00
|
|
|
ALfloat (*RESTRICT buffer)[BUFFERSIZE] = mBuffer;
|
|
|
|
const ALfloat fc = mEdgeCoeff;
|
2017-05-02 04:54:59 -07:00
|
|
|
ALsizei base;
|
2018-01-16 08:49:15 -08:00
|
|
|
ALsizei i, k;
|
2013-05-18 01:33:01 -07:00
|
|
|
|
2013-05-20 04:16:48 -07:00
|
|
|
for(base = 0;base < SamplesToDo;)
|
2013-05-18 01:33:01 -07:00
|
|
|
{
|
2016-12-23 12:37:48 -08:00
|
|
|
/* Perform 4x oversampling to avoid aliasing. Oversampling greatly
|
|
|
|
* improves distortion quality and allows to implement lowpass and
|
|
|
|
* bandpass filters using high frequencies, at which classic IIR
|
|
|
|
* filters became unstable.
|
|
|
|
*/
|
2018-01-16 08:49:15 -08:00
|
|
|
ALsizei todo = mini(BUFFERSIZE, (SamplesToDo-base) * 4);
|
2013-05-18 01:33:01 -07:00
|
|
|
|
2018-01-16 08:49:15 -08:00
|
|
|
/* Fill oversample buffer using zero stuffing. Multiply the sample by
|
|
|
|
* the amount of oversampling to maintain the signal's power.
|
|
|
|
*/
|
|
|
|
for(i = 0;i < todo;i++)
|
|
|
|
buffer[0][i] = !(i&3) ? SamplesIn[0][(i>>2)+base] * 4.0f : 0.0f;
|
2013-05-20 04:16:48 -07:00
|
|
|
|
2016-12-23 12:37:48 -08:00
|
|
|
/* First step, do lowpass filtering of original signal. Additionally
|
|
|
|
* perform buffer interpolation and lowpass cutoff for oversampling
|
|
|
|
* (which is fortunately first step of distortion). So combine three
|
|
|
|
* operations into the one.
|
|
|
|
*/
|
2018-12-04 22:31:08 -08:00
|
|
|
mLowpass.process(buffer[1], buffer[0], todo);
|
2013-05-20 04:16:48 -07:00
|
|
|
|
2016-12-23 12:37:48 -08:00
|
|
|
/* Second step, do distortion using waveshaper function to emulate
|
|
|
|
* signal processing during tube overdriving. Three steps of
|
|
|
|
* waveshaping are intended to modify waveform without boost/clipping/
|
|
|
|
* attenuation process.
|
|
|
|
*/
|
2018-01-16 08:49:15 -08:00
|
|
|
for(i = 0;i < todo;i++)
|
2013-05-20 04:16:48 -07:00
|
|
|
{
|
2018-01-16 08:49:15 -08:00
|
|
|
ALfloat smp = buffer[1][i];
|
2013-05-20 04:16:48 -07:00
|
|
|
|
2016-12-23 12:37:48 -08:00
|
|
|
smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
|
|
|
|
smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp)) * -1.0f;
|
|
|
|
smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
|
2013-05-20 04:16:48 -07:00
|
|
|
|
2018-01-16 08:49:15 -08:00
|
|
|
buffer[0][i] = smp;
|
2013-05-20 04:16:48 -07:00
|
|
|
}
|
|
|
|
|
2016-12-23 12:37:48 -08:00
|
|
|
/* Third step, do bandpass filtering of distorted signal. */
|
2018-12-04 22:31:08 -08:00
|
|
|
mBandpass.process(buffer[1], buffer[0], todo);
|
2016-07-25 19:04:54 -07:00
|
|
|
|
2018-01-16 08:49:15 -08:00
|
|
|
todo >>= 2;
|
|
|
|
for(k = 0;k < NumChannels;k++)
|
2013-05-20 04:16:48 -07:00
|
|
|
{
|
2014-12-18 09:23:55 -08:00
|
|
|
/* Fourth step, final, do attenuation and perform decimation,
|
2018-01-11 04:34:51 -08:00
|
|
|
* storing only one sample out of four.
|
2014-12-18 09:23:55 -08:00
|
|
|
*/
|
2018-11-19 22:34:26 -08:00
|
|
|
ALfloat gain = mGain[k];
|
2014-11-07 03:43:33 -08:00
|
|
|
if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
|
2013-05-20 04:16:48 -07:00
|
|
|
continue;
|
|
|
|
|
2018-01-16 08:49:15 -08:00
|
|
|
for(i = 0;i < todo;i++)
|
|
|
|
SamplesOut[k][base+i] += gain * buffer[1][i*4];
|
2013-05-20 04:16:48 -07:00
|
|
|
}
|
|
|
|
|
2018-01-16 08:49:15 -08:00
|
|
|
base += todo;
|
2013-05-18 01:33:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 12:47:18 -07:00
|
|
|
|
2018-11-17 06:53:20 -08:00
|
|
|
struct DistortionStateFactory final : public EffectStateFactory {
|
2018-11-19 22:34:26 -08:00
|
|
|
EffectState *create() override;
|
2018-11-17 06:53:20 -08:00
|
|
|
};
|
2013-10-07 14:49:36 -07:00
|
|
|
|
2018-11-19 22:34:26 -08:00
|
|
|
EffectState *DistortionStateFactory::create()
|
|
|
|
{ return new ALdistortionState{}; }
|
2013-05-21 12:47:18 -07:00
|
|
|
|
2019-01-09 19:43:54 +01:00
|
|
|
EffectStateFactory *DistortionStateFactory_getFactory()
|
2013-05-21 12:47:18 -07:00
|
|
|
{
|
2018-11-17 06:53:20 -08:00
|
|
|
static DistortionStateFactory DistortionFactory{};
|
2018-11-19 06:43:37 -08:00
|
|
|
return &DistortionFactory;
|
2013-05-21 12:47:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-25 15:59:59 -08:00
|
|
|
void ALdistortion_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint UNUSED(val))
|
|
|
|
{ alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param); }
|
|
|
|
void ALdistortion_setParamiv(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, const ALint *UNUSED(vals))
|
|
|
|
{ alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x", param); }
|
2013-05-29 11:17:45 -07:00
|
|
|
void ALdistortion_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
|
2013-05-18 01:33:01 -07:00
|
|
|
{
|
2013-05-25 22:07:31 -07:00
|
|
|
ALeffectProps *props = &effect->Props;
|
2013-05-18 01:33:01 -07:00
|
|
|
switch(param)
|
|
|
|
{
|
|
|
|
case AL_DISTORTION_EDGE:
|
2013-05-26 00:01:07 -07:00
|
|
|
if(!(val >= AL_DISTORTION_MIN_EDGE && val <= AL_DISTORTION_MAX_EDGE))
|
2018-01-25 15:59:59 -08:00
|
|
|
SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion edge out of range");
|
2013-05-26 00:01:07 -07:00
|
|
|
props->Distortion.Edge = val;
|
2013-05-18 01:33:01 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_DISTORTION_GAIN:
|
2013-05-26 00:01:07 -07:00
|
|
|
if(!(val >= AL_DISTORTION_MIN_GAIN && val <= AL_DISTORTION_MAX_GAIN))
|
2018-01-25 15:59:59 -08:00
|
|
|
SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion gain out of range");
|
2013-05-26 00:01:07 -07:00
|
|
|
props->Distortion.Gain = val;
|
2013-05-18 01:33:01 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_DISTORTION_LOWPASS_CUTOFF:
|
2013-05-26 00:01:07 -07:00
|
|
|
if(!(val >= AL_DISTORTION_MIN_LOWPASS_CUTOFF && val <= AL_DISTORTION_MAX_LOWPASS_CUTOFF))
|
2018-01-25 15:59:59 -08:00
|
|
|
SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion low-pass cutoff out of range");
|
2013-05-26 00:01:07 -07:00
|
|
|
props->Distortion.LowpassCutoff = val;
|
2013-05-18 01:33:01 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_DISTORTION_EQCENTER:
|
2013-05-26 00:01:07 -07:00
|
|
|
if(!(val >= AL_DISTORTION_MIN_EQCENTER && val <= AL_DISTORTION_MAX_EQCENTER))
|
2018-01-25 15:59:59 -08:00
|
|
|
SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion EQ center out of range");
|
2013-05-26 00:01:07 -07:00
|
|
|
props->Distortion.EQCenter = val;
|
2013-05-18 01:33:01 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_DISTORTION_EQBANDWIDTH:
|
2013-05-26 00:01:07 -07:00
|
|
|
if(!(val >= AL_DISTORTION_MIN_EQBANDWIDTH && val <= AL_DISTORTION_MAX_EQBANDWIDTH))
|
2018-01-25 15:59:59 -08:00
|
|
|
SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion EQ bandwidth out of range");
|
2013-05-26 00:01:07 -07:00
|
|
|
props->Distortion.EQBandwidth = val;
|
2013-05-18 01:33:01 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-01-25 15:59:59 -08:00
|
|
|
alSetError(context, AL_INVALID_ENUM, "Invalid distortion float property 0x%04x",
|
|
|
|
param);
|
2013-05-18 01:33:01 -07:00
|
|
|
}
|
|
|
|
}
|
2013-05-29 11:17:45 -07:00
|
|
|
void ALdistortion_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
|
2018-01-24 17:07:01 -08:00
|
|
|
{ ALdistortion_setParamf(effect, context, param, vals[0]); }
|
2013-05-18 01:33:01 -07:00
|
|
|
|
2018-01-25 15:59:59 -08:00
|
|
|
void ALdistortion_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint *UNUSED(val))
|
|
|
|
{ alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param); }
|
|
|
|
void ALdistortion_getParamiv(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint *UNUSED(vals))
|
|
|
|
{ alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x", param); }
|
2013-10-07 12:56:41 -07:00
|
|
|
void ALdistortion_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
|
2013-05-18 01:33:01 -07:00
|
|
|
{
|
2013-05-25 22:07:31 -07:00
|
|
|
const ALeffectProps *props = &effect->Props;
|
2013-05-18 01:33:01 -07:00
|
|
|
switch(param)
|
|
|
|
{
|
|
|
|
case AL_DISTORTION_EDGE:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Distortion.Edge;
|
2013-05-18 01:33:01 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_DISTORTION_GAIN:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Distortion.Gain;
|
2013-05-18 01:33:01 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_DISTORTION_LOWPASS_CUTOFF:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Distortion.LowpassCutoff;
|
2013-05-18 01:33:01 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_DISTORTION_EQCENTER:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Distortion.EQCenter;
|
2013-05-18 01:33:01 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AL_DISTORTION_EQBANDWIDTH:
|
2013-05-25 22:07:31 -07:00
|
|
|
*val = props->Distortion.EQBandwidth;
|
2013-05-18 01:33:01 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2018-01-25 15:59:59 -08:00
|
|
|
alSetError(context, AL_INVALID_ENUM, "Invalid distortion float property 0x%04x",
|
|
|
|
param);
|
2013-05-18 01:33:01 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-07 12:56:41 -07:00
|
|
|
void ALdistortion_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
|
2018-01-24 17:07:01 -08:00
|
|
|
{ ALdistortion_getParamf(effect, context, param, vals); }
|
2013-05-24 23:26:59 -07:00
|
|
|
|
|
|
|
DEFINE_ALEFFECT_VTABLE(ALdistortion);
|