From 7fbbf89cc16734e59882ab71e2aed54e4c048733 Mon Sep 17 00:00:00 2001 From: Eskil Q Date: Sat, 2 Jan 2021 09:32:30 +0100 Subject: add IntoInterleavedSamples struct --- mumd/src/audio.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'mumd/src') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index afe644c..828dbc5 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -452,6 +452,12 @@ trait StreamingSignalExt: StreamingSignal { stream: self } } + + fn into_interleaved_samples(self) -> IntoInterleavedSamples + where + Self: Sized { + IntoInterleavedSamples { signal: self, current_frame: None } + } } struct Next<'a, S: ?Sized> { @@ -471,6 +477,35 @@ impl<'a, S: StreamingSignal + Unpin> Future for Next<'a, S> { } } +struct IntoInterleavedSamples { + signal: S, + current_frame: Option<::Channels>, +} + +impl Stream for IntoInterleavedSamples + where + S: StreamingSignal + Unpin, + <::Frame as Frame>::Channels: Unpin { + type Item = ::Sample; + + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + let s = self.get_mut(); + loop { + if s.current_frame.is_none() { + match S::poll_next(Pin::new(&mut s.signal), cx) { + Poll::Ready(val) => { + s.current_frame = Some(val.channels()); + } + Poll::Pending => return Poll::Pending, + } + } + if let Some(channel) = s.current_frame.as_mut().unwrap().next() { + return Poll::Ready(Some(channel)); + } + } + } +} + struct FromStream { stream: S, next: Option, -- cgit v1.2.1