diff --git a/sfizz/Synth.cpp b/sfizz/Synth.cpp index 8a095f81..d9157e14 100644 --- a/sfizz/Synth.cpp +++ b/sfizz/Synth.cpp @@ -143,6 +143,10 @@ void sfz::Synth::handleGlobalOpcodes(const std::vector& members) case hash("sw_default"): setValueFromOpcode(member, defaultSwitch, Default::keyRange); break; + case hash("volume"): + // FIXME : Probably best not to mess with this and let the host control the volume + // setValueFromOpcode(member, volume, Default::volumeRange); + break; } } } @@ -350,6 +354,8 @@ void sfz::Synth::renderBlock(AudioSpan buffer) noexcept voice->renderBlock(tempSpan); buffer.add(tempSpan); } + + buffer.applyGain(db2mag(volume)); } void sfz::Synth::noteOn(int delay, int channel, int noteNumber, uint8_t velocity) noexcept @@ -492,3 +498,12 @@ size_t sfz::Synth::getNumPreloadedSamples() const noexcept { return filePool.getNumPreloadedSamples(); } + +float sfz::Synth::getVolume() const noexcept +{ + return volume; +} +void sfz::Synth::setVolume(float volume) noexcept +{ + this->volume = Default::volumeRange.clamp(volume); +} \ No newline at end of file diff --git a/sfizz/Synth.h b/sfizz/Synth.h index 13c10e9e..19242ffb 100644 --- a/sfizz/Synth.h +++ b/sfizz/Synth.h @@ -51,6 +51,9 @@ public: void setSamplesPerBlock(int samplesPerBlock) noexcept; void setSampleRate(float sampleRate) noexcept; + float getVolume() const noexcept; + void setVolume(float volume) noexcept; + void renderBlock(AudioSpan buffer) noexcept; void noteOn(int delay, int channel, int noteNumber, uint8_t velocity) noexcept; void noteOff(int delay, int channel, int noteNumber, uint8_t velocity) noexcept; @@ -96,6 +99,7 @@ private: AudioBuffer tempBuffer { 2, config::defaultSamplesPerBlock }; int samplesPerBlock { config::defaultSamplesPerBlock }; float sampleRate { config::defaultSampleRate }; + float volume { Default::volume }; std::uniform_real_distribution randNoteDistribution { 0, 1 }; unsigned fileTicket { 1 }; diff --git a/sfizz/sfizz.cpp b/sfizz/sfizz.cpp index 7aaa5ce5..0e0e63dd 100644 --- a/sfizz/sfizz.cpp +++ b/sfizz/sfizz.cpp @@ -131,6 +131,18 @@ void sfizz_force_garbage_collection(sfizz_synth_t* synth) self->garbageCollect(); } +void sfizz_set_volume(sfizz_synth_t* synth, float volume) +{ + auto self = reinterpret_cast(synth); + self->setVolume(volume); +} + +float sfizz_get_volume(sfizz_synth_t* synth) +{ + auto self = reinterpret_cast(synth); + return self->getVolume(); +} + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/sfizz/sfizz.h b/sfizz/sfizz.h index d11b6233..c5537bcd 100644 --- a/sfizz/sfizz.h +++ b/sfizz/sfizz.h @@ -216,6 +216,22 @@ void sfizz_render_block(sfizz_synth_t* synth, float** channels, int num_channels */ void sfizz_force_garbage_collection(sfizz_synth_t* synth); +/** + * @brief Set the global instrument volume. + * + * @param synth + * @param volume the new volume + */ +void sfizz_set_volume(sfizz_synth_t* synth, float volume); + +/** + * @brief Get the global instrument volume. + * + * @param synth + * @return float the instrument volume + */ +float sfizz_get_volume(sfizz_synth_t* synth); + #ifdef __cplusplus } #endif \ No newline at end of file