Skip to content

Commit b2f44db

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

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ pub fn serial_commands(args: &crate::ClapCli) {
242242
random_eq_cmd(&serialdevs);
243243
}
244244

245+
if ledmatrix_args.input_eq {
246+
input_eq_cmd(&serialdevs);
247+
}
248+
245249
if ledmatrix_args.clock {
246250
clock_cmd(&serialdevs);
247251
}
@@ -636,6 +640,81 @@ fn random_eq_cmd(serialdevs: &Vec<String>) {
636640
}
637641
}
638642

643+
644+
// The data-type for storing analyzer results
645+
#[derive(Debug, Clone)]
646+
pub struct AnalyzerResult {
647+
spectrum: vis_core::analyzer::Spectrum<Vec<f32>>,
648+
volume: f32,
649+
beat: f32,
650+
}
651+
652+
// Equalizer-like animation that expands as volume goes up and retracts as it goes down
653+
fn input_eq_cmd(serialdevs: &Vec<String>) {
654+
// Example from https://github.com/Rahix/visualizer2/blob/canon/README.md
655+
656+
// Initialize the logger. Take a look at the sources if you want to customize
657+
// the logger.
658+
vis_core::default_log();
659+
660+
// Load the default config source. More about config later on. You can also
661+
// do this manually if you have special requirements.
662+
vis_core::default_config();
663+
664+
// Initialize some analyzer-tools. These will be moved into the analyzer closure
665+
// later on.
666+
let mut analyzer = vis_core::analyzer::FourierBuilder::new()
667+
.length(512)
668+
.window(vis_core::analyzer::window::nuttall)
669+
.plan();
670+
671+
let spectrum = vis_core::analyzer::Spectrum::new(vec![0.0; analyzer.buckets()], 0.0, 1.0);
672+
673+
let mut frames = vis_core::Visualizer::new(
674+
AnalyzerResult {
675+
spectrum,
676+
volume: 0.0,
677+
beat: 0.0,
678+
},
679+
// This closure is the "analyzer". It will be executed in a loop to always
680+
// have the latest data available.
681+
move |info, samples| {
682+
analyzer.analyze(samples);
683+
684+
info.spectrum.fill_from(&analyzer.average());
685+
info.volume = samples.volume(0.3) * 400.0;
686+
info.beat = info.spectrum.slice(50.0, 100.0).max() * 0.01;
687+
info
688+
},
689+
)
690+
// Build the frame iterator which is the base of your loop later on
691+
.frames();
692+
693+
for frame in frames.iter() {
694+
// This is just a primitive example, your vis core belongs here
695+
696+
frame.info(|info| {
697+
for serialdev in serialdevs {
698+
eq_cmd(
699+
serialdev,
700+
&[
701+
info.volume as u8,
702+
info.volume as u8,
703+
info.volume as u8,
704+
info.volume as u8,
705+
info.volume as u8,
706+
info.volume as u8,
707+
info.volume as u8,
708+
info.volume as u8,
709+
info.volume as u8,
710+
],
711+
)
712+
}
713+
});
714+
thread::sleep(Duration::from_millis(30));
715+
}
716+
}
717+
639718
/// Display 9 values in equalizer diagram starting from the middle, going up and down
640719
/// TODO: Implement a commandline parameter for this
641720
fn eq_cmd(serialdev: &str, vals: &[u8]) {

inputmodule-control/src/ledmatrix.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ pub struct LedMatrixSubcommand {
8686
#[arg(long)]
8787
pub random_eq: bool,
8888

89+
/// Input EQ
90+
#[arg(long)]
91+
pub input_eq: bool,
92+
8993
/// EQ with custom values
9094
#[arg(long, num_args(9))]
9195
pub eq: Option<Vec<u8>>,

0 commit comments

Comments
 (0)