sampler, meter: switch to rms; reenable viewer

This commit is contained in:
🪞👃🪞 2025-05-11 01:28:05 +03:00
parent 6db5df5210
commit b9c101081b
5 changed files with 45 additions and 31 deletions

View file

@ -8,14 +8,14 @@ pub enum MeteringMode {
}
#[derive(Debug, Default, Clone)]
pub struct Meter(pub f32);
pub struct Log10Meter(pub f32);
render!(TuiOut: |self: Meter, to| {
render!(TuiOut: |self: Log10Meter, to| {
let [x, y, w, h] = to.area();
let signal = 100.0 - f32::max(0.0, f32::min(100.0, self.0.abs()));
let v = (signal * h as f32 / 100.0).ceil() as u16;
let y2 = y + h;
//to.blit(&format!("\r{v} {} {signal}", self.0), x * 30, y, Some(Style::default()));
//to.blit(&format!("\r{v} {} {signal}", self.0), x * 20, y, None);
for y in y..(y + v) {
for x in x..(x + w) {
to.blit(&"", x, y2 - y, Some(Style::default().green()));
@ -23,16 +23,32 @@ render!(TuiOut: |self: Meter, to| {
}
});
pub fn to_rms (samples: &[f32]) -> f32 {
let sum = samples.iter()
.map(|s|*s)
.reduce(|sum, sample|sum + sample)
.unwrap_or(0.0);
(sum / samples.len() as f32).sqrt()
}
pub fn to_log10 (samples: &[f32]) -> f32 {
let total: f32 = samples.iter().map(|x|x.abs()).sum();
let count = samples.len() as f32;
10. * (total / count).log10()
}
#[derive(Debug, Default, Clone)]
pub struct RmsMeter(pub f32);
render!(TuiOut: |self: RmsMeter, to| {
let [x, y, w, h] = to.area();
let signal = f32::max(0.0, f32::min(100.0, self.0.abs()));
let v = (signal * h as f32).ceil() as u16;
let y2 = y + h;
//to.blit(&format!("\r{v} {} {signal}", self.0), x * 30, y, Some(Style::default()));
for y in y..(y + v) {
for x in x..(x + w) {
to.blit(&"", x, y2.saturating_sub(y), Some(Style::default().green()));
}
}
});
pub fn to_rms (samples: &[f32]) -> f32 {
let sum = samples.iter()
.map(|s|*s)
.reduce(|sum, sample|sum + sample.abs())
.unwrap_or(0.0);
(sum / samples.len() as f32).sqrt()
}