#![allow(clippy::needless_doctest_main)]
use std::fmt::{Debug, Display, Formatter, Result};
use fon::{chan::Ch32, Frame, Resampler, Sink};
use crate::ffi;
#[derive(Default)]
pub struct Speakers(pub(super) ffi::Speakers);
impl Display for Speakers {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
self.0.fmt(f)
}
}
impl Debug for Speakers {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
<Self as Display>::fmt(self, f)
}
}
impl Speakers {
pub fn query() -> Vec<Self> {
ffi::device_list(Self)
}
pub fn supports<F>(&self) -> bool
where
F: Frame<Chan = Ch32>,
{
let count = F::CHAN_COUNT;
let bit = count - 1;
(self.0.channels() & (1 << bit)) != 0
}
pub async fn play<F: Frame<Chan = Ch32>>(&mut self) -> SpeakersSink<'_, F> {
(&mut self.0).await;
SpeakersSink(self.0.play())
}
}
pub struct SpeakersSink<'a, F: Frame<Chan = Ch32>>(ffi::SpeakersSink<'a, F>);
impl<F: Frame<Chan = Ch32>> Debug for SpeakersSink<'_, F> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result {
write!(fmt, "SpeakersSink(rate: {})", self.sample_rate())
}
}
impl<F: Frame<Chan = Ch32>> Sink<F> for SpeakersSink<'_, F> {
fn sample_rate(&self) -> f64 {
self.0.sample_rate()
}
fn resampler(&mut self) -> &mut Resampler<F> {
self.0.resampler()
}
fn buffer(&mut self) -> &mut [F] {
self.0.buffer()
}
}