diff --git a/src/sfizz/Region.cpp b/src/sfizz/Region.cpp index e22d6fb6..808f031e 100644 --- a/src/sfizz/Region.cpp +++ b/src/sfizz/Region.cpp @@ -52,14 +52,12 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) break; case hash("delay_random"): setValueFromOpcode(opcode, delayRandom, Default::delayRange); - delayDistribution.param(std::uniform_real_distribution::param_type(0, delayRandom)); break; case hash("offset"): setValueFromOpcode(opcode, offset, Default::offsetRange); break; case hash("offset_random"): setValueFromOpcode(opcode, offsetRandom, Default::offsetRange); - offsetDistribution.param(std::uniform_int_distribution::param_type(0, offsetRandom)); break; case hash("end"): setValueFromOpcode(opcode, sampleEnd, Default::sampleEndRange); @@ -301,7 +299,6 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) break; case hash("amp_random"): setValueFromOpcode(opcode, ampRandom, Default::ampRandomRange); - volumeDistribution.param(std::uniform_real_distribution::param_type(0, ampRandom)); break; case hash("amp_velcurve_&"): { @@ -643,7 +640,6 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) break; case hash("pitch_random"): setValueFromOpcode(opcode, pitchRandom, Default::pitchRandomRange); - pitchDistribution.param(std::uniform_int_distribution::param_type(-pitchRandom, pitchRandom)); break; case hash("transpose"): setValueFromOpcode(opcode, transpose, Default::transposeRange); @@ -895,8 +891,9 @@ void sfz::Region::registerTempo(float secondsPerQuarter) noexcept bpmSwitched = false; } -float sfz::Region::getBasePitchVariation(int noteNumber, uint8_t velocity) noexcept +float sfz::Region::getBasePitchVariation(int noteNumber, uint8_t velocity) const noexcept { + std::uniform_int_distribution pitchDistribution { -pitchRandom, pitchRandom }; auto pitchVariationInCents = pitchKeytrack * (noteNumber - (int)pitchKeycenter); // note difference with pitch center pitchVariationInCents += tune; // sample tuning pitchVariationInCents += config::centPerSemitone * transpose; // sample transpose @@ -905,20 +902,21 @@ float sfz::Region::getBasePitchVariation(int noteNumber, uint8_t velocity) noexc return centsFactor(pitchVariationInCents); } -float sfz::Region::getBaseVolumedB(int noteNumber) noexcept +float sfz::Region::getBaseVolumedB(int noteNumber) const noexcept { + std::uniform_real_distribution volumeDistribution { -ampRandom, ampRandom }; auto baseVolumedB = volume + volumeDistribution(Random::randomGenerator); if (trigger == SfzTrigger::release || trigger == SfzTrigger::release_key) baseVolumedB -= rtDecay * midiState.getNoteDuration(noteNumber); return baseVolumedB; } -float sfz::Region::getBaseGain() noexcept +float sfz::Region::getBaseGain() const noexcept { return normalizePercents(amplitude); } -float sfz::Region::getPhase() noexcept +float sfz::Region::getPhase() const noexcept { float phase; if (oscillatorPhase >= 0) { @@ -931,13 +929,15 @@ float sfz::Region::getPhase() noexcept return phase; } -uint32_t sfz::Region::getOffset(Oversampling factor) noexcept +uint32_t sfz::Region::getOffset(Oversampling factor) const noexcept { + std::uniform_int_distribution offsetDistribution { 0, offsetRandom }; return (offset + offsetDistribution(Random::randomGenerator)) * static_cast(factor); } -float sfz::Region::getDelay() noexcept +float sfz::Region::getDelay() const noexcept { + std::uniform_real_distribution delayDistribution { 0, delayRandom }; return delay + delayDistribution(Random::randomGenerator); } @@ -988,7 +988,7 @@ float crossfadeOut(const sfz::Range& crossfadeRange, U value, SfzCrossfadeCur return 1.0f; } -float sfz::Region::getNoteGain(int noteNumber, uint8_t velocity) noexcept +float sfz::Region::getNoteGain(int noteNumber, uint8_t velocity) const noexcept { float baseGain { 1.0f }; @@ -1009,7 +1009,7 @@ float sfz::Region::getNoteGain(int noteNumber, uint8_t velocity) noexcept return baseGain; } -float sfz::Region::getCrossfadeGain(const sfz::SfzCCArray& ccState) noexcept +float sfz::Region::getCrossfadeGain(const sfz::SfzCCArray& ccState) const noexcept { float gain { 1.0f }; diff --git a/src/sfizz/Region.h b/src/sfizz/Region.h index b3966a47..1fd0fa67 100644 --- a/src/sfizz/Region.h +++ b/src/sfizz/Region.h @@ -135,7 +135,7 @@ struct Region { * @param velocity * @return float */ - float getBasePitchVariation(int noteNumber, uint8_t velocity) noexcept; + float getBasePitchVariation(int noteNumber, uint8_t velocity) const noexcept; /** * @brief Get the note-related gain of the region depending on which note has been * pressed and at which velocity. @@ -144,7 +144,7 @@ struct Region { * @param velocity * @return float */ - float getNoteGain(int noteNumber, uint8_t velocity) noexcept; + float getNoteGain(int noteNumber, uint8_t velocity) const noexcept; /** * @brief Get the additional crossfade gain of the region depending on the * CC values @@ -152,7 +152,7 @@ struct Region { * @param ccState * @return float */ - float getCrossfadeGain(const SfzCCArray& ccState) noexcept; + float getCrossfadeGain(const SfzCCArray& ccState) const noexcept; /** * @brief Get the base volume of the region depending on which note has been * pressed to trigger the region. @@ -160,19 +160,19 @@ struct Region { * @param noteNumber * @return float */ - float getBaseVolumedB(int noteNumber) noexcept; + float getBaseVolumedB(int noteNumber) const noexcept; /** * @brief Get the base gain of the region. * * @return float */ - float getBaseGain() noexcept; + float getBaseGain() const noexcept; /** * @brief Get the base gain of the region. * * @return float */ - float getPhase() noexcept; + float getPhase() const noexcept; /** * @brief Computes the gain value related to the velocity of the note * @@ -184,13 +184,13 @@ struct Region { * * @return uint32_t */ - uint32_t getOffset(Oversampling factor = Oversampling::x1) noexcept; + uint32_t getOffset(Oversampling factor = Oversampling::x1) const noexcept; /** * @brief Get the region delay in seconds * * @return float */ - float getDelay() noexcept; + float getDelay() const noexcept; /** * @brief Get the index of the sample end, either natural end or forced * loop. @@ -333,11 +333,6 @@ private: absl::string_view defaultPath { "" }; int sequenceCounter { 0 }; - - std::uniform_real_distribution volumeDistribution { -sfz::Default::ampRandom, sfz::Default::ampRandom }; - std::uniform_real_distribution delayDistribution { 0, sfz::Default::delayRandom }; - std::uniform_int_distribution offsetDistribution { 0, sfz::Default::offsetRandom }; - std::uniform_int_distribution pitchDistribution { -sfz::Default::pitchRandom, sfz::Default::pitchRandom }; LEAK_DETECTOR(Region); };