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/input.rs | 42 ++++++-------- mumd/src/audio/noise_gate.rs | 133 ++++++++++++++++++++++++------------------- mumd/src/audio/output.rs | 24 ++++---- 3 files changed, 105 insertions(+), 94 deletions(-) (limited to 'mumd/src/audio') diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index 4a1ed3d..e45ff27 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -1,11 +1,11 @@ -use cpal::{InputCallbackInfo, Sample, SampleFormat, SampleRate, StreamConfig}; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; -use tokio::sync::watch; +use cpal::{InputCallbackInfo, Sample, SampleFormat, SampleRate, StreamConfig}; use log::*; +use tokio::sync::watch; -use crate::state::StatePhase; use crate::audio::SAMPLE_RATE; use crate::error::{AudioError, AudioStream}; +use crate::state::StatePhase; pub fn callback( mut input_sender: futures_channel::mpsc::Sender, @@ -17,10 +17,7 @@ pub fn callback( return; } let input_volume = *input_volume_receiver.borrow(); - for sample in data - .iter() - .map(|e| e.to_f32()) - .map(|e| e * input_volume) { + for sample in data.iter().map(|e| e.to_f32()).map(|e| e * input_volume) { if let Err(_e) = input_sender.try_send(sample) { warn!("Error sending audio: {}", _e); } @@ -44,7 +41,10 @@ pub struct DefaultAudioInputDevice { } impl DefaultAudioInputDevice { - pub fn new(input_volume: f32, phase_watcher: watch::Receiver) -> Result { + pub fn new( + input_volume: f32, + phase_watcher: watch::Receiver, + ) -> Result { let sample_rate = SampleRate(SAMPLE_RATE); let host = cpal::default_host(); @@ -76,29 +76,17 @@ impl DefaultAudioInputDevice { let input_stream = match input_supported_sample_format { SampleFormat::F32 => input_device.build_input_stream( &input_config, - callback::( - sample_sender, - input_volume_receiver, - phase_watcher, - ), + callback::(sample_sender, input_volume_receiver, phase_watcher), err_fn, ), SampleFormat::I16 => input_device.build_input_stream( &input_config, - callback::( - sample_sender, - input_volume_receiver, - phase_watcher, - ), + callback::(sample_sender, input_volume_receiver, phase_watcher), err_fn, ), SampleFormat::U16 => input_device.build_input_stream( &input_config, - callback::( - sample_sender, - input_volume_receiver, - phase_watcher, - ), + callback::(sample_sender, input_volume_receiver, phase_watcher), err_fn, ), } @@ -116,10 +104,14 @@ impl DefaultAudioInputDevice { impl AudioInputDevice for DefaultAudioInputDevice { fn play(&self) -> Result<(), AudioError> { - self.stream.play().map_err(|e| AudioError::InputPlayError(e)) + self.stream + .play() + .map_err(|e| AudioError::InputPlayError(e)) } fn pause(&self) -> Result<(), AudioError> { - self.stream.pause().map_err(|e| AudioError::InputPauseError(e)) + self.stream + .pause() + .map_err(|e| AudioError::InputPauseError(e)) } fn set_volume(&self, volume: f32) { self.volume_sender.send(volume).unwrap(); 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)) } diff --git a/mumd/src/audio/output.rs b/mumd/src/audio/output.rs index 658c1c8..a2f6bcc 100644 --- a/mumd/src/audio/output.rs +++ b/mumd/src/audio/output.rs @@ -1,10 +1,10 @@ -use crate::network::VoiceStreamType; use crate::audio::SAMPLE_RATE; use crate::error::{AudioError, AudioStream}; +use crate::network::VoiceStreamType; -use log::*; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; -use cpal::{SampleFormat, SampleRate, StreamConfig, OutputCallbackInfo, Sample}; +use cpal::{OutputCallbackInfo, Sample, SampleFormat, SampleRate, StreamConfig}; +use log::*; use mumble_protocol::voice::VoicePacketPayload; use std::collections::{HashMap, VecDeque}; use std::ops::AddAssign; @@ -39,10 +39,7 @@ impl ClientStream { let sample_rate = self.sample_rate; let channels = self.channels; self.buffer_clients.entry(client).or_insert_with(|| { - let opus_decoder = opus::Decoder::new( - sample_rate, - channels - ).unwrap(); + let opus_decoder = opus::Decoder::new(sample_rate, channels).unwrap(); (VecDeque::new(), opus_decoder) }) } @@ -139,7 +136,10 @@ impl DefaultAudioOutputDevice { .with_sample_rate(sample_rate); let output_supported_sample_format = output_supported_config.sample_format(); let output_config: StreamConfig = output_supported_config.into(); - let client_streams = Arc::new(std::sync::Mutex::new(ClientStream::new(sample_rate.0, output_config.channels))); + let client_streams = Arc::new(std::sync::Mutex::new(ClientStream::new( + sample_rate.0, + output_config.channels, + ))); let err_fn = |err| error!("An error occurred on the output audio stream: {}", err); @@ -187,11 +187,15 @@ impl DefaultAudioOutputDevice { impl AudioOutputDevice for DefaultAudioOutputDevice { fn play(&self) -> Result<(), AudioError> { - self.stream.play().map_err(|e| AudioError::OutputPlayError(e)) + self.stream + .play() + .map_err(|e| AudioError::OutputPlayError(e)) } fn pause(&self) -> Result<(), AudioError> { - self.stream.pause().map_err(|e| AudioError::OutputPauseError(e)) + self.stream + .pause() + .map_err(|e| AudioError::OutputPauseError(e)) } fn set_volume(&self, volume: f32) { -- cgit v1.2.1