aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/audio/input.rs
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-04-08 14:42:18 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-06-13 12:17:58 +0200
commit64978fa17b6e0882d6bacb6e626b0bc9ead2c81d (patch)
tree0b872f676244525b0c3cb017c16c5094dc777e01 /mumd/src/audio/input.rs
parenteee62e0892b1247ce321cd30747ab90d58f49732 (diff)
downloadmum-64978fa17b6e0882d6bacb6e626b0bc9ead2c81d.tar.gz
document mumd::audio
Diffstat (limited to 'mumd/src/audio/input.rs')
-rw-r--r--mumd/src/audio/input.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs
index a1227e3..10aea91 100644
--- a/mumd/src/audio/input.rs
+++ b/mumd/src/audio/input.rs
@@ -1,3 +1,4 @@
+//! Listens to the microphone and sends it to the networking.
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{InputCallbackInfo, Sample, SampleFormat, SampleRate, StreamConfig};
use log::*;
@@ -8,6 +9,7 @@ use crate::audio::transformers::{NoiseGate, Transformer};
use crate::error::{AudioError, AudioStream};
use crate::state::StatePhase;
+/// Generates a callback that receives [Sample]s and sends them as floats to a [futures_channel::mpsc::Sender].
pub fn callback<T: Sample>(
mut input_sender: futures_channel::mpsc::Sender<Vec<u8>>,
mut transformers: Vec<Box<dyn Transformer + Send + 'static>>,
@@ -44,10 +46,15 @@ pub fn callback<T: Sample>(
}
pub trait AudioInputDevice {
+ /// Starts the device.
fn play(&self) -> Result<(), AudioError>;
+ /// Stops the device.
fn pause(&self) -> Result<(), AudioError>;
+ /// Sets the input volume of the device.
fn set_volume(&self, volume: f32);
+ /// Returns a receiver to this device's values.
fn sample_receiver(&mut self) -> Option<futures_channel::mpsc::Receiver<Vec<u8>>>;
+ /// The amount of channels this device has.
fn num_channels(&self) -> usize;
}
@@ -59,6 +66,7 @@ pub struct DefaultAudioInputDevice {
}
impl DefaultAudioInputDevice {
+ /// Initializes the default audio input.
pub fn new(
input_volume: f32,
phase_watcher: watch::Receiver<StatePhase>,
@@ -162,17 +170,21 @@ impl AudioInputDevice for DefaultAudioInputDevice {
.play()
.map_err(|e| AudioError::InputPlayError(e))
}
+
fn pause(&self) -> Result<(), AudioError> {
self.stream
.pause()
.map_err(|e| AudioError::InputPauseError(e))
}
+
fn set_volume(&self, volume: f32) {
self.volume_sender.send(volume).unwrap();
}
+
fn sample_receiver(&mut self) -> Option<futures_channel::mpsc::Receiver<Vec<u8>>> {
self.sample_receiver.take()
}
+
fn num_channels(&self) -> usize {
self.channels as usize
}