Write the value 0 if there is no sample data available anymore

This commit is contained in:
Pierre Krieger 2015-09-10 18:15:13 +02:00
parent b86d1ff48c
commit b7462737a3

View file

@ -17,6 +17,8 @@ use cpal::SampleFormat;
pub fn convert_and_write<I, S>(mut samples: I, output: &mut UnknownTypeBuffer)
where I: Iterator<Item=S>, S: Sample
{
let samples = samples.chain(iter::repeat(Sample::zero_value()));
match output {
&mut UnknownTypeBuffer::U16(ref mut buffer) => {
for (i, o) in samples.zip(buffer.iter_mut()) {
@ -43,6 +45,8 @@ pub trait Sample: cpal::Sample {
/// Returns the average inside a list.
fn average(data: &[Self]) -> Self;
fn zero_value() -> Self;
fn to_i16(&self) -> i16;
fn to_u16(&self) -> u16;
fn to_f32(&self) -> f32;
@ -55,6 +59,11 @@ impl Sample for u16 {
(sum / data.len()) as u16
}
#[inline]
fn zero_value() -> u16 {
32768
}
#[inline]
fn to_i16(&self) -> i16 {
if *self >= 32768 {
@ -82,6 +91,11 @@ impl Sample for i16 {
(sum / data.len() as isize) as i16
}
#[inline]
fn zero_value() -> i16 {
0
}
#[inline]
fn to_i16(&self) -> i16 {
*self
@ -113,6 +127,11 @@ impl Sample for f32 {
(sum / data.len() as f64) as f32
}
#[inline]
fn zero_value() -> f32 {
0.0
}
#[inline]
fn to_i16(&self) -> i16 {
if *self >= 0.0 {