use crate::FRAME_MS;
pub const SPEECH_LEVEL: f32 = 0.15;
pub const SPEECH_HANGOVER_MS: u32 = 400;
#[derive(Default)]
pub struct SpeechGate {
hang: u32,
}
impl SpeechGate {
pub fn open(&mut self, level: f32) -> bool {
if level > SPEECH_LEVEL {
self.hang = SPEECH_HANGOVER_MS / FRAME_MS;
} else {
self.hang = self.hang.saturating_sub(1);
}
self.hang > 0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn speech_opens_the_gate_at_once_and_a_hangover_closes_it() {
let mut gate = SpeechGate::default();
assert!(!gate.open(0.0), "silence sends nothing");
assert!(!gate.open(SPEECH_LEVEL), "the threshold itself is not speech");
assert!(gate.open(0.4), "the first loud frame goes out");
for _ in 1..SPEECH_HANGOVER_MS / FRAME_MS {
assert!(gate.open(0.0), "a pause shorter than the hangover keeps sending");
}
assert!(!gate.open(0.0), "the hangover ends");
assert!(gate.open(0.3), "speech again reopens it");
}
}