diff options
| author | Eskil Queseth <eskilq@kth.se> | 2021-06-12 15:58:44 +0200 |
|---|---|---|
| committer | Eskil Queseth <eskilq@kth.se> | 2021-06-12 15:58:44 +0200 |
| commit | 3cb2c612fb030278aaf5e4d49d42cf689bdc9cc0 (patch) | |
| tree | b16df1d03495e9fc87fd82ede016c88ed94c530f | |
| parent | 62be7e8d5f22888d95984ce2634f676ca8b055c4 (diff) | |
| download | mum-3cb2c612fb030278aaf5e4d49d42cf689bdc9cc0.tar.gz | |
re-add noise gate
| -rw-r--r-- | mumd/src/audio.rs | 1 | ||||
| -rw-r--r-- | mumd/src/audio/input.rs | 7 | ||||
| -rw-r--r-- | mumd/src/audio/transformers.rs | 17 |
3 files changed, 22 insertions, 3 deletions
diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 6264879..29c5668 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -1,5 +1,6 @@ pub mod input; pub mod output; +pub mod transformers; use crate::audio::input::{AudioInputDevice, DefaultAudioInputDevice}; use crate::audio::output::{AudioOutputDevice, ClientStream, DefaultAudioOutputDevice}; diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index 162dd2c..b0e621b 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -4,12 +4,13 @@ use log::*; use tokio::sync::watch; use crate::audio::SAMPLE_RATE; +use crate::audio::transformers::{create_noise_gate}; use crate::error::{AudioError, AudioStream}; use crate::state::StatePhase; pub fn callback<T: Sample>( mut input_sender: futures_channel::mpsc::Sender<Vec<u8>>, - transformers: Vec<Box<dyn Fn(&mut [f32]) -> Option<&mut [f32]> + Send + 'static>>, + mut transformers: Vec<Box<dyn FnMut(&mut [f32]) -> Option<&mut [f32]> + Send + 'static>>, frame_size: u32, sample_rate: u32, channels: u16, @@ -39,7 +40,7 @@ pub fn callback<T: Sample>( while buffer.len() + data.len() > buffer_size { buffer.extend(data.by_ref().take(buffer_size - buffer.len())); let encoded = transformers - .iter() + .iter_mut() .try_fold(&mut buffer[..], |acc, e| e(acc)) .map(|buf| opus_encoder.encode_vec_float(&*buf, buffer_size).unwrap()); @@ -103,7 +104,7 @@ impl DefaultAudioInputDevice { let (volume_sender, input_volume_receiver) = watch::channel::<f32>(input_volume); - let transformers = vec![]; + let transformers = vec![Box::new(create_noise_gate(10, 0.6)) as Box<dyn FnMut(&mut [f32]) -> Option<&mut [f32]> + Send + 'static>]; let input_stream = match input_supported_sample_format { SampleFormat::F32 => input_device.build_input_stream( diff --git a/mumd/src/audio/transformers.rs b/mumd/src/audio/transformers.rs new file mode 100644 index 0000000..5c9681b --- /dev/null +++ b/mumd/src/audio/transformers.rs @@ -0,0 +1,17 @@ +use dasp_ring_buffer::Bounded; + + +pub fn create_noise_gate(chunks: usize, mute_percentage: f32) -> impl FnMut(&mut [f32]) -> Option<&mut [f32]> { + let mut peaks = Bounded::from_full(vec![0.0; chunks]); + let mut alltime_high: f32 = 0.0; + move |buf: &mut [f32]| { + let max = buf.iter().map(|e| e.abs()).max_by(|a, b| a.partial_cmp(b).unwrap()).unwrap(); + peaks.push(max); + alltime_high = alltime_high.max(max); + if peaks.iter().any(|e| *e >= alltime_high * mute_percentage) { + Some(buf) + } else { + None + } + } +} |
