Skip to main content

speech_level

Function speech_level 

Source
pub fn speech_level(pcm: &[f32], sample_rate: u32) -> f32
Expand description

The loudest 10 ms window in pcm, the reference “speech level” the tail rule compares against.

Examples found in repository?
examples/tail_writer_probe.rs (line 19)
6fn main() {
7    let path = std::env::args()
8        .nth(1)
9        .expect("usage: tail_writer_probe <wav>");
10    let bytes = std::fs::read(&path).expect("readable wav");
11    let pcm: Vec<f32> = bytes[44..]
12        .as_chunks::<2>()
13        .0
14        .iter()
15        .map(|pair| f32::from(i16::from_le_bytes(*pair)) / 32_767.0)
16        .collect();
17
18    let offline = ftts_core::audio::trailing_noise_samples(&pcm, 24_000);
19    let level = ftts_core::audio::speech_level(&pcm, 24_000);
20    println!("input samples      : {}", pcm.len());
21    println!("offline detector   : {offline} samples");
22    println!("utterance level    : {level:.5}");
23
24    // Exactly the CLI's packetization.
25    let mut writer =
26        ftts_core::audio::WavWriter::new_trimming_tail(Cursor::new(Vec::new()), 24_000)
27            .expect("header");
28    for packet in pcm.chunks(1_920) {
29        writer.write_samples(packet).expect("write");
30    }
31    let out = writer.finish().expect("finish").into_inner();
32    let written = (out.len() - 44) / 2;
33    println!(
34        "writer wrote       : {written} samples (trimmed {})",
35        pcm.len() - written
36    );
37
38    // What the detector sees when handed only the tail, which is the writer's situation.
39    let hold = 24_000 * 250 / 1000;
40    let tail = &pcm[pcm.len().saturating_sub(hold)..];
41    println!(
42        "tail-only relative : {} samples (tail len {})",
43        ftts_core::audio::trailing_noise_samples_relative_to(tail, 24_000, level),
44        tail.len()
45    );
46}