aboutsummaryrefslogtreecommitdiffstats
path: root/alc/effects/distortion.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'alc/effects/distortion.cpp')
-rw-r--r--alc/effects/distortion.cpp204
1 files changed, 58 insertions, 146 deletions
diff --git a/alc/effects/distortion.cpp b/alc/effects/distortion.cpp
index 7dd43008..b4e2167e 100644
--- a/alc/effects/distortion.cpp
+++ b/alc/effects/distortion.cpp
@@ -20,82 +20,90 @@
#include "config.h"
-#include <cmath>
+#include <algorithm>
+#include <array>
#include <cstdlib>
-
-#include <cmath>
-
-#include "al/auxeffectslot.h"
-#include "alcmain.h"
-#include "alcontext.h"
-#include "alu.h"
-#include "filters/biquad.h"
+#include <iterator>
+
+#include "alc/effects/base.h"
+#include "almalloc.h"
+#include "alnumbers.h"
+#include "alnumeric.h"
+#include "alspan.h"
+#include "core/bufferline.h"
+#include "core/context.h"
+#include "core/devformat.h"
+#include "core/device.h"
+#include "core/effectslot.h"
+#include "core/filters/biquad.h"
+#include "core/mixer.h"
+#include "core/mixer/defs.h"
+#include "intrusive_ptr.h"
namespace {
struct DistortionState final : public EffectState {
/* Effect gains for each channel */
- ALfloat mGain[MAX_OUTPUT_CHANNELS]{};
+ float mGain[MaxAmbiChannels]{};
/* Effect parameters */
BiquadFilter mLowpass;
BiquadFilter mBandpass;
- ALfloat mAttenuation{};
- ALfloat mEdgeCoeff{};
+ float mAttenuation{};
+ float mEdgeCoeff{};
- ALfloat mBuffer[2][BUFFERSIZE]{};
+ alignas(16) float mBuffer[2][BufferLineSize]{};
- ALboolean deviceUpdate(const ALCdevice *device) override;
- void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
- void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut) override;
+ void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override;
+ void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
+ const EffectTarget target) override;
+ void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
+ const al::span<FloatBufferLine> samplesOut) override;
DEF_NEWDEL(DistortionState)
};
-ALboolean DistortionState::deviceUpdate(const ALCdevice*)
+void DistortionState::deviceUpdate(const DeviceBase*, const BufferStorage*)
{
mLowpass.clear();
mBandpass.clear();
- return AL_TRUE;
}
-void DistortionState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
+void DistortionState::update(const ContextBase *context, const EffectSlot *slot,
+ const EffectProps *props, const EffectTarget target)
{
- const ALCdevice *device{context->mDevice.get()};
+ const DeviceBase *device{context->mDevice};
/* Store waveshaper edge settings. */
- const ALfloat edge{
- minf(std::sin(al::MathDefs<float>::Pi()*0.5f * props->Distortion.Edge), 0.99f)};
+ const float edge{minf(std::sin(al::numbers::pi_v<float>*0.5f * props->Distortion.Edge),
+ 0.99f)};
mEdgeCoeff = 2.0f * edge / (1.0f-edge);
- ALfloat cutoff{props->Distortion.LowpassCutoff};
+ float cutoff{props->Distortion.LowpassCutoff};
/* Bandwidth value is constant in octaves. */
- ALfloat bandwidth{(cutoff / 2.0f) / (cutoff * 0.67f)};
- /* Multiply sampling frequency by the amount of oversampling done during
+ float bandwidth{(cutoff / 2.0f) / (cutoff * 0.67f)};
+ /* Divide normalized frequency by the amount of oversampling done during
* processing.
*/
- auto frequency = static_cast<ALfloat>(device->Frequency);
- mLowpass.setParams(BiquadType::LowPass, 1.0f, cutoff / (frequency*4.0f),
- mLowpass.rcpQFromBandwidth(cutoff / (frequency*4.0f), bandwidth));
+ auto frequency = static_cast<float>(device->Frequency);
+ mLowpass.setParamsFromBandwidth(BiquadType::LowPass, cutoff/frequency/4.0f, 1.0f, bandwidth);
cutoff = props->Distortion.EQCenter;
/* Convert bandwidth in Hz to octaves. */
bandwidth = props->Distortion.EQBandwidth / (cutoff * 0.67f);
- mBandpass.setParams(BiquadType::BandPass, 1.0f, cutoff / (frequency*4.0f),
- mBandpass.rcpQFromBandwidth(cutoff / (frequency*4.0f), bandwidth));
+ mBandpass.setParamsFromBandwidth(BiquadType::BandPass, cutoff/frequency/4.0f, 1.0f, bandwidth);
- ALfloat coeffs[MAX_AMBI_CHANNELS];
- CalcDirectionCoeffs({0.0f, 0.0f, -1.0f}, 0.0f, coeffs);
+ static constexpr auto coeffs = CalcDirectionCoeffs({0.0f, 0.0f, -1.0f});
mOutTarget = target.Main->Buffer;
- ComputePanGains(target.Main, coeffs, slot->Params.Gain*props->Distortion.Gain, mGain);
+ ComputePanGains(target.Main, coeffs.data(), slot->Gain*props->Distortion.Gain, mGain);
}
void DistortionState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
{
- const ALfloat fc{mEdgeCoeff};
+ const float fc{mEdgeCoeff};
for(size_t base{0u};base < samplesToDo;)
{
/* Perform 4x oversampling to avoid aliasing. Oversampling greatly
@@ -103,7 +111,7 @@ void DistortionState::process(const size_t samplesToDo, const al::span<const Flo
* bandpass filters using high frequencies, at which classic IIR
* filters became unstable.
*/
- size_t todo{minz(BUFFERSIZE, (samplesToDo-base) * 4)};
+ size_t todo{minz(BufferLineSize, (samplesToDo-base) * 4)};
/* Fill oversample buffer using zero stuffing. Multiply the sample by
* the amount of oversampling to maintain the signal's power.
@@ -116,36 +124,35 @@ void DistortionState::process(const size_t samplesToDo, const al::span<const Flo
* (which is fortunately first step of distortion). So combine three
* operations into the one.
*/
- mLowpass.process(mBuffer[1], mBuffer[0], todo);
+ mLowpass.process({mBuffer[0], todo}, mBuffer[1]);
/* 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.
*/
- for(size_t i{0u};i < todo;i++)
+ auto proc_sample = [fc](float smp) -> float
{
- ALfloat smp{mBuffer[1][i]};
-
- 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));
-
- mBuffer[0][i] = smp;
- }
+ smp = (1.0f + fc) * smp/(1.0f + fc*std::abs(smp));
+ smp = (1.0f + fc) * smp/(1.0f + fc*std::abs(smp)) * -1.0f;
+ smp = (1.0f + fc) * smp/(1.0f + fc*std::abs(smp));
+ return smp;
+ };
+ std::transform(std::begin(mBuffer[1]), std::begin(mBuffer[1])+todo, std::begin(mBuffer[0]),
+ proc_sample);
/* Third step, do bandpass filtering of distorted signal. */
- mBandpass.process(mBuffer[1], mBuffer[0], todo);
+ mBandpass.process({mBuffer[0], todo}, mBuffer[1]);
todo >>= 2;
- const ALfloat *outgains{mGain};
+ const float *outgains{mGain};
for(FloatBufferLine &output : samplesOut)
{
/* Fourth step, final, do attenuation and perform decimation,
* storing only one sample out of four.
*/
- const ALfloat gain{*(outgains++)};
- if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
+ const float gain{*(outgains++)};
+ if(!(std::fabs(gain) > GainSilenceThreshold))
continue;
for(size_t i{0u};i < todo;i++)
@@ -157,106 +164,11 @@ void DistortionState::process(const size_t samplesToDo, const al::span<const Flo
}
-void Distortion_setParami(EffectProps*, ALCcontext *context, ALenum param, ALint)
-{ context->setError(AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param); }
-void Distortion_setParamiv(EffectProps*, ALCcontext *context, ALenum param, const ALint*)
-{ context->setError(AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x", param); }
-void Distortion_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
-{
- switch(param)
- {
- case AL_DISTORTION_EDGE:
- if(!(val >= AL_DISTORTION_MIN_EDGE && val <= AL_DISTORTION_MAX_EDGE))
- SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion edge out of range");
- props->Distortion.Edge = val;
- break;
-
- case AL_DISTORTION_GAIN:
- if(!(val >= AL_DISTORTION_MIN_GAIN && val <= AL_DISTORTION_MAX_GAIN))
- SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion gain out of range");
- props->Distortion.Gain = val;
- break;
-
- case AL_DISTORTION_LOWPASS_CUTOFF:
- if(!(val >= AL_DISTORTION_MIN_LOWPASS_CUTOFF && val <= AL_DISTORTION_MAX_LOWPASS_CUTOFF))
- SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion low-pass cutoff out of range");
- props->Distortion.LowpassCutoff = val;
- break;
-
- case AL_DISTORTION_EQCENTER:
- if(!(val >= AL_DISTORTION_MIN_EQCENTER && val <= AL_DISTORTION_MAX_EQCENTER))
- SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion EQ center out of range");
- props->Distortion.EQCenter = val;
- break;
-
- case AL_DISTORTION_EQBANDWIDTH:
- if(!(val >= AL_DISTORTION_MIN_EQBANDWIDTH && val <= AL_DISTORTION_MAX_EQBANDWIDTH))
- SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion EQ bandwidth out of range");
- props->Distortion.EQBandwidth = val;
- break;
-
- default:
- context->setError(AL_INVALID_ENUM, "Invalid distortion float property 0x%04x", param);
- }
-}
-void Distortion_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
-{ Distortion_setParamf(props, context, param, vals[0]); }
-
-void Distortion_getParami(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
-{ context->setError(AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param); }
-void Distortion_getParamiv(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
-{ context->setError(AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x", param); }
-void Distortion_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
-{
- switch(param)
- {
- case AL_DISTORTION_EDGE:
- *val = props->Distortion.Edge;
- break;
-
- case AL_DISTORTION_GAIN:
- *val = props->Distortion.Gain;
- break;
-
- case AL_DISTORTION_LOWPASS_CUTOFF:
- *val = props->Distortion.LowpassCutoff;
- break;
-
- case AL_DISTORTION_EQCENTER:
- *val = props->Distortion.EQCenter;
- break;
-
- case AL_DISTORTION_EQBANDWIDTH:
- *val = props->Distortion.EQBandwidth;
- break;
-
- default:
- context->setError(AL_INVALID_ENUM, "Invalid distortion float property 0x%04x", param);
- }
-}
-void Distortion_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
-{ Distortion_getParamf(props, context, param, vals); }
-
-DEFINE_ALEFFECT_VTABLE(Distortion);
-
-
struct DistortionStateFactory final : public EffectStateFactory {
- EffectState *create() override { return new DistortionState{}; }
- EffectProps getDefaultProps() const noexcept override;
- const EffectVtable *getEffectVtable() const noexcept override { return &Distortion_vtable; }
+ al::intrusive_ptr<EffectState> create() override
+ { return al::intrusive_ptr<EffectState>{new DistortionState{}}; }
};
-EffectProps DistortionStateFactory::getDefaultProps() const noexcept
-{
- EffectProps props{};
- props.Distortion.Edge = AL_DISTORTION_DEFAULT_EDGE;
- props.Distortion.Gain = AL_DISTORTION_DEFAULT_GAIN;
- props.Distortion.LowpassCutoff = AL_DISTORTION_DEFAULT_LOWPASS_CUTOFF;
- props.Distortion.EQCenter = AL_DISTORTION_DEFAULT_EQCENTER;
- props.Distortion.EQBandwidth = AL_DISTORTION_DEFAULT_EQBANDWIDTH;
- return props;
-}
-
} // namespace
EffectStateFactory *DistortionStateFactory_getFactory()