aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/audio/noise_gate.rs
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-06-06 23:26:24 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-06-06 23:26:24 +0200
commitbe76c2aa51733a0cf495e92659fbcbe527f41149 (patch)
tree617fb1caa999c076a45233b4bedea6a78192db25 /mumd/src/audio/noise_gate.rs
parent7fc5a1a36404ee4cbc09d20c955e6edd3d2ac523 (diff)
downloadmum-be76c2aa51733a0cf495e92659fbcbe527f41149.tar.gz
cargo fmt
Diffstat (limited to 'mumd/src/audio/noise_gate.rs')
-rw-r--r--mumd/src/audio/noise_gate.rs133
1 files changed, 74 insertions, 59 deletions
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<S: StreamingSignal> {
}
impl<S: StreamingSignal> StreamingNoiseGate<S> {
- pub fn new(
- signal: S,
- deactivation_delay: usize,
- ) -> StreamingNoiseGate<S> {
+ pub fn new(signal: S, deactivation_delay: usize) -> StreamingNoiseGate<S> {
Self {
open: 0,
signal,
@@ -31,10 +28,11 @@ impl<S: StreamingSignal> StreamingNoiseGate<S> {
}
impl<S> StreamingSignal for StreamingNoiseGate<S>
- where
- S: StreamingSignal + Unpin,
- FloatSample<S::Frame>: Unpin,
- <S as StreamingSignal>::Frame: Unpin {
+where
+ S: StreamingSignal + Unpin,
+ FloatSample<S::Frame>: Unpin,
+ <S as StreamingSignal>::Frame: Unpin,
+{
type Frame = S::Frame;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Frame> {
@@ -47,18 +45,30 @@ impl<S> StreamingSignal for StreamingNoiseGate<S>
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<S> 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<Self::Frame> {
@@ -109,23 +120,24 @@ impl<S> 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<Self>
- where
- Self: Sized {
- IntoInterleavedSamples { signal: self, current_frame: None }
+ where
+ Self: Sized,
+ {
+ IntoInterleavedSamples {
+ signal: self,
+ current_frame: None,
+ }
}
}
-impl<S> StreamingSignalExt for S
- where S: StreamingSignal {}
+impl<S> 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<Self::Output> {
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<S: StreamingSignal> {
}
impl<S> Stream for IntoInterleavedSamples<S>
- where
- S: StreamingSignal + Unpin,
- <<S as StreamingSignal>::Frame as Frame>::Channels: Unpin {
+where
+ S: StreamingSignal + Unpin,
+ <<S as StreamingSignal>::Frame as Frame>::Channels: Unpin,
+{
type Item = <S::Frame as Frame>::Sample;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
@@ -176,9 +187,10 @@ struct FromStream<S> {
}
impl<S> StreamingSignal for FromStream<S>
- 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<Self::Frame> {
@@ -187,9 +199,7 @@ impl<S> StreamingSignal for FromStream<S>
return Poll::Ready(<Self::Frame as Frame>::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(<Self::Frame as Frame>::EQUILIBRIUM);
@@ -203,20 +213,21 @@ impl<S> StreamingSignal for FromStream<S>
}
}
-
pub struct FromInterleavedSamplesStream<S, F>
- where
- F: Frame {
+where
+ F: Frame,
+{
stream: S,
next_buf: Vec<F::Sample>,
underlying_exhausted: bool,
}
pub fn from_interleaved_samples_stream<S, F>(stream: S) -> FromInterleavedSamplesStream<S, F>
- where
- S: Stream + Unpin,
- S::Item: Sample,
- F: Frame<Sample = S::Item> {
+where
+ S: Stream + Unpin,
+ S::Item: Sample,
+ F: Frame<Sample = S::Item>,
+{
FromInterleavedSamplesStream {
stream,
next_buf: Vec::new(),
@@ -225,10 +236,11 @@ pub fn from_interleaved_samples_stream<S, F>(stream: S) -> FromInterleavedSample
}
impl<S, F> StreamingSignal for FromInterleavedSamplesStream<S, F>
- where
- S: Stream + Unpin,
- S::Item: Sample + Unpin,
- F: Frame<Sample = S::Item> + Unpin {
+where
+ S: Stream + Unpin,
+ S::Item: Sample + Unpin,
+ F: Frame<Sample = S::Item> + Unpin,
+{
type Frame = F;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Frame> {
@@ -270,22 +282,21 @@ pub struct OpusEncoder<S> {
}
impl<S, I> OpusEncoder<S>
- where
- S: Stream<Item = I>,
- I: ToSample<f32> {
+where
+ S: Stream<Item = I>,
+ I: ToSample<f32>,
+{
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<S, I> OpusEncoder<S>
}
impl<S, I> Stream for OpusEncoder<S>
- where
- S: Stream<Item = I> + Unpin,
- I: Sample + ToSample<f32> {
+where
+ S: Stream<Item = I> + Unpin,
+ I: Sample + ToSample<f32>,
+{
type Item = Vec<u8>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
@@ -328,7 +340,10 @@ impl<S, I> Stream for OpusEncoder<S>
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))
}