#[cfg(feature = "desktop")]
#[path = "./audio/sdl2_audio.rs"]
pub mod audio_util;
#[cfg(feature = "web")]
#[path = "./audio/web_audio.rs"]
pub mod audio_util;
pub type DeviceCB<State> = fn(&mut State, &mut [f32]);
pub use audio_util::*;
pub trait GenericAudioSpecs {
fn sample_rate(&self) -> Option<u32>;
fn bits_per_sample(&self) -> Option<u32>;
fn channels(&self) ->Option<u32>;
}
pub struct DesiredSpecs {
pub sample_rate: Option<u32>,
pub channels: Option<u32>,
pub buffer_size: Option<u32>,
}
impl DesiredSpecs {
#[allow(dead_code)]
fn get_specs(&self) -> (u32, usize, usize) {
(
self.sample_rate.unwrap_or(48000),
self.channels.unwrap_or(2) as usize,
self.buffer_size.unwrap_or(1024) as usize,
)
}
}
pub struct AudioDeviceCore<Callback, State> {
cb: Option<Callback>,
state: Option<State>,
desired_specs: DesiredSpecs,
}
impl<Callback, State> AudioDeviceCore<Callback, State>
where
Callback: FnMut(&mut State, &mut [f32]) + Copy + 'static,
State: 'static,
{
pub fn new() -> Self {
Self {
cb: None,
state: None,
desired_specs: DesiredSpecs {
sample_rate: None,
channels: None,
buffer_size: None,
},
}
}
pub fn with_callback(mut self, cb: Callback) -> Self {
self.cb = Some(cb);
self
}
pub fn with_state(mut self, state: State) -> Self {
self.state = Some(state);
self
}
pub fn with_specs(mut self, specs: DesiredSpecs) -> Self {
self.desired_specs = specs;
self
}
pub fn callback(&self) -> Callback {
self.cb.unwrap()
}
}