use std::{
sync::atomic::{AtomicUsize, Ordering},
time::Instant,
};
pub(crate) mod playback;
pub mod status;
pub mod amplified;
pub mod converted;
pub mod empty;
pub mod file;
pub mod guarded;
pub mod mapped;
pub mod measured;
pub mod metered;
pub mod mixed;
pub mod panned;
pub mod resampled;
pub mod synth;
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct SourceTime {
pub pos_in_frames: u64,
pub pos_instant: Instant,
}
impl SourceTime {
pub fn new() -> Self {
Self {
pos_in_frames: 0,
pos_instant: Instant::now(),
}
}
pub fn with_added_frames(&self, frames: u64) -> Self {
let mut copy = *self;
copy.add_frames(frames);
copy
}
pub fn add_frames(&mut self, frames: u64) {
self.pos_in_frames += frames;
}
}
impl Default for SourceTime {
fn default() -> Self {
Self::new()
}
}
pub trait Source: Send + Sync + 'static {
fn into_box(self) -> Box<dyn Source>
where
Self: Sized,
{
Box::new(self)
}
fn sample_rate(&self) -> u32;
fn channel_count(&self) -> usize;
fn is_exhausted(&self) -> bool;
fn weight(&self) -> usize;
fn write(&mut self, output: &mut [f32], time: &SourceTime) -> usize;
}
pub(crate) fn unique_source_id() -> usize {
static ID_COUNTER: AtomicUsize = AtomicUsize::new(1);
ID_COUNTER.fetch_add(1, Ordering::Relaxed)
}
impl Source for Box<dyn Source> {
fn into_box(self) -> Box<dyn Source> {
self
}
fn sample_rate(&self) -> u32 {
(**self).sample_rate()
}
fn channel_count(&self) -> usize {
(**self).channel_count()
}
fn is_exhausted(&self) -> bool {
(**self).is_exhausted()
}
fn weight(&self) -> usize {
(**self).weight()
}
fn write(&mut self, output: &mut [f32], time: &SourceTime) -> usize {
(**self).write(output, time)
}
}