diff options
| author | Eskil Q <eskilq@kth.se> | 2021-01-02 09:32:30 +0100 |
|---|---|---|
| committer | Eskil Q <eskilq@kth.se> | 2021-01-02 09:32:30 +0100 |
| commit | 7fbbf89cc16734e59882ab71e2aed54e4c048733 (patch) | |
| tree | 9341935b9e5ad3608db7d65f3f8ee6ae55f60a78 /mumd/src/audio.rs | |
| parent | f52329ef65b96d1e5d1fd25dabd51f0fdd23ff92 (diff) | |
| download | mum-7fbbf89cc16734e59882ab71e2aed54e4c048733.tar.gz | |
add IntoInterleavedSamples struct
Diffstat (limited to 'mumd/src/audio.rs')
| -rw-r--r-- | mumd/src/audio.rs | 35 |
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>, |
