pub trait SampleConvert {
fn to_f32(self) -> f32;
fn from_f32(sample: f32) -> Self;
}
impl SampleConvert for i16 {
#[inline(always)]
fn to_f32(self) -> f32 {
if self < 0 {
self as f32 / 32768.0
} else {
self as f32 / 32767.0
}
}
#[inline(always)]
fn from_f32(sample: f32) -> Self {
let clamped = sample.clamp(-1.0, 1.0);
if clamped < 0.0 {
(clamped * 32768.0).round() as i16
} else {
(clamped * 32767.0).round() as i16
}
}
}
impl SampleConvert for i32 {
#[inline(always)]
fn to_f32(self) -> f32 {
self as f32 / 2147483648.0
}
#[inline(always)]
fn from_f32(sample: f32) -> Self {
(sample.clamp(-1.0, 1.0) * 2147483648.0).round() as i32
}
}
impl SampleConvert for f32 {
#[inline(always)]
fn to_f32(self) -> f32 {
self
}
#[inline(always)]
fn from_f32(sample: f32) -> Self {
sample
}
}