use std::time::Duration;
use crate::{Sample, Source};
pub fn stoppable<I>(source: I) -> Stoppable<I> {
Stoppable {
input: source,
stopped: false,
}
}
#[derive(Clone, Debug)]
pub struct Stoppable<I> {
input: I,
stopped: bool,
}
impl<I> Stoppable<I> {
#[inline]
pub fn stop(&mut self) {
self.stopped = true;
}
#[inline]
pub fn inner(&self) -> &I {
&self.input
}
#[inline]
pub fn inner_mut(&mut self) -> &mut I {
&mut self.input
}
#[inline]
pub fn into_inner(self) -> I {
self.input
}
}
impl<I> Iterator for Stoppable<I>
where
I: Source,
I::Item: Sample,
{
type Item = I::Item;
#[inline]
fn next(&mut self) -> Option<I::Item> {
if self.stopped {
None
} else {
self.input.next()
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.input.size_hint()
}
}
impl<I> Source for Stoppable<I>
where
I: Source,
I::Item: Sample,
{
#[inline]
fn current_frame_len(&self) -> Option<usize> {
self.input.current_frame_len()
}
#[inline]
fn channels(&self) -> u16 {
self.input.channels()
}
#[inline]
fn sample_rate(&self) -> u32 {
self.input.sample_rate()
}
#[inline]
fn total_duration(&self) -> Option<Duration> {
self.input.total_duration()
}
}