Use a proper type for WAVE decoding instead of a Box

This commit is contained in:
Pierre Krieger 2015-09-27 10:41:46 +02:00
parent ab59b0fc8b
commit 112c8c94e5
2 changed files with 18 additions and 15 deletions

View file

@ -1,19 +1,21 @@
use std::io::{Read, Seek, SeekFrom}; use std::io::{Read, Seek, SeekFrom};
use super::Decoder; use super::Decoder;
use conversions;
use conversions::ChannelsCountConverter;
use conversions::SamplesRateConverter;
use conversions::DataConverter;
use cpal; use cpal;
use hound::WavReader; use hound::WavReader;
pub struct WavDecoder { pub struct WavDecoder<R> where R: Read + Seek {
reader: Box<Iterator<Item=f32> + Send>, reader: DataConverter<SamplesRateConverter<ChannelsCountConverter<SamplesIterator<R>>>, f32>,
total_duration_ms: u32, total_duration_ms: u32,
} }
impl WavDecoder { impl<R> WavDecoder<R> where R: Read + Seek {
pub fn new<R>(mut data: R, output_channels: u16, output_samples_rate: u32) pub fn new(mut data: R, output_channels: u16, output_samples_rate: u32)
-> Result<WavDecoder, R> -> Result<WavDecoder<R>, R>
where R: Read + Seek + Send + 'static
{ {
if !is_wave(data.by_ref()) { if !is_wave(data.by_ref()) {
return Err(data); return Err(data);
@ -24,13 +26,13 @@ impl WavDecoder {
let total_duration_ms = reader.duration() * 1000 / spec.sample_rate; let total_duration_ms = reader.duration() * 1000 / spec.sample_rate;
let reader = SamplesIterator { reader: reader, samples_read: 0 }; let reader = SamplesIterator { reader: reader, samples_read: 0 };
let reader = conversions::ChannelsCountConverter::new(reader, spec.channels, 2); let reader = ChannelsCountConverter::new(reader, spec.channels, 2);
let reader = conversions::SamplesRateConverter::new(reader, cpal::SamplesRate(spec.sample_rate), let reader = SamplesRateConverter::new(reader, cpal::SamplesRate(spec.sample_rate),
cpal::SamplesRate(output_samples_rate), output_channels); cpal::SamplesRate(output_samples_rate), output_channels);
let reader = conversions::DataConverter::new(reader); let reader = DataConverter::new(reader);
Ok(WavDecoder { Ok(WavDecoder {
reader: Box::new(reader), reader: reader,
total_duration_ms: total_duration_ms, total_duration_ms: total_duration_ms,
}) })
} }
@ -76,13 +78,13 @@ fn is_wave<R>(mut data: R) -> bool where R: Read + Seek {
true true
} }
impl Decoder for WavDecoder { impl<R> Decoder for WavDecoder<R> where R: Read + Seek {
fn get_total_duration_ms(&self) -> u32 { fn get_total_duration_ms(&self) -> u32 {
self.total_duration_ms self.total_duration_ms
} }
} }
impl Iterator for WavDecoder { impl<R> Iterator for WavDecoder<R> where R: Read + Seek {
type Item = f32; type Item = f32;
#[inline] #[inline]
@ -96,4 +98,4 @@ impl Iterator for WavDecoder {
} }
} }
impl ExactSizeIterator for WavDecoder {} impl<R> ExactSizeIterator for WavDecoder<R> where R: Read + Seek {}

View file

@ -227,6 +227,7 @@ fn background(rx: Receiver<Command>) {
// sleeping so that we get a loop every `FIXED_STEP_MS` millisecond // sleeping so that we get a loop every `FIXED_STEP_MS` millisecond
let time_taken = time::precise_time_ns() - before_updates; let time_taken = time::precise_time_ns() - before_updates;
println!("{:?}", time_taken / 1000);
let sleep = FIXED_STEP_NS.saturating_sub(time_taken); let sleep = FIXED_STEP_NS.saturating_sub(time_taken);
thread::park_timeout_ms((sleep / 1000000) as u32); thread::park_timeout_ms((sleep / 1000000) as u32);
} }