Skip to content

Commit

Permalink
Accumulators progress
Browse files Browse the repository at this point in the history
  • Loading branch information
exa04 committed Dec 17, 2024
1 parent 2f296ed commit c0c464b
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 201 deletions.
42 changes: 37 additions & 5 deletions examples/visualizers/src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use cyma::prelude::*;
use cyma::utils::accumulators::{PeakAccumulator, RMSAccumulator};
use cyma::utils::MonoChannel;
use nih_plug::editor::Editor;
use nih_plug_vizia::widgets::ResizeHandle;
Expand Down Expand Up @@ -42,19 +41,21 @@ pub(crate) fn create(
.border_width(Pixels(0.5))
.color(Color::rgb(30, 30, 30));

Graph::new(
Graph::peak(
cx,
PeakAccumulator::new(10.0, 50.0),
10.0,
50.0,
(-32.0, 8.0),
ValueScaling::Decibels,
channel.clone(),
)
.color(Color::rgba(255, 255, 255, 60))
.background_color(Color::rgba(255, 255, 255, 30));

Graph::new(
Graph::rms(
cx,
RMSAccumulator::new(10.0, 250.0),
10.0,
250.0,
(-32.0, 8.0),
ValueScaling::Decibels,
channel.clone(),
Expand All @@ -81,6 +82,37 @@ pub(crate) fn create(
.right(Pixels(8.0))
.left(Stretch(1.0));
});
ZStack::new(cx, |cx| {
Meter::rms(
cx,
800.0,
(-32.0, 8.0),
ValueScaling::Decibels,
Orientation::Vertical,
channel.clone(),
)
.background_color(Color::rgba(64, 128, 255, 50));
Meter::peak(
cx,
160.0,
(-32.0, 8.0),
ValueScaling::Decibels,
Orientation::Vertical,
channel.clone(),
)
.background_color(Color::rgba(255, 255, 255, 30));
Meter::peak(
cx,
500.0,
(-32.0, 8.0),
ValueScaling::Decibels,
Orientation::Vertical,
channel.clone(),
)
.color(Color::rgba(255, 255, 255, 120));
})
.background_color(Color::rgb(8, 8, 8))
.width(Pixels(24.0));
})
.background_color(Color::rgb(16, 16, 16))
.border_width(Pixels(1.0))
Expand Down
29 changes: 24 additions & 5 deletions src/utils/accumulators.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::utils::RingBuffer;

pub trait ValueAccumulator {
pub trait Accumulator {
#[inline]
fn accumulate(&mut self, sample: f32) -> Option<f32>;
fn prev(&self) -> f32;
#[inline]
fn set_sample_rate(&mut self, sample_rate: f32);
#[inline]
Expand Down Expand Up @@ -57,7 +58,7 @@ impl PeakAccumulator {
}
}

impl ValueAccumulator for PeakAccumulator {
impl Accumulator for PeakAccumulator {
fn accumulate(&mut self, sample: f32) -> Option<f32> {
self.max_acc = self.max_acc.max(sample.abs());
self.t += 1.0;
Expand All @@ -82,6 +83,10 @@ impl ValueAccumulator for PeakAccumulator {
}
}

fn prev(&self) -> f32 {
self.prev
}

fn set_sample_rate(&mut self, sample_rate: f32) {
self.sample_rate = sample_rate;
self.update();
Expand Down Expand Up @@ -131,7 +136,7 @@ impl MinimumAccumulator {
}
}

impl ValueAccumulator for MinimumAccumulator {
impl Accumulator for MinimumAccumulator {
fn accumulate(&mut self, sample: f32) -> Option<f32> {
self.min_acc = self.min_acc.min(sample.abs());
self.t += 1.0;
Expand All @@ -156,6 +161,10 @@ impl ValueAccumulator for MinimumAccumulator {
}
}

fn prev(&self) -> f32 {
self.prev
}

fn set_sample_rate(&mut self, sample_rate: f32) {
self.sample_rate = sample_rate;
self.update();
Expand All @@ -170,6 +179,7 @@ impl ValueAccumulator for MinimumAccumulator {
pub struct RMSAccumulator {
duration: f32,
rms_window: f32,
prev: f32,

size: usize,
sample_rate: f32,
Expand All @@ -184,6 +194,7 @@ impl RMSAccumulator {
Self {
duration,
rms_window,
prev: 0.0,

size: 1,
sample_delta: 0.0,
Expand All @@ -203,7 +214,7 @@ impl RMSAccumulator {
}
}

impl ValueAccumulator for RMSAccumulator {
impl Accumulator for RMSAccumulator {
fn accumulate(&mut self, sample: f32) -> Option<f32> {
let squared_value = sample * sample;

Expand All @@ -217,12 +228,20 @@ impl ValueAccumulator for RMSAccumulator {
let rms = (self.sum_acc / self.squared_buffer.len() as f32).sqrt();
self.t += self.sample_delta;

Some(if rms.is_nan() { 0.0 } else { rms })
let value = if rms.is_nan() { 0.0 } else { rms };

self.prev = value;

Some(value)
} else {
None
}
}

fn prev(&self) -> f32 {
self.prev
}

fn set_sample_rate(&mut self, sample_rate: f32) {
self.sample_rate = sample_rate;
self.update();
Expand Down
78 changes: 0 additions & 78 deletions src/utils/buffers/mod.rs

This file was deleted.

102 changes: 0 additions & 102 deletions src/utils/buffers/peak_buffer.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Generic utility functions and structures.
pub mod accumulators;
pub mod buffers;
mod channel;
mod ring_buffer;
mod spectrum;
Expand Down
Loading

0 comments on commit c0c464b

Please sign in to comment.