use crate::sound::data::SampleRate;
use crate::sound::mixer::Command;
#[cfg(not(target_arch = "wasm32"))]
use native as sys;
#[cfg(target_arch = "wasm32")]
use web as sys;
pub(crate) struct Output(Option<sys::Backend>);
impl Output {
pub(crate) fn open() -> Self {
sys::open().map_or_else(Self::silent, |backend| Self(Some(backend)))
}
pub(crate) const fn silent() -> Self {
Self(None)
}
pub(crate) fn rate(&self) -> Option<MixRate> {
self.0.as_ref().and_then(sys::Backend::rate)
}
pub(crate) fn frame(&mut self, commands: impl Iterator<Item = Command>) {
let Some(backend) = &mut self.0 else {
return;
};
backend.frame(commands);
}
pub(crate) fn unlock(&mut self) {
if let Some(backend) = &mut self.0 {
backend.unlock();
}
}
pub(crate) fn unlocked(&self) -> bool {
self.0.as_ref().is_none_or(sys::Backend::unlocked)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct MixRate(SampleRate);
impl MixRate {
#[cfg(test)]
pub(crate) const fn chosen(rate: SampleRate) -> Self {
Self(rate)
}
pub(crate) const fn get(self) -> SampleRate {
self.0
}
}
#[cfg(not(target_arch = "wasm32"))]
mod native;
#[cfg(target_arch = "wasm32")]
mod web;