Skip to content

Commit

Permalink
Add histogram to visualizers example.
Browse files Browse the repository at this point in the history
  • Loading branch information
exa04 committed May 31, 2024
1 parent 83568f0 commit 0f24eea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 9 additions & 1 deletion examples/visualizers/src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cyma::prelude::*;
use cyma::{
utils::{PeakBuffer, RingBuffer, SpectrumOutput, WaveformBuffer},
utils::{HistogramBuffer, PeakBuffer, RingBuffer, SpectrumOutput, WaveformBuffer},
visualizers::{
Graph, Grid, Lissajous, LissajousGrid, Meter, Oscilloscope, SpectrumAnalyzer,
SpectrumAnalyzerModifiers, SpectrumAnalyzerVariant, UnitRuler, Waveform,
Expand All @@ -14,6 +14,7 @@ use std::sync::{Arc, Mutex};
pub(crate) struct Data {
pub(crate) oscilloscope_buffer: Arc<Mutex<WaveformBuffer>>,
pub(crate) peak_buffer: Arc<Mutex<PeakBuffer>>,
pub(crate) histogram_buffer: Arc<Mutex<HistogramBuffer>>,
pub(crate) lissajous_buffer: Arc<Mutex<RingBuffer<(f32, f32)>>>,
pub(crate) spectrum: Arc<Mutex<SpectrumOutput>>,

Expand All @@ -24,13 +25,15 @@ impl Data {
pub(crate) fn new(
oscilloscope_buffer: Arc<Mutex<WaveformBuffer>>,
peak_buffer: Arc<Mutex<PeakBuffer>>,
histogram_buffer: Arc<Mutex<HistogramBuffer>>,
lissajous_buffer: Arc<Mutex<RingBuffer<(f32, f32)>>>,
spectrum: Arc<Mutex<SpectrumOutput>>,
waveform: Arc<Mutex<Vec<f32>>>,
) -> Self {
Self {
oscilloscope_buffer,
peak_buffer,
histogram_buffer,
lissajous_buffer,
spectrum,
waveform,
Expand Down Expand Up @@ -190,6 +193,11 @@ fn peak_graph(cx: &mut Context) {
Graph::new(cx, Data::peak_buffer, (-32.0, 8.0), ValueScaling::Decibels)
.color(Color::rgba(255, 255, 255, 160))
.background_color(Color::rgba(255, 255, 255, 60));

Histogram::new(cx, Data::histogram_buffer, (-32.0, 8.0))
.color(Color::rgba(120, 120, 255, 160))
.background_color(Color::rgba(120, 120, 255, 100))
.width(Pixels(120.));
})
.background_color(Color::rgb(16, 16, 16));

Expand Down
18 changes: 16 additions & 2 deletions examples/visualizers/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use cyma::prelude::*;
use cyma::utils::{PeakBuffer, RingBuffer, SpectrumInput, SpectrumOutput, WaveformBuffer};
use cyma::utils::{
HistogramBuffer, PeakBuffer, RingBuffer, SpectrumInput, SpectrumOutput, WaveformBuffer,
};
use nih_plug::prelude::*;
use nih_plug_vizia::ViziaState;
use std::{
Expand All @@ -16,6 +18,7 @@ pub struct VisualizersDemo {
oscilloscope_buffer: Arc<Mutex<WaveformBuffer>>,
peak_buffer: Arc<Mutex<PeakBuffer>>,
lissajous_buffer: Arc<Mutex<RingBuffer<(f32, f32)>>>,
histogram_buffer: Arc<Mutex<HistogramBuffer>>,

spectrum_input: SpectrumInput,
spectrum_output: Arc<Mutex<SpectrumOutput>>,
Expand All @@ -37,6 +40,7 @@ impl Default for VisualizersDemo {
params: Arc::new(DemoParams::default()),
oscilloscope_buffer: Arc::new(Mutex::new(WaveformBuffer::new(800, 5.0))),
peak_buffer: Arc::new(Mutex::new(PeakBuffer::new(800, 10.0, 50.))),
histogram_buffer: Arc::new(Mutex::new(HistogramBuffer::new(512, 1.0))),
lissajous_buffer: Arc::new(Mutex::new(RingBuffer::new(2048))),

spectrum_input,
Expand All @@ -45,7 +49,6 @@ impl Default for VisualizersDemo {
// This is just some dummy data that doesn't change.
waveform: Arc::new(Mutex::new(
(0..256)
.into_iter()
.map(|x| {
let x = 2. * PI * x as f32 / 256.;
0.6 * (x).sin() + 0.3 * (x * 2.).sin() + 0.1 * (x * 4.).sin()
Expand Down Expand Up @@ -98,6 +101,7 @@ impl Plugin for VisualizersDemo {
editor::Data::new(
self.oscilloscope_buffer.clone(),
self.peak_buffer.clone(),
self.histogram_buffer.clone(),
self.lissajous_buffer.clone(),
self.spectrum_output.clone(),
self.waveform.clone(),
Expand All @@ -124,6 +128,12 @@ impl Plugin for VisualizersDemo {
}
Err(_) => return false,
}
match self.histogram_buffer.lock() {
Ok(mut buffer) => {
buffer.set_sample_rate(buffer_config.sample_rate);
}
Err(_) => return false,
}

self.spectrum_input
.update_sample_rate(buffer_config.sample_rate);
Expand All @@ -147,6 +157,10 @@ impl Plugin for VisualizersDemo {
.lock()
.unwrap()
.enqueue_buffer(buffer, None);
self.histogram_buffer
.lock()
.unwrap()
.enqueue_buffer(buffer, None);

if buffer.channels() > 1 {
for mut sample in buffer.iter_samples() {
Expand Down

0 comments on commit 0f24eea

Please sign in to comment.