Merge pull request #226 from whentze/master

Implement `total_duration` for `FlacDecoder`
This commit is contained in:
Pierre Krieger 2019-06-08 14:58:56 +02:00 committed by GitHub
commit 8992fb9769
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 1 deletions

View file

@ -18,6 +18,7 @@ where
bits_per_sample: u32,
sample_rate: u32,
channels: u16,
samples: Option<u64>,
}
impl<R> FlacDecoder<R>
@ -43,6 +44,7 @@ where
bits_per_sample: spec.bits_per_sample,
sample_rate: spec.sample_rate,
channels: spec.channels as u16,
samples: spec.samples,
})
}
}
@ -68,7 +70,9 @@ where
#[inline]
fn total_duration(&self) -> Option<Duration> {
None
// `samples` in FLAC means "inter-channel samples" aka frames
// so we do not divide by `self.channels` here.
self.samples.map(|s| Duration::from_micros(s * 1_000_000 / self.sample_rate as u64))
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

21
tests/flac_test.rs Normal file
View file

@ -0,0 +1,21 @@
extern crate rodio;
use rodio::Source;
use std::{io::BufReader, time::Duration};
#[test]
fn test_flac_encodings() {
// 16 bit FLAC file exported from Audacity (2 channels, compression level 5)
let file = std::fs::File::open("tests/audacity16bit_level5.flac").unwrap();
let mut decoder = rodio::Decoder::new(BufReader::new(file)).unwrap();
assert!(decoder.any(|x| x != 0)); // File is not just silence
assert_eq!(decoder.total_duration(), Some(Duration::from_secs(3))); // duration is calculated correctly
// 24 bit FLAC file exported from Audacity (2 channels, various compression levels)
for level in &[0, 5, 8] {
let file = std::fs::File::open(format!("tests/audacity24bit_level{}.flac", level)).unwrap();
let mut decoder = rodio::Decoder::new(BufReader::new(file)).unwrap();
assert!(decoder.any(|x| x != 0));
assert_eq!(decoder.total_duration(), Some(Duration::from_secs(3)));
}
}