Fix WavDecoder::total_duration (#383)

This commit is contained in:
Alphyr 2021-07-06 21:55:45 +02:00 committed by GitHub
parent f6bb97e0d3
commit ba78af2070
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,7 @@ where
R: Read + Seek, R: Read + Seek,
{ {
reader: SamplesIterator<R>, reader: SamplesIterator<R>,
total_duration: Duration,
sample_rate: u32, sample_rate: u32,
channels: u16, channels: u16,
} }
@ -27,15 +28,22 @@ where
let reader = WavReader::new(data).unwrap(); let reader = WavReader::new(data).unwrap();
let spec = reader.spec(); let spec = reader.spec();
let len = reader.len() as u64;
let reader = SamplesIterator { let reader = SamplesIterator {
reader, reader,
samples_read: 0, samples_read: 0,
}; };
let sample_rate = spec.sample_rate;
let channels = spec.channels;
let total_duration =
Duration::from_micros((1_000_000 * len) / (sample_rate as u64 * channels as u64));
Ok(WavDecoder { Ok(WavDecoder {
reader, reader,
sample_rate: spec.sample_rate, total_duration,
channels: spec.channels, sample_rate,
channels,
}) })
} }
pub fn into_inner(self) -> R { pub fn into_inner(self) -> R {
@ -114,8 +122,7 @@ where
#[inline] #[inline]
fn total_duration(&self) -> Option<Duration> { fn total_duration(&self) -> Option<Duration> {
let ms = self.len() as u64 * 1000 / (self.channels as u64 * self.sample_rate as u64); Some(self.total_duration)
Some(Duration::from_millis(ms))
} }
} }