Skip to content

Commit e2459b3

Browse files
committed
Attempt to weave in visualizer2 example to show volume waveform
1 parent d89cede commit e2459b3

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

inputmodule-control/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ serialport = "4.2.0"
1111
image = "0.24.5"
1212
rand = "0.8.5"
1313
chrono = "0.4.23"
14+
15+
vis-core = { git = "https://github.com/Rahix/visualizer2.git" }

inputmodule-control/src/inputmodule.rs

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -617,22 +617,80 @@ fn display_gray_image_cmd(serialdev: &str, image_path: &str) {
617617
commit_cols(&mut port);
618618
}
619619

620+
// The data-type for storing analyzer results
621+
#[derive(Debug, Clone)]
622+
pub struct AnalyzerResult {
623+
spectrum: vis_core::analyzer::Spectrum<Vec<f32>>,
624+
volume: f32,
625+
beat: f32,
626+
}
627+
620628
/// Display an equlizer looking animation with random values.
621629
fn random_eq_cmd(serialdevs: &Vec<String>) {
622-
loop {
623-
// Lower values more likely, makes it look nicer
624-
//weights = [i*i for i in range(33, 0, -1)]
625-
let population: Vec<u8> = (1..34).collect();
626-
let mut rng = thread_rng();
627-
let vals = population
628-
.choose_multiple_weighted(&mut rng, 9, |item| (34 - item) ^ 2)
629-
.unwrap()
630-
.copied()
631-
.collect::<Vec<_>>();
632-
for serialdev in serialdevs {
633-
eq_cmd(serialdev, vals.as_slice());
634-
}
635-
thread::sleep(Duration::from_millis(200));
630+
// Example from https://github.com/Rahix/visualizer2/blob/canon/README.md
631+
632+
// Initialize the logger. Take a look at the sources if you want to customize
633+
// the logger.
634+
vis_core::default_log();
635+
636+
// Load the default config source. More about config later on. You can also
637+
// do this manually if you have special requirements.
638+
vis_core::default_config();
639+
640+
// Initialize some analyzer-tools. These will be moved into the analyzer closure
641+
// later on.
642+
let mut analyzer = vis_core::analyzer::FourierBuilder::new()
643+
.length(512)
644+
.window(vis_core::analyzer::window::nuttall)
645+
.plan();
646+
647+
let spectrum = vis_core::analyzer::Spectrum::new(vec![0.0; analyzer.buckets()], 0.0, 1.0);
648+
649+
let mut frames = vis_core::Visualizer::new(
650+
AnalyzerResult {
651+
spectrum,
652+
volume: 0.0,
653+
beat: 0.0,
654+
},
655+
// This closure is the "analyzer". It will be executed in a loop to always
656+
// have the latest data available.
657+
move |info, samples| {
658+
analyzer.analyze(samples);
659+
660+
info.spectrum.fill_from(&analyzer.average());
661+
info.volume = samples.volume(0.3) * 400.0;
662+
info.beat = info.spectrum.slice(50.0, 100.0).max() * 0.01;
663+
info
664+
},
665+
)
666+
// Build the frame iterator which is the base of your loop later on
667+
.frames();
668+
669+
for frame in frames.iter() {
670+
// This is just a primitive example, your vis core belongs here
671+
672+
frame.info(|info| {
673+
// for n in 0..info.volume as usize {
674+
// print!("{}",n);
675+
// }
676+
for serialdev in serialdevs {
677+
eq_cmd(
678+
serialdev,
679+
&[
680+
info.volume as u8,
681+
info.volume as u8,
682+
info.volume as u8,
683+
info.volume as u8,
684+
info.volume as u8,
685+
info.volume as u8,
686+
info.volume as u8,
687+
info.volume as u8,
688+
info.volume as u8,
689+
],
690+
)
691+
}
692+
});
693+
thread::sleep(Duration::from_millis(30));
636694
}
637695
}
638696

0 commit comments

Comments
 (0)