From 9777219aa26dc64c0a3dc3d9d2023b8a1c4295fb Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Fri, 1 Jan 2021 14:30:34 +0100 Subject: add initial streaming signals --- mumd/src/audio/input.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'mumd/src/audio') diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index 7405fdb..d04c728 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -4,6 +4,7 @@ use mumble_protocol::voice::VoicePacketPayload; use std::collections::VecDeque; use std::sync::{Arc, Mutex}; use tokio::sync::{mpsc, watch}; +use log::*; pub fn callback( mut opus_encoder: opus::Encoder, @@ -26,6 +27,7 @@ pub fn callback( let buf = Arc::new(Mutex::new(VecDeque::new())); move |data: &[T], _info: &InputCallbackInfo| { + debug!("{:?}", _info); let mut buf = buf.lock().unwrap(); let input_volume = *input_volume_receiver.borrow(); let out: Vec = data -- cgit v1.2.1 From 65d7b5e907ffbb594319e13684f7f566c0ad2264 Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Fri, 1 Jan 2021 15:08:05 +0100 Subject: add AudioStream struct --- mumd/src/audio/input.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'mumd/src/audio') diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index 914891b..01fd1f3 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -5,6 +5,10 @@ use std::collections::VecDeque; use std::sync::{Arc, Mutex}; use tokio::sync::{mpsc, watch}; use log::*; +use futures::Stream; +use std::pin::Pin; +use std::task::{Context, Poll}; +use futures_util::task::Waker; pub fn callback( mut opus_encoder: opus::Encoder, @@ -54,3 +58,38 @@ pub fn callback( } } } + +struct AudioStream { + data: Arc, Option)>>, +} + +impl AudioStream { + fn new() -> Self { + Self { + data: Arc::new(Mutex::new((VecDeque::new(), None))) + } + } + + fn insert_sample(&self, sample: T) { + let mut data = self.data.lock().unwrap(); + data.0.push_back(sample); + if let Some(waker) = data.1.take() { + waker.wake(); + } + } +} + +impl Stream for AudioStream { + type Item = T; + + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let s = self.get_mut(); + let mut data = s.data.lock().unwrap(); + if data.0.len() > 0 { + Poll::Ready(data.0.pop_front()) + } else { + data.1 = Some(cx.waker().clone()); + Poll::Pending + } + } +} \ No newline at end of file -- cgit v1.2.1 From 76a3f1ea5489048e6d32982119429daa05dde3e0 Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Sat, 2 Jan 2021 11:17:49 +0100 Subject: make audio sending use streams --- mumd/src/audio/input.rs | 46 ++++++---------------------------------------- 1 file changed, 6 insertions(+), 40 deletions(-) (limited to 'mumd/src/audio') diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index 01fd1f3..8f0fe6e 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -1,9 +1,7 @@ -use bytes::Bytes; use cpal::{InputCallbackInfo, Sample}; -use mumble_protocol::voice::VoicePacketPayload; use std::collections::VecDeque; use std::sync::{Arc, Mutex}; -use tokio::sync::{mpsc, watch}; +use tokio::sync::watch; use log::*; use futures::Stream; use std::pin::Pin; @@ -11,50 +9,18 @@ use std::task::{Context, Poll}; use futures_util::task::Waker; pub fn callback( - mut opus_encoder: opus::Encoder, - input_sender: mpsc::Sender, - sample_rate: u32, + mut input_sender: futures::channel::mpsc::Sender, input_volume_receiver: watch::Receiver, - opus_frame_size_blocks: u32, // blocks of 2.5ms ) -> impl FnMut(&[T], &InputCallbackInfo) + Send + 'static { - if !(opus_frame_size_blocks == 1 - || opus_frame_size_blocks == 2 - || opus_frame_size_blocks == 4 - || opus_frame_size_blocks == 8) - { - panic!( - "Unsupported amount of opus frame blocks {}", - opus_frame_size_blocks - ); - } - let opus_frame_size = opus_frame_size_blocks * sample_rate / 400; - - let buf = Arc::new(Mutex::new(VecDeque::new())); move |data: &[T], _info: &InputCallbackInfo| { - debug!("{:?}", _info); - let mut buf = buf.lock().unwrap(); let input_volume = *input_volume_receiver.borrow(); - let out: Vec = data + for sample in 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); - let mut opus_buf: Vec = vec![0; opus_frame_size as usize]; - let result = opus_encoder - .encode_float(&Vec::from(buf.clone()), &mut opus_buf) - .unwrap(); - opus_buf.truncate(result); - let bytes = Bytes::copy_from_slice(&opus_buf); - match input_sender.try_send(VoicePacketPayload::Opus(bytes, false)) { - Ok(_) => {} - Err(_e) => { - //warn!("Error sending audio packet: {:?}", e); - } + .map(|e| e * input_volume) { + if let Err(_e) = input_sender.try_send(sample) { + // warn!("Error sending audio: {}", e) } - *buf = tail; } } } -- cgit v1.2.1 From 0f225e518b6889f604cb440f14824b21ed49bf37 Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Sat, 2 Jan 2021 11:24:59 +0100 Subject: make a bunch of functions sync --- mumd/src/audio/input.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'mumd/src/audio') diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index 8f0fe6e..febcb17 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -2,7 +2,6 @@ use cpal::{InputCallbackInfo, Sample}; use std::collections::VecDeque; use std::sync::{Arc, Mutex}; use tokio::sync::watch; -use log::*; use futures::Stream; use std::pin::Pin; use std::task::{Context, Poll}; -- cgit v1.2.1 From 30a0c3b479b1ff39ad2bf9fbd58c93634ed418b2 Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Sat, 2 Jan 2021 11:25:29 +0100 Subject: remove AudioStream struct --- mumd/src/audio/input.rs | 35 ----------------------------------- 1 file changed, 35 deletions(-) (limited to 'mumd/src/audio') diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index febcb17..517c9ce 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -22,39 +22,4 @@ pub fn callback( } } } -} - -struct AudioStream { - data: Arc, Option)>>, -} - -impl AudioStream { - fn new() -> Self { - Self { - data: Arc::new(Mutex::new((VecDeque::new(), None))) - } - } - - fn insert_sample(&self, sample: T) { - let mut data = self.data.lock().unwrap(); - data.0.push_back(sample); - if let Some(waker) = data.1.take() { - waker.wake(); - } - } -} - -impl Stream for AudioStream { - type Item = T; - - fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { - let s = self.get_mut(); - let mut data = s.data.lock().unwrap(); - if data.0.len() > 0 { - Poll::Ready(data.0.pop_front()) - } else { - data.1 = Some(cx.waker().clone()); - Poll::Pending - } - } } \ No newline at end of file -- cgit v1.2.1 From b35a9c0a48f3f853b2d0e1551d33682189d77055 Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Sat, 2 Jan 2021 11:27:04 +0100 Subject: remove unused imports --- mumd/src/audio/input.rs | 6 ------ 1 file changed, 6 deletions(-) (limited to 'mumd/src/audio') diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index 517c9ce..9ea82e0 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -1,11 +1,5 @@ use cpal::{InputCallbackInfo, Sample}; -use std::collections::VecDeque; -use std::sync::{Arc, Mutex}; use tokio::sync::watch; -use futures::Stream; -use std::pin::Pin; -use std::task::{Context, Poll}; -use futures_util::task::Waker; pub fn callback( mut input_sender: futures::channel::mpsc::Sender, -- cgit v1.2.1 From 6e07c2bc4bba206e15bbe8838a322a5c506be9a1 Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Sat, 2 Jan 2021 15:05:52 +0100 Subject: remove deps and make noise gate more efficent --- mumd/src/audio/input.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'mumd/src/audio') diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index 9ea82e0..da6dc61 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -1,5 +1,6 @@ use cpal::{InputCallbackInfo, Sample}; use tokio::sync::watch; +use log::*; pub fn callback( mut input_sender: futures::channel::mpsc::Sender, @@ -12,7 +13,7 @@ pub fn callback( .map(|e| e.to_f32()) .map(|e| e * input_volume) { if let Err(_e) = input_sender.try_send(sample) { - // warn!("Error sending audio: {}", e) + warn!("Error sending audio: {}", _e); } } } -- cgit v1.2.1 From 1af9b90133a8d6102a09102bbd6f726f598c24fc Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Mon, 4 Jan 2021 21:55:33 +0100 Subject: re-add newline --- mumd/src/audio/input.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mumd/src/audio') diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index da6dc61..deb0fb8 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -17,4 +17,4 @@ pub fn callback( } } } -} \ No newline at end of file +} -- cgit v1.2.1