From 347a95ea8dad026f20566d142d092fabb1e5a45c Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sun, 27 Jun 2021 22:01:15 +0200 Subject: [PATCH] =?UTF-8?q?Implement=20silence=20detection=20and=20?= =?UTF-8?q?=E2=80=9Estandby=E2=80=9C=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.rs | 3 +++ src/main.rs | 16 ++++++++++++++++ src/signal_processing.rs | 5 +++++ 3 files changed, 24 insertions(+) diff --git a/src/config.rs b/src/config.rs index ee44588..f1e4323 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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; diff --git a/src/main.rs b/src/main.rs index 8baf502..bee4f99 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,6 +65,9 @@ fn main() // array for samples directly read from stream let mut samples: VecDeque = 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(); } diff --git a/src/signal_processing.rs b/src/signal_processing.rs index b233082..9ba4a23 100644 --- a/src/signal_processing.rs +++ b/src/signal_processing.rs @@ -104,6 +104,11 @@ impl SignalProcessing Ok(()) } + pub fn is_silent(&self) -> bool + { + return self.fft_input.iter().sum::() == 0.0; + } + pub fn update_fft(&mut self) -> fftw::error::Result<()> { self.fft_plan.r2c(&mut self.fft_input, &mut self.fft_output)?;