use super::Wrapper;
use crate::NextSample;
use crate::Sound;
use std::sync::mpsc;
pub struct CompletionNotifier<S: Sound> {
inner: S,
sender: Option<mpsc::SyncSender<()>>,
}
impl<S> CompletionNotifier<S>
where
S: Sound,
{
pub fn new(inner: S) -> (Self, mpsc::Receiver<()>) {
let (sender, receiver) = mpsc::sync_channel(1);
let controllable = CompletionNotifier {
inner,
sender: Some(sender),
};
(controllable, receiver)
}
}
impl<S> Sound for CompletionNotifier<S>
where
S: Sound,
{
fn channel_count(&self) -> u16 {
self.inner.channel_count()
}
fn sample_rate(&self) -> u32 {
self.inner.sample_rate()
}
fn next_sample(&mut self) -> Result<NextSample, crate::Error> {
let next = self.inner.next_sample()?;
if let NextSample::Finished = next {
if let Some(sender) = self.sender.take() {
let _res = sender.send(());
}
}
Ok(next)
}
fn on_start_of_batch(&mut self) {
self.inner.on_start_of_batch();
}
}
impl<S> Wrapper for CompletionNotifier<S>
where
S: Sound,
{
type Inner = S;
fn inner(&self) -> &S {
&self.inner
}
fn inner_mut(&mut self) -> &mut Self::Inner {
&mut self.inner
}
fn into_inner(self) -> S {
self.inner
}
}