From 8565037ac25736888656885db4d6902c2f74de79 Mon Sep 17 00:00:00 2001 From: thibaudk Date: Mon, 28 Oct 2024 11:12:04 +0000 Subject: [PATCH] [messages] working messages between ui and processor thread --- Amuencha/AmuenchaModel.cpp | 18 +++++++++--------- Amuencha/AmuenchaModel.hpp | 6 +++--- Amuencha/AmuenchaUi.hpp | 20 ++++++++++---------- Amuencha/SpiralDisplay.cpp | 3 ++- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Amuencha/AmuenchaModel.cpp b/Amuencha/AmuenchaModel.cpp index 3845422..f4f7b16 100644 --- a/Amuencha/AmuenchaModel.cpp +++ b/Amuencha/AmuenchaModel.cpp @@ -47,7 +47,7 @@ void Analyser::analyzer_setup(float max_buffer_duration) // Could also span more for even better measurements, with larger // computation cost and latency float f = frequencies[idx]; - int window_size = (int)(min(inputs.periods_nb.value / f, max_buffer_duration * 0.001f) + int window_size = (int)(min(inputs.periods.value / f, max_buffer_duration * 0.001f) * sampling_rate); vector window(window_size); vector window_deriv(window_size); @@ -89,9 +89,9 @@ void Analyser::analyzer_setup(float max_buffer_duration) i += 4; continue; } - float t = (float)(i-window_size-1) / sampling_rate; - float re = cosf(-two_pi*t*f); - float im = sinf(-two_pi*t*f); + float t = (float)(i - window_size - 1) / sampling_rate; + float re = cosf(-two_pi * t * f); + float im = sinf(-two_pi * t * f); v4sf ws = { re * window[i], im * window[i], @@ -102,7 +102,7 @@ void Analyser::analyzer_setup(float max_buffer_duration) wsum += window[i]; ++i; } - power_normalization_factors[idx] = 1. / (wsum*wsum); + power_normalization_factors[idx] = 1. / (wsum * wsum); big_buffer_size = max(big_buffer_size, window_size); } @@ -122,11 +122,11 @@ void Analyser::initialize_window(std::vector& window) for (int i{0}; i < size; ++i) { float p = i * two_over_N - 1.; - window[i] = boost::math::cyl_bessel_i(0., alpha_pi * sqrt(1. - p*p)) * inv_denom; + window[i] = boost::math::cyl_bessel_i(0., alpha_pi * sqrt(1. - p * p)) * inv_denom; } } -void Analyser::initialize_window_deriv(std::vector &window) +void Analyser::initialize_window_deriv(std::vector& window) { // Derivative of the Kaiser window with a parameter of alpha=3 that nullifies the window on edges int size = window.size(); @@ -137,8 +137,8 @@ void Analyser::initialize_window_deriv(std::vector &window) for (int i{1}; i < size; ++i) { float p = i * two_over_N - 1.; - window[i] = boost::math::cyl_bessel_i(1., alpha_pi * sqrt(1. - p*p)) * - inv_denom * alpha_pi / sqrt(1. - p*p) * (-p)*two_over_N; + window[i] = boost::math::cyl_bessel_i(1., alpha_pi * sqrt(1. - p * p)) * + inv_denom * alpha_pi / sqrt(1. - p * p) * (-p) * two_over_N; } // lim I1(x)/x as x->0 = 1/2 window[0] = 0.5 * inv_denom * alpha_pi * alpha_pi * two_over_N; diff --git a/Amuencha/AmuenchaModel.hpp b/Amuencha/AmuenchaModel.hpp index 26c193a..7392089 100644 --- a/Amuencha/AmuenchaModel.hpp +++ b/Amuencha/AmuenchaModel.hpp @@ -22,7 +22,7 @@ public: int max; }; - std::function send_message; + std::function send_message; // Define inputs and outputs ports. // See the docs at https://github.com/celtera/avendish @@ -43,8 +43,8 @@ public: self.send_message({.min = self.inputs.min.value, .max = this->value}); } } max; - halp::spinbox_i32<"Analyze num. periods", - halp::range{.min = 0, .max = 99, .init = 30}> periods_nb; + halp::spinbox_i32<"Periods", + halp::range{.min = 0, .max = 99, .init = 30}> periods; } inputs; void process_message(const std::vector& frequencies) diff --git a/Amuencha/AmuenchaUi.hpp b/Amuencha/AmuenchaUi.hpp index 5d54085..30aec3d 100644 --- a/Amuencha/AmuenchaUi.hpp +++ b/Amuencha/AmuenchaUi.hpp @@ -20,7 +20,7 @@ struct Analyser::ui halp_meta(layout, vbox) halp::item<&ins::min> min; halp::item<&ins::max> max; - halp::item<&ins::periods_nb> periods; + halp::item<&ins::periods> periods; } controls; halp::custom_actions_item spiral{.x = 0, .y = 0}; @@ -29,16 +29,16 @@ struct Analyser::ui // Define the communication between UI and processor. struct bus { - // std::function&)> send_message; + std::function&)> send_message; - // // Set up connections - // void init(ui& self) - // { - // self.spiral.on_new_frequencies = [&] - // { - // send_message(self.spiral.get_frequencies()); - // }; - // } + // Set up connections + void init(ui& self) + { + self.spiral.on_new_frequencies = [&] + { + send_message(self.spiral.get_frequencies()); + }; + } // Receive a message on the UI thread from the processing thread static void process_message(ui& self, const processor_to_ui& msg) diff --git a/Amuencha/SpiralDisplay.cpp b/Amuencha/SpiralDisplay.cpp index ead5ece..ca83074 100644 --- a/Amuencha/SpiralDisplay.cpp +++ b/Amuencha/SpiralDisplay.cpp @@ -4,6 +4,7 @@ Amuencha::SpiralDisplay::SpiralDisplay() : min_midi_note{24} , max_midi_note{72} , gain{1.f} + , on_new_frequencies{[]{}} { for (int i{0}; i < 12; i++) note_positions[i] = std::polar(.9f, half_pi - i * two_pi / 12); @@ -90,7 +91,7 @@ void Amuencha::SpiralDisplay::compute_frequencies() fill(display_spectrum[id].begin(), display_spectrum[id].end(), 0.); } - // on_new_frequencies(); + on_new_frequencies(); } void Amuencha::SpiralDisplay::power_handler(int ID, const std::vector &reassigned_frequencies, const std::vector &power_spectrum)