mirror of
https://github.com/RustAudio/rodio
synced 2025-03-02 14:07:15 +00:00
Add time to duration tests
This commit is contained in:
parent
77896cc498
commit
94a8197273
1 changed files with 43 additions and 3 deletions
|
@ -170,9 +170,7 @@ impl Source for SymphoniaDecoder {
|
|||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.total_duration.map(|Time { seconds, frac }| {
|
||||
Duration::new(seconds, if frac > 0.0 { (1f64 / frac) as u32 } else { 0 })
|
||||
})
|
||||
self.total_duration.map(time_to_duration)
|
||||
}
|
||||
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), source::SeekError> {
|
||||
|
@ -306,6 +304,17 @@ fn skip_back_a_tiny_bit(
|
|||
Time { seconds, frac }
|
||||
}
|
||||
|
||||
fn time_to_duration(time: Time) -> Duration {
|
||||
Duration::new(
|
||||
time.seconds,
|
||||
if time.frac > 0.0 {
|
||||
(1f64 / time.frac) as u32
|
||||
} else {
|
||||
0
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
impl Iterator for SymphoniaDecoder {
|
||||
type Item = i16;
|
||||
|
||||
|
@ -332,3 +341,34 @@ impl Iterator for SymphoniaDecoder {
|
|||
Some(sample)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::Duration;
|
||||
|
||||
use symphonia::core::units::Time;
|
||||
|
||||
use crate::decoder::symphonia::time_to_duration;
|
||||
|
||||
#[test]
|
||||
fn test_time_to_dur_zero_frac() {
|
||||
let time = Time {
|
||||
seconds: 7,
|
||||
frac: 0.0,
|
||||
};
|
||||
let duration = Duration::new(7, 0);
|
||||
|
||||
assert_eq!(time_to_duration(time), duration);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_time_to_dur_non_zero_frac() {
|
||||
let time = Time {
|
||||
seconds: 7,
|
||||
frac: 0.3,
|
||||
};
|
||||
let duration = Duration::new(7, (1.0 / 0.3) as u32);
|
||||
|
||||
assert_eq!(time_to_duration(time), duration);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue