Skip to content

Commit

Permalink
add level graph
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Apr 21, 2024
1 parent 62aff9b commit 9aa6d10
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
62 changes: 44 additions & 18 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LambParams>,
// peak_meter: Arc<AtomicF32>,
gain_reduction_left: Arc<AtomicF32>,
gain_reduction_right: Arc<AtomicF32>,
level_buffer: Arc<Mutex<PeakBuffer>>,
gr_buffer: Arc<Mutex<MinimaBuffer>>,
}

Expand All @@ -34,13 +36,15 @@ impl LambData {
// peak_meter: Arc<AtomicF32>,
gain_reduction_left: Arc<AtomicF32>,
gain_reduction_right: Arc<AtomicF32>,
level_buffer: Arc<Mutex<PeakBuffer>>,
gr_buffer: Arc<Mutex<MinimaBuffer>>,
) -> Self {
Self {
params,
// peak_meter,
gain_reduction_left,
gain_reduction_right,
level_buffer,
gr_buffer,
}
}
Expand All @@ -61,6 +65,7 @@ pub(crate) fn create(
// peak_meter: Arc<AtomicF32>,
gain_reduction_left: Arc<AtomicF32>,
gain_reduction_right: Arc<AtomicF32>,
level_buffer: Arc<Mutex<PeakBuffer>>,
gr_buffer: Arc<Mutex<MinimaBuffer>>,
editor_state: Arc<ViziaState>,
) -> Option<Box<dyn Editor>> {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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"),
Expand All @@ -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.));
}
28 changes: 16 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,7 +22,6 @@ pub struct Lamb {
params: Arc<LambParams>,
dsp: Box<dsp::LambRs>,
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]>,
Expand All @@ -42,6 +41,7 @@ pub struct Lamb {
gain_reduction_right: Arc<AtomicF32>,

// These buffers will hold the sample data for the visualizers.
level_buffer: Arc<Mutex<PeakBuffer>>,
gr_buffer: Arc<Mutex<MinimaBuffer>>,
}
impl Default for Lamb {
Expand All @@ -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::<MAX_SOUNDCARD_BUFFER_SIZE>(),
temp_output_buffer_r : f64::default_boxed_array::<MAX_SOUNDCARD_BUFFER_SIZE>(),
temp_output_buffer_gr_l : f64::default_boxed_array::<MAX_SOUNDCARD_BUFFER_SIZE>(),
temp_output_buffer_gr_r : f64::default_boxed_array::<MAX_SOUNDCARD_BUFFER_SIZE>(),
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))),
}
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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
}

Expand All @@ -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(),
)
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 9aa6d10

Please sign in to comment.