From 8d48c93931f99519e20a0440f86d4c13fd21cc9f Mon Sep 17 00:00:00 2001 From: thibaudk Date: Wed, 2 Oct 2024 17:16:34 +0100 Subject: [PATCH] [bits] use gama's rounding + make current_frame multichannel --- CMakeLists.txt | 2 +- Ottobit/OttobitModel.cpp | 42 +++++++++++++++++++++++----------------- Ottobit/OttobitModel.hpp | 25 ++++++++++++------------ Ottobit/OttobitUi.hpp | 2 +- 4 files changed, 39 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dc7be91..b2195c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ avnd_score_plugin_add( Ottobit/OttobitUi.hpp TARGET ottobit MAIN_CLASS Ottobit - NAMESPACE Example + NAMESPACE Meris ) avnd_score_plugin_finalize( diff --git a/Ottobit/OttobitModel.cpp b/Ottobit/OttobitModel.cpp index 03c8a74..b438242 100644 --- a/Ottobit/OttobitModel.cpp +++ b/Ottobit/OttobitModel.cpp @@ -1,6 +1,8 @@ +#include + #include "Ottobit.hpp" -namespace Example +namespace Meris { void Ottobit::operator()(tick t) { @@ -16,34 +18,25 @@ void Ottobit::operator()(tick t) { step_size = (1. - inputs.rate) * RATE; previous_rate = inputs.rate; - qDebug() << "step size: " << step_size; } // Process the input buffer - for(int i = 0; i < inputs.audio.channels; i++) + for(int i{0}; i < inputs.audio.channels; i++) { auto* in = inputs.audio[i]; auto* out = outputs.audio[i]; - // Re-init skipped_frames and current_frame for each channel - skipped_frames = 0; - current_frame = in[0]; + // Init current_frame for each channel + double current_frame{crush_frame(in[0])}; - for(int j = 0; j < t.frames; j++) + for(int j{0}; j < t.frames; j++) { - if (skipped_frames <= step_size) - skipped_frames++; + if (skipped_frames[i] <= step_size) + skipped_frames[i]++; else { - skipped_frames = 0; - - if (inputs.bits == BITS) - current_frame = in[j]; - else - { - int in_scaled{static_cast((in[j] + 1) * bit_factor)}; - current_frame = (static_cast(in_scaled) / bit_factor) - 1; - } + skipped_frames[i] = 0; + current_frame = crush_frame(in[j]); } out[j] = current_frame; @@ -55,4 +48,17 @@ void Ottobit::set_bit_factor() { bit_factor = pow(2, inputs.bits) - 1; } + +double Ottobit::crush_frame(const double& f) +{ + if (inputs.bits == BITS) + return f; + else + { + // Rounding using static_cast + // return static_cast(static_cast(f * bit_factor)) / bit_factor; + // Close, but prefer Lance Putnama's Gama's use of roundMagic + return gam::scl::round(f * bit_factor) / bit_factor; + } +} } diff --git a/Ottobit/OttobitModel.hpp b/Ottobit/OttobitModel.hpp index edf6a3b..bdd52d6 100644 --- a/Ottobit/OttobitModel.hpp +++ b/Ottobit/OttobitModel.hpp @@ -7,7 +7,7 @@ #include #include -namespace Example +namespace Meris { class Ottobit @@ -38,22 +38,12 @@ public: halp::dynamic_audio_bus<"Output", double> audio; } outputs; - // Local variables - int bit_factor, - skipped_frames{0}, - step_size{0}; - - double previous_bits{BITS}, - previous_rate{1}, - current_frame; - - void set_bit_factor(); - using setup = halp::setup; void prepare(setup info) { // Initialization, this method will be called with buffer size, etc. set_bit_factor(); + skipped_frames.resize(info.input_channels); } // Do our processing for N samples @@ -64,6 +54,17 @@ public: // UI is defined in another file to keep things clear. struct ui; + +private: + // Local variables + int bit_factor, step_size{0}; + + std::vector skipped_frames; + + double previous_bits{BITS}, previous_rate{1}; + + void set_bit_factor(); + double crush_frame(const double& f); }; } diff --git a/Ottobit/OttobitUi.hpp b/Ottobit/OttobitUi.hpp index 73abd32..cc42a4c 100644 --- a/Ottobit/OttobitUi.hpp +++ b/Ottobit/OttobitUi.hpp @@ -2,7 +2,7 @@ #include #include -namespace Example +namespace Meris { struct Ottobit::ui {