rodio/tests/wav_test.rs
Aaron Kofsky d20a3c661e Fix reading WAV of different encodings. (#199)
* Read 32 bit float and 24 bit int encoded files correctly.

Before, we were assuming that all WAV files are encoded as 16 bit integers.
This is not true, WAVs can actually also be encoded as 32 bit floats and
24 bit integers. Hound returns `Err` when we attempt to read files as the
wrong encoding, which was getting `unwrap_or`'d to just zero. This explains
why the file would load but not play any sound. We fix this by first
checking for the appropriate encoding, and then converting to an i16. This
might cause some slight dither issues but hopefully it is quiet enough that
it doesn't matter.

Fixes #195.

* Add tests of reading wav files different encodings

* Use `.map()` instead of control flow & unwrapping.

This makes things slightly cleaner to read.
Also remove extraneous print statement.
2018-11-09 21:58:43 +01:00

31 lines
1.3 KiB
Rust

extern crate rodio;
use std::io::BufReader;
#[test]
fn test_wav_encodings() {
// 16 bit wav file exported from Audacity (1 channel)
let file = std::fs::File::open("tests/audacity16bit.wav").unwrap();
let mut decoder = rodio::Decoder::new(BufReader::new(file)).unwrap();
assert!(decoder.any(|x| x != 0)); // Assert not all zeros
// 16 bit wav file exported from LMMS (2 channels)
let file = std::fs::File::open("tests/lmms16bit.wav").unwrap();
let mut decoder = rodio::Decoder::new(BufReader::new(file)).unwrap();
assert!(decoder.any(|x| x != 0));
// 24 bit wav file exported from LMMS (2 channels)
let file = std::fs::File::open("tests/lmms24bit.wav").unwrap();
let mut decoder = rodio::Decoder::new(BufReader::new(file)).unwrap();
assert!(decoder.any(|x| x != 0));
// 32 bit wav file exported from Audacity (1 channel)
let file = std::fs::File::open("tests/audacity32bit.wav").unwrap();
let mut decoder = rodio::Decoder::new(BufReader::new(file)).unwrap();
assert!(decoder.any(|x| x != 0));
// 32 bit wav file exported from LMMS (2 channels)
let file = std::fs::File::open("tests/lmms32bit.wav").unwrap();
let mut decoder = rodio::Decoder::new(BufReader::new(file)).unwrap();
assert!(decoder.any(|x| x != 0));
}