initial commit w/ working waveform from default stereo audio output monitor capture via pw

This commit is contained in:
ao kami 2025-07-13 17:22:52 +02:00
commit b7c7d1c5c9
5 changed files with 356 additions and 0 deletions

38
src/utils.rs Normal file
View file

@ -0,0 +1,38 @@
pub fn update_waveform( rgba: &mut crate::Rgba, samples: (&Vec<f32>, &Vec<f32>) ) {
let (left_samples, right_samples) = samples;
rgba.pixels.fill(255);
let left_column_width : usize = left_samples.len() / rgba.width as usize;
let right_column_width : usize = right_samples.len() / rgba.width as usize;
let mut last_rows = ((rgba.height/2) as usize, (rgba.height/2) as usize);
for x in 0..rgba.width as usize {
// LEFT CHANNEL
let column_values : &[f32] = &left_samples[x*left_column_width..(x+1)*left_column_width];
let column_average : f32 = (column_values.iter().cloned().reduce(|acc, v| acc+v).unwrap_or(0f32) / column_values.len() as f32).clamp(-0.99, 0.99);
let row = (128f32 - (column_average * 128f32)).round() as usize;
// Draw vertical lines from last row
let range : Vec<usize> = if row <= last_rows.0 {(row..=last_rows.0).collect()} else {(last_rows.0..=row).rev().collect()};
for row in range {
let coord = (row * rgba.width as usize * 4) + (x * 4);
rgba.pixels[coord+0] = 0;
rgba.pixels[coord+1] = 0;
}
last_rows.0 = row;
// RIGHT CHANNEL
let column_values : &[f32] = &right_samples[x*right_column_width..(x+1)*right_column_width];
let column_average : f32 = (column_values.iter().cloned().reduce(|acc, v| acc+v).unwrap_or(0f32) / column_values.len() as f32).clamp(-0.99, 0.99);
let row = (128f32 - (column_average * 128f32)).round() as usize;
// Draw vertical lines from last row
let range : Vec<usize> = if row <= last_rows.1 {(row..=last_rows.1).collect()} else {(last_rows.1..=row).rev().collect()};
for row in range {
let coord = (row * rgba.width as usize * 4) + (x * 4);
rgba.pixels[coord+1] = 0;
rgba.pixels[coord+2] = 0;
}
last_rows.1 = row;
}
}