meter: extract to_log10, fix types of to_rms

This commit is contained in:
🪞👃🪞 2025-05-11 00:45:48 +03:00
parent 7bc37e7659
commit 6db5df5210
2 changed files with 12 additions and 14 deletions

View file

@ -15,7 +15,7 @@ render!(TuiOut: |self: Meter, to| {
let signal = 100.0 - f32::max(0.0, f32::min(100.0, self.0.abs())); 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 v = (signal * h as f32 / 100.0).ceil() as u16;
let y2 = y + h; let y2 = y + h;
//to.blit(&format!("\r{v} {} {signal}", self.0), x * 20, y, None); //to.blit(&format!("\r{v} {} {signal}", self.0), x * 30, y, Some(Style::default()));
for y in y..(y + v) { for y in y..(y + v) {
for x in x..(x + w) { for x in x..(x + w) {
to.blit(&"", x, y2 - y, Some(Style::default().green())); to.blit(&"", x, y2 - y, Some(Style::default().green()));
@ -23,14 +23,16 @@ render!(TuiOut: |self: Meter, to| {
} }
}); });
pub fn to_rms (samples: &[u32]) -> f32 { pub fn to_rms (samples: &[f32]) -> f32 {
let sum: usize = samples.iter() let sum = samples.iter()
.map(|s|*s as usize) .map(|s|*s)
.reduce(|sum, sample|sum + sample) .reduce(|sum, sample|sum + sample)
.unwrap_or(0); .unwrap_or(0.0);
(sum as f32 / samples.len() as f32).sqrt() (sum / samples.len() as f32).sqrt()
} }
pub fn to_log10 (samples: &[u32]) -> f32 { pub fn to_log10 (samples: &[f32]) -> f32 {
0.0 let total: f32 = samples.iter().map(|x|x.abs()).sum();
let count = samples.len() as f32;
10. * (total / count).log10()
} }

View file

@ -41,9 +41,7 @@ impl Sampler {
for ((input, meter), channel) in samples_with_meters { for ((input, meter), channel) in samples_with_meters {
let slice = input.port().as_slice(scope); let slice = input.port().as_slice(scope);
length = length.max(slice.len()); length = length.max(slice.len());
let total: f32 = slice.iter().map(|x|x.abs()).sum(); *meter = to_log10(slice);
let count = slice.len() as f32;
*meter = 10. * (total / count).log10();
channel.extend_from_slice(slice); channel.extend_from_slice(slice);
} }
sample.end += length; sample.end += length;
@ -53,9 +51,7 @@ impl Sampler {
fn update_input_meters (&mut self, scope: &ProcessScope) { fn update_input_meters (&mut self, scope: &ProcessScope) {
for (input, meter) in self.audio_ins.iter().zip(self.input_meters.iter_mut()) { for (input, meter) in self.audio_ins.iter().zip(self.input_meters.iter_mut()) {
let slice = input.port().as_slice(scope); let slice = input.port().as_slice(scope);
let total: f32 = slice.iter().map(|x|x.abs()).sum(); *meter = to_log10(slice);
let count = slice.len() as f32;
*meter = 10. * (total / count).log10();
} }
} }