use crate::Sound;
use super::{SetSpeed, SetVolume};
pub trait SetStopped {
fn set_stopped(&mut self);
}
pub struct Stoppable<S: Sound> {
inner: S,
stopped: bool,
}
impl<S> Stoppable<S>
where
S: Sound,
{
pub fn new(inner: S) -> Self {
Stoppable {
inner,
stopped: false,
}
}
pub fn inner(&self) -> &S {
&self.inner
}
pub fn inner_mut(&mut self) -> &mut S {
&mut self.inner
}
pub fn into_inner(self) -> S {
self.inner
}
}
impl<S> Sound for Stoppable<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<crate::NextSample, crate::Error> {
if self.stopped {
return Ok(crate::NextSample::Finished);
}
self.inner.next_sample()
}
fn on_start_of_batch(&mut self) {
self.inner.on_start_of_batch()
}
}
impl<S> Stoppable<S>
where
S: Sound,
{
pub fn stopped(&self) -> bool {
self.stopped
}
}
impl<S> SetStopped for Stoppable<S>
where
S: Sound,
{
fn set_stopped(&mut self) {
self.stopped = true;
}
}
impl<S> SetVolume for Stoppable<S>
where
S: Sound + SetVolume,
{
fn set_volume(&mut self, multiplier: f32) {
self.inner.set_volume(multiplier)
}
}
impl<S> SetSpeed for Stoppable<S>
where
S: Sound + SetSpeed,
{
fn set_speed(&mut self, multiplier: f32) {
self.inner.set_speed(multiplier)
}
}
#[cfg(test)]
#[path = "./tests/stoppable.rs"]
mod tests;