aboutsummaryrefslogtreecommitdiffstats
path: root/mumd
diff options
context:
space:
mode:
authorEskil Q <eskilq@kth.se>2021-01-02 15:05:52 +0100
committerEskil Q <eskilq@kth.se>2021-01-02 15:05:52 +0100
commit6e07c2bc4bba206e15bbe8838a322a5c506be9a1 (patch)
tree8504a7a621d674a28b2233648067e06d0dcd4828 /mumd
parentb35a9c0a48f3f853b2d0e1551d33682189d77055 (diff)
downloadmum-6e07c2bc4bba206e15bbe8838a322a5c506be9a1.tar.gz
remove deps and make noise gate more efficent
Diffstat (limited to 'mumd')
-rw-r--r--mumd/Cargo.toml2
-rw-r--r--mumd/src/audio.rs95
-rw-r--r--mumd/src/audio/input.rs3
3 files changed, 49 insertions, 51 deletions
diff --git a/mumd/Cargo.toml b/mumd/Cargo.toml
index d12cec2..d13bdc8 100644
--- a/mumd/Cargo.toml
+++ b/mumd/Cargo.toml
@@ -18,8 +18,6 @@ notifications = ["libnotify"]
[dependencies]
mumlib = { version = "0.3", path = "../mumlib" }
-argparse = "0.2"
-async-stream = "0.3.0"
cpal = "0.13"
bytes = "1.0"
dasp_interpolate = { version = "0.11", features = ["linear"] }
diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs
index 4b0cb47..d4ef4d6 100644
--- a/mumd/src/audio.rs
+++ b/mumd/src/audio.rs
@@ -16,11 +16,11 @@ use std::sync::{Arc, Mutex};
use tokio::sync::watch;
use dasp_frame::Frame;
use dasp_sample::{SignedSample, ToSample, Sample};
-use dasp_ring_buffer::Fixed;
use futures::Stream;
use futures::task::{Context, Poll};
use std::pin::Pin;
use std::future::{Future};
+use std::fmt::Debug;
//TODO? move to mumlib
pub const EVENT_SOUNDS: &[(&'static [u8], NotificationEvents)] = &[
@@ -203,8 +203,11 @@ impl Audio {
4,
input_config.sample_rate.0,
input_config.channels as usize,
- StreamingSignalExt::into_interleaved_samples(from_interleaved_samples_stream::<_, f32>(sample_receiver))); //TODO attach a noise gate
- //TODO group frames correctly
+ StreamingSignalExt::into_interleaved_samples(
+ StreamingNoiseGate::new(
+ from_interleaved_samples_stream::<_, f32>(sample_receiver), //TODO group frames correctly
+ 0.09,
+ 10_000)));
output_stream.play().unwrap();
@@ -348,25 +351,23 @@ impl Audio {
}
struct NoiseGate<S: Signal> {
- open: bool,
+ open: usize,
signal: S,
- buffer: dasp_ring_buffer::Fixed<Vec<S::Frame>>,
activate_threshold: <<S::Frame as Frame>::Sample as Sample>::Float,
- deactivate_threshold: <<S::Frame as Frame>::Sample as Sample>::Float,
+ deactivation_delay: usize,
}
impl<S: Signal> NoiseGate<S> {
pub fn new(
signal: S,
activate_threshold: <<S::Frame as Frame>::Sample as Sample>::Float,
- deactivate_threshold: <<S::Frame as Frame>::Sample as Sample>::Float
+ deactivation_delay: usize,
) -> NoiseGate<S> {
Self {
- open: false,
+ open: 0,
signal,
- buffer: Fixed::from(vec![<S::Frame as Frame>::EQUILIBRIUM; 4096]),
activate_threshold,
- deactivate_threshold,
+ deactivation_delay
}
}
}
@@ -376,26 +377,26 @@ impl<S: Signal> Signal for NoiseGate<S> {
fn next(&mut self) -> Self::Frame {
let frame = self.signal.next();
- self.buffer.push(frame);
- if self.open && self.buffer
- .iter()
- .all(|f| f.to_float_frame()
- .channels()
- .all(|s| abs(s - <<<S::Frame as Frame>::Sample as Sample>::Float as Sample>::EQUILIBRIUM) <= self.deactivate_threshold)) {
- self.open = false;
- } else if !self.open && self.buffer
- .iter()
- .any(|f| f.to_float_frame()
- .channels()
- .any(|s| abs(s - <<<S::Frame as Frame>::Sample as Sample>::Float as Sample>::EQUILIBRIUM) >= self.activate_threshold)) {
- self.open = true;
+ match self.open {
+ 0 => {
+ if frame.to_float_frame().channels().any(|e| abs(e) >= self.activate_threshold) {
+ self.open = self.deactivation_delay;
+ }
+ }
+ _ => {
+ if frame.to_float_frame().channels().any(|e| abs(e) >= self.activate_threshold) {
+ self.open = self.deactivation_delay;
+ } else {
+ self.open -= 1;
+ }
+ }
}
- if self.open {
+ if self.open != 0 {
frame
} else {
- S::Frame::EQUILIBRIUM
+ <S::Frame as Frame>::EQUILIBRIUM
}
}
@@ -405,25 +406,23 @@ impl<S: Signal> Signal for NoiseGate<S> {
}
struct StreamingNoiseGate<S: StreamingSignal> {
- open: bool,
+ open: usize,
signal: S,
- buffer: dasp_ring_buffer::Fixed<Vec<S::Frame>>,
activate_threshold: <<S::Frame as Frame>::Sample as Sample>::Float,
- deactivate_threshold: <<S::Frame as Frame>::Sample as Sample>::Float,
+ deactivation_delay: usize,
}
impl<S: StreamingSignal> StreamingNoiseGate<S> {
pub fn new(
signal: S,
activate_threshold: <<S::Frame as Frame>::Sample as Sample>::Float,
- deactivate_threshold: <<S::Frame as Frame>::Sample as Sample>::Float
+ deactivation_delay: usize,
) -> StreamingNoiseGate<S> {
Self {
- open: false,
+ open: 0,
signal,
- buffer: Fixed::from(vec![<S::Frame as Frame>::EQUILIBRIUM; 4096]),
activate_threshold,
- deactivate_threshold,
+ deactivation_delay
}
}
}
@@ -442,26 +441,26 @@ impl<S> StreamingSignal for StreamingNoiseGate<S>
Poll::Ready(v) => v,
Poll::Pending => return Poll::Pending,
};
- s.buffer.push(frame);
- if s.open && s.buffer
- .iter()
- .all(|f| f.to_float_frame()
- .channels()
- .all(|sample| abs(sample - <<<S::Frame as Frame>::Sample as Sample>::Float as Sample>::EQUILIBRIUM) <= s.deactivate_threshold)) {
- s.open = false;
- } else if !s.open && s.buffer
- .iter()
- .any(|f| f.to_float_frame()
- .channels()
- .any(|sample| abs(sample - <<<S::Frame as Frame>::Sample as Sample>::Float as Sample>::EQUILIBRIUM) >= s.activate_threshold)) {
- s.open = true;
+ match s.open {
+ 0 => {
+ if frame.to_float_frame().channels().any(|e| abs(e) >= s.activate_threshold) {
+ s.open = s.deactivation_delay;
+ }
+ }
+ _ => {
+ if frame.to_float_frame().channels().any(|e| abs(e) >= s.activate_threshold) {
+ s.open = s.deactivation_delay;
+ } else {
+ s.open -= 1;
+ }
+ }
}
- if s.open {
+ if s.open != 0 {
Poll::Ready(frame)
} else {
- Poll::Ready(S::Frame::EQUILIBRIUM)
+ Poll::Ready(<S::Frame as Frame>::EQUILIBRIUM)
}
}
@@ -567,7 +566,7 @@ struct FromStream<S> {
underlying_exhausted: bool,
}
-fn from_stream<S>(mut stream: S) -> FromStream<S>
+fn from_stream<S>(stream: S) -> FromStream<S>
where
S: Stream + Unpin,
S::Item: Frame {
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<T: Sample>(
mut input_sender: futures::channel::mpsc::Sender<f32>,
@@ -12,7 +13,7 @@ pub fn callback<T: Sample>(
.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);
}
}
}