use std::cmp;
use std::fs;
use std::io;
use std::marker;
use std::mem;
use std::path;
use super::{Error, Result, Sample, SampleFormat, WavSpec};
pub trait ReadExt: io::Read {
fn read_into(&mut self, buf: &mut [u8]) -> io::Result<()>;
fn read_bytes(&mut self, n: usize) -> io::Result<Vec<u8>>;
fn skip_bytes(&mut self, n: usize) -> io::Result<()>;
fn read_i8(&mut self) -> io::Result<i8>;
fn read_u8(&mut self) -> io::Result<u8>;
fn read_le_i16(&mut self) -> io::Result<i16>;
fn read_le_u16(&mut self) -> io::Result<u16>;
fn read_le_i24(&mut self) -> io::Result<i32>;
fn read_le_u24(&mut self) -> io::Result<u32>;
fn read_le_i32(&mut self) -> io::Result<i32>;
fn read_le_u32(&mut self) -> io::Result<u32>;
fn read_le_f32(&mut self) -> io::Result<f32>;
}
impl<R> ReadExt for R
where R: io::Read
{
#[inline(always)]
fn read_into(&mut self, buf: &mut [u8]) -> io::Result<()> {
let mut n = 0;
while n < buf.len() {
let progress = try!(self.read(&mut buf[n..]));
if progress > 0 {
n += progress;
} else {
return Err(io::Error::new(io::ErrorKind::Other, "Failed to read enough bytes."));
}
}
Ok(())
}
#[inline(always)]
fn skip_bytes(&mut self, n: usize) -> io::Result<()> {
let mut n_read = 0;
let mut buf = [0u8; 1024];
while n_read < n {
let end = cmp::min(n - n_read, 1024);
let progress = try!(self.read(&mut buf[0..end]));
if progress > 0 {
n_read += progress;
} else {
return Err(io::Error::new(io::ErrorKind::Other, "Failed to read enough bytes."));
}
}
Ok(())
}
#[inline(always)]
fn read_bytes(&mut self, n: usize) -> io::Result<Vec<u8>> {
let mut buf = Vec::with_capacity(n);
unsafe { buf.set_len(n); }
try!(self.read_into(&mut buf[..]));
Ok(buf)
}
#[inline(always)]
fn read_i8(&mut self) -> io::Result<i8> {
self.read_u8().map(|x| x as i8)
}
#[inline(always)]
fn read_u8(&mut self) -> io::Result<u8> {
let mut buf = [0u8; 1];
try!(self.read_into(&mut buf));
Ok(buf[0])
}
#[inline(always)]
fn read_le_i16(&mut self) -> io::Result<i16> {
self.read_le_u16().map(|x| x as i16)
}
#[inline(always)]
fn read_le_u16(&mut self) -> io::Result<u16> {
let mut buf = [0u8; 2];
try!(self.read_into(&mut buf));
Ok((buf[1] as u16) << 8 | (buf[0] as u16))
}
#[inline(always)]
fn read_le_i24(&mut self) -> io::Result<i32> {
self.read_le_u24().map(|x|
if x & (1 << 23) == 0 {
x as i32
} else {
(x | 0xff_00_00_00) as i32
}
)
}
#[inline(always)]
fn read_le_u24(&mut self) -> io::Result<u32> {
let mut buf = [0u8; 3];
try!(self.read_into(&mut buf));
Ok((buf[2] as u32) << 16 | (buf[1] as u32) << 8 | (buf[0] as u32))
}
#[inline(always)]
fn read_le_i32(&mut self) -> io::Result<i32> {
self.read_le_u32().map(|x| x as i32)
}
#[inline(always)]
fn read_le_u32(&mut self) -> io::Result<u32> {
let mut buf = [0u8; 4];
try!(self.read_into(&mut buf));
Ok((buf[3] as u32) << 24 | (buf[2] as u32) << 16 |
(buf[1] as u32) << 8 | (buf[0] as u32) << 0)
}
#[inline(always)]
fn read_le_f32(&mut self) -> io::Result<f32> {
self.read_le_u32().map(|u| unsafe { mem::transmute(u) })
}
}
enum ChunkKind {
Fmt,
Fact,
Data,
Unknown,
}
struct ChunkHeader {
pub kind: ChunkKind,
pub len: u32,
}
#[derive(Clone, Copy)]
pub struct WavSpecEx {
pub spec: WavSpec,
pub bytes_per_sample: u16,
}
pub struct WavReader<R> {
spec: WavSpec,
bytes_per_sample: u16,
num_samples: u32,
samples_read: u32,
reader: R,
}
pub struct WavSamples<'wr, R, S>
where R: 'wr
{
reader: &'wr mut WavReader<R>,
phantom_sample: marker::PhantomData<S>,
}
pub struct WavIntoSamples<R, S> {
reader: WavReader<R>,
phantom_sample: marker::PhantomData<S>,
}
pub fn read_wave_header<R: io::Read>(reader: &mut R) -> Result<u64> {
if b"RIFF" != &try!(reader.read_bytes(4))[..] {
return Err(Error::FormatError("no RIFF tag found"));
}
let file_len = try!(reader.read_le_u32());
if b"WAVE" != &try!(reader.read_bytes(4))[..] {
return Err(Error::FormatError("no WAVE tag found"));
}
Ok(file_len as u64 + 8)
}
pub fn read_until_data<R: io::Read>(mut reader: R) -> Result<(WavSpecEx, u32)> {
let mut spec_opt = None;
loop {
let header = try!(WavReader::read_chunk_header(&mut reader));
match header.kind {
ChunkKind::Fmt => {
let spec = try!(WavReader::read_fmt_chunk(&mut reader, header.len));
spec_opt = Some(spec);
}
ChunkKind::Fact => {
let _samples_per_channel = reader.read_le_u32();
}
ChunkKind::Data => {
if let Some(spec) = spec_opt {
return Ok((spec, header.len));
} else {
return Err(Error::FormatError("missing fmt chunk"));
}
}
ChunkKind::Unknown => {
try!(reader.skip_bytes(header.len as usize));
}
}
}
}
impl<R> WavReader<R>
where R: io::Read
{
fn read_chunk_header(reader: &mut R) -> Result<ChunkHeader> {
let mut kind_str = [0; 4];
try!(reader.read_into(&mut kind_str));
let len = try!(reader.read_le_u32());
let kind = match &kind_str[..] {
b"fmt " => ChunkKind::Fmt,
b"fact" => ChunkKind::Fact,
b"data" => ChunkKind::Data,
_ => ChunkKind::Unknown,
};
Ok(ChunkHeader { kind: kind, len: len })
}
fn read_fmt_chunk(reader: &mut R, chunk_len: u32) -> Result<WavSpecEx> {
if chunk_len < 16 {
return Err(Error::FormatError("invalid fmt chunk size"));
}
let format_tag = try!(reader.read_le_u16());
let n_channels = try!(reader.read_le_u16());
let n_samples_per_sec = try!(reader.read_le_u32());
let n_bytes_per_sec = try!(reader.read_le_u32());
let block_align = try!(reader.read_le_u16());
let bits_per_sample = try!(reader.read_le_u16());
if n_channels == 0 {
return Err(Error::FormatError("file contains zero channels"));
}
if (Some(bits_per_sample) != (block_align / n_channels).checked_mul(8)) ||
(Some(n_bytes_per_sec) != (block_align as u32).checked_mul(n_samples_per_sec)) {
return Err(Error::FormatError("inconsistent fmt chunk"));
}
if bits_per_sample % 8 != 0 {
return Err(Error::FormatError("bits per sample is not a multiple of 8"));
}
if bits_per_sample == 0 {
return Err(Error::FormatError("bits per sample is 0"));
}
let spec = WavSpec {
channels: n_channels,
sample_rate: n_samples_per_sec,
bits_per_sample: bits_per_sample,
sample_format: SampleFormat::Int,
};
const PCM: u16 = 0x0001;
const ADPCM: u16 = 0x0002;
const IEEE_FLOAT: u16 = 0x0003;
const EXTENSIBLE: u16 = 0xfffe;
match format_tag {
PCM => WavReader::read_wave_format_pcm(reader, chunk_len, spec),
ADPCM => Err(Error::Unsupported),
IEEE_FLOAT => WavReader::read_wave_format_ieee_float(reader, chunk_len, spec),
EXTENSIBLE => WavReader::read_wave_format_extensible(reader, chunk_len, spec),
_ => Err(Error::Unsupported),
}
}
fn read_wave_format_pcm(mut reader: R, chunk_len: u32, spec: WavSpec) -> Result<WavSpecEx> {
let is_wave_format_ex = match chunk_len {
16 => false,
18 => true,
40 => true,
_ => return Err(Error::FormatError("unexpected fmt chunk size")),
};
if is_wave_format_ex {
let _cb_size = try!(reader.read_le_u16());
match spec.bits_per_sample {
8 => {}
16 => {}
24 => {}
_ => return Err(Error::FormatError("bits per sample is not 8 or 16")),
}
}
if chunk_len == 40 {
try!(reader.skip_bytes(22));
}
let spec_ex = WavSpecEx {
spec: spec,
bytes_per_sample: spec.bits_per_sample / 8,
};
Ok(spec_ex)
}
fn read_wave_format_ieee_float(mut reader: R, chunk_len: u32, spec: WavSpec)
-> Result<WavSpecEx> {
let is_wave_format_ex = chunk_len == 18;
if !is_wave_format_ex && chunk_len != 16 {
return Err(Error::FormatError("unexpected fmt chunk size"));
}
if is_wave_format_ex {
let cb_size = try!(reader.read_le_u16());
if cb_size != 0 {
return Err(Error::FormatError("unexpected WAVEFORMATEX size"));
}
}
if spec.bits_per_sample != 32 {
return Err(Error::FormatError("bits per sample is not 32"));
}
let spec_ex = WavSpecEx {
spec: WavSpec {
sample_format: SampleFormat::Float,
..spec
},
bytes_per_sample: spec.bits_per_sample / 8,
};
Ok(spec_ex)
}
fn read_wave_format_extensible(mut reader: R, chunk_len: u32, spec: WavSpec)
-> Result<WavSpecEx> {
if chunk_len < 40 {
return Err(Error::FormatError("unexpected fmt chunk size"));
}
let cb_size = try!(reader.read_le_u16());
if cb_size != 22 {
return Err(Error::FormatError("unexpected WAVEFORMATEXTENSIBLE size"));
}
let valid_bits_per_sample = try!(reader.read_le_u16());
let _channel_mask = try!(reader.read_le_u32());
let mut subformat = [0u8; 16];
try!(reader.read_into(&mut subformat));
let sample_format = match subformat {
super::KSDATAFORMAT_SUBTYPE_PCM => SampleFormat::Int,
super::KSDATAFORMAT_SUBTYPE_IEEE_FLOAT => SampleFormat::Float,
_ => return Err(Error::Unsupported),
};
let spec_ex = WavSpecEx {
spec: WavSpec {
bits_per_sample: valid_bits_per_sample,
sample_format: sample_format,
..spec
},
bytes_per_sample: spec.bits_per_sample / 8,
};
Ok(spec_ex)
}
pub fn new(mut reader: R) -> Result<WavReader<R>> {
try!(read_wave_header(&mut reader));
let (spec_ex, data_len) = try!(read_until_data(&mut reader));
let num_samples = data_len / spec_ex.bytes_per_sample as u32;
if num_samples * spec_ex.bytes_per_sample as u32 != data_len {
let msg = "data chunk length is not a multiple of sample size";
return Err(Error::FormatError(msg));
}
if num_samples % spec_ex.spec.channels as u32 != 0 {
return Err(Error::FormatError("invalid data chunk length"));
}
let wav_reader = WavReader {
spec: spec_ex.spec,
bytes_per_sample: spec_ex.bytes_per_sample,
num_samples: num_samples,
samples_read: 0,
reader: reader,
};
Ok(wav_reader)
}
pub fn spec(&self) -> WavSpec {
self.spec
}
pub fn samples<'wr, S: Sample>(&'wr mut self) -> WavSamples<'wr, R, S> {
WavSamples {
reader: self,
phantom_sample: marker::PhantomData,
}
}
pub fn into_samples<S: Sample>(self) -> WavIntoSamples<R, S> {
WavIntoSamples {
reader: self,
phantom_sample: marker::PhantomData,
}
}
pub fn duration(&self) -> u32 {
self.num_samples / self.spec.channels as u32
}
pub fn len(&self) -> u32 {
self.num_samples
}
pub fn into_inner(self) -> R {
self.reader
}
pub fn seek(&mut self, time: u32) -> io::Result<()>
where R: io::Seek,
{
let bytes_per_sample = self.spec.bits_per_sample / 8;
let sample_position = time * self.spec.channels as u32;
let offset_samples = sample_position as i64 - self.samples_read as i64;
let offset_bytes = offset_samples * bytes_per_sample as i64;
try!(self.reader.seek(io::SeekFrom::Current(offset_bytes)));
self.samples_read = sample_position;
Ok(())
}
}
impl WavReader<io::BufReader<fs::File>> {
pub fn open<P: AsRef<path::Path>>(filename: P) -> Result<WavReader<io::BufReader<fs::File>>> {
let file = try!(fs::File::open(filename));
let buf_reader = io::BufReader::new(file);
WavReader::new(buf_reader)
}
}
fn iter_next<R, S>(reader: &mut WavReader<R>) -> Option<Result<S>>
where R: io::Read,
S: Sample
{
if reader.samples_read < reader.num_samples {
reader.samples_read += 1;
let sample = Sample::read(&mut reader.reader,
reader.spec.sample_format,
reader.bytes_per_sample,
reader.spec.bits_per_sample);
Some(sample.map_err(Error::from))
} else {
None
}
}
fn iter_size_hint<R>(reader: &WavReader<R>) -> (usize, Option<usize>) {
let samples_left = reader.num_samples - reader.samples_read;
(samples_left as usize, Some(samples_left as usize))
}
impl<'wr, R, S> Iterator for WavSamples<'wr, R, S>
where R: io::Read,
S: Sample
{
type Item = Result<S>;
fn next(&mut self) -> Option<Result<S>> {
iter_next(&mut self.reader)
}
fn size_hint(&self) -> (usize, Option<usize>) {
iter_size_hint(&self.reader)
}
}
impl<'wr, R, S> ExactSizeIterator for WavSamples<'wr, R, S>
where R: io::Read,
S: Sample
{
}
impl<R, S> Iterator for WavIntoSamples<R, S>
where R: io::Read,
S: Sample
{
type Item = Result<S>;
fn next(&mut self) -> Option<Result<S>> {
iter_next(&mut self.reader)
}
fn size_hint(&self) -> (usize, Option<usize>) {
iter_size_hint(&self.reader)
}
}
impl<R, S> ExactSizeIterator for WavIntoSamples<R, S>
where R: io::Read,
S: Sample
{
}
#[test]
fn duration_and_len_agree() {
let files = &["testsamples/pcmwaveformat-16bit-44100Hz-mono.wav",
"testsamples/waveformatex-16bit-44100Hz-stereo.wav",
"testsamples/waveformatextensible-32bit-48kHz-stereo.wav"];
for fname in files {
let reader = WavReader::open(fname).unwrap();
assert_eq!(reader.spec().channels as u32 * reader.duration(),
reader.len());
}
}
#[test]
fn read_wav_pcm_wave_format_pcm() {
let mut wav_reader = WavReader::open("testsamples/pcmwaveformat-16bit-44100Hz-mono.wav")
.unwrap();
assert_eq!(wav_reader.spec().channels, 1);
assert_eq!(wav_reader.spec().sample_rate, 44100);
assert_eq!(wav_reader.spec().bits_per_sample, 16);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Int);
let samples: Vec<i16> = wav_reader.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(&samples[..], &[2, -3, 5, -7]);
}
#[test]
fn read_wav_skips_unknown_chunks() {
let files = ["testsamples/pcmwaveformat-16bit-44100Hz-mono-extra.wav",
"testsamples/waveformatex-16bit-44100Hz-mono-extra.wav"];
for file in &files {
let mut wav_reader = WavReader::open(file).unwrap();
assert_eq!(wav_reader.spec().channels, 1);
assert_eq!(wav_reader.spec().sample_rate, 44100);
assert_eq!(wav_reader.spec().bits_per_sample, 16);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Int);
let sample = wav_reader.samples::<i16>().next().unwrap().unwrap();
assert_eq!(sample, 2);
}
}
#[test]
fn len_and_size_hint_are_correct() {
let mut wav_reader = WavReader::open("testsamples/pcmwaveformat-16bit-44100Hz-mono.wav")
.unwrap();
assert_eq!(wav_reader.len(), 4);
{
let mut samples = wav_reader.samples::<i16>();
assert_eq!(samples.size_hint(), (4, Some(4)));
samples.next();
assert_eq!(samples.size_hint(), (3, Some(3)));
}
assert_eq!(wav_reader.len(), 4);
{
let mut samples = wav_reader.samples::<i16>();
assert_eq!(samples.size_hint(), (3, Some(3)));
samples.next();
assert_eq!(samples.size_hint(), (2, Some(2)));
}
}
#[test]
fn size_hint_is_exact() {
let files = &["testsamples/pcmwaveformat-16bit-44100Hz-mono.wav",
"testsamples/waveformatex-16bit-44100Hz-stereo.wav",
"testsamples/waveformatextensible-32bit-48kHz-stereo.wav"];
for fname in files {
let mut reader = WavReader::open(fname).unwrap();
let len = reader.len();
let mut iter = reader.samples::<i32>();
for i in 0..len {
let remaining = (len - i) as usize;
assert_eq!(iter.size_hint(), (remaining, Some(remaining)));
assert!(iter.next().is_some());
}
assert!(iter.next().is_none());
}
}
#[test]
fn samples_equals_into_samples() {
let wav_reader_val = WavReader::open("testsamples/pcmwaveformat-8bit-44100Hz-mono.wav").unwrap();
let mut wav_reader_ref = WavReader::open("testsamples/pcmwaveformat-8bit-44100Hz-mono.wav").unwrap();
let samples_val: Vec<i16> = wav_reader_val.into_samples()
.map(|r| r.unwrap())
.collect();
let samples_ref: Vec<i16> = wav_reader_ref.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(samples_val, samples_ref);
}
#[test]
fn read_wav_wave_format_ex_pcm() {
let mut wav_reader = WavReader::open("testsamples/waveformatex-16bit-44100Hz-mono.wav")
.unwrap();
assert_eq!(wav_reader.spec().channels, 1);
assert_eq!(wav_reader.spec().sample_rate, 44100);
assert_eq!(wav_reader.spec().bits_per_sample, 16);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Int);
let samples: Vec<i16> = wav_reader.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(&samples[..], &[2, -3, 5, -7]);
}
#[test]
fn read_wav_wave_format_ex_ieee_float() {
let mut wav_reader = WavReader::open("testsamples/waveformatex-ieeefloat-44100Hz-mono.wav")
.unwrap();
assert_eq!(wav_reader.spec().channels, 1);
assert_eq!(wav_reader.spec().sample_rate, 44100);
assert_eq!(wav_reader.spec().bits_per_sample, 32);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Float);
let samples: Vec<f32> = wav_reader.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(&samples[..], &[2.0, 3.0, -16411.0, 1019.0]);
}
#[test]
fn read_wav_stereo() {
let mut wav_reader = WavReader::open("testsamples/waveformatex-16bit-44100Hz-stereo.wav")
.unwrap();
assert_eq!(wav_reader.spec().channels, 2);
assert_eq!(wav_reader.spec().sample_rate, 44100);
assert_eq!(wav_reader.spec().bits_per_sample, 16);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Int);
let samples: Vec<i16> = wav_reader.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(&samples[..], &[2, -3, 5, -7, 11, -13, 17, -19]);
}
#[test]
fn read_wav_pcm_wave_format_8bit() {
let mut wav_reader = WavReader::open("testsamples/pcmwaveformat-8bit-44100Hz-mono.wav")
.unwrap();
assert_eq!(wav_reader.spec().channels, 1);
assert_eq!(wav_reader.spec().bits_per_sample, 8);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Int);
let samples: Vec<i16> = wav_reader.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(&samples[..], &[19, -53, 89, -127]);
}
#[test]
fn read_wav_wave_format_ex_8bit() {
let mut wav_reader = WavReader::open("testsamples/waveformatex-8bit-11025Hz-mono.wav").unwrap();
assert_eq!(wav_reader.spec().channels, 1);
assert_eq!(wav_reader.spec().bits_per_sample, 8);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Int);
let samples: Vec<i32> = wav_reader.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(&samples[..], &[-128, -128, -128, -128]);
}
#[test]
fn read_wav_wave_format_extensible_pcm_24bit() {
let mut wav_reader = WavReader::open("testsamples/waveformatextensible-24bit-192kHz-mono.wav")
.unwrap();
assert_eq!(wav_reader.spec().channels, 1);
assert_eq!(wav_reader.spec().sample_rate, 192_000);
assert_eq!(wav_reader.spec().bits_per_sample, 24);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Int);
let samples: Vec<i32> = wav_reader.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(&samples[..], &[-17, 4_194_319, -6_291_437, 8_355_817]);
}
#[test]
fn read_wav_32bit() {
let mut wav_reader = WavReader::open("testsamples/waveformatextensible-32bit-48kHz-stereo.wav")
.unwrap();
assert_eq!(wav_reader.spec().bits_per_sample, 32);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Int);
let samples: Vec<i32> = wav_reader.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(&samples[..], &[19, -229_373, 33_587_161, -2_147_483_497]);
}
#[test]
fn read_wav_wave_format_extensible_ieee_float() {
let mut wav_reader =
WavReader::open("testsamples/waveformatextensible-ieeefloat-44100Hz-mono.wav").unwrap();
assert_eq!(wav_reader.spec().channels, 1);
assert_eq!(wav_reader.spec().sample_rate, 44100);
assert_eq!(wav_reader.spec().bits_per_sample, 32);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Float);
let samples: Vec<f32> = wav_reader.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(&samples[..], &[2.0, 3.0, -16411.0, 1019.0]);
}
#[test]
fn read_wav_nonstandard_01() {
let mut wav_reader = WavReader::open("testsamples/nonstandard-01.wav").unwrap();
assert_eq!(wav_reader.spec().bits_per_sample, 24);
assert_eq!(wav_reader.spec().sample_format, SampleFormat::Int);
let samples: Vec<i32> = wav_reader.samples()
.map(|r| r.unwrap())
.collect();
assert_eq!(&samples[..], &[0, 0]);
}
#[test]
fn wide_read_should_signal_error() {
let mut reader24 = WavReader::open("testsamples/waveformatextensible-24bit-192kHz-mono.wav")
.unwrap();
assert!(reader24.samples::<i8>().next().unwrap().is_err());
assert!(reader24.samples::<i16>().next().unwrap().is_err());
assert!(reader24.samples::<i32>().next().unwrap().is_ok());
let mut reader32 = WavReader::open("testsamples/waveformatextensible-32bit-48kHz-stereo.wav")
.unwrap();
assert!(reader32.samples::<i8>().next().unwrap().is_err());
assert!(reader32.samples::<i16>().next().unwrap().is_err());
assert!(reader32.samples::<i32>().next().unwrap().is_ok());
}
#[test]
fn sample_format_mismatch_should_signal_error() {
let mut reader_f32 = WavReader::open("testsamples/waveformatex-ieeefloat-44100Hz-mono.wav")
.unwrap();
assert!(reader_f32.samples::<i8>().next().unwrap().is_err());
assert!(reader_f32.samples::<i16>().next().unwrap().is_err());
assert!(reader_f32.samples::<i32>().next().unwrap().is_err());
assert!(reader_f32.samples::<f32>().next().unwrap().is_ok());
let mut reader_i8 = WavReader::open("testsamples/pcmwaveformat-8bit-44100Hz-mono.wav").unwrap();
assert!(reader_i8.samples::<i8>().next().unwrap().is_ok());
assert!(reader_i8.samples::<i16>().next().unwrap().is_ok());
assert!(reader_i8.samples::<i32>().next().unwrap().is_ok());
assert!(reader_i8.samples::<f32>().next().unwrap().is_err());
}
#[test]
fn fuzz_crashes_should_be_fixed() {
use std::fs;
use std::ffi::OsStr;
let dir = fs::read_dir("testsamples/fuzz").ok()
.expect("failed to enumerate fuzz test corpus");
for path in dir {
let path = path.ok().expect("failed to obtain path info").path();
let is_file = fs::metadata(&path).unwrap().file_type().is_file();
if is_file && path.extension() == Some(OsStr::new("wav")) {
println!(" testing {} ...", path.to_str()
.expect("unsupported filename"));
let mut reader = match WavReader::open(path) {
Ok(r) => r,
Err(..) => continue,
};
match reader.spec().sample_format {
SampleFormat::Int => {
for sample in reader.samples::<i32>() {
match sample {
Ok(..) => { }
Err(..) => break,
}
}
}
SampleFormat::Float => {
for sample in reader.samples::<f32>() {
match sample {
Ok(..) => { }
Err(..) => break,
}
}
}
}
}
}
}
#[test]
fn seek_is_consistent() {
let files = &["testsamples/pcmwaveformat-16bit-44100Hz-mono.wav",
"testsamples/waveformatex-16bit-44100Hz-stereo.wav",
"testsamples/waveformatextensible-32bit-48kHz-stereo.wav"];
for fname in files {
let mut reader = WavReader::open(fname).unwrap();
let count = reader.samples::<i32>().count();
reader.seek(0).unwrap();
assert_eq!(reader.samples_read, 0);
assert_eq!(count, reader.samples::<i32>().count());
let last_time = reader.duration() - 1;
let channels = reader.spec.channels;
reader.seek(last_time).unwrap();
{
let mut samples = reader.samples::<i32>();
for _ in 0..channels {
assert!(samples.next().is_some());
}
assert!(samples.next().is_none());
}
let num_samples = reader.len();
reader.seek(num_samples).unwrap();
assert!(reader.samples::<i32>().next().is_none());
reader.seek(::std::u32::MAX / channels as u32).unwrap();
assert!(reader.samples::<i32>().next().is_none());
}
}