Implement silence detection and „standby“ mode
This commit is contained in:
parent
fc43035f9b
commit
347a95ea8d
|
@ -16,3 +16,6 @@ pub const UDP_SERVER_ADDR: &str = "192.168.23.118:2703";
|
|||
|
||||
pub const FPS_ANIMATION: f32 = SAMP_RATE / SAMPLES_PER_UPDATE as f32;
|
||||
pub const FPS_LEDS: f32 = 60.0;
|
||||
|
||||
// “standby mode” configuration
|
||||
pub const STANDBY_MAX_SILENT_SAMPLES: usize = SAMP_RATE as usize;
|
||||
|
|
16
src/main.rs
16
src/main.rs
|
@ -65,6 +65,9 @@ fn main()
|
|||
// array for samples directly read from stream
|
||||
let mut samples: VecDeque<i16> = VecDeque::with_capacity(config::BLOCK_LEN);
|
||||
|
||||
// counts silent (zero-valued) samples
|
||||
let mut silent_samples: usize = 0;
|
||||
|
||||
// main loop
|
||||
loop {
|
||||
|
||||
|
@ -98,6 +101,19 @@ fn main()
|
|||
{
|
||||
let mut s = sigproc.borrow_mut();
|
||||
s.import_i16_mono_from_iter(samples.iter()).unwrap();
|
||||
|
||||
if s.is_silent() {
|
||||
silent_samples += config::BLOCK_LEN;
|
||||
|
||||
if silent_samples >= config::STANDBY_MAX_SILENT_SAMPLES {
|
||||
// too many silent samples in a row: stop any signal processing until something
|
||||
// else occurs at the input again
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
silent_samples = 0;
|
||||
}
|
||||
|
||||
s.update_fft().unwrap();
|
||||
}
|
||||
|
||||
|
|
|
@ -104,6 +104,11 @@ impl SignalProcessing
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn is_silent(&self) -> bool
|
||||
{
|
||||
return self.fft_input.iter().sum::<f32>() == 0.0;
|
||||
}
|
||||
|
||||
pub fn update_fft(&mut self) -> fftw::error::Result<()>
|
||||
{
|
||||
self.fft_plan.r2c(&mut self.fft_input, &mut self.fft_output)?;
|
||||
|
|
Loading…
Reference in a new issue