From daa50f89023048694480a55796374ad1dba7ce15 Mon Sep 17 00:00:00 2001 From: thibaudk Date: Tue, 1 Oct 2024 00:43:19 +0100 Subject: [PATCH] [model, ui] functional rate reduction --- Ottobit/OttobitModel.cpp | 33 ++++++++++++++++++++++++++------- Ottobit/OttobitModel.hpp | 13 +++++++++---- Ottobit/OttobitUi.hpp | 5 +++-- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/Ottobit/OttobitModel.cpp b/Ottobit/OttobitModel.cpp index 2bd5700..03c8a74 100644 --- a/Ottobit/OttobitModel.cpp +++ b/Ottobit/OttobitModel.cpp @@ -4,28 +4,47 @@ namespace Example { void Ottobit::operator()(tick t) { - // Only compute bit factor when the control changes + // Only compute bit_factor when the control changes if (inputs.bits != previous_bits) { set_bit_factor(); previous_bits = inputs.bits; } + // Only compute skipped_frames when the control changes + if (inputs.rate != previous_rate) + { + 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++) { 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]; + for(int j = 0; j < t.frames; j++) { - if (inputs.bits != BITS) - { - int in_scaled{static_cast((in[j] + 1) * bit_factor)}; - current_frame = (static_cast(in_scaled) / bit_factor) - 1; - } + if (skipped_frames <= step_size) + skipped_frames++; else - current_frame = in[j]; + { + 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; + } + } out[j] = current_frame; } diff --git a/Ottobit/OttobitModel.hpp b/Ottobit/OttobitModel.hpp index 925f444..edf6a3b 100644 --- a/Ottobit/OttobitModel.hpp +++ b/Ottobit/OttobitModel.hpp @@ -13,6 +13,7 @@ namespace Example class Ottobit { #define BITS 16 +#define RATE 64 public: halp_meta(name, "Ottobit") @@ -29,6 +30,7 @@ public: { using mapper = halp::log_mapper>; } bits; + halp::knob_f32<"Rate", halp::range{.min = .1, .max = 1., .init = 1.}> rate; } inputs; struct @@ -37,9 +39,13 @@ public: } outputs; // Local variables - double previous_bits; - int bit_factor; - double current_frame; + int bit_factor, + skipped_frames{0}, + step_size{0}; + + double previous_bits{BITS}, + previous_rate{1}, + current_frame; void set_bit_factor(); @@ -47,7 +53,6 @@ public: void prepare(setup info) { // Initialization, this method will be called with buffer size, etc. - previous_bits = BITS; set_bit_factor(); } diff --git a/Ottobit/OttobitUi.hpp b/Ottobit/OttobitUi.hpp index 60d76a1..73abd32 100644 --- a/Ottobit/OttobitUi.hpp +++ b/Ottobit/OttobitUi.hpp @@ -10,10 +10,11 @@ struct Ottobit::ui using enum halp::layouts; halp_meta(name, "Ottobit") - halp_meta(layout, vbox) + halp_meta(layout, hbox) halp_meta(background, dark) - halp::label title{"Bits"}; halp::item<&ins::bits> bits; + + halp::item<&ins::rate> rate; }; }