aboutsummaryrefslogtreecommitdiffstats
path: root/mumd
diff options
context:
space:
mode:
Diffstat (limited to 'mumd')
-rw-r--r--mumd/src/audio.rs25
-rw-r--r--mumd/src/network/tcp.rs1
-rw-r--r--mumd/src/state.rs4
3 files changed, 23 insertions, 7 deletions
diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs
index edc2f7f..828942b 100644
--- a/mumd/src/audio.rs
+++ b/mumd/src/audio.rs
@@ -13,13 +13,13 @@ use std::ops::AddAssign;
use std::sync::Arc;
use std::sync::Mutex;
use tokio::sync::mpsc::{self, Receiver, Sender};
+use tokio::sync::watch;
struct ClientStream {
buffer: VecDeque<f32>, //TODO ring buffer?
opus_decoder: opus::Decoder,
}
-//TODO remove pub where possible
pub struct Audio {
pub output_config: StreamConfig,
pub output_stream: Stream,
@@ -27,12 +27,12 @@ pub struct Audio {
pub input_config: StreamConfig,
pub input_stream: Stream,
pub input_buffer: Arc<Mutex<VecDeque<f32>>>,
- input_channel_receiver: Option<Receiver<VoicePacketPayload>>, //TODO unbounded? mbe ring buffer and drop the first packet
+ input_channel_receiver: Option<Receiver<VoicePacketPayload>>,
+ input_volume_sender: watch::Sender<f32>,
- client_streams: Arc<Mutex<HashMap<u32, ClientStream>>>, //TODO move to user state
+ client_streams: Arc<Mutex<HashMap<u32, ClientStream>>>,
}
-//TODO split into input/output
impl Audio {
pub fn new() -> Self {
let host = cpal::default_host();
@@ -99,6 +99,8 @@ impl Audio {
.unwrap();
let (input_sender, input_receiver) = mpsc::channel(100);
+ let (input_volume_sender, input_volume_receiver) = watch::channel::<f32>(1.0);
+
let input_buffer = Arc::new(Mutex::new(VecDeque::new()));
let input_stream = match input_supported_sample_format {
SampleFormat::F32 => input_device.build_input_stream(
@@ -107,6 +109,7 @@ impl Audio {
input_encoder,
input_sender,
input_config.sample_rate.0,
+ input_volume_receiver.clone(),
4, // 10 ms
),
err_fn,
@@ -117,6 +120,7 @@ impl Audio {
input_encoder,
input_sender,
input_config.sample_rate.0,
+ input_volume_receiver.clone(),
4, // 10 ms
),
err_fn,
@@ -127,6 +131,7 @@ impl Audio {
input_encoder,
input_sender,
input_config.sample_rate.0,
+ input_volume_receiver.clone(),
4, // 10 ms
),
err_fn,
@@ -142,6 +147,7 @@ impl Audio {
input_config,
input_stream,
input_buffer,
+ input_volume_sender,
input_channel_receiver: Some(input_receiver),
client_streams,
}
@@ -195,6 +201,10 @@ impl Audio {
pub fn clear_clients(&mut self) {
self.client_streams.lock().unwrap().clear();
}
+
+ pub fn set_input_volume(&self, input_volume: f32) {
+ self.input_volume_sender.broadcast(input_volume).unwrap();
+ }
}
impl ClientStream {
@@ -280,6 +290,7 @@ fn input_callback<T: Sample>(
mut opus_encoder: opus::Encoder,
mut input_sender: Sender<VoicePacketPayload>,
sample_rate: u32,
+ input_volume_receiver: watch::Receiver<f32>,
opus_frame_size_blocks: u32, // blocks of 2.5ms
) -> impl FnMut(&[T], &InputCallbackInfo) + Send + 'static {
if !(opus_frame_size_blocks == 1
@@ -297,7 +308,10 @@ fn input_callback<T: Sample>(
let buf = Arc::new(Mutex::new(VecDeque::new()));
move |data: &[T], _info: &InputCallbackInfo| {
let mut buf = buf.lock().unwrap();
- let out: Vec<f32> = data.iter().map(|e| e.to_f32()).collect();
+ let input_volume = *input_volume_receiver.borrow();
+ let out: Vec<f32> = data.iter().map(|e| e.to_f32())
+ .map(|e| e * input_volume)
+ .collect();
buf.extend(out);
while buf.len() >= opus_frame_size as usize {
let tail = buf.split_off(opus_frame_size as usize);
@@ -308,7 +322,6 @@ fn input_callback<T: Sample>(
opus_buf.truncate(result);
let bytes = Bytes::copy_from_slice(&opus_buf);
match input_sender.try_send(VoicePacketPayload::Opus(bytes, false)) {
- //TODO handle full buffer / disconnect
Ok(_) => {}
Err(_e) => {
//warn!("Error sending audio packet: {:?}", e);
diff --git a/mumd/src/network/tcp.rs b/mumd/src/network/tcp.rs
index ea4ef86..88d2b59 100644
--- a/mumd/src/network/tcp.rs
+++ b/mumd/src/network/tcp.rs
@@ -233,7 +233,6 @@ async fn listen(
break;
}
Some(Some(packet)) => {
- //TODO handle types separately
match packet.unwrap() {
ControlPacket::TextMessage(msg) => {
info!(
diff --git a/mumd/src/state.rs b/mumd/src/state.rs
index 2c48955..fd1c831 100644
--- a/mumd/src/state.rs
+++ b/mumd/src/state.rs
@@ -157,6 +157,10 @@ impl State {
.unwrap();
(false, Ok(None))
}
+ Command::InputVolumeSet(volume) => {
+ self.audio.set_input_volume(volume);
+ (false, Ok(None))
+ }
}
}