diff options
author | Chris Robinson <[email protected]> | 2023-12-23 19:38:04 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2023-12-23 22:08:56 -0800 |
commit | 28744c0418749dd8b2d1eb191280c881faad00bb (patch) | |
tree | 5c99107332cd89e78badec931ee6fa6d22bead62 | |
parent | cfb6bdfabfdf70ce5bcb650d356c3a9c1db3df0c (diff) |
Use a separate struct and union member for flanger properties
-rw-r--r-- | al/effects/chorus.cpp | 88 | ||||
-rw-r--r-- | alc/effects/chorus.cpp | 81 | ||||
-rw-r--r-- | core/effects/base.h | 12 |
3 files changed, 101 insertions, 80 deletions
diff --git a/al/effects/chorus.cpp b/al/effects/chorus.cpp index aabeff97..24aa0a49 100644 --- a/al/effects/chorus.cpp +++ b/al/effects/chorus.cpp @@ -168,7 +168,7 @@ void Flanger_setParami(EffectProps *props, ALenum param, int val) { case AL_FLANGER_WAVEFORM: if(auto formopt = WaveformFromEnum(val)) - props->Chorus.Waveform = *formopt; + props->Flanger.Waveform = *formopt; else throw effect_exception{AL_INVALID_VALUE, "Invalid flanger waveform: 0x%04x", val}; break; @@ -176,7 +176,7 @@ void Flanger_setParami(EffectProps *props, ALenum param, int val) case AL_FLANGER_PHASE: if(!(val >= AL_FLANGER_MIN_PHASE && val <= AL_FLANGER_MAX_PHASE)) throw effect_exception{AL_INVALID_VALUE, "Flanger phase out of range: %d", val}; - props->Chorus.Phase = val; + props->Flanger.Phase = val; break; default: @@ -192,25 +192,25 @@ void Flanger_setParamf(EffectProps *props, ALenum param, float val) case AL_FLANGER_RATE: if(!(val >= AL_FLANGER_MIN_RATE && val <= AL_FLANGER_MAX_RATE)) throw effect_exception{AL_INVALID_VALUE, "Flanger rate out of range: %f", val}; - props->Chorus.Rate = val; + props->Flanger.Rate = val; break; case AL_FLANGER_DEPTH: if(!(val >= AL_FLANGER_MIN_DEPTH && val <= AL_FLANGER_MAX_DEPTH)) throw effect_exception{AL_INVALID_VALUE, "Flanger depth out of range: %f", val}; - props->Chorus.Depth = val; + props->Flanger.Depth = val; break; case AL_FLANGER_FEEDBACK: if(!(val >= AL_FLANGER_MIN_FEEDBACK && val <= AL_FLANGER_MAX_FEEDBACK)) throw effect_exception{AL_INVALID_VALUE, "Flanger feedback out of range: %f", val}; - props->Chorus.Feedback = val; + props->Flanger.Feedback = val; break; case AL_FLANGER_DELAY: if(!(val >= AL_FLANGER_MIN_DELAY && val <= AL_FLANGER_MAX_DELAY)) throw effect_exception{AL_INVALID_VALUE, "Flanger delay out of range: %f", val}; - props->Chorus.Delay = val; + props->Flanger.Delay = val; break; default: @@ -225,11 +225,11 @@ void Flanger_getParami(const EffectProps *props, ALenum param, int *val) switch(param) { case AL_FLANGER_WAVEFORM: - *val = EnumFromWaveform(props->Chorus.Waveform); + *val = EnumFromWaveform(props->Flanger.Waveform); break; case AL_FLANGER_PHASE: - *val = props->Chorus.Phase; + *val = props->Flanger.Phase; break; default: @@ -243,19 +243,19 @@ void Flanger_getParamf(const EffectProps *props, ALenum param, float *val) switch(param) { case AL_FLANGER_RATE: - *val = props->Chorus.Rate; + *val = props->Flanger.Rate; break; case AL_FLANGER_DEPTH: - *val = props->Chorus.Depth; + *val = props->Flanger.Depth; break; case AL_FLANGER_FEEDBACK: - *val = props->Chorus.Feedback; + *val = props->Flanger.Feedback; break; case AL_FLANGER_DELAY: - *val = props->Chorus.Delay; + *val = props->Flanger.Delay; break; default: @@ -268,12 +268,12 @@ void Flanger_getParamfv(const EffectProps *props, ALenum param, float *vals) EffectProps genDefaultFlangerProps() noexcept { EffectProps props{}; - props.Chorus.Waveform = *WaveformFromEnum(AL_FLANGER_DEFAULT_WAVEFORM); - props.Chorus.Phase = AL_FLANGER_DEFAULT_PHASE; - props.Chorus.Rate = AL_FLANGER_DEFAULT_RATE; - props.Chorus.Depth = AL_FLANGER_DEFAULT_DEPTH; - props.Chorus.Feedback = AL_FLANGER_DEFAULT_FEEDBACK; - props.Chorus.Delay = AL_FLANGER_DEFAULT_DELAY; + props.Flanger.Waveform = *WaveformFromEnum(AL_FLANGER_DEFAULT_WAVEFORM); + props.Flanger.Phase = AL_FLANGER_DEFAULT_PHASE; + props.Flanger.Rate = AL_FLANGER_DEFAULT_RATE; + props.Flanger.Depth = AL_FLANGER_DEFAULT_DEPTH; + props.Flanger.Feedback = AL_FLANGER_DEFAULT_FEEDBACK; + props.Flanger.Delay = AL_FLANGER_DEFAULT_DELAY; return props; } @@ -292,8 +292,9 @@ const EffectProps FlangerEffectProps{genDefaultFlangerProps()}; namespace { struct EaxChorusTraits { - using Props = EAXCHORUSPROPERTIES; + using EaxProps = EAXCHORUSPROPERTIES; using Committer = EaxChorusCommitter; + using AlProps = ChorusProps; static constexpr auto efx_effect() { return AL_EFFECT_CHORUS; } @@ -357,8 +358,9 @@ struct EaxChorusTraits { }; // EaxChorusTraits struct EaxFlangerTraits { - using Props = EAXFLANGERPROPERTIES; + using EaxProps = EAXFLANGERPROPERTIES; using Committer = EaxFlangerCommitter; + using AlProps = FlangerProps; static constexpr auto efx_effect() { return AL_EFFECT_FLANGER; } @@ -424,7 +426,9 @@ struct EaxFlangerTraits { template<typename TTraits> struct ChorusFlangerEffect { using Traits = TTraits; + using EaxProps = typename Traits::EaxProps; using Committer = typename Traits::Committer; + using AlProps = typename Traits::AlProps; using Exception = typename Committer::Exception; struct WaveformValidator { @@ -494,7 +498,7 @@ struct ChorusFlangerEffect { }; // DelayValidator struct AllValidator { - void operator()(const typename Traits::Props& all) const + void operator()(const EaxProps& all) const { WaveformValidator{}(all.ulWaveform); PhaseValidator{}(all.lPhase); @@ -508,7 +512,7 @@ struct ChorusFlangerEffect { public: static void SetDefaults(EaxEffectProps &props) { - auto&& all = props.emplace<typename Traits::Props>(); + auto&& all = props.emplace<EaxProps>(); all.ulWaveform = Traits::eax_default_waveform(); all.lPhase = Traits::eax_default_phase(); all.flRate = Traits::eax_default_rate(); @@ -518,99 +522,83 @@ public: } - static void Get(const EaxCall &call, const typename Traits::Props &all) + static void Get(const EaxCall &call, const EaxProps &all) { switch(call.get_property_id()) { case Traits::eax_none_param_id(): break; - case Traits::eax_allparameters_param_id(): call.template set_value<Exception>(all); break; - case Traits::eax_waveform_param_id(): call.template set_value<Exception>(all.ulWaveform); break; - case Traits::eax_phase_param_id(): call.template set_value<Exception>(all.lPhase); break; - case Traits::eax_rate_param_id(): call.template set_value<Exception>(all.flRate); break; - case Traits::eax_depth_param_id(): call.template set_value<Exception>(all.flDepth); break; - case Traits::eax_feedback_param_id(): call.template set_value<Exception>(all.flFeedback); break; - case Traits::eax_delay_param_id(): call.template set_value<Exception>(all.flDelay); break; - default: Committer::fail_unknown_property_id(); } } - static void Set(const EaxCall &call, typename Traits::Props &all) + static void Set(const EaxCall &call, EaxProps &all) { switch(call.get_property_id()) { case Traits::eax_none_param_id(): break; - case Traits::eax_allparameters_param_id(): Committer::template defer<AllValidator>(call, all); break; - case Traits::eax_waveform_param_id(): Committer::template defer<WaveformValidator>(call, all.ulWaveform); break; - case Traits::eax_phase_param_id(): Committer::template defer<PhaseValidator>(call, all.lPhase); break; - case Traits::eax_rate_param_id(): Committer::template defer<RateValidator>(call, all.flRate); break; - case Traits::eax_depth_param_id(): Committer::template defer<DepthValidator>(call, all.flDepth); break; - case Traits::eax_feedback_param_id(): Committer::template defer<FeedbackValidator>(call, all.flFeedback); break; - case Traits::eax_delay_param_id(): Committer::template defer<DelayValidator>(call, all.flDelay); break; - default: Committer::fail_unknown_property_id(); } } - static bool Commit(const typename Traits::Props &props, EaxEffectProps &props_, EffectProps &al_props_) + static bool Commit(const EaxProps &props, EaxEffectProps &props_, AlProps &al_props_) { - if(auto *cur = std::get_if<typename Traits::Props>(&props_); cur && *cur == props) + if(auto *cur = std::get_if<EaxProps>(&props_); cur && *cur == props) return false; props_ = props; - al_props_.Chorus.Waveform = Traits::eax_waveform(props.ulWaveform); - al_props_.Chorus.Phase = static_cast<int>(props.lPhase); - al_props_.Chorus.Rate = props.flRate; - al_props_.Chorus.Depth = props.flDepth; - al_props_.Chorus.Feedback = props.flFeedback; - al_props_.Chorus.Delay = props.flDelay; + al_props_.Waveform = Traits::eax_waveform(props.ulWaveform); + al_props_.Phase = static_cast<int>(props.lPhase); + al_props_.Rate = props.flRate; + al_props_.Depth = props.flDepth; + al_props_.Feedback = props.flFeedback; + al_props_.Delay = props.flDelay; return true; } @@ -638,7 +626,7 @@ template<> bool EaxChorusCommitter::commit(const EAXCHORUSPROPERTIES &props) { using Committer = ChorusFlangerEffect<EaxChorusTraits>; - return Committer::Commit(props, mEaxProps, mAlProps); + return Committer::Commit(props, mEaxProps, mAlProps.Chorus); } void EaxChorusCommitter::SetDefaults(EaxEffectProps &props) @@ -675,7 +663,7 @@ template<> bool EaxFlangerCommitter::commit(const EAXFLANGERPROPERTIES &props) { using Committer = ChorusFlangerEffect<EaxFlangerTraits>; - return Committer::Commit(props, mEaxProps, mAlProps); + return Committer::Commit(props, mEaxProps, mAlProps.Flanger); } void EaxFlangerCommitter::SetDefaults(EaxEffectProps &props) diff --git a/alc/effects/chorus.cpp b/alc/effects/chorus.cpp index f56c9f2c..52aaa9a6 100644 --- a/alc/effects/chorus.cpp +++ b/alc/effects/chorus.cpp @@ -48,7 +48,14 @@ namespace { using uint = unsigned int; -struct ChorusState final : public EffectState { +constexpr auto inv_sqrt2 = static_cast<float>(1.0 / al::numbers::sqrt2); +constexpr auto lcoeffs_pw = CalcDirectionCoeffs(std::array{-1.0f, 0.0f, 0.0f}); +constexpr auto rcoeffs_pw = CalcDirectionCoeffs(std::array{ 1.0f, 0.0f, 0.0f}); +constexpr auto lcoeffs_nrml = CalcDirectionCoeffs(std::array{-inv_sqrt2, 0.0f, inv_sqrt2}); +constexpr auto rcoeffs_nrml = CalcDirectionCoeffs(std::array{ inv_sqrt2, 0.0f, inv_sqrt2}); + + +struct ChorusState : public EffectState { std::vector<float> mDelayBuffer; uint mOffset{0}; @@ -79,19 +86,40 @@ struct ChorusState final : public EffectState { void calcTriangleDelays(const size_t todo); void calcSinusoidDelays(const size_t todo); - void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override; + void deviceUpdate(const DeviceBase *device, const float MaxDelay); + void update(const ContextBase *context, const EffectSlot *slot, const ChorusWaveform waveform, + const float delay, const float depth, const float feedback, const float rate, + int phase, const EffectTarget target); + + void deviceUpdate(const DeviceBase *device, const BufferStorage*) override + { deviceUpdate(device, ChorusMaxDelay); } void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props, - const EffectTarget target) override; + const EffectTarget target) override + { + update(context, slot, props->Chorus.Waveform, props->Chorus.Delay, props->Chorus.Depth, + props->Chorus.Feedback, props->Chorus.Rate, props->Chorus.Phase, target); + } void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, - const al::span<FloatBufferLine> samplesOut) override; + const al::span<FloatBufferLine> samplesOut) final; }; -void ChorusState::deviceUpdate(const DeviceBase *Device, const BufferStorage*) -{ - constexpr float max_delay{maxf(ChorusMaxDelay, FlangerMaxDelay)}; +struct FlangerState final : public ChorusState { + void deviceUpdate(const DeviceBase *device, const BufferStorage*) final + { ChorusState::deviceUpdate(device, FlangerMaxDelay); } + void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props, + const EffectTarget target) final + { + ChorusState::update(context, slot, props->Flanger.Waveform, props->Flanger.Delay, + props->Flanger.Depth, props->Flanger.Feedback, props->Flanger.Rate, + props->Flanger.Phase, target); + } +}; + +void ChorusState::deviceUpdate(const DeviceBase *Device, const float MaxDelay) +{ const auto frequency = static_cast<float>(Device->Frequency); - const size_t maxlen{NextPowerOf2(float2uint(max_delay*2.0f*frequency) + 1u)}; + const size_t maxlen{NextPowerOf2(float2uint(MaxDelay*2.0f*frequency) + 1u)}; if(maxlen != mDelayBuffer.size()) decltype(mDelayBuffer)(maxlen).swap(mDelayBuffer); @@ -103,39 +131,35 @@ void ChorusState::deviceUpdate(const DeviceBase *Device, const BufferStorage*) } } -void ChorusState::update(const ContextBase *Context, const EffectSlot *Slot, - const EffectProps *props, const EffectTarget target) +void ChorusState::update(const ContextBase *context, const EffectSlot *slot, + const ChorusWaveform waveform, const float delay, const float depth, const float feedback, + const float rate, int phase, const EffectTarget target) { - constexpr int mindelay{(MaxResamplerPadding>>1) << MixerFracBits}; + static constexpr int mindelay{(MaxResamplerPadding>>1) << MixerFracBits}; /* The LFO depth is scaled to be relative to the sample delay. Clamp the * delay and depth to allow enough padding for resampling. */ - const DeviceBase *device{Context->mDevice}; + const DeviceBase *device{context->mDevice}; const auto frequency = static_cast<float>(device->Frequency); - mWaveform = props->Chorus.Waveform; + mWaveform = waveform; - mDelay = maxi(float2int(props->Chorus.Delay*frequency*MixerFracOne + 0.5f), mindelay); - mDepth = minf(props->Chorus.Depth * static_cast<float>(mDelay), + mDelay = maxi(float2int(delay*frequency*MixerFracOne + 0.5f), mindelay); + mDepth = minf(depth * static_cast<float>(mDelay), static_cast<float>(mDelay - mindelay)); - mFeedback = props->Chorus.Feedback; + mFeedback = feedback; /* Gains for left and right sides */ - static constexpr auto inv_sqrt2 = static_cast<float>(1.0 / al::numbers::sqrt2); - static constexpr auto lcoeffs_pw = CalcDirectionCoeffs(std::array{-1.0f, 0.0f, 0.0f}); - static constexpr auto rcoeffs_pw = CalcDirectionCoeffs(std::array{ 1.0f, 0.0f, 0.0f}); - static constexpr auto lcoeffs_nrml = CalcDirectionCoeffs(std::array{-inv_sqrt2, 0.0f, inv_sqrt2}); - static constexpr auto rcoeffs_nrml = CalcDirectionCoeffs(std::array{ inv_sqrt2, 0.0f, inv_sqrt2}); - auto &lcoeffs = (device->mRenderMode != RenderMode::Pairwise) ? lcoeffs_nrml : lcoeffs_pw; - auto &rcoeffs = (device->mRenderMode != RenderMode::Pairwise) ? rcoeffs_nrml : rcoeffs_pw; + const bool ispairwise{device->mRenderMode == RenderMode::Pairwise}; + const auto lcoeffs = (!ispairwise) ? al::span{lcoeffs_nrml} : al::span{lcoeffs_pw}; + const auto rcoeffs = (!ispairwise) ? al::span{rcoeffs_nrml} : al::span{rcoeffs_pw}; mOutTarget = target.Main->Buffer; - ComputePanGains(target.Main, lcoeffs, Slot->Gain, mGains[0].Target); - ComputePanGains(target.Main, rcoeffs, Slot->Gain, mGains[1].Target); + ComputePanGains(target.Main, lcoeffs, slot->Gain, mGains[0].Target); + ComputePanGains(target.Main, rcoeffs, slot->Gain, mGains[1].Target); - float rate{props->Chorus.Rate}; if(!(rate > 0.0f)) { mLfoOffset = 0; @@ -148,7 +172,7 @@ void ChorusState::update(const ContextBase *Context, const EffectSlot *Slot, /* Calculate LFO coefficient (number of samples per cycle). Limit the * max range to avoid overflow when calculating the displacement. */ - uint lfo_range{float2uint(minf(frequency/rate + 0.5f, float{INT_MAX/360 - 180}))}; + const uint lfo_range{float2uint(minf(frequency/rate + 0.5f, float{INT_MAX/360 - 180}))}; mLfoOffset = mLfoOffset * lfo_range / mLfoRange; mLfoRange = lfo_range; @@ -163,7 +187,6 @@ void ChorusState::update(const ContextBase *Context, const EffectSlot *Slot, } /* Calculate lfo phase displacement */ - int phase{props->Chorus.Phase}; if(phase < 0) phase = 360 + phase; mLfoDisp = (mLfoRange*static_cast<uint>(phase) + 180) / 360; } @@ -311,7 +334,7 @@ struct ChorusStateFactory final : public EffectStateFactory { */ struct FlangerStateFactory final : public EffectStateFactory { al::intrusive_ptr<EffectState> create() override - { return al::intrusive_ptr<EffectState>{new ChorusState{}}; } + { return al::intrusive_ptr<EffectState>{new FlangerState{}}; } }; } // namespace diff --git a/core/effects/base.h b/core/effects/base.h index 3852731a..672d3f04 100644 --- a/core/effects/base.h +++ b/core/effects/base.h @@ -101,6 +101,15 @@ struct ChorusProps { float Delay; }; +struct FlangerProps { + ChorusWaveform Waveform; + int Phase; + float Rate; + float Depth; + float Feedback; + float Delay; +}; + struct CompressorProps { bool OnOff; }; @@ -174,7 +183,8 @@ struct ConvolutionProps { union EffectProps { ReverbProps Reverb; AutowahProps Autowah; - ChorusProps Chorus; /* Also Flanger */ + ChorusProps Chorus; + FlangerProps Flanger; CompressorProps Compressor; DistortionProps Distortion; EchoProps Echo; |