use cpal::{FromSample, Sample, SampleFormat, SizedSample};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OutputType {
ASIO,
WASAPI,
DirectSound,
WDMKS,
MME,
CoreAudio,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SampleRate {
Hz8k = 8000,
Hz11_025k = 11025,
Hz16k = 16000,
Hz44_1k = 44100,
Hz48k = 48000,
Hz88_2k = 88200,
Hz96k = 96000,
Hz176_4k = 176400,
Hz192k = 192000,
Hz352_8 = 352800,
}
impl SampleRate {
pub const ALL: [SampleRate; 10] = [
SampleRate::Hz8k,
SampleRate::Hz11_025k,
SampleRate::Hz16k,
SampleRate::Hz44_1k,
SampleRate::Hz48k,
SampleRate::Hz88_2k,
SampleRate::Hz96k,
SampleRate::Hz176_4k,
SampleRate::Hz192k,
SampleRate::Hz352_8,
];
pub fn hz(self) -> u32 {
self as u32
}
pub fn from_hz(hz: u32) -> Option<Self> {
Self::ALL.into_iter().find(|rate| rate.hz() == hz)
}
}
pub trait SampleType: SizedSample + Send + 'static {
const SILENCE: Self;
fn format() -> SampleFormat;
fn mix(self, other: Self) -> Self;
fn to_f32(self) -> f32;
fn from_f32(sample: f32) -> Self;
}
impl<T> SampleType for T
where
T: SizedSample + Send + 'static + FromSample<f32>,
f32: FromSample<T>,
{
const SILENCE: Self = <T as Sample>::EQUILIBRIUM;
fn format() -> SampleFormat {
<T as SizedSample>::FORMAT
}
fn mix(self, other: Self) -> Self {
self.add_amp(other.to_signed_sample())
}
fn to_f32(self) -> f32 {
self.to_sample::<f32>()
}
fn from_f32(sample: f32) -> Self {
sample.to_sample::<T>()
}
}