rodio icon indicating copy to clipboard operation
rodio copied to clipboard

Incorrect total_duration while using symphonia features

Open Kamil729 opened this issue 1 year ago • 1 comments

While using rodio with symphonia features enabled values reported by Decoder::total_duration are incorrect. It works fine while using other backends. Calculating it with symphonia directly also return correct value. Minimum example with 7s long, flac file:

Cargo.toml
[dependencies]
rodio = { version = "0.19.0", default-features = false, features = [
    "symphonia-all",
] }
main.rs
use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr, time::Duration};

use rodio::{Decoder, Source};

fn main() {
    let track_path = PathBuf::from_str("test.flac").unwrap();
    let file = BufReader::new(File::open(track_path).unwrap());
    let track_source = Decoder::new(file).unwrap();
    let track_duration = track_source.total_duration();
    let real_duration = Some(Duration::from_secs(7));

    assert_eq!(track_duration, real_duration);
}
Output
thread 'main' panicked at src/main.rs:12:5:
assertion `left == right` failed
  left: Some(11.294967295s)
 right: Some(7s)

Audio file More complex example and comparison with symphonia

Kamil729 avatar Aug 08 '24 19:08 Kamil729

Very nice bug report! I had a quick look and I think I see the issue. I suspect the problem is that rodio uses the default_track (probed.format.default_track()) to calculate the total_duration. While for playback we do the same as your symphonia code. That is we pick the first track that has a codec type unequal to CODEC_TYPE_NULL.

Ill add a test and see if I can fix it, when I have time but I have to work through a PR first. If you want to take a crack at it yourself, it should be relatively easy. If you do please write a test and see if the current assets (in the assets folder) do give the correct duration (they probably do not :sweat:). On the off chance they do work please add your Audio file to the assets.

Ps; Its totally fine if you rather not work on this, I am already very happy with the extremely clear bug report you just gave us :+1:

yara-blue avatar Aug 09 '24 00:08 yara-blue