From 9777219aa26dc64c0a3dc3d9d2023b8a1c4295fb Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Fri, 1 Jan 2021 14:30:34 +0100 Subject: add initial streaming signals --- mumd/src/audio.rs | 133 ++++++++++++++++++++++++++++++++++++++++++++++-- mumd/src/audio/input.rs | 2 + 2 files changed, 132 insertions(+), 3 deletions(-) diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index bed9ceb..ee5516a 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -4,7 +4,7 @@ pub mod output; use crate::audio::output::SaturatingAdd; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; -use cpal::{SampleFormat, SampleRate, Stream, StreamConfig}; +use cpal::{SampleFormat, SampleRate, StreamConfig}; use dasp_interpolate::linear::Linear; use dasp_signal::{self as signal, Signal}; use log::*; @@ -17,6 +17,13 @@ use tokio::sync::{mpsc, watch}; use dasp_frame::Frame; use dasp_sample::{Sample, SignedSample}; use dasp_ring_buffer::Fixed; +use futures::Stream; +use futures::task::{Context, Poll}; +use std::pin::Pin; +use tokio::stream::StreamExt; +use std::convert::identity; +use std::future::Future; +use std::mem; //TODO? move to mumlib pub const EVENT_SOUNDS: &[(&'static [u8], NotificationEvents)] = &[ @@ -65,8 +72,8 @@ pub enum NotificationEvents { pub struct Audio { output_config: StreamConfig, - _output_stream: Stream, - _input_stream: Stream, + _output_stream: cpal::Stream, + _input_stream: cpal::Stream, input_channel_receiver: Option>, input_volume_sender: watch::Sender, @@ -418,4 +425,124 @@ fn abs(sample: S) -> S { } else { -sample } +} + +trait StreamingSignal { + type Frame: Frame; + + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll; +} + +trait StreamingSignalExt: StreamingSignal { + fn next(&mut self) -> Next<'_, Self> { + Next { + stream: self + } + } +} + +struct Next<'a, S: ?Sized> { + stream: &'a mut S +} + +impl<'a, S: StreamingSignal + Unpin> Future for Next<'a, S> { + type Output = S::Frame; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + match S::poll_next(Pin::new(self.stream), cx) { + Poll::Ready(val) => { + Poll::Ready(val) + } + Poll::Pending => Poll::Pending + } + } +} + +struct FromStream { + stream: S, + next: Option, +} + +async fn from_stream(mut stream: S) -> FromStream + where + S: Stream + Unpin, + S::Item: Frame { + let next = stream.next().await; + FromStream { stream, next } +} + +impl StreamingSignal for FromStream + where + S: Stream + Unpin, + S::Item: Frame + Unpin { + type Frame = S::Item; + + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let s = self.get_mut(); + match s.next.take() { + Some(v) => { + match S::poll_next(Pin::new(&mut s.stream), cx) { + Poll::Ready(val) => { + s.next = val; + Poll::Ready(v) + } + Poll::Pending => Poll::Pending + } + } + None => Poll::Ready(::EQUILIBRIUM) + } + } +} + + +struct FromInterleavedSamplesStream + where + F: Frame { + stream: S, + next: Option>, +} + +async fn from_interleaved_samples_stream(mut stream: S) -> FromInterleavedSamplesStream + where + S: Stream + Unpin, + S::Item: Sample, + F: Frame { + let mut data = Vec::with_capacity(F::CHANNELS); + for _ in 0..F::CHANNELS { + data.push(stream.next().await); + } + let data = data.into_iter().flat_map(identity).collect::>(); + FromInterleavedSamplesStream { stream, next: if data.len() == F::CHANNELS { Some(data) } else { None } } +} + +impl StreamingSignal for FromInterleavedSamplesStream + where + S: Stream + Unpin, + S::Item: Sample + Unpin, + F: Frame { + type Frame = F; + + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let s = self.get_mut(); + if s.next.is_some() { + if s.next.as_ref().unwrap().len() == F::CHANNELS { + let mut data_buf = mem::replace(&mut s.next, Some(Vec::new())).unwrap().into_iter(); + Poll::Ready(F::from_samples(&mut data_buf).unwrap()) + } else { + match S::poll_next(Pin::new(&mut s.stream), cx) { + Poll::Ready(Some(v)) => { + s.next.as_mut().unwrap().push(v); + Poll::Pending + } + Poll::Ready(None) => { + s.next = None; + Poll::Ready(F::EQUILIBRIUM) + } + Poll::Pending => Poll::Pending, + } + } + } else { + Poll::Ready(F::EQUILIBRIUM) + } + } } \ No newline at end of file diff --git a/mumd/src/audio/input.rs b/mumd/src/audio/input.rs index 7405fdb..d04c728 100644 --- a/mumd/src/audio/input.rs +++ b/mumd/src/audio/input.rs @@ -4,6 +4,7 @@ use mumble_protocol::voice::VoicePacketPayload; use std::collections::VecDeque; use std::sync::{Arc, Mutex}; use tokio::sync::{mpsc, watch}; +use log::*; pub fn callback( mut opus_encoder: opus::Encoder, @@ -26,6 +27,7 @@ pub fn callback( 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 = data -- cgit v1.2.1