aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/audio/input.rs
diff options
context:
space:
mode:
authorEskil Q <eskilq@kth.se>2021-01-02 11:17:49 +0100
committerEskil Q <eskilq@kth.se>2021-01-02 11:17:49 +0100
commit76a3f1ea5489048e6d32982119429daa05dde3e0 (patch)
tree856e08e0ff7f5335e96cfd6752bc55fcb512ea8f /mumd/src/audio/input.rs
parent08e64c1b9d622026bcbe1f80d2d5d64dd80af8f9 (diff)
downloadmum-76a3f1ea5489048e6d32982119429daa05dde3e0.tar.gz
make audio sending use streams
Diffstat (limited to 'mumd/src/audio/input.rs')
-rw-r--r--mumd/src/audio/input.rs46
1 files changed, 6 insertions, 40 deletions
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<T: Sample>(
- mut opus_encoder: opus::Encoder,
- input_sender: mpsc::Sender<VoicePacketPayload>,
- sample_rate: u32,
+ mut input_sender: futures::channel::mpsc::Sender<f32>,
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
- || 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<f32> = 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<u8> = 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;
}
}
}