mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
meter: extract to_log10, fix types of to_rms
This commit is contained in:
parent
7bc37e7659
commit
6db5df5210
2 changed files with 12 additions and 14 deletions
|
|
@ -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 v = (signal * h as f32 / 100.0).ceil() as u16;
|
||||
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 x in x..(x + w) {
|
||||
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 {
|
||||
let sum: usize = samples.iter()
|
||||
.map(|s|*s as usize)
|
||||
pub fn to_rms (samples: &[f32]) -> f32 {
|
||||
let sum = samples.iter()
|
||||
.map(|s|*s)
|
||||
.reduce(|sum, sample|sum + sample)
|
||||
.unwrap_or(0);
|
||||
(sum as f32 / samples.len() as f32).sqrt()
|
||||
.unwrap_or(0.0);
|
||||
(sum / samples.len() as f32).sqrt()
|
||||
}
|
||||
|
||||
pub fn to_log10 (samples: &[u32]) -> f32 {
|
||||
0.0
|
||||
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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,9 +41,7 @@ impl Sampler {
|
|||
for ((input, meter), channel) in samples_with_meters {
|
||||
let slice = input.port().as_slice(scope);
|
||||
length = length.max(slice.len());
|
||||
let total: f32 = slice.iter().map(|x|x.abs()).sum();
|
||||
let count = slice.len() as f32;
|
||||
*meter = 10. * (total / count).log10();
|
||||
*meter = to_log10(slice);
|
||||
channel.extend_from_slice(slice);
|
||||
}
|
||||
sample.end += length;
|
||||
|
|
@ -53,9 +51,7 @@ impl Sampler {
|
|||
fn update_input_meters (&mut self, scope: &ProcessScope) {
|
||||
for (input, meter) in self.audio_ins.iter().zip(self.input_meters.iter_mut()) {
|
||||
let slice = input.port().as_slice(scope);
|
||||
let total: f32 = slice.iter().map(|x|x.abs()).sum();
|
||||
let count = slice.len() as f32;
|
||||
*meter = 10. * (total / count).log10();
|
||||
*meter = to_log10(slice);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue