Skip to content

Commit

Permalink
Add dispatchers for Graph, Meter and Oscilloscope
Browse files Browse the repository at this point in the history
  • Loading branch information
exa04 committed Jan 4, 2025
1 parent b3587b1 commit 011df7d
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 356 deletions.
76 changes: 38 additions & 38 deletions examples/visualizers/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,49 +86,49 @@ pub(crate) fn create(editor_data: Data, editor_state: Arc<ViziaState>) -> Option
.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,
// Data::bus,
// )
// .background_color(Color::rgba(255, 92, 92, 50));
// Meter::peak(
// cx,
// 400.0,
// (-32.0, 8.0),
// ValueScaling::Decibels,
// Orientation::Vertical,
// Data::bus,
// )
// .background_color(Color::rgba(255, 255, 255, 30));
// Meter::peak(
// cx,
// 800.0,
// (-32.0, 8.0),
// ValueScaling::Decibels,
// Orientation::Vertical,
// Data::bus,
// )
// .color(Color::rgba(255, 255, 255, 120));
// })
// .background_color(Color::rgb(8, 8, 8))
// .width(Pixels(24.0));
ZStack::new(cx, |cx| {
Meter::rms(
cx,
Data::bus,
800.0,
(-32.0, 8.0),
ValueScaling::Decibels,
Orientation::Vertical,
)
.background_color(Color::rgba(255, 92, 92, 50));
Meter::peak(
cx,
Data::bus,
400.0,
(-32.0, 8.0),
ValueScaling::Decibels,
Orientation::Vertical,
)
.background_color(Color::rgba(255, 255, 255, 30));
Meter::peak(
cx,
Data::bus,
800.0,
(-32.0, 8.0),
ValueScaling::Decibels,
Orientation::Vertical,
)
.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))
.border_color(Color::rgb(48, 48, 48));

// HStack::new(cx, |cx| {
// Oscilloscope::new(cx, 10.0, (-1.0, 1.0), ValueScaling::Linear, Data::bus)
// .color(Color::rgba(255, 255, 255, 120));
// })
// .background_color(Color::rgb(16, 16, 16))
// .border_width(Pixels(1.0))
// .border_color(Color::rgb(48, 48, 48));
HStack::new(cx, |cx| {
Oscilloscope::new(cx, Data::bus, 10.0, (-1.0, 1.0), ValueScaling::Linear)
.color(Color::rgba(255, 255, 255, 120));
})
.background_color(Color::rgb(16, 16, 16))
.border_width(Pixels(1.0))
.border_color(Color::rgb(48, 48, 48));

Label::new(
cx,
Expand Down
4 changes: 2 additions & 2 deletions examples/visualizers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cyma::prelude::*;
use nih_plug::prelude::*;
use nih_plug_vizia::ViziaState;
use std::sync::{Arc, Mutex};
use std::sync::Arc;

mod editor;

Expand Down Expand Up @@ -86,7 +86,7 @@ impl Plugin for VisualizersPlugin {
_: &mut impl ProcessContext<Self>,
) -> ProcessStatus {
if self.params.editor_state.is_open() {
self.bus.send_buffer(buffer);
self.bus.send_buffer_summing(buffer);
}
ProcessStatus::Normal
}
Expand Down
115 changes: 44 additions & 71 deletions src/bus/into_bus.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,44 @@
// use core::slice;
// use nih_plug::prelude::AtomicF32;
// use std::{
// iter::{Copied, Map},
// sync::{atomic::Ordering, Arc},
// };

// use super::*;

// pub struct IntoMonoBus<const C: usize, D>
// where
// for<'a> D: FnMut([f32; C]) -> f32 + 'static + Copy + Clone,
// {
// dispatchers: DispatcherList<C>,
// sample_rate: Arc<AtomicF32>,
// downmixer: D,
// }

// impl<const C: usize, D> IntoMonoBus<C, D>
// where
// for<'a> D: FnMut([f32; C]) -> f32 + 'static + Copy + Clone,
// {
// pub fn new(dispatchers: DispatcherList<C>, sample_rate: Arc<AtomicF32>, downmixer: D) -> Self {
// Self {
// dispatchers,
// sample_rate,
// downmixer,
// }
// }
// }

// impl<const C: usize, D> Bus for IntoMonoBus<C, D>
// where
// for<'a> D: FnMut([f32; C]) -> f32 + 'static + Copy + Clone,
// {
// type Input = f32;
// type InputIter<'a> = slice::Iter<'a, f32>;
// type Output = [f32; C];
// type OutputIter<'a> = slice::Iter<'a, [f32; C]>;

// fn set_sample_rate(&self, sample_rate: f32) {
// self.sample_rate.store(sample_rate, Ordering::Relaxed);
// }

// fn sample_rate(&self) -> f32 {
// self.sample_rate.load(Ordering::Relaxed)
// }

// fn update(&self) {
// // TODO
// }

// fn register_dispatcher<F>(&self, action: F) -> Rc<dyn for<'a> Fn(Self::OutputIter<'a>)>
// where
// F: for<'a> Fn(Self::InputIter<'a>) + 'static,
// {
// let mut downmix = self.downmixer.clone();

// let dispatcher: Rc<dyn for<'a> Fn(Self::OutputIter<'a>)> = Rc::new(move |data| {
// let mapped = data.copied().map(move |x| downmix(x)).collect::<Vec<_>>();
// action(mapped.iter())
// });

// self.dispatchers
// .write()
// .unwrap()
// .push(Rc::downgrade(&dispatcher));

// dispatcher
// }
// }
use core::slice;
use std::sync::Arc;

use super::*;

#[derive(Clone)]
pub struct IntoMonoBus<const C: usize, D>
where
for<'a> D: Fn([f32; C]) -> f32 + 'static + Copy + Clone,
{
pub(crate) bus: MultiChannelBus<C>,
pub(crate) downmixer: D,
}

impl<const C: usize, D> Bus<f32> for IntoMonoBus<C, D>
where
for<'a> D: Fn([f32; C]) -> f32 + 'static + Copy + Clone,
{
type I<'a> = slice::Iter<'a, f32>;
type O<'a> = <MultiChannelBus<C> as Bus<[f32; C]>>::I<'a>;

fn register_dispatcher<F: for<'a> Fn(Self::I<'a>) + Sync + Send + 'static>(
&self,
dispatcher: F,
) -> Arc<dyn for<'a> Fn(Self::O<'a>) + Sync + Send> {
self.bus.register_dispatcher(move |samples| {
let mono_samples = samples.map(|x| x.into_iter().sum::<f32>() / C as f32);
})
}

fn update(&self) {
self.bus.update()
}

#[inline]
fn set_sample_rate(&self, sample_rate: f32) {
self.bus.set_sample_rate(sample_rate)
}

#[inline]
fn sample_rate(&self) -> f32 {
self.bus.sample_rate()
}
}
18 changes: 10 additions & 8 deletions src/bus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use std::{
rc::{Rc, Weak},
sync::{Arc, RwLock},
};
use std::sync::Arc;

mod into_bus;
mod mono;
mod multichannel;

pub use into_bus::*;
pub use mono::*;
pub use multichannel::*;

pub type StereoBus = MultiChannelBus<2>;

pub trait Bus<T: Clone + Copy + Sized + 'static>: Clone {
type I<'a>: Iterator<Item = &'a T>;
type O<'a>: Iterator;

fn set_sample_rate(&self, sample_rate: f32);
fn sample_rate(&self) -> f32;
fn update(&self);
fn register_dispatcher<F: for<'a> Fn(Self::I<'a>) + Sync + Send + 'static>(
&self,
dispatcher: F,
) -> Arc<dyn for<'a> Fn(Self::I<'a>) + Sync + Send>;
fn update(&self);
fn set_sample_rate(&self, sample_rate: f32);
fn sample_rate(&self) -> f32;
) -> Arc<dyn for<'a> Fn(Self::O<'a>) + Sync + Send>;
}
9 changes: 7 additions & 2 deletions src/bus/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Default for MonoBus {

impl MonoBus {
#[inline]
pub fn send_buffer(&self, buffer: &mut Buffer) {
pub fn send_buffer_summing(&self, buffer: &mut Buffer) {
let channels = buffer.channels();

if channels == 1 {
Expand All @@ -53,8 +53,11 @@ impl MonoBus {
}
}

// TODO Cleanup automatically

impl Bus<f32> for MonoBus {
type I<'a> = slice::Iter<'a, f32>;
type O<'a> = Self::I<'a>;

fn set_sample_rate(&self, sample_rate: f32) {
self.sample_rate
Expand All @@ -68,7 +71,9 @@ impl Bus<f32> for MonoBus {
fn update(&self) {
let samples = self.channel.1.try_iter().collect::<Vec<_>>();

if samples.is_empty() { return; }
if samples.is_empty() {
return;
}

self.dispatchers
.read()
Expand Down
Loading

0 comments on commit 011df7d

Please sign in to comment.