From 9568198a1cc7feeda661d34a4db5526e90d9aaee Mon Sep 17 00:00:00 2001 From: thibaudk Date: Mon, 7 Oct 2024 22:53:11 +0100 Subject: [PATCH] [FM] functional vibrato --- Senior/SeniorModel.cpp | 26 ++++++++++++++-------- Senior/SeniorModel.hpp | 49 +++++++++++++++++++++++++++++++----------- Senior/SeniorUi.hpp | 16 +++++++++++--- 3 files changed, 66 insertions(+), 25 deletions(-) diff --git a/Senior/SeniorModel.cpp b/Senior/SeniorModel.cpp index 8eb7af4..7e04fc4 100644 --- a/Senior/SeniorModel.cpp +++ b/Senior/SeniorModel.cpp @@ -14,10 +14,10 @@ void Senior::operator()(tick t) } // Only compute skipped_frames when the control changes - if (inputs.rate != previous_rate) + if (inputs.s_rate != previous_rate) { - step_size = (1. - inputs.rate) * RATE; - previous_rate = inputs.rate; + step_size = (1. - inputs.s_rate) * RATE; + previous_rate = inputs.s_rate; } // Process the input buffer @@ -27,7 +27,7 @@ void Senior::operator()(tick t) auto* out = outputs.audio[i % outputs.audio.channels]; // Init current_frame for each channel - double current_frame{crush_frame(in[i])}; + double current_frame{crush(in[i])}; for(int j{0}; j < t.frames; j++) { @@ -36,11 +36,18 @@ void Senior::operator()(tick t) else { skipped_frames[i] = 0; - current_frame = crush_frame(in[j]); + current_frame = crush(in[j]); } - fms[i].delay(lfos[i].cos()*0.0025 + 0.05); - out[j] = fms[i](current_frame); + if (!inputs.depth) + out[j] = current_frame; + else + { + lfo.set(inputs.freq, phases[i], 0); + fms[i].delay(lfo.cos() * inputs.depth + .5); + phases[i] = lfo.phase(); + out[j] = fms[i](current_frame); + } } } } @@ -50,7 +57,7 @@ void Senior::set_bit_factor() bit_factor = pow(2, inputs.bits) - 1; } -double Senior::crush_frame(const double& f) +double Senior::crush(const double& f) { if (inputs.bits == BITS) return f; @@ -60,4 +67,5 @@ double Senior::crush_frame(const double& f) // Close, but prefer the use of roundMagic from Lance Putnam's Gama return gam::scl::round(f * bit_factor) / bit_factor; } -} + +} // namespace Ottobit diff --git a/Senior/SeniorModel.hpp b/Senior/SeniorModel.hpp index 88869fc..e9d5396 100644 --- a/Senior/SeniorModel.hpp +++ b/Senior/SeniorModel.hpp @@ -30,11 +30,22 @@ public: struct ins { halp::dynamic_audio_bus<"Input", double> audio; - struct : halp::knob_f32<"Bits", halp::range{.min = 3, .max = BITS, .init = BITS}> + halp::knob_f32<"SAMPLE RATE", halp::range{.min = .1, .max = 1., .init = 1.}> s_rate; + + using log_map = halp::log_mapper>; + struct : halp::knob_f32<"BITS", halp::range{.min = 3, .max = BITS, .init = BITS}> { - using mapper = halp::log_mapper>; + using mapper = log_map; } bits; - halp::knob_f32<"Rate", halp::range{.min = .1, .max = 1., .init = 1.}> rate; + struct : halp::knob_f32<"FREQ", halp::range{.min = .4, .max = 200., .init = 5.}> + { + using mapper = log_map; + } freq; + struct : halp::knob_f32<"DEPTH", halp::range{.min = .0, .max = 1., .init = 0.}> + { + using mapper = log_map; + } + depth; } inputs; struct @@ -47,17 +58,26 @@ public: { // Initialization, this method will be called with buffer size, etc. set_bit_factor(); + lfo.set_sample_rate(info.rate); + lfo.freq(5); + skipped_frames.resize(info.input_channels); - lfos.resize(info.input_channels); fms.resize(info.input_channels); - for (auto [lfo, fm] : std::views::zip(lfos, fms)) + for (auto& fm : fms) { - lfo.set_sample_rate(info.rate); - lfo.set(5, 0, 0); fm.set_sample_rate(info.rate); fm.maxDelay(0.1); } + + phases.resize(info.output_channels); + + float phase_frac{1.f / info.output_channels}; + + for (int i{0}; i < info.output_channels; i++) + { + phases[i] = i * phase_frac; + } } // Do our processing for N samples @@ -72,18 +92,21 @@ public: private: int bit_factor, step_size{0}; + + double previous_bits{BITS}, + previous_rate{1}; + + std::vector phases; std::vector skipped_frames; - double previous_bits{BITS}, previous_rate{1}; - - std::vector> lfos; std::vector> fms; + gam::LFO lfo{5.}; + void set_bit_factor(); - double crush_frame(const double& f); + double crush(const double& f); }; -} +} // namespace Ottobit diff --git a/Senior/SeniorUi.hpp b/Senior/SeniorUi.hpp index 69988f8..c3c2951 100644 --- a/Senior/SeniorUi.hpp +++ b/Senior/SeniorUi.hpp @@ -10,11 +10,21 @@ struct Senior::ui using enum halp::layouts; halp_meta(name, "Senior") - halp_meta(layout, hbox) + halp_meta(layout, vbox) halp_meta(background, dark) - halp::item<&ins::bits> bits; + struct + { + halp_meta(layout, hbox) + halp::item<&ins::bits> bits; + halp::item<&ins::s_rate> s_rate; + } res; - halp::item<&ins::rate> rate; + struct + { + halp_meta(layout, hbox) + halp::item<&ins::depth> depth; + halp::item<&ins::freq> freq; + } mod; }; }