aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEskil Q <eskilq@kth.se>2021-01-02 09:32:30 +0100
committerEskil Q <eskilq@kth.se>2021-01-02 09:32:30 +0100
commit7fbbf89cc16734e59882ab71e2aed54e4c048733 (patch)
tree9341935b9e5ad3608db7d65f3f8ee6ae55f60a78
parentf52329ef65b96d1e5d1fd25dabd51f0fdd23ff92 (diff)
downloadmum-7fbbf89cc16734e59882ab71e2aed54e4c048733.tar.gz
add IntoInterleavedSamples struct
-rw-r--r--mumd/src/audio.rs35
1 files changed, 35 insertions, 0 deletions
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<Self>
+ 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<S: StreamingSignal> {
+ signal: S,
+ current_frame: Option<<S::Frame as Frame>::Channels>,
+}
+
+impl<S> Stream for IntoInterleavedSamples<S>
+ where
+ S: StreamingSignal + Unpin,
+ <<S as StreamingSignal>::Frame as Frame>::Channels: Unpin {
+ type Item = <S::Frame as Frame>::Sample;
+
+ fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
+ 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<S: Stream> {
stream: S,
next: Option<S::Item>,