From 96f6bd6357976e0feeb102e3c323c9c0d5b523f2 Mon Sep 17 00:00:00 2001 From: luna <223230@proton.me> Date: Sun, 21 Apr 2024 00:13:49 +0200 Subject: [PATCH 1/6] Enqueue GR buffer using `enqueue()` --- src/lib.rs | 83 +++++++++++++----------------------------------------- 1 file changed, 19 insertions(+), 64 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fed776b..8a16b45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,56 +168,6 @@ impl Plugin for Lamb { let count = buffer.samples() as i32; self.accum_buffer.read_from_buffer(buffer); - // for channel_samples in buffer.iter_samples() { - // let mut amplitude = 0.0; - // let num_samples = channel_samples.len(); - - // for sample in channel_samples { - // amplitude += *sample; - // } - - // To save resources, a plugin can (and probably should!) only perform expensive - // calculations that are only displayed on the GUI while the GUI is open - if self.params.editor_state.is_open() { - // amplitude = (amplitude / num_samples as f32).abs(); - // let current_peak_meter = self.peak_meter.load(std::sync::atomic::Ordering::Relaxed); - // let new_peak_meter = if amplitude > current_peak_meter { - // amplitude - // } else { - // current_peak_meter * self.peak_meter_decay_weight - // + amplitude * (1.0 - self.peak_meter_decay_weight) - // }; - - // self.peak_meter - // .store(new_peak_meter, std::sync::atomic::Ordering::Relaxed); - self.gain_reduction_left.store( - self.dsp - .get_param(GAIN_REDUCTION_LEFT_PI) - .expect("no GR read") as f32, - std::sync::atomic::Ordering::Relaxed, - ); - self.gain_reduction_right.store( - self.dsp - .get_param(GAIN_REDUCTION_RIGHT_PI) - .expect("no GR read") as f32, - std::sync::atomic::Ordering::Relaxed, - ); - } - let output = buffer.as_slice(); - - let mut gr_buffer = Buffer::default(); - - let mut real_buffers = vec![vec![0.0; MAX_SOUNDCARD_BUFFER_SIZE]; 2]; - - unsafe { - gr_buffer.set_slices(MAX_SOUNDCARD_BUFFER_SIZE, |output_slices| { - let (first_channel, other_channels) = real_buffers.split_at_mut(1); - *output_slices = vec![&mut first_channel[0], &mut other_channels[0]]; - }) - }; - - let gr_output = gr_buffer.as_slice(); - let bypass: f64 = match self.params.bypass.value() { true => 1.0, false => 0.0, @@ -266,28 +216,33 @@ impl Plugin for Lamb { &mut self.temp_output_buffer_gr_r, ], ); + let latency_samples = self.dsp.get_param(LATENCY_PI).expect("no latency read") as u32; + context.set_latency_samples(latency_samples); - for i in 0..count as usize { - output[0][i] = self.temp_output_buffer_l[i] as f32; - output[1][i] = self.temp_output_buffer_r[i] as f32; - gr_output[0][i] = self.temp_output_buffer_gr_l[i] as f32; - gr_output[1][i] = self.temp_output_buffer_gr_r[i] as f32; - } + if self.params.editor_state.is_open() { + self.gain_reduction_left.store( + self.dsp + .get_param(GAIN_REDUCTION_LEFT_PI) + .expect("no GR read") as f32, + std::sync::atomic::Ordering::Relaxed, + ); + self.gain_reduction_right.store( + self.dsp + .get_param(GAIN_REDUCTION_RIGHT_PI) + .expect("no GR read") as f32, + std::sync::atomic::Ordering::Relaxed, + ); - // To save resources, a plugin can (and probably should!) only perform expensive - // calculations that are only displayed on the GUI while the GUI is open - if self.params.editor_state.is_open() { + for i in 0..count as usize { self.peak_buffer .lock() .unwrap() - .enqueue_buffer(&mut gr_buffer, None); + .enqueue((self.temp_output_buffer_gr_l[i] + self.temp_output_buffer_gr_l[i]) as f32 / 2.0); } - - let latency_samples = self.dsp.get_param(LATENCY_PI).expect("no latency read") as u32; - context.set_latency_samples(latency_samples); + } ProcessStatus::Normal - } + } } impl ClapPlugin for Lamb { From 5900b6819790e552d658146f9158d80978e6c760 Mon Sep 17 00:00:00 2001 From: luna <223230@proton.me> Date: Sun, 21 Apr 2024 00:28:00 +0200 Subject: [PATCH 2/6] Update to latest `cyma` --- Cargo.lock | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/editor.rs | 26 ++++++++--------- src/lib.rs | 6 ++-- 3 files changed, 95 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 96b943e..3c6eb00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1122,11 +1122,13 @@ dependencies = [ [[package]] name = "cyma" version = "0.1.0" -source = "git+https://github.com/223230/cyma.git#6e54760b784f3eb9437b97f10499319ccade61a9" +source = "git+https://github.com/223230/cyma.git#a58addb981374235020a7add0c904b1217f307d3" dependencies = [ "lazy_static", "nih_plug", "nih_plug_vizia", + "realfft", + "triple_buffer", ] [[package]] @@ -2408,6 +2410,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num-complex" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +dependencies = [ + "num-traits", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -2425,6 +2436,15 @@ dependencies = [ "syn 2.0.55", ] +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.18" @@ -2838,6 +2858,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "primal-check" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9df7f93fd637f083201473dab4fee2db4c429d32e55e3299980ab3957ab916a0" +dependencies = [ + "num-integer", +] + [[package]] name = "proc-macro-crate" version = "1.3.1" @@ -2983,6 +3012,15 @@ dependencies = [ "font-types", ] +[[package]] +name = "realfft" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953d9f7e5cdd80963547b456251296efc2626ed4e3cbf36c869d9564e0220571" +dependencies = [ + "rustfft", +] + [[package]] name = "redox_syscall" version = "0.3.5" @@ -3078,6 +3116,21 @@ dependencies = [ "semver", ] +[[package]] +name = "rustfft" +version = "6.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43806561bc506d0c5d160643ad742e3161049ac01027b5e6d7524091fd401d86" +dependencies = [ + "num-complex", + "num-integer", + "num-traits", + "primal-check", + "strength_reduce", + "transpose", + "version_check", +] + [[package]] name = "rustix" version = "0.37.27" @@ -3332,6 +3385,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strength_reduce" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe895eb47f22e2ddd4dabc02bce419d2e643c8e3b585c78158b349195bc24d82" + [[package]] name = "strsim" version = "0.11.0" @@ -3564,6 +3623,25 @@ dependencies = [ "once_cell", ] +[[package]] +name = "transpose" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad61aed86bc3faea4300c7aee358b4c6d0c8d6ccc36524c96e4c92ccf26e77e" +dependencies = [ + "num-integer", + "strength_reduce", +] + +[[package]] +name = "triple_buffer" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "316e1ab00252ce5980b8a70eb50f9818f47a20d341394c56ffab88e764b3caed" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "ttf-parser" version = "0.18.1" diff --git a/src/editor.rs b/src/editor.rs index 69e0513..abbff8a 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -9,7 +9,8 @@ use std::sync::atomic::Ordering; use std::sync::{Arc, Mutex}; use cyma::{ - utils::{PeakBuffer, ValueScaling}, + utils::{MinimaBuffer}, + prelude::*, visualizers::{ Graph, Grid, Meter, UnitRuler, }, @@ -23,7 +24,7 @@ struct LambData { // peak_meter: Arc, gain_reduction_left: Arc, gain_reduction_right: Arc, - peak_buffer: Arc>, + peak_buffer: Arc>, } impl LambData { @@ -32,7 +33,7 @@ impl LambData { // peak_meter: Arc, gain_reduction_left: Arc, gain_reduction_right: Arc, - peak_buffer: Arc>, + peak_buffer: Arc>, ) -> Self { Self { params, @@ -59,7 +60,7 @@ pub(crate) fn create( // peak_meter: Arc, gain_reduction_left: Arc, gain_reduction_right: Arc, - peak_buffer: Arc>, + peak_buffer: Arc>, editor_state: Arc, ) -> Option> { create_vizia_editor(editor_state, ViziaTheming::Custom, move |cx, _| { @@ -399,17 +400,16 @@ fn peak_graph(cx: &mut Context) { ZStack::new(cx, |cx| { Grid::new( cx, + ValueScaling::Linear, (-32.0, 8.0), - 0.0, vec![6.0, 0.0, -6.0, -12.0, -18.0, -24.0, -30.0], + Orientation::Vertical ) - .color(Color::rgb(60, 60, 60)) - ; + .color(Color::rgb(60, 60, 60)); Graph::new(cx, LambData::peak_buffer, (-32.0, 6.0), ValueScaling::Decibels) .color(Color::rgba(0, 0, 0, 160)) - .background_color(Color::rgba(16, 16, 16, 60)) - ; + .background_color(Color::rgba(16, 16, 16, 60)); }) // .background_color(Color::rgb(16, 16, 16)) ; @@ -417,6 +417,7 @@ fn peak_graph(cx: &mut Context) { UnitRuler::new( cx, (-32.0, 8.0), + ValueScaling::Linear, vec![ (6.0, "6db"), (0.0, "0db"), @@ -439,10 +440,9 @@ fn peak_graph(cx: &mut Context) { ValueScaling::Decibels, Orientation::Vertical, ) - .width(Pixels(32.0)) - .color(Color::rgb(0, 0, 0)) - .background_color(Color::rgb(80, 80, 80)) - ; + .width(Pixels(32.0)) + .color(Color::rgb(0, 0, 0)) + .background_color(Color::rgb(80, 80, 80)); }) .top(Pixels(13.0)) // .height(Pixels(280.0)) diff --git a/src/lib.rs b/src/lib.rs index 8a16b45..eb7060e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ use std::sync::{Arc, Mutex}; mod buffer; mod dsp; use buffer::*; -use cyma::utils::{PeakBuffer, VisualizerBuffer}; +use cyma::utils::{MinimaBuffer, VisualizerBuffer}; use default_boxed::DefaultBoxed; @@ -42,7 +42,7 @@ pub struct Lamb { gain_reduction_right: Arc, // These buffers will hold the sample data for the visualizers. - peak_buffer: Arc>, + peak_buffer: Arc>, } impl Default for Lamb { fn default() -> Self { @@ -64,7 +64,7 @@ impl Default for Lamb { temp_output_buffer_gr_l : f64::default_boxed_array::(), temp_output_buffer_gr_r : f64::default_boxed_array::(), sample_rate: 48000.0, - peak_buffer: Arc::new(Mutex::new(PeakBuffer::new(800, 48000.0, 10.0))), + peak_buffer: Arc::new(Mutex::new(MinimaBuffer::new(800, 10.0, 10.0))), } } } From 3dc049eb3307b47acd92153769b2be4a6f450ba9 Mon Sep 17 00:00:00 2001 From: luna <223230@proton.me> Date: Sun, 21 Apr 2024 00:43:01 +0200 Subject: [PATCH 3/6] Correctly name GR buffer --- src/editor.rs | 19 ++++++++++--------- src/lib.rs | 10 +++++----- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index abbff8a..8647665 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -24,7 +24,7 @@ struct LambData { // peak_meter: Arc, gain_reduction_left: Arc, gain_reduction_right: Arc, - peak_buffer: Arc>, + gr_buffer: Arc>, } impl LambData { @@ -33,14 +33,14 @@ impl LambData { // peak_meter: Arc, gain_reduction_left: Arc, gain_reduction_right: Arc, - peak_buffer: Arc>, + gr_buffer: Arc>, ) -> Self { Self { params, // peak_meter, gain_reduction_left, gain_reduction_right, - peak_buffer, + gr_buffer, } } } @@ -60,7 +60,7 @@ pub(crate) fn create( // peak_meter: Arc, gain_reduction_left: Arc, gain_reduction_right: Arc, - peak_buffer: Arc>, + gr_buffer: Arc>, editor_state: Arc, ) -> Option> { create_vizia_editor(editor_state, ViziaTheming::Custom, move |cx, _| { @@ -76,7 +76,7 @@ pub(crate) fn create( // peak_meter: peak_meter.clone(), gain_reduction_left: gain_reduction_left.clone(), gain_reduction_right: gain_reduction_right.clone(), - peak_buffer: peak_buffer.clone(), + gr_buffer: gr_buffer.clone(), } .build(cx); @@ -407,9 +407,10 @@ fn peak_graph(cx: &mut Context) { ) .color(Color::rgb(60, 60, 60)); - Graph::new(cx, LambData::peak_buffer, (-32.0, 6.0), ValueScaling::Decibels) - .color(Color::rgba(0, 0, 0, 160)) - .background_color(Color::rgba(16, 16, 16, 60)); + Graph::new(cx, LambData::gr_buffer, (-32.0, 0.0), ValueScaling::Decibels) + .color(Color::rgba(160, 0, 0, 160)) + .background_color(Color::rgba(255, 16, 16, 60)) + .should_fill_from_top(true); }) // .background_color(Color::rgb(16, 16, 16)) ; @@ -435,7 +436,7 @@ fn peak_graph(cx: &mut Context) { Meter::new( cx, - LambData::peak_buffer, + LambData::gr_buffer, (-32.0, 6.0), ValueScaling::Decibels, Orientation::Vertical, diff --git a/src/lib.rs b/src/lib.rs index eb7060e..02ce69a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,7 @@ pub struct Lamb { gain_reduction_right: Arc, // These buffers will hold the sample data for the visualizers. - peak_buffer: Arc>, + gr_buffer: Arc>, } impl Default for Lamb { fn default() -> Self { @@ -64,7 +64,7 @@ impl Default for Lamb { temp_output_buffer_gr_l : f64::default_boxed_array::(), temp_output_buffer_gr_r : f64::default_boxed_array::(), sample_rate: 48000.0, - peak_buffer: Arc::new(Mutex::new(MinimaBuffer::new(800, 10.0, 10.0))), + gr_buffer: Arc::new(Mutex::new(MinimaBuffer::new(800, 10.0, 0.0))), } } } @@ -133,7 +133,7 @@ impl Plugin for Lamb { self.sample_rate = buffer_config.sample_rate; - match self.peak_buffer.lock() { + match self.gr_buffer.lock() { Ok(mut buffer) => { buffer.set_sample_rate(buffer_config.sample_rate); } @@ -154,7 +154,7 @@ impl Plugin for Lamb { // self.peak_meter.clone(), self.gain_reduction_left.clone(), self.gain_reduction_right.clone(), - self.peak_buffer.clone(), + self.gr_buffer.clone(), self.params.editor_state.clone(), ) } @@ -234,7 +234,7 @@ impl Plugin for Lamb { ); for i in 0..count as usize { - self.peak_buffer + self.gr_buffer .lock() .unwrap() .enqueue((self.temp_output_buffer_gr_l[i] + self.temp_output_buffer_gr_l[i]) as f32 / 2.0); From ba56e6e0222c09b946f013fa01ab0901ffc3dd2c Mon Sep 17 00:00:00 2001 From: luna <223230@proton.me> Date: Sun, 21 Apr 2024 00:43:34 +0200 Subject: [PATCH 4/6] Appropriately style GR meter --- src/editor.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 8647665..b874097 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -15,6 +15,7 @@ use cyma::{ Graph, Grid, Meter, UnitRuler, }, }; +use cyma::visualizers::GraphModifiers; include!("gain_reduction_meter.rs"); @@ -401,8 +402,8 @@ fn peak_graph(cx: &mut Context) { Grid::new( cx, ValueScaling::Linear, - (-32.0, 8.0), - vec![6.0, 0.0, -6.0, -12.0, -18.0, -24.0, -30.0], + (-32.0, 0.0), + vec![-6.0, -12.0, -18.0, -24.0, -30.0], Orientation::Vertical ) .color(Color::rgb(60, 60, 60)); @@ -417,11 +418,9 @@ fn peak_graph(cx: &mut Context) { UnitRuler::new( cx, - (-32.0, 8.0), + (-32.0, 0.0), ValueScaling::Linear, vec![ - (6.0, "6db"), - (0.0, "0db"), (-6.0, "-6db"), (-12.0, "-12db"), (-18.0, "-18db"), From bd060af600fb19bf0a7cbc4cddde8d65c612c4e2 Mon Sep 17 00:00:00 2001 From: luna <223230@proton.me> Date: Sun, 21 Apr 2024 01:56:49 +0200 Subject: [PATCH 5/6] Fill GR curve from 0dB --- Cargo.lock | 2 +- src/editor.rs | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c6eb00..8626756 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1122,7 +1122,7 @@ dependencies = [ [[package]] name = "cyma" version = "0.1.0" -source = "git+https://github.com/223230/cyma.git#a58addb981374235020a7add0c904b1217f307d3" +source = "git+https://github.com/223230/cyma.git#f62bde104887b4b43d4f81f01624077ba8444e4f" dependencies = [ "lazy_static", "nih_plug", diff --git a/src/editor.rs b/src/editor.rs index b874097..c3ed3d0 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -402,25 +402,26 @@ fn peak_graph(cx: &mut Context) { Grid::new( cx, ValueScaling::Linear, - (-32.0, 0.0), - vec![-6.0, -12.0, -18.0, -24.0, -30.0], + (-32.0, 6.0), + vec![0.0, -6.0, -12.0, -18.0, -24.0, -30.0], Orientation::Vertical ) .color(Color::rgb(60, 60, 60)); - Graph::new(cx, LambData::gr_buffer, (-32.0, 0.0), ValueScaling::Decibels) + Graph::new(cx, LambData::gr_buffer, (-32.0, 6.0), ValueScaling::Decibels) .color(Color::rgba(160, 0, 0, 160)) .background_color(Color::rgba(255, 16, 16, 60)) - .should_fill_from_top(true); + .fill_from(0.0); }) // .background_color(Color::rgb(16, 16, 16)) ; UnitRuler::new( cx, - (-32.0, 0.0), + (-32.0, 6.0), ValueScaling::Linear, vec![ + (-0.0, "0db"), (-6.0, "-6db"), (-12.0, "-12db"), (-18.0, "-18db"), From bdfc6c0142ea35f1a4d12a98fcf63305df7bec78 Mon Sep 17 00:00:00 2001 From: luna <223230@proton.me> Date: Sun, 21 Apr 2024 02:08:35 +0200 Subject: [PATCH 6/6] Re-add output Must've gotten deleted on accident before :/ --- src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 02ce69a..de62146 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -218,6 +218,12 @@ impl Plugin for Lamb { ); let latency_samples = self.dsp.get_param(LATENCY_PI).expect("no latency read") as u32; context.set_latency_samples(latency_samples); + + let output = buffer.as_slice(); + for i in 0..count as usize { + output[0][i] = self.temp_output_buffer_l[i] as f32; + output[1][i] = self.temp_output_buffer_r[i] as f32; + } if self.params.editor_state.is_open() { self.gain_reduction_left.store(