mirror of
https://github.com/RustAudio/rodio
synced 2024-12-13 05:32:32 +00:00
Merge pull request #226 from whentze/master
Implement `total_duration` for `FlacDecoder`
This commit is contained in:
commit
8992fb9769
6 changed files with 26 additions and 1 deletions
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
BIN
tests/audacity16bit_level5.flac
Normal file
BIN
tests/audacity16bit_level5.flac
Normal file
Binary file not shown.
BIN
tests/audacity24bit_level0.flac
Normal file
BIN
tests/audacity24bit_level0.flac
Normal file
Binary file not shown.
BIN
tests/audacity24bit_level5.flac
Normal file
BIN
tests/audacity24bit_level5.flac
Normal file
Binary file not shown.
BIN
tests/audacity24bit_level8.flac
Normal file
BIN
tests/audacity24bit_level8.flac
Normal file
Binary file not shown.
21
tests/flac_test.rs
Normal file
21
tests/flac_test.rs
Normal 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)));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue