From 3d1a5ac54c75a4e6662f43b0996db10627f7dccd Mon Sep 17 00:00:00 2001 From: Zachary Churchill Date: Sat, 11 Mar 2023 14:51:03 -0500 Subject: [PATCH] add `EmptyCallback` source (#430) Co-authored-by: est31 --- src/source/empty_callback.rs | 55 ++++++++++++++++++++++++++++++++++++ src/source/mod.rs | 2 ++ 2 files changed, 57 insertions(+) create mode 100644 src/source/empty_callback.rs diff --git a/src/source/empty_callback.rs b/src/source/empty_callback.rs new file mode 100644 index 0000000..163f3be --- /dev/null +++ b/src/source/empty_callback.rs @@ -0,0 +1,55 @@ +use std::marker::PhantomData; +use std::time::Duration; + +use crate::{Sample, Source}; + +/// An empty source which executes a callback function +pub struct EmptyCallback { + pub phantom_data: PhantomData, + pub callback: Box, +} + +impl EmptyCallback { + #[inline] + pub fn new(callback: Box) -> EmptyCallback { + EmptyCallback { + phantom_data: PhantomData, + callback, + } + } +} + +impl Iterator for EmptyCallback { + type Item = S; + + #[inline] + fn next(&mut self) -> Option { + (self.callback)(); + None + } +} + +impl Source for EmptyCallback +where + S: Sample, +{ + #[inline] + fn current_frame_len(&self) -> Option { + None + } + + #[inline] + fn channels(&self) -> u16 { + 1 + } + + #[inline] + fn sample_rate(&self) -> u32 { + 48000 + } + + #[inline] + fn total_duration(&self) -> Option { + Some(Duration::new(0, 0)) + } +} diff --git a/src/source/mod.rs b/src/source/mod.rs index 45796fe..8b2eb6b 100644 --- a/src/source/mod.rs +++ b/src/source/mod.rs @@ -9,6 +9,7 @@ use crate::Sample; pub use self::amplify::Amplify; pub use self::blt::BltFilter; pub use self::buffered::Buffered; +pub use self::empty_callback::EmptyCallback; pub use self::channel_volume::ChannelVolume; pub use self::crossfade::Crossfade; pub use self::delay::Delay; @@ -35,6 +36,7 @@ pub use self::zero::Zero; mod amplify; mod blt; mod buffered; +mod empty_callback; mod channel_volume; mod crossfade; mod delay;