add EmptyCallback source (#430)

Co-authored-by: est31 <est31@users.noreply.github.com>
This commit is contained in:
Zachary Churchill 2023-03-11 14:51:03 -05:00 committed by GitHub
parent d5b9ae3467
commit 3d1a5ac54c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View file

@ -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<S> {
pub phantom_data: PhantomData<S>,
pub callback: Box<dyn Send + Fn()>,
}
impl<S> EmptyCallback<S> {
#[inline]
pub fn new(callback: Box<dyn Send + Fn()>) -> EmptyCallback<S> {
EmptyCallback {
phantom_data: PhantomData,
callback,
}
}
}
impl<S> Iterator for EmptyCallback<S> {
type Item = S;
#[inline]
fn next(&mut self) -> Option<S> {
(self.callback)();
None
}
}
impl<S> Source for EmptyCallback<S>
where
S: Sample,
{
#[inline]
fn current_frame_len(&self) -> Option<usize> {
None
}
#[inline]
fn channels(&self) -> u16 {
1
}
#[inline]
fn sample_rate(&self) -> u32 {
48000
}
#[inline]
fn total_duration(&self) -> Option<Duration> {
Some(Duration::new(0, 0))
}
}

View file

@ -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;