From 50c4b5cdab8be8f11fc5214a6aebb7cc26fe637a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 7 Sep 2019 06:21:07 -0400 Subject: [PATCH] General: Amend clamp parameter order Batch replace on switching to standardized variants assumed common ordering, which evidently wasn't the case --- include/amuse/EffectChorus.hpp | 6 +- include/amuse/EffectDelay.hpp | 18 +++--- include/amuse/EffectReverb.hpp | 12 ++-- include/amuse/Emitter.hpp | 2 +- include/amuse/Listener.hpp | 4 +- lib/EffectChorus.cpp | 6 +- lib/EffectDelay.cpp | 12 ++-- lib/EffectReverb.cpp | 12 ++-- lib/Emitter.cpp | 18 +++--- lib/Engine.cpp | 4 +- lib/Envelope.cpp | 30 ++++++---- lib/SongConverter.cpp | 4 +- lib/SongState.cpp | 4 +- lib/SoundMacroState.cpp | 8 +-- lib/Voice.cpp | 101 ++++++++++++++++++--------------- lib/VolumeTable.cpp | 26 +++++---- 16 files changed, 144 insertions(+), 123 deletions(-) diff --git a/include/amuse/EffectChorus.hpp b/include/amuse/EffectChorus.hpp index 648489e..239065a 100644 --- a/include/amuse/EffectChorus.hpp +++ b/include/amuse/EffectChorus.hpp @@ -38,21 +38,21 @@ public: using ImpType = EffectChorusImp; void setBaseDelay(uint32_t baseDelay) { - baseDelay = std::clamp(5u, baseDelay, 15u); + baseDelay = std::clamp(baseDelay, 5u, 15u); x90_baseDelay = baseDelay; m_dirty = true; } uint32_t getBaseDelay() const { return x90_baseDelay; } void setVariation(uint32_t variation) { - variation = std::clamp(0u, variation, 5u); + variation = std::clamp(variation, 0u, 5u); x94_variation = variation; m_dirty = true; } uint32_t getVariation() const { return x94_variation; } void setPeriod(uint32_t period) { - period = std::clamp(500u, period, 10000u); + period = std::clamp(period, 500u, 10000u); x98_period = period; m_dirty = true; } diff --git a/include/amuse/EffectDelay.hpp b/include/amuse/EffectDelay.hpp index c511d2d..a4f377c 100644 --- a/include/amuse/EffectDelay.hpp +++ b/include/amuse/EffectDelay.hpp @@ -51,41 +51,41 @@ public: using ImpType = EffectDelayImp; void setDelay(uint32_t delay) { - delay = std::clamp(10u, delay, 5000u); + delay = std::clamp(delay, 10u, 5000u); for (int i = 0; i < 8; ++i) x3c_delay[i] = delay; m_dirty = true; } void setChanDelay(int chanIdx, uint32_t delay) { - delay = std::clamp(10u, delay, 5000u); + delay = std::clamp(delay, 10u, 5000u); x3c_delay[chanIdx] = delay; m_dirty = true; } uint32_t getChanDelay(int chanIdx) const { return x3c_delay[chanIdx]; } void setFeedback(uint32_t feedback) { - feedback = std::clamp(0u, feedback, 100u); + feedback = std::clamp(feedback, 0u, 100u); for (int i = 0; i < 8; ++i) x48_feedback[i] = feedback; m_dirty = true; } void setChanFeedback(int chanIdx, uint32_t feedback) { - feedback = std::clamp(0u, feedback, 100u); + feedback = std::clamp(feedback, 0u, 100u); x48_feedback[chanIdx] = feedback; m_dirty = true; } uint32_t getChanFeedback(int chanIdx) const { return x48_feedback[chanIdx]; } void setOutput(uint32_t output) { - output = std::clamp(0u, output, 100u); + output = std::clamp(output, 0u, 100u); for (int i = 0; i < 8; ++i) x54_output[i] = output; m_dirty = true; } void setChanOutput(int chanIdx, uint32_t output) { - output = std::clamp(0u, output, 100u); + output = std::clamp(output, 0u, 100u); x54_output[chanIdx] = output; m_dirty = true; } @@ -93,9 +93,9 @@ public: void setParams(const EffectDelayInfo& info) { for (int i = 0; i < 8; ++i) { - x3c_delay[i] = std::clamp(10u, info.delay[i], 5000u); - x48_feedback[i] = std::clamp(0u, info.feedback[i], 100u); - x54_output[i] = std::clamp(0u, info.output[i], 100u); + x3c_delay[i] = std::clamp(info.delay[i], 10u, 5000u); + x48_feedback[i] = std::clamp(info.feedback[i], 0u, 100u); + x54_output[i] = std::clamp(info.output[i], 0u, 100u); } m_dirty = true; } diff --git a/include/amuse/EffectReverb.hpp b/include/amuse/EffectReverb.hpp index ec980fb..ea9337c 100644 --- a/include/amuse/EffectReverb.hpp +++ b/include/amuse/EffectReverb.hpp @@ -75,31 +75,31 @@ public: using ImpType = EffectReverbStdImp; void setColoration(float coloration) { - x140_x1c8_coloration = std::clamp(0.f, coloration, 1.f); + x140_x1c8_coloration = std::clamp(coloration, 0.f, 1.f); m_dirty = true; } float getColoration() const { return x140_x1c8_coloration; } void setMix(float mix) { - x144_x1cc_mix = std::clamp(0.f, mix, 1.f); + x144_x1cc_mix = std::clamp(mix, 0.f, 1.f); m_dirty = true; } float getMix() const { return x144_x1cc_mix; } void setTime(float time) { - x148_x1d0_time = std::clamp(0.01f, time, 10.f); + x148_x1d0_time = std::clamp(time, 0.01f, 10.f); m_dirty = true; } float getTime() const { return x148_x1d0_time; } void setDamping(float damping) { - x14c_x1d4_damping = std::clamp(0.f, damping, 1.f); + x14c_x1d4_damping = std::clamp(damping, 0.f, 1.f); m_dirty = true; } float getDamping() const { return x14c_x1d4_damping; } void setPreDelay(float preDelay) { - x150_x1d8_preDelay = std::clamp(0.f, preDelay, 0.1f); + x150_x1d8_preDelay = std::clamp(preDelay, 0.f, 0.1f); m_dirty = true; } float getPreDelay() const { return x150_x1d8_preDelay; } @@ -126,7 +126,7 @@ public: using ImpType = EffectReverbHiImp; void setCrosstalk(float crosstalk) { - x1dc_crosstalk = std::clamp(0.f, crosstalk, 1.f); + x1dc_crosstalk = std::clamp(crosstalk, 0.f, 1.f); m_dirty = true; } float getCrosstalk() const { return x1dc_crosstalk; } diff --git a/include/amuse/Emitter.hpp b/include/amuse/Emitter.hpp index c337bc6..6499dc9 100644 --- a/include/amuse/Emitter.hpp +++ b/include/amuse/Emitter.hpp @@ -56,7 +56,7 @@ public: void setVectors(const float* pos, const float* dir); void setMaxVol(float maxVol) { - m_maxVol = std::clamp(0.f, maxVol, 1.f); + m_maxVol = std::clamp(maxVol, 0.f, 1.f); m_dirty = true; } diff --git a/include/amuse/Listener.hpp b/include/amuse/Listener.hpp index da715f3..6a98da7 100644 --- a/include/amuse/Listener.hpp +++ b/include/amuse/Listener.hpp @@ -19,10 +19,10 @@ class Listener { public: Listener(float volume, float frontDiff, float backDiff, float soundSpeed) - : m_volume(std::clamp(0.f, volume, 1.f)), m_frontDiff(frontDiff), m_backDiff(backDiff), m_soundSpeed(soundSpeed) {} + : m_volume(std::clamp(volume, 0.f, 1.f)), m_frontDiff(frontDiff), m_backDiff(backDiff), m_soundSpeed(soundSpeed) {} void setVectors(const float* pos, const float* dir, const float* heading, const float* up); void setVolume(float vol) { - m_volume = std::clamp(0.f, vol, 1.f); + m_volume = std::clamp(vol, 0.f, 1.f); m_dirty = true; } }; diff --git a/lib/EffectChorus.cpp b/lib/EffectChorus.cpp index 548a586..67d5160 100644 --- a/lib/EffectChorus.cpp +++ b/lib/EffectChorus.cpp @@ -143,9 +143,9 @@ static const float rsmpTab12khz[] = /* clang-format on */ EffectChorus::EffectChorus(uint32_t baseDelay, uint32_t variation, uint32_t period) -: x90_baseDelay(std::clamp(5u, baseDelay, 15u)) -, x94_variation(std::clamp(0u, variation, 5u)) -, x98_period(std::clamp(500u, period, 10000u)) {} +: x90_baseDelay(std::clamp(baseDelay, 5u, 15u)) +, x94_variation(std::clamp(variation, 0u, 5u)) +, x98_period(std::clamp(period, 500u, 10000u)) {} template EffectChorusImp::EffectChorusImp(uint32_t baseDelay, uint32_t variation, uint32_t period, double sampleRate) diff --git a/lib/EffectDelay.cpp b/lib/EffectDelay.cpp index 0c6450d..1975763 100644 --- a/lib/EffectDelay.cpp +++ b/lib/EffectDelay.cpp @@ -10,9 +10,9 @@ namespace amuse { template EffectDelayImp::EffectDelayImp(uint32_t initDelay, uint32_t initFeedback, uint32_t initOutput, double sampleRate) { - initDelay = std::clamp(10u, initDelay, 5000u); - initFeedback = std::clamp(0u, initFeedback, 100u); - initOutput = std::clamp(0u, initOutput, 100u); + initDelay = std::clamp(initDelay, 10u, 5000u); + initFeedback = std::clamp(initFeedback, 0u, 100u); + initOutput = std::clamp(initOutput, 0u, 100u); for (int i = 0; i < 8; ++i) { x3c_delay[i] = initDelay; @@ -26,9 +26,9 @@ EffectDelayImp::EffectDelayImp(uint32_t initDelay, uint32_t initFeedback, uin template EffectDelayImp::EffectDelayImp(const EffectDelayInfo& info, double sampleRate) { for (int i = 0; i < 8; ++i) { - x3c_delay[i] = std::clamp(10u, info.delay[i], 5000u); - x48_feedback[i] = std::clamp(0u, info.feedback[i], 100u); - x54_output[i] = std::clamp(0u, info.output[i], 100u); + x3c_delay[i] = std::clamp(info.delay[i], 10u, 5000u); + x48_feedback[i] = std::clamp(info.feedback[i], 0u, 100u); + x54_output[i] = std::clamp(info.output[i], 0u, 100u); } _setup(sampleRate); diff --git a/lib/EffectReverb.cpp b/lib/EffectReverb.cpp index 5b25dca..19e8b69 100644 --- a/lib/EffectReverb.cpp +++ b/lib/EffectReverb.cpp @@ -57,14 +57,14 @@ void ReverbDelayLine::setdelay(int32_t delay) { } EffectReverbStd::EffectReverbStd(float coloration, float mix, float time, float damping, float preDelay) -: x140_x1c8_coloration(std::clamp(0.f, coloration, 1.f)) -, x144_x1cc_mix(std::clamp(0.f, mix, 1.f)) -, x148_x1d0_time(std::clamp(0.01f, time, 10.f)) -, x14c_x1d4_damping(std::clamp(0.f, damping, 1.f)) -, x150_x1d8_preDelay(std::clamp(0.f, preDelay, 0.1f)) {} +: x140_x1c8_coloration(std::clamp(coloration, 0.f, 1.f)) +, x144_x1cc_mix(std::clamp(mix, 0.f, 1.f)) +, x148_x1d0_time(std::clamp(time, 0.01f, 10.f)) +, x14c_x1d4_damping(std::clamp(damping, 0.f, 1.f)) +, x150_x1d8_preDelay(std::clamp(preDelay, 0.f, 0.1f)) {} EffectReverbHi::EffectReverbHi(float coloration, float mix, float time, float damping, float preDelay, float crosstalk) -: EffectReverbStd(coloration, mix, time, damping, preDelay), x1dc_crosstalk(std::clamp(0.f, crosstalk, 1.0f)) {} +: EffectReverbStd(coloration, mix, time, damping, preDelay), x1dc_crosstalk(std::clamp(crosstalk, 0.f, 1.0f)) {} template EffectReverbStdImp::EffectReverbStdImp(float coloration, float mix, float time, float damping, float preDelay, diff --git a/lib/Emitter.cpp b/lib/Emitter.cpp index 45ad01c..f189d7c 100644 --- a/lib/Emitter.cpp +++ b/lib/Emitter.cpp @@ -19,8 +19,8 @@ Emitter::Emitter(Engine& engine, const AudioGroup& group, ObjToken vox, f : Entity(engine, group, vox->getGroupId(), vox->getObjectId()) , m_vox(vox) , m_maxDist(maxDist) -, m_minVol(std::clamp(0.f, minVol, 1.f)) -, m_falloff(std::clamp(-1.f, falloff, 1.f)) +, m_minVol(std::clamp(minVol, 0.f, 1.f)) +, m_falloff(std::clamp(falloff, -1.f, 1.f)) , m_doppler(doppler) {} void Emitter::_destroy() { @@ -60,13 +60,13 @@ void Emitter::_update() { for (auto& listener : m_engine.m_activeListeners) { Vector3f listenerToEmitter; Delta(listenerToEmitter, m_pos, listener->m_pos); - float dist = Length(listenerToEmitter); - float panDist = Dot(listenerToEmitter, listener->m_right); - float frontPan = std::clamp(-1.f, panDist / listener->m_frontDiff, 1.f); - float backPan = std::clamp(-1.f, panDist / listener->m_backDiff, 1.f); - float spanDist = -Dot(listenerToEmitter, listener->m_heading); - float span = - std::clamp(-1.f, spanDist > 0.f ? spanDist / listener->m_backDiff : spanDist / listener->m_frontDiff, 1.f); + const float dist = Length(listenerToEmitter); + const float panDist = Dot(listenerToEmitter, listener->m_right); + const float frontPan = std::clamp(panDist / listener->m_frontDiff, -1.f, 1.f); + const float backPan = std::clamp(panDist / listener->m_backDiff, -1.f, 1.f); + const float spanDist = -Dot(listenerToEmitter, listener->m_heading); + const float span = + std::clamp(spanDist > 0.f ? spanDist / listener->m_backDiff : spanDist / listener->m_frontDiff, -1.f, 1.f); /* Calculate attenuation */ float att = _attenuationCurve(dist); diff --git a/lib/Engine.cpp b/lib/Engine.cpp index fd35d35..5828454 100644 --- a/lib/Engine.cpp +++ b/lib/Engine.cpp @@ -251,7 +251,7 @@ ObjToken Engine::fxStart(SFXId sfxId, float vol, float pan, ObjTokensetVolume(vol); float evalPan = pan != 0.f ? pan : ((entry->panning - 64.f) / 63.f); - evalPan = std::clamp(-1.f, evalPan, 1.f); + evalPan = std::clamp(evalPan, -1.f, 1.f); (*ret)->setPan(evalPan); return *ret; } @@ -273,7 +273,7 @@ ObjToken Engine::fxStart(const AudioGroup* group, GroupId groupId, SFXId (*ret)->setVolume(vol); float evalPan = pan != 0.f ? pan : ((entry.panning - 64.f) / 63.f); - evalPan = std::clamp(-1.f, evalPan, 1.f); + evalPan = std::clamp(evalPan, -1.f, 1.f); (*ret)->setPan(evalPan); return *ret; } diff --git a/lib/Envelope.cpp b/lib/Envelope.cpp index 6b3b2e5..152a762 100644 --- a/lib/Envelope.cpp +++ b/lib/Envelope.cpp @@ -42,8 +42,9 @@ void Envelope::reset(const ADSRDLS* adsr, int8_t note, int8_t vel) { void Envelope::keyOff(const Voice& vox) { double releaseTime = m_releaseTime; - if (vox.m_state.m_useAdsrControllers) - releaseTime = MIDItoTIME[std::clamp(0, int(vox.getCtrlValue(vox.m_state.m_midiRelease)), 103)] / 1000.0; + if (vox.m_state.m_useAdsrControllers) { + releaseTime = MIDItoTIME[std::clamp(int(vox.getCtrlValue(vox.m_state.m_midiRelease)), 0, 103)] / 1000.0; + } m_phase = (releaseTime != 0.0) ? State::Release : State::Complete; m_curTime = 0.0; @@ -64,8 +65,9 @@ float Envelope::advance(double dt, const Voice& vox) { switch (m_phase) { case State::Attack: { double attackTime = m_attackTime; - if (vox.m_state.m_useAdsrControllers) - attackTime = MIDItoTIME[std::clamp(0, int(vox.getCtrlValue(vox.m_state.m_midiAttack)), 103)] / 1000.0; + if (vox.m_state.m_useAdsrControllers) { + attackTime = MIDItoTIME[std::clamp(int(vox.getCtrlValue(vox.m_state.m_midiAttack)), 0, 103)] / 1000.0; + } if (attackTime == 0.0) { m_phase = State::Decay; @@ -85,12 +87,14 @@ float Envelope::advance(double dt, const Voice& vox) { } case State::Decay: { double decayTime = m_decayTime; - if (vox.m_state.m_useAdsrControllers) - decayTime = MIDItoTIME[std::clamp(0, int(vox.getCtrlValue(vox.m_state.m_midiDecay)), 103)] / 1000.0; + if (vox.m_state.m_useAdsrControllers) { + decayTime = MIDItoTIME[std::clamp(int(vox.getCtrlValue(vox.m_state.m_midiDecay)), 0, 103)] / 1000.0; + } double sustainFactor = m_sustainFactor; - if (vox.m_state.m_useAdsrControllers) - sustainFactor = std::clamp(0, int(vox.getCtrlValue(vox.m_state.m_midiSustain)), 127) / 127.0; + if (vox.m_state.m_useAdsrControllers) { + sustainFactor = std::clamp(int(vox.getCtrlValue(vox.m_state.m_midiSustain)), 0, 127) / 127.0; + } if (decayTime == 0.0) { m_phase = State::Sustain; @@ -110,15 +114,17 @@ float Envelope::advance(double dt, const Voice& vox) { } case State::Sustain: { double sustainFactor = m_sustainFactor; - if (vox.m_state.m_useAdsrControllers) - sustainFactor = std::clamp(0, int(vox.getCtrlValue(vox.m_state.m_midiSustain)), 127) / 127.0; + if (vox.m_state.m_useAdsrControllers) { + sustainFactor = std::clamp(int(vox.getCtrlValue(vox.m_state.m_midiSustain)), 0, 127) / 127.0; + } return sustainFactor; } case State::Release: { double releaseTime = m_releaseTime; - if (vox.m_state.m_useAdsrControllers) - releaseTime = MIDItoTIME[std::clamp(0, int(vox.getCtrlValue(vox.m_state.m_midiRelease)), 103)] / 1000.0; + if (vox.m_state.m_useAdsrControllers) { + releaseTime = MIDItoTIME[std::clamp(int(vox.getCtrlValue(vox.m_state.m_midiRelease)), 0, 103)] / 1000.0; + } if (releaseTime == 0.0) { m_phase = State::Complete; diff --git a/lib/SongConverter.cpp b/lib/SongConverter.cpp index a104b62..e68d004 100644 --- a/lib/SongConverter.cpp +++ b/lib/SongConverter.cpp @@ -683,7 +683,7 @@ std::vector SongConverter::SongToMIDI(const unsigned char* data, int& v /* Update pitch */ trk.m_pitchVal += trk.m_nextPitchDelta; events.emplace(regStart + trk.m_nextPitchTick, - Event{PitchEvent{}, trk.m_midiChan, std::clamp(0, trk.m_pitchVal + 0x2000, 0x4000)}); + Event{PitchEvent{}, trk.m_midiChan, std::clamp(trk.m_pitchVal + 0x2000, 0, 0x4000)}); if (trk.m_pitchWheelData[0] != 0x80 || trk.m_pitchWheelData[1] != 0x00) { auto delta = DecodeDelta(trk.m_pitchWheelData); trk.m_nextPitchTick += delta.first; @@ -700,7 +700,7 @@ std::vector SongConverter::SongToMIDI(const unsigned char* data, int& v /* Update modulation */ trk.m_modVal += trk.m_nextModDelta; events.emplace(regStart + trk.m_nextModTick, - Event{CtrlEvent{}, trk.m_midiChan, 1, uint8_t(std::clamp(0, trk.m_modVal / 128, 127)), 0}); + Event{CtrlEvent{}, trk.m_midiChan, 1, uint8_t(std::clamp(trk.m_modVal / 128, 0, 127)), 0}); if (trk.m_modWheelData[0] != 0x80 || trk.m_modWheelData[1] != 0x00) { auto delta = DecodeDelta(trk.m_modWheelData); trk.m_nextModTick += delta.first; diff --git a/lib/SongState.cpp b/lib/SongState.cpp index bca2d95..13fe123 100644 --- a/lib/SongState.cpp +++ b/lib/SongState.cpp @@ -443,7 +443,7 @@ bool SongState::Track::advance(Sequencer& seq, double dt) { if (pitchTick + remPitchTicks > nextTick) { /* Update pitch */ m_pitchVal += m_nextPitchDelta; - seq.setPitchWheel(m_midiChan, std::clamp(-1.f, m_pitchVal / 8191.f, 1.f)); + seq.setPitchWheel(m_midiChan, std::clamp(m_pitchVal / 8191.f, -1.f, 1.f)); if (m_pitchWheelData[0] != 0x80 || m_pitchWheelData[1] != 0x00) { auto delta = DecodeDelta(m_pitchWheelData); m_nextPitchTick += delta.first; @@ -467,7 +467,7 @@ bool SongState::Track::advance(Sequencer& seq, double dt) { if (modTick + remModTicks > nextTick) { /* Update modulation */ m_modVal += m_nextModDelta; - seq.setCtrlValue(m_midiChan, 1, int8_t(std::clamp(0, m_modVal / 127, 127))); + seq.setCtrlValue(m_midiChan, 1, int8_t(std::clamp(m_modVal / 127, 0, 127))); if (m_modWheelData[0] != 0x80 || m_modWheelData[1] != 0x00) { auto delta = DecodeDelta(m_modWheelData); m_nextModTick += delta.first; diff --git a/lib/SoundMacroState.cpp b/lib/SoundMacroState.cpp index e82cb7a..fd8b86d 100644 --- a/lib/SoundMacroState.cpp +++ b/lib/SoundMacroState.cpp @@ -67,7 +67,7 @@ float SoundMacroState::Evaluator::evaluate(double time, const Voice& vox, const break; case 135: /* Time since macro-start (ms) */ - thisValue = std::clamp(0.f, float(st.m_execTime * 1000.f), 16383.f); + thisValue = std::clamp(float(st.m_execTime * 1000.f), 0.f, 16383.f); break; default: thisValue = vox.getCtrlValue(comp.m_midiCtrl); @@ -462,7 +462,7 @@ const SoundMacro::CmdIntrospection SoundMacro::CmdScaleVolume::Introspective = { {FIELD_HEAD(SoundMacro::CmdScaleVolume, originalVol), "Original Vol"sv, 0, 1, 1}}}; bool SoundMacro::CmdScaleVolume::Do(SoundMacroState& st, Voice& vox) const { int32_t eval = int32_t(originalVol ? st.m_initVel : st.m_curVel) * scale / 127 + add; - eval = std::clamp(0, eval, 127); + eval = std::clamp(eval, 0, 127); if (table.id != 0) { const Curve* curveData = vox.getAudioGroup().getPool().tableAsCurves(table.id); @@ -504,7 +504,7 @@ bool SoundMacro::CmdEnvelope::Do(SoundMacroState& st, Voice& vox) const { double secTime = ticksOrMs / q; int32_t eval = int32_t(st.m_curVel) * scale / 127 + add; - eval = std::clamp(0, eval, 127); + eval = std::clamp(eval, 0, 127); const Curve* curveData; if (table.id != 0) @@ -601,7 +601,7 @@ bool SoundMacro::CmdFadeIn::Do(SoundMacroState& st, Voice& vox) const { float secTime = ticksOrMs / q; int32_t eval = int32_t(st.m_curVel) * scale / 127 + add; - eval = std::clamp(0, eval, 127); + eval = std::clamp(eval, 0, 127); const Curve* curveData; if (table.id != 0) diff --git a/lib/Voice.cpp b/lib/Voice.cpp index ee9bb00..2a80088 100644 --- a/lib/Voice.cpp +++ b/lib/Voice.cpp @@ -114,8 +114,8 @@ void Voice::_doKeyOff() { void Voice::_setTotalPitch(int32_t cents, bool slew) { // fprintf(stderr, "PITCH %d %d \n", cents, slew); - int32_t interval = std::clamp(0, cents, 12700) - m_curSample->getPitch() * 100; - double ratio = std::exp2(interval / 1200.0) * m_dopplerRatio; + const int32_t interval = std::clamp(cents, 0, 12700) - m_curSample->getPitch() * 100; + const double ratio = std::exp2(interval / 1200.0) * m_dopplerRatio; m_sampleRate = m_curSample->m_sampleRate * ratio; m_backendVoice->setPitchRatio(ratio, slew); } @@ -200,8 +200,8 @@ void Voice::_procSamplePre(int16_t& samp) { dt = m_sampleRate * 160; if (rem != 0) { /* Lerp within 160-sample block */ - float t = rem / 160.f; - float l = std::clamp(0.f, m_lastLevel * (1.f - t) + m_nextLevel * t, 1.f); + const float t = rem / 160.f; + const float l = std::clamp(m_lastLevel * (1.f - t) + m_nextLevel * t, 0.f, 1.f); /* Apply total volume to sample using decibel scale */ samp = ApplyVolume(m_lerpedCache.getVolume(l * m_engine.m_masterVolume, m_dlsVol), samp); @@ -218,12 +218,13 @@ void Voice::_procSamplePre(int16_t& samp) { /* Process active envelope */ if (m_envelopeTime >= 0.0) { m_envelopeTime += dt; - float start = m_envelopeStart; - float end = m_envelopeEnd; - float t = std::clamp(0.f, float(m_envelopeTime / m_envelopeDur), 1.f); - if (m_envelopeCurve && m_envelopeCurve->data.size() >= 128) + const float start = m_envelopeStart; + const float end = m_envelopeEnd; + float t = std::clamp(float(m_envelopeTime / m_envelopeDur), 0.f, 1.f); + if (m_envelopeCurve && m_envelopeCurve->data.size() >= 128) { t = m_envelopeCurve->data[t * 127.f] / 127.f; - m_envelopeVol = std::clamp(0.f, (start * (1.0f - t)) + (end * t), 1.f); + } + m_envelopeVol = std::clamp((start * (1.0f - t)) + (end * t), 0.f, 1.f); // printf("%d %f\n", m_vid, m_envelopeVol); @@ -280,7 +281,7 @@ void Voice::_procSamplePre(int16_t& samp) { } } - m_nextLevel = std::clamp(0.f, m_nextLevel, 1.f); + m_nextLevel = std::clamp(m_nextLevel, 0.f, 1.f); /* Apply total volume to sample using decibel scale */ samp = ApplyVolume(m_nextLevelCache.getVolume(m_nextLevel * m_engine.m_masterVolume, m_dlsVol), samp); @@ -288,8 +289,8 @@ void Voice::_procSamplePre(int16_t& samp) { template T Voice::_procSampleMaster(double time, T samp) { - float evalVol = m_state.m_volumeSel ? (m_state.m_volumeSel.evaluate(time, *this, m_state) / 127.f) : 1.f; - return ApplyVolume(m_masterCache.getVolume(std::clamp(0.f, evalVol, 1.f), m_dlsVol), samp); + const float evalVol = m_state.m_volumeSel ? (m_state.m_volumeSel.evaluate(time, *this, m_state) / 127.f) : 1.f; + return ApplyVolume(m_masterCache.getVolume(std::clamp(evalVol, 0.f, 1.f), m_dlsVol), samp); } template @@ -297,7 +298,7 @@ T Voice::_procSampleAuxA(double time, T samp) { float evalVol = m_state.m_volumeSel ? (m_state.m_volumeSel.evaluate(time, *this, m_state) / 127.f) : 1.f; evalVol *= m_state.m_reverbSel ? (m_state.m_reverbSel.evaluate(time, *this, m_state) / 127.f) : m_curReverbVol; evalVol += m_state.m_preAuxASel ? (m_state.m_preAuxASel.evaluate(time, *this, m_state) / 127.f) : 0.f; - return ApplyVolume(m_auxACache.getVolume(std::clamp(0.f, evalVol, 1.f), m_dlsVol), samp); + return ApplyVolume(m_auxACache.getVolume(std::clamp(evalVol, 0.f, 1.f), m_dlsVol), samp); } template @@ -305,7 +306,7 @@ T Voice::_procSampleAuxB(double time, T samp) { float evalVol = m_state.m_volumeSel ? (m_state.m_volumeSel.evaluate(time, *this, m_state) / 127.f) : 1.f; evalVol *= m_state.m_postAuxB ? (m_state.m_postAuxB.evaluate(time, *this, m_state) / 127.f) : m_curAuxBVol; evalVol += m_state.m_preAuxBSel ? (m_state.m_preAuxBSel.evaluate(time, *this, m_state) / 127.f) : 0.f; - return ApplyVolume(m_auxBCache.getVolume(std::clamp(0.f, evalVol, 1.f), m_dlsVol), samp); + return ApplyVolume(m_auxBCache.getVolume(std::clamp(evalVol, 0.f, 1.f), m_dlsVol), samp); } uint32_t Voice::_GetBlockSampleCount(SampleFormat fmt) { @@ -343,7 +344,7 @@ void Voice::preSupplyAudio(double dt) { bool panDirty = false; if (m_state.m_panSel) { float evalPan = (m_state.m_panSel.evaluate(m_voiceTime, *this, m_state) - 64.f) / 63.f; - evalPan = std::clamp(-1.f, evalPan, 1.f); + evalPan = std::clamp(evalPan, -1.f, 1.f); if (evalPan != m_curPan) { m_curPan = evalPan; panDirty = true; @@ -351,7 +352,7 @@ void Voice::preSupplyAudio(double dt) { } if (m_state.m_spanSel) { float evalSpan = (m_state.m_spanSel.evaluate(m_voiceTime, *this, m_state) - 64.f) / 63.f; - evalSpan = std::clamp(-1.f, evalSpan, 1.f); + evalSpan = std::clamp(evalSpan, -1.f, 1.f); if (evalSpan != m_curSpan) { m_curSpan = evalSpan; panDirty = true; @@ -361,8 +362,8 @@ void Voice::preSupplyAudio(double dt) { _setPan(m_curPan); if (m_state.m_pitchWheelSel) { - float evalPWheel = (m_state.m_pitchWheelSel.evaluate(m_voiceTime, *this, m_state) - 64.f) / 63.f; - _setPitchWheel(std::clamp(-1.f, evalPWheel, 1.f)); + const float evalPWheel = (m_state.m_pitchWheelSel.evaluate(m_voiceTime, *this, m_state) - 64.f) / 63.f; + _setPitchWheel(std::clamp(evalPWheel, -1.f, 1.f)); } /* Process active pan-sweep */ @@ -370,9 +371,9 @@ void Voice::preSupplyAudio(double dt) { if (!m_panningQueue.empty()) { Panning& p = m_panningQueue.front(); p.m_time += dt; - double start = (p.m_pos - 64) / 64.0; - double end = (p.m_pos + p.m_width - 64) / 64.0; - double t = std::clamp(0.0, p.m_time / p.m_dur, 1.0); + const double start = (p.m_pos - 64) / 64.0; + const double end = (p.m_pos + p.m_width - 64) / 64.0; + const double t = std::clamp(p.m_time / p.m_dur, 0.0, 1.0); _setPan(float((start * (1.0 - t)) + (end * t))); refresh = true; @@ -385,9 +386,9 @@ void Voice::preSupplyAudio(double dt) { if (!m_spanningQueue.empty()) { Panning& s = m_spanningQueue.front(); s.m_time += dt; - double start = (s.m_pos - 64) / 64.0; - double end = (s.m_pos + s.m_width - 64) / 64.0; - double t = std::clamp(0.0, s.m_time / s.m_dur, 1.0); + const double start = (s.m_pos - 64) / 64.0; + const double end = (s.m_pos + s.m_width - 64) / 64.0; + const double t = std::clamp(s.m_time / s.m_dur, 0.0, 1.0); _setSurroundPan(float((start * (1.0 - t)) + (end * t))); refresh = true; @@ -402,7 +403,7 @@ void Voice::preSupplyAudio(double dt) { m_pitchDirty = false; if (m_portamentoTime >= 0.f) { m_portamentoTime += dt; - float t = std::clamp(0.f, m_portamentoTime / m_state.m_portamentoTime, 1.f); + const float t = std::clamp(m_portamentoTime / m_state.m_portamentoTime, 0.f, 1.f); newPitch = (m_curPitch * (1.0f - t)) + (m_portamentoTarget * t); refresh = true; @@ -849,14 +850,15 @@ void Voice::startSample(SampleId sampId, int32_t offset) { m_backendVoice->resetSampleRate(m_curSample->m_sampleRate); m_needsSlew = false; - int32_t numSamples = m_curSample->getNumSamples(); + const int32_t numSamples = m_curSample->getNumSamples(); if (offset) { if (m_curSample->isLooped()) { if (offset > int32_t(m_curSample->m_loopStartSample)) offset = ((offset - m_curSample->m_loopStartSample) % m_curSample->m_loopLengthSamples) + m_curSample->m_loopStartSample; - } else - offset = std::clamp(0, offset, numSamples); + } else { + offset = std::clamp(offset, 0, numSamples); + } } m_curSamplePos = offset; m_prev1 = 0; @@ -895,7 +897,7 @@ void Voice::setVolume(float vol) { if (m_destroyed) return; - m_targetUserVol = std::clamp(0.f, vol, 1.f); + m_targetUserVol = std::clamp(vol, 0.f, 1.f); for (ObjToken& vox : m_childVoices) vox->setVolume(vol); } @@ -1011,12 +1013,13 @@ void Voice::_panLaw(float coefs[8], float frontPan, float backPan, float totalSp } void Voice::_setPan(float pan) { - if (m_destroyed || m_emitter) + if (m_destroyed || m_emitter) { return; + } - m_curPan = std::clamp(-1.f, pan, 1.f); - float totalPan = std::clamp(-1.f, m_curPan, 1.f); - float totalSpan = std::clamp(-1.f, m_curSpan, 1.f); + m_curPan = std::clamp(pan, -1.f, 1.f); + const float totalPan = std::clamp(m_curPan, -1.f, 1.f); + const float totalSpan = std::clamp(m_curSpan, -1.f, 1.f); float coefs[8] = {}; _panLaw(coefs, totalPan, totalPan, totalSpan); _setChannelCoefs(coefs); @@ -1032,7 +1035,7 @@ void Voice::setPan(float pan) { } void Voice::_setSurroundPan(float span) { - m_curSpan = std::clamp(-1.f, span, 1.f); + m_curSpan = std::clamp(span, -1.f, 1.f); _setPan(m_curPan); } @@ -1063,8 +1066,8 @@ void Voice::setChannelCoefs(const float coefs[8]) { void Voice::startEnvelope(double dur, float vol, const Curve* envCurve) { m_envelopeTime = 0.f; m_envelopeDur = dur; - m_envelopeStart = std::clamp(0.f, m_envelopeVol, 1.f); - m_envelopeEnd = std::clamp(0.f, vol, 1.f); + m_envelopeStart = std::clamp(m_envelopeVol, 0.f, 1.f); + m_envelopeEnd = std::clamp(vol, 0.f, 1.f); m_envelopeCurve = envCurve; } @@ -1072,7 +1075,7 @@ void Voice::startFadeIn(double dur, float vol, const Curve* envCurve) { m_envelopeTime = 0.f; m_envelopeDur = dur; m_envelopeStart = 0.f; - m_envelopeEnd = std::clamp(0.f, vol, 1.f); + m_envelopeEnd = std::clamp(vol, 0.f, 1.f); m_envelopeCurve = envCurve; } @@ -1134,21 +1137,25 @@ void Voice::setPitchSweep2(uint8_t times, int16_t add) { } void Voice::setReverbVol(float rvol) { - if (m_destroyed) + if (m_destroyed) { return; + } - m_curReverbVol = std::clamp(0.f, rvol, 1.f); - for (ObjToken& vox : m_childVoices) + m_curReverbVol = std::clamp(rvol, 0.f, 1.f); + for (ObjToken& vox : m_childVoices) { vox->setReverbVol(rvol); + } } void Voice::setAuxBVol(float bvol) { - if (m_destroyed) + if (m_destroyed) { return; + } - m_curAuxBVol = std::clamp(0.f, bvol, 1.f); - for (ObjToken& vox : m_childVoices) + m_curAuxBVol = std::clamp(bvol, 0.f, 1.f); + for (ObjToken& vox : m_childVoices) { vox->setAuxBVol(bvol); + } } void Voice::setAdsr(ObjectId adsrId, bool dls) { @@ -1207,14 +1214,16 @@ void Voice::_setPitchWheel(float pitchWheel) { } void Voice::setPitchWheel(float pitchWheel) { - if (m_destroyed) + if (m_destroyed) { return; + } - m_curPitchWheel = std::clamp(-1.f, pitchWheel, 1.f); + m_curPitchWheel = std::clamp(pitchWheel, -1.f, 1.f); _setPitchWheel(m_curPitchWheel); - for (ObjToken& vox : m_childVoices) + for (ObjToken& vox : m_childVoices) { vox->setPitchWheel(pitchWheel); + } } void Voice::setPitchWheelRange(int8_t up, int8_t down) { diff --git a/lib/VolumeTable.cpp b/lib/VolumeTable.cpp index 1b3f094..4776923 100644 --- a/lib/VolumeTable.cpp +++ b/lib/VolumeTable.cpp @@ -37,22 +37,28 @@ static const float DLSVolumeTable[] = { 0.892802f, 0.907744f, 0.922810f, 0.938000f, 0.953314f, 0.968752f, 0.984314f, 1.000000f, 1.000000f}; float LookupVolume(float vol) { - vol = std::clamp(0.f, vol * 127.f, 127.f); - float f = std::floor(vol); - float c = std::ceil(vol); - if (f == c) + vol = std::clamp(vol * 127.f, 0.f, 127.f); + const float f = std::floor(vol); + const float c = std::ceil(vol); + + if (f == c) { return VolumeTable[int(f)]; - float t = vol - f; + } + + const float t = vol - f; return (1.f - t) * VolumeTable[int(f)] + t * VolumeTable[int(c)]; } float LookupDLSVolume(float vol) { - vol = std::clamp(0.f, vol * 127.f, 127.f); - float f = std::floor(vol); - float c = std::ceil(vol); - if (f == c) + vol = std::clamp(vol * 127.f, 0.f, 127.f); + const float f = std::floor(vol); + const float c = std::ceil(vol); + + if (f == c) { return DLSVolumeTable[int(f)]; - float t = vol - f; + } + + const float t = vol - f; return (1.f - t) * DLSVolumeTable[int(f)] + t * DLSVolumeTable[int(c)]; }