From be76c2aa51733a0cf495e92659fbcbe527f41149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 6 Jun 2021 23:26:24 +0200 Subject: cargo fmt --- mumd/src/audio/noise_gate.rs | 133 ++++++++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 59 deletions(-) (limited to 'mumd/src/audio/noise_gate.rs') diff --git a/mumd/src/audio/noise_gate.rs b/mumd/src/audio/noise_gate.rs index 824ffe0..bd1a262 100644 --- a/mumd/src/audio/noise_gate.rs +++ b/mumd/src/audio/noise_gate.rs @@ -1,5 +1,5 @@ use dasp_frame::Frame; -use dasp_sample::{SignedSample, ToSample, Sample}; +use dasp_sample::{Sample, SignedSample, ToSample}; use dasp_signal::Signal; use futures_util::stream::Stream; use opus::Channels; @@ -17,10 +17,7 @@ pub struct StreamingNoiseGate { } impl StreamingNoiseGate { - pub fn new( - signal: S, - deactivation_delay: usize, - ) -> StreamingNoiseGate { + pub fn new(signal: S, deactivation_delay: usize) -> StreamingNoiseGate { Self { open: 0, signal, @@ -31,10 +28,11 @@ impl StreamingNoiseGate { } impl StreamingSignal for StreamingNoiseGate - where - S: StreamingSignal + Unpin, - FloatSample: Unpin, - ::Frame: Unpin { +where + S: StreamingSignal + Unpin, + FloatSample: Unpin, + ::Frame: Unpin, +{ type Frame = S::Frame; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { @@ -47,18 +45,30 @@ 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) { + 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.alltime_high.mul_amp(MUTE_PERCENTAGE.to_sample())) { + 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.alltime_high.mul_amp(MUTE_PERCENTAGE.to_sample())) { + 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; @@ -98,8 +108,9 @@ pub trait StreamingSignal { } impl StreamingSignal for S - where - S: Signal + Unpin { +where + S: Signal + Unpin, +{ type Frame = S::Frame; fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll { @@ -109,23 +120,24 @@ impl StreamingSignal for S pub trait StreamingSignalExt: StreamingSignal { fn next(&mut self) -> Next<'_, Self> { - Next { - stream: self - } + Next { stream: self } } fn into_interleaved_samples(self) -> IntoInterleavedSamples - where - Self: Sized { - IntoInterleavedSamples { signal: self, current_frame: None } + where + Self: Sized, + { + IntoInterleavedSamples { + signal: self, + current_frame: None, + } } } -impl StreamingSignalExt for S - where S: StreamingSignal {} +impl StreamingSignalExt for S where S: StreamingSignal {} pub struct Next<'a, S: ?Sized> { - stream: &'a mut S + stream: &'a mut S, } impl<'a, S: StreamingSignal + Unpin> Future for Next<'a, S> { @@ -133,10 +145,8 @@ impl<'a, S: StreamingSignal + Unpin> Future for Next<'a, S> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { match S::poll_next(Pin::new(self.stream), cx) { - Poll::Ready(val) => { - Poll::Ready(val) - } - Poll::Pending => Poll::Pending + Poll::Ready(val) => Poll::Ready(val), + Poll::Pending => Poll::Pending, } } } @@ -147,9 +157,10 @@ pub struct IntoInterleavedSamples { } impl Stream for IntoInterleavedSamples - where - S: StreamingSignal + Unpin, - <::Frame as Frame>::Channels: Unpin { +where + S: StreamingSignal + Unpin, + <::Frame as Frame>::Channels: Unpin, +{ type Item = ::Sample; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { @@ -176,9 +187,10 @@ struct FromStream { } impl StreamingSignal for FromStream - where - S: Stream + Unpin, - S::Item: Frame + Unpin { +where + S: Stream + Unpin, + S::Item: Frame + Unpin, +{ type Frame = S::Item; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { @@ -187,9 +199,7 @@ impl StreamingSignal for FromStream return Poll::Ready(::EQUILIBRIUM); } match S::poll_next(Pin::new(&mut s.stream), cx) { - Poll::Ready(Some(val)) => { - Poll::Ready(val) - } + Poll::Ready(Some(val)) => Poll::Ready(val), Poll::Ready(None) => { s.underlying_exhausted = true; return Poll::Ready(::EQUILIBRIUM); @@ -203,20 +213,21 @@ impl StreamingSignal for FromStream } } - pub struct FromInterleavedSamplesStream - where - F: Frame { +where + F: Frame, +{ stream: S, next_buf: Vec, underlying_exhausted: bool, } pub fn from_interleaved_samples_stream(stream: S) -> FromInterleavedSamplesStream - where - S: Stream + Unpin, - S::Item: Sample, - F: Frame { +where + S: Stream + Unpin, + S::Item: Sample, + F: Frame, +{ FromInterleavedSamplesStream { stream, next_buf: Vec::new(), @@ -225,10 +236,11 @@ pub fn from_interleaved_samples_stream(stream: S) -> FromInterleavedSample } impl StreamingSignal for FromInterleavedSamplesStream - where - S: Stream + Unpin, - S::Item: Sample + Unpin, - F: Frame + Unpin { +where + S: Stream + Unpin, + S::Item: Sample + Unpin, + F: Frame + Unpin, +{ type Frame = F; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { @@ -270,22 +282,21 @@ pub struct OpusEncoder { } impl OpusEncoder - where - S: Stream, - I: ToSample { +where + S: Stream, + I: ToSample, +{ pub fn new(frame_size: u32, sample_rate: u32, channels: usize, stream: S) -> Self { let encoder = opus::Encoder::new( sample_rate, match channels { 1 => Channels::Mono, 2 => Channels::Stereo, - _ => unimplemented!( - "Only 1 or 2 channels supported, got {})", - channels - ), + _ => unimplemented!("Only 1 or 2 channels supported, got {})", channels), }, opus::Application::Voip, - ).unwrap(); + ) + .unwrap(); Self { encoder, frame_size, @@ -298,9 +309,10 @@ impl OpusEncoder } impl Stream for OpusEncoder - where - S: Stream + Unpin, - I: Sample + ToSample { +where + S: Stream + Unpin, + I: Sample + ToSample, +{ type Item = Vec; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { @@ -328,7 +340,10 @@ impl Stream for OpusEncoder s.input_buffer.clear(); } - let encoded = s.encoder.encode_vec_float(&s.input_buffer, opus_frame_size).unwrap(); + let encoded = s + .encoder + .encode_vec_float(&s.input_buffer, opus_frame_size) + .unwrap(); s.input_buffer.clear(); Poll::Ready(Some(encoded)) } -- cgit v1.2.1