clap

CLAP Audio Plugin API
Log | Files | Refs | README | LICENSE

commit c1c73f9d37cfff6eb590352ca096904acd1d2c77
parent 83113832f91571def8cdb24f6b0d3ae393293c6d
Author: Alexandre BIQUE <bique.alexandre@gmail.com>
Date:   Tue, 31 Aug 2021 13:00:09 +0200

Trivial parameter smoothing

Diffstat:
Mexamples/plugins/core-plugin.cc | 6+++---
Mexamples/plugins/dc-offset/dc-offset.cc | 2+-
Mexamples/plugins/parameters.hh | 81++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
3 files changed, 72 insertions(+), 17 deletions(-)

diff --git a/examples/plugins/core-plugin.cc b/examples/plugins/core-plugin.cc @@ -180,7 +180,7 @@ namespace clap { auto p = parameters_.getById(paramId); if (!p) return; - p->setValue(value.value); + p->setValueSmoothed(value.value, 128); clap_event ev; ev.time = 0; @@ -224,7 +224,7 @@ namespace clap { std::terminate(); } - p->setValue(ev->param_value.value); + p->setValueSmoothed(ev->param_value.value, 128); pluginToGuiQueue_.set(p->info().id, {ev->param_value.value, p->modulation()}); } break; @@ -240,7 +240,7 @@ namespace clap { std::terminate(); } - p->setModulation(ev->param_mod.amount); + p->setModulationSmoothed(ev->param_mod.amount, 128); pluginToGuiQueue_.set(p->info().id, {p->value(), ev->param_mod.amount}); } break; diff --git a/examples/plugins/dc-offset/dc-offset.cc b/examples/plugins/dc-offset/dc-offset.cc @@ -88,7 +88,7 @@ namespace clap { /* Process as many samples as possible until the next event */ for (; i < N; ++i) { - float offset = offsetParam_->step(1); + float offset = offsetParam_->step(); for (int c = 0; c < channelCount_; ++c) out[c][i] = in[c][i] + offset; } diff --git a/examples/plugins/parameters.hh b/examples/plugins/parameters.hh @@ -16,14 +16,12 @@ namespace clap { class Parameter { public: - explicit Parameter(const clap_param_info &info) : info_(info) { - info_.cookie = this; - } + explicit Parameter(const clap_param_info &info) : info_(info) { info_.cookie = this; } - Parameter(const Parameter&) = delete; - Parameter(Parameter&&) = delete; - Parameter& operator=(const Parameter&) = delete; - Parameter& operator=(Parameter&&) = delete; + Parameter(const Parameter &) = delete; + Parameter(Parameter &&) = delete; + Parameter &operator=(const Parameter &) = delete; + Parameter &operator=(Parameter &&) = delete; const double value() const noexcept { return value_; } const double modulation() const noexcept { return modulation_; } @@ -35,21 +33,78 @@ namespace clap { modulation_ = 0; } - void setValue(double val) { value_ = val; } + void setValueImmediately(double val) { + value_ = val; + valueRamp_ = 0; + valueSteps_ = 0; + } + void setModulationImmediately(double mod) { + modulation_ = mod; + modulationRamp_ = 0; + modulationSteps_ = 0; + } + + void setValueSmoothed(double val, uint16_t steps) + { + assert(steps > 0); + valueRamp_ = (val - value_) / steps; + valueSteps_ = steps; + } + + void setModulationSmoothed(double mod, uint16_t steps) + { + assert(steps > 0); + modulationRamp_ = (mod - modulation_) / steps; + modulationSteps_ = steps; + } - void setModulation(double mod) { modulation_ = mod; } + // Advances the value by 1 samples and return the new value + modulation + double step() { + if (valueSteps_ > 0) [[unlikely]] + { + value_ += valueRamp_; + --valueSteps_; + } + + if (modulationSteps_ > 0) [[unlikely]] + { + modulation_ += modulationRamp_; + --modulationSteps_; + } + + return value_ + modulation_; + } // Advances the value by n samples and return the new value + modulation double step(uint32_t n) { - // TODO smooth + if (valueSteps_ > 0) [[unlikely]] + { + auto k = std::min<uint32_t>(valueSteps_, n); + value_ += k * valueRamp_; + valueSteps_ -= k; + } + + if (modulationSteps_ > 0) [[unlikely]] + { + auto k = std::min<uint32_t>(valueSteps_, n); + modulation_ += k * modulationRamp_; + modulationSteps_ -= k; + } + return value_ + modulation_; } private: clap_param_info info_; - double value_; - double modulation_; + double value_ = 0; + double modulation_ = 0; + + double valueRamp_ = 0; + double modulationRamp_ = 0; + + uint16_t valueSteps_ = 0; + uint16_t modulationSteps_ = 0; }; class Parameters { @@ -90,7 +145,7 @@ namespace clap { auto *p = getById(v.first); if (!p) continue; - p->setValue(v.second); + p->setValueImmediately(v.second); } }