From bba6fa6e4c8b26c4ac518ef80577e5ab8ef8b0fd Mon Sep 17 00:00:00 2001 From: ed Date: Wed, 14 Nov 2018 14:01:33 +0000 Subject: [PATCH] Updated the sampler synthesiser classes to use the new ADSR class --- .../sampler/juce_Sampler.cpp | 59 ++++--------------- .../juce_audio_formats/sampler/juce_Sampler.h | 15 +++-- 2 files changed, 19 insertions(+), 55 deletions(-) diff --git a/modules/juce_audio_formats/sampler/juce_Sampler.cpp b/modules/juce_audio_formats/sampler/juce_Sampler.cpp index ee5b64b296..905791ae67 100644 --- a/modules/juce_audio_formats/sampler/juce_Sampler.cpp +++ b/modules/juce_audio_formats/sampler/juce_Sampler.cpp @@ -48,8 +48,8 @@ SamplerSound::SamplerSound (const String& soundName, source.read (data.get(), 0, length + 4, 0, true, true); - attackSamples = roundToInt (attackTimeSecs * sourceSampleRate); - releaseSamples = roundToInt (releaseTimeSecs * sourceSampleRate); + params.attack = static_cast (attackTimeSecs); + params.release = static_cast (releaseTimeSecs); } } @@ -87,24 +87,10 @@ void SamplerVoice::startNote (int midiNoteNumber, float velocity, SynthesiserSou lgain = velocity; rgain = velocity; - isInAttack = (sound->attackSamples > 0); - isInRelease = false; + adsr.setSampleRate (sound->sourceSampleRate); + adsr.setParameters (sound->params); - if (isInAttack) - { - attackReleaseLevel = 0.0f; - attackDelta = (float) (pitchRatio / sound->attackSamples); - } - else - { - attackReleaseLevel = 1.0f; - attackDelta = 0.0f; - } - - if (sound->releaseSamples > 0) - releaseDelta = (float) (-pitchRatio / sound->releaseSamples); - else - releaseDelta = -1.0f; + adsr.noteOn(); } else { @@ -116,12 +102,12 @@ void SamplerVoice::stopNote (float /*velocity*/, bool allowTailOff) { if (allowTailOff) { - isInAttack = false; - isInRelease = true; + adsr.noteOff(); } else { clearCurrentNote(); + adsr.reset(); } } @@ -151,35 +137,10 @@ void SamplerVoice::renderNextBlock (AudioBuffer& outputBuffer, int startS float r = (inR != nullptr) ? (inR[pos] * invAlpha + inR[pos + 1] * alpha) : l; - l *= lgain; - r *= rgain; + auto envelopeValue = adsr.getNextSample(); - if (isInAttack) - { - l *= attackReleaseLevel; - r *= attackReleaseLevel; - - attackReleaseLevel += attackDelta; - - if (attackReleaseLevel >= 1.0f) - { - attackReleaseLevel = 1.0f; - isInAttack = false; - } - } - else if (isInRelease) - { - l *= attackReleaseLevel; - r *= attackReleaseLevel; - - attackReleaseLevel += releaseDelta; - - if (attackReleaseLevel <= 0.0f) - { - stopNote (0.0f, false); - break; - } - } + l *= lgain * envelopeValue; + r *= rgain * envelopeValue; if (outR != nullptr) { diff --git a/modules/juce_audio_formats/sampler/juce_Sampler.h b/modules/juce_audio_formats/sampler/juce_Sampler.h index a8cd0717cd..ce2a794f53 100644 --- a/modules/juce_audio_formats/sampler/juce_Sampler.h +++ b/modules/juce_audio_formats/sampler/juce_Sampler.h @@ -83,12 +83,14 @@ public: */ AudioBuffer* getAudioData() const noexcept { return data.get(); } + //============================================================================== + /** Changes the parameters of the ADSR envelope which will be applied to the sample. */ + void setEnvelopeParameters (ADSR::Parameters parametersToUse) { params = parametersToUse; } //============================================================================== bool appliesToNote (int midiNoteNumber) override; bool appliesToChannel (int midiChannel) override; - private: //============================================================================== friend class SamplerVoice; @@ -97,8 +99,9 @@ private: std::unique_ptr> data; double sourceSampleRate; BigInteger midiNotes; - int length = 0, attackSamples = 0, releaseSamples = 0; - int midiRootNote = 0; + int length = 0, midiRootNote = 0; + + ADSR::Parameters params; JUCE_LEAK_DETECTOR (SamplerSound) }; @@ -136,13 +139,13 @@ public: void renderNextBlock (AudioBuffer&, int startSample, int numSamples) override; - private: //============================================================================== double pitchRatio = 0; double sourceSamplePosition = 0; - float lgain = 0, rgain = 0, attackReleaseLevel = 0, attackDelta = 0, releaseDelta = 0; - bool isInAttack = false, isInRelease = false; + float lgain = 0, rgain = 0; + + ADSR adsr; JUCE_LEAK_DETECTOR (SamplerVoice) };