From 4aa12943714cf6b8d1ae996af424b58c7368e5d8 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Thu, 11 Feb 2021 23:13:09 +0100 Subject: change noise gate to be dynamig instead of static --- mumd/src/audio.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 598dde6..88eb601 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -210,7 +210,6 @@ impl Audio { StreamingSignalExt::into_interleaved_samples( StreamingNoiseGate::new( from_interleaved_samples_stream::<_, f32>(sample_receiver), //TODO group frames correctly - 0.09, 10_000))).enumerate().map(|(i, e)| VoicePacket::Audio { _dst: std::marker::PhantomData, target: 0, // normal speech @@ -408,21 +407,20 @@ fn get_default_sfx() -> Cow<'static, [u8]> { struct StreamingNoiseGate { open: usize, signal: S, - activate_threshold: <::Sample as Sample>::Float, deactivation_delay: usize, + alltime_high: <::Sample as Sample>::Float, } impl StreamingNoiseGate { pub fn new( signal: S, - activate_threshold: <::Sample as Sample>::Float, deactivation_delay: usize, ) -> StreamingNoiseGate { Self { open: 0, signal, - activate_threshold, - deactivation_delay + deactivation_delay, + alltime_high: <<::Sample as Sample>::Float as Sample>::EQUILIBRIUM, } } } @@ -435,6 +433,8 @@ impl StreamingSignal for StreamingNoiseGate type Frame = S::Frame; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + const MUTE_PERCENTAGE: f32 = 0.1; + let s = self.get_mut(); let frame = match S::poll_next(Pin::new(&mut s.signal), cx) { @@ -442,14 +442,18 @@ impl StreamingSignal for StreamingNoiseGate Poll::Pending => return Poll::Pending, }; + if let Some(highest) = frame.to_float_frame().channels().find(|e| abs(e.clone()) > s.alltime_high) { + s.alltime_high = highest; + } + match s.open { 0 => { - if frame.to_float_frame().channels().any(|e| abs(e) >= s.activate_threshold) { + if frame.to_float_frame().channels().any(|e| abs(e) >= s.alltime_high.mul_amp(MUTE_PERCENTAGE.to_sample())) { s.open = s.deactivation_delay; } } _ => { - if frame.to_float_frame().channels().any(|e| abs(e) >= s.activate_threshold) { + if frame.to_float_frame().channels().any(|e| abs(e) >= s.alltime_high.mul_amp(MUTE_PERCENTAGE.to_sample())) { s.open = s.deactivation_delay; } else { s.open -= 1; -- cgit v1.2.1