diff --git a/src/editor.rs b/src/editor.rs index c3ed3d0..9bc00da 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -8,23 +8,25 @@ use nih_plug_vizia::{assets, create_vizia_editor, ViziaState, ViziaTheming}; use std::sync::atomic::Ordering; use std::sync::{Arc, Mutex}; +use cyma::visualizers::GraphModifiers; use cyma::{ - utils::{MinimaBuffer}, prelude::*, - visualizers::{ - Graph, Grid, Meter, UnitRuler, - }, + utils::{MinimaBuffer, PeakBuffer}, + visualizers::{Graph, Grid, Meter, UnitRuler}, }; -use cyma::visualizers::GraphModifiers; include!("gain_reduction_meter.rs"); +const METER_MIN: f32 = -32.0; +const METER_MAX: f32 = 0.0; + #[derive(Lens, Clone)] struct LambData { params: Arc, // peak_meter: Arc, gain_reduction_left: Arc, gain_reduction_right: Arc, + level_buffer: Arc>, gr_buffer: Arc>, } @@ -34,6 +36,7 @@ impl LambData { // peak_meter: Arc, gain_reduction_left: Arc, gain_reduction_right: Arc, + level_buffer: Arc>, gr_buffer: Arc>, ) -> Self { Self { @@ -41,6 +44,7 @@ impl LambData { // peak_meter, gain_reduction_left, gain_reduction_right, + level_buffer, gr_buffer, } } @@ -61,6 +65,7 @@ pub(crate) fn create( // peak_meter: Arc, gain_reduction_left: Arc, gain_reduction_right: Arc, + level_buffer: Arc>, gr_buffer: Arc>, editor_state: Arc, ) -> Option> { @@ -77,6 +82,7 @@ pub(crate) fn create( // peak_meter: peak_meter.clone(), gain_reduction_left: gain_reduction_left.clone(), gain_reduction_right: gain_reduction_right.clone(), + level_buffer: level_buffer.clone(), gr_buffer: gr_buffer.clone(), } .build(cx); @@ -402,13 +408,19 @@ fn peak_graph(cx: &mut Context) { Grid::new( cx, ValueScaling::Linear, - (-32.0, 6.0), + (METER_MIN, METER_MAX), 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, 6.0), ValueScaling::Decibels) + // level + Graph::new(cx, LambData::level_buffer, (METER_MIN, METER_MAX), ValueScaling::Decibels) + .color(Color::rgba(60, 60, 60, 160)) + .background_color(Color::rgba(60, 60, 60, 60)); + + // gain reduction + Graph::new(cx, LambData::gr_buffer, (METER_MIN, METER_MAX), ValueScaling::Decibels) .color(Color::rgba(160, 0, 0, 160)) .background_color(Color::rgba(255, 16, 16, 60)) .fill_from(0.0); @@ -418,7 +430,7 @@ fn peak_graph(cx: &mut Context) { UnitRuler::new( cx, - (-32.0, 6.0), + (METER_MIN, METER_MAX), ValueScaling::Linear, vec![ (-0.0, "0db"), @@ -433,23 +445,37 @@ fn peak_graph(cx: &mut Context) { .font_size(12.) .color(Color::rgb(160, 160, 160)) .width(Pixels(32.)); - Meter::new( cx, - LambData::gr_buffer, - (-32.0, 6.0), + LambData::level_buffer, + (METER_MIN, METER_MAX), ValueScaling::Decibels, Orientation::Vertical, ) .width(Pixels(32.0)) - .color(Color::rgb(0, 0, 0)) - .background_color(Color::rgb(80, 80, 80)); + .color(Color::rgba(60, 60, 60, 160)) + .background_color(Color::rgba(60, 60, 60, 60)); + + HStack::new(cx, |cx| { + Meter::new( + cx, + LambData::gr_buffer, + (METER_MIN, METER_MAX), + ValueScaling::Decibels, + Orientation::Vertical, + ) + .background_color(Color::rgb(250, 250, 250)) + .color(Color::rgba(160, 0, 0, 160)); + }) + .width(Pixels(32.0)) + .background_color(Color::rgb(251, 195, 195)); + // .background_color(Color::rgba(160, 0, 0, 60)); }) .top(Pixels(13.0)) // .height(Pixels(280.0)) - .height(Pixels(200.0)) - .width(Percentage(100.0)) - .col_between(Pixels(8.)) - .border_color(Color::rgb(80, 80, 80)) - .border_width(Pixels(1.)); + .height(Pixels(200.0)) + .width(Percentage(100.0)) + .col_between(Pixels(8.)) + .border_color(Color::rgb(80, 80, 80)) + .border_width(Pixels(1.)); } diff --git a/src/lib.rs b/src/lib.rs index de62146..3d4a52b 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::{MinimaBuffer, VisualizerBuffer}; +use cyma::utils::{PeakBuffer, MinimaBuffer, VisualizerBuffer}; use default_boxed::DefaultBoxed; @@ -22,7 +22,6 @@ pub struct Lamb { params: Arc, dsp: Box, accum_buffer: TempBuffer, - // gr_buffer: TempBuffer, temp_output_buffer_l: Box<[f64]>, temp_output_buffer_r: Box<[f64]>, temp_output_buffer_gr_l: Box<[f64]>, @@ -42,6 +41,7 @@ pub struct Lamb { gain_reduction_right: Arc, // These buffers will hold the sample data for the visualizers. + level_buffer: Arc>, gr_buffer: Arc>, } impl Default for Lamb { @@ -57,13 +57,13 @@ impl Default for Lamb { dsp: dsp::LambRs::default_boxed(), accum_buffer: TempBuffer::default(), - // gr_buffer: TempBuffer::default(), temp_output_buffer_l : f64::default_boxed_array::(), temp_output_buffer_r : f64::default_boxed_array::(), temp_output_buffer_gr_l : f64::default_boxed_array::(), temp_output_buffer_gr_r : f64::default_boxed_array::(), sample_rate: 48000.0, + level_buffer: Arc::new(Mutex::new(PeakBuffer::new(800, 10.0, 0.0))), gr_buffer: Arc::new(Mutex::new(MinimaBuffer::new(800, 10.0, 0.0))), } } @@ -123,14 +123,6 @@ impl Plugin for Lamb { // function if you do not need it. self.dsp.init(buffer_config.sample_rate as i32); self.accum_buffer.resize(2, MAX_SOUNDCARD_BUFFER_SIZE); - // self.gr_buffer.resize(2, MAX_SOUNDCARD_BUFFER_SIZE); - - // After `PEAK_METER_DECAY_MS` milliseconds of pure silence, the peak meter's value should - // have dropped by 12 dB - // self.peak_meter_decay_weight = 0.25f64 - // .powf((buffer_config.sample_rate as f64 * PEAK_METER_DECAY_MS / 1000.0).recip()) - // as f32; - self.sample_rate = buffer_config.sample_rate; match self.gr_buffer.lock() { @@ -140,6 +132,13 @@ impl Plugin for Lamb { Err(_) => return false, } + match self.level_buffer.lock() { + Ok(mut buffer) => { + buffer.set_sample_rate(buffer_config.sample_rate); + } + Err(_) => return false, + } + true } @@ -154,6 +153,7 @@ impl Plugin for Lamb { // self.peak_meter.clone(), self.gain_reduction_left.clone(), self.gain_reduction_right.clone(), + self.level_buffer.clone(), self.gr_buffer.clone(), self.params.editor_state.clone(), ) @@ -243,7 +243,11 @@ impl Plugin for Lamb { self.gr_buffer .lock() .unwrap() - .enqueue((self.temp_output_buffer_gr_l[i] + self.temp_output_buffer_gr_l[i]) as f32 / 2.0); + .enqueue((self.temp_output_buffer_gr_l[i] + self.temp_output_buffer_gr_r[i]) as f32 / 2.0); + self.level_buffer + .lock() + .unwrap() + .enqueue((self.temp_output_buffer_l[i] + self.temp_output_buffer_r[i]) as f32 / 2.0); } }