Skip to content

Commit 442c462

Browse files
committed
Attempt to weave in visualizer2 example to show volume waveform
1 parent 1cb5542 commit 442c462

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

inputmodule-control/src/inputmodule.rs

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

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

0 commit comments

Comments
 (0)