aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/audio/input.rs
diff options
context:
space:
mode:
authorKapten Z∅∅m <55669224+default-username-852@users.noreply.github.com>2021-01-04 22:48:18 +0100
committerGitHub <noreply@github.com>2021-01-04 22:48:18 +0100
commit531dc28e2c09a13336b057044bf8721d4b2c4c00 (patch)
tree88d41a434b3a0c242ac7b35c6afefff0f75ee656 /mumd/src/audio/input.rs
parentbe7748be2f1e9d1e88ebd093da9eec16d1ad4049 (diff)
parent50b322f4ef974765a2948dfb08b1c9e8128b1bed (diff)
downloadmum-531dc28e2c09a13336b057044bf8721d4b2c4c00.tar.gz
Merge pull request #51 from mum-rs/noise-gate
Noise gate
Diffstat (limited to 'mumd/src/audio/input.rs')
-rw-r--r--mumd/src/audio/input.rs48
1 files changed, 7 insertions, 41 deletions
diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs
index fe0d21f..deb0fb8 100644
--- a/mumd/src/audio/input.rs
+++ b/mumd/src/audio/input.rs
@@ -1,54 +1,20 @@
-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::*;
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| {
- 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;
}
}
}