aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/audio
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
parent7fc5a1a36404ee4cbc09d20c955e6edd3d2ac523 (diff)
downloadmum-be76c2aa51733a0cf495e92659fbcbe527f41149.tar.gz
cargo fmt
Diffstat (limited to 'mumd/src/audio')
-rw-r--r--mumd/src/audio/input.rs42
-rw-r--r--mumd/src/audio/noise_gate.rs133
-rw-r--r--mumd/src/audio/output.rs24
3 files changed, 105 insertions, 94 deletions
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<T: Sample>(
mut input_sender: futures_channel::mpsc::Sender<f32>,
@@ -17,10 +17,7 @@ pub fn callback<T: Sample>(
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<StatePhase>) -> Result<Self, AudioError> {
+ pub fn new(
+ input_volume: f32,
+ phase_watcher: watch::Receiver<StatePhase>,
+ ) -> Result<Self, AudioError> {
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::<f32>(
- sample_sender,
- input_volume_receiver,
- phase_watcher,
- ),
+ callback::<f32>(sample_sender, input_volume_receiver, phase_watcher),
err_fn,
),
SampleFormat::I16 => input_device.build_input_stream(
&input_config,
- callback::<i16>(
- sample_sender,
- input_volume_receiver,
- phase_watcher,
- ),
+ callback::<i16>(sample_sender, input_volume_receiver, phase_watcher),
err_fn,
),
SampleFormat::U16 => input_device.build_input_stream(
&input_config,
- callback::<u16>(
- sample_sender,
- input_volume_receiver,
- phase_watcher,
- ),
+ callback::<u16>(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<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))
}
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) {