Cleanup and add tests for convert_samples_rate

This commit is contained in:
Pierre Krieger 2014-12-22 16:31:37 +01:00
parent 4239fae948
commit aa83f64443
3 changed files with 65 additions and 18 deletions

View file

@ -25,9 +25,12 @@ impl SampleFormat {
/// Trait for containers that contain PCM data.
#[unstable = "Will be rewritten with associated types"]
pub trait Sample: Copy {
pub trait Sample: Copy + Clone {
fn get_format(Option<Self>) -> SampleFormat;
/// Returns `(self + other) / 2`.
fn interpolate(self, other: Self) -> Self;
/// Turns the data into samples of type `I16`.
fn to_vec_i16(&[Self]) -> Cow<Vec<i16>, [i16]>;
/// Turns the data into samples of type `U16`.
@ -41,6 +44,10 @@ impl Sample for u16 {
SampleFormat::U16
}
fn interpolate(self, other: u16) -> u16 {
(self + other) / 2
}
fn to_vec_i16(input: &[u16]) -> Cow<Vec<i16>, [i16]> {
Cow::Owned(input.iter().map(|&value| {
if value >= 32768 {
@ -65,6 +72,10 @@ impl Sample for i16 {
SampleFormat::I16
}
fn interpolate(self, other: i16) -> i16 {
(self + other) / 2
}
fn to_vec_i16(input: &[i16]) -> Cow<Vec<i16>, [i16]> {
Cow::Borrowed(input)
}
@ -95,6 +106,10 @@ impl Sample for f32 {
SampleFormat::F32
}
fn interpolate(self, other: f32) -> f32 {
(self + other) / 2.0
}
fn to_vec_i16(input: &[f32]) -> Cow<Vec<i16>, [i16]> {
unimplemented!()
}