rodio icon indicating copy to clipboard operation
rodio copied to clipboard

Get current sink track playback time

Open thexeromin opened this issue 2 years ago • 4 comments

Is there any way to get current sink track playback time? I have searched in the docs but haven't found anything yet.

thexeromin avatar Feb 11 '23 13:02 thexeromin

I have the same issue.

mario-hess avatar Feb 15 '23 18:02 mario-hess

Hi! When trying to solve this for myself I found this test in the buffer.rs file.

    #[test]
    fn duration_basic() {
        let buf = SamplesBuffer::new(2, 2, vec![0i16, 0, 0, 0, 0, 0]);
        let dur = buf.total_duration().unwrap();
        assert_eq!(dur.as_secs(), 1);
        assert_eq!(dur.subsec_nanos(), 500_000_000);
    }

I can imagine some function in a sink that calls buf.total_duration() of the currently playing song. Reading through the sink.rs file I can see that there is a datatype queue_tx that looks promising. If the repository authors are comfortable with it I'd be happy to try implementing it.

    #[inline]
    pub fn get_duration(&self) -> u64 {
        // Somehow get first element of queue_tx as buffer
        // Return duration of that buffer
    }

pixmusix avatar Feb 22 '23 22:02 pixmusix

There a bit of a problem, because the currently played track is not in the queue_tx, as it only has the "to-be-played" sources. The currently played is hold by queue_rx, which is immediately after construction moved into the OutputStreamHandle, so you don't have very much leverage on it.

Also Source::total_duration() is total track length, not remaining, not sure if relevant here.

The easiest way way to get the total_duration is on track load, create your own trackname->duration map and save the information from the Decoder you create by calling its total_duration() before appending it to the sink.

For duration elapsed/duration remaining its quite a bit harder, but i have hacked something together, which is very bad, but it works by injecting a callback into the next() function of the iterator and calculating the time from the amount of callback-calls and the sample_rate * channels.

This could probably be done quite a bit better by having a counter inside the decoder for each next() call and from that calculating the elapsed duration.

BastianHentschel avatar Sep 07 '23 09:09 BastianHentschel

If you're still interested you could look over #510 and see if that solves part of the problem

BastianHentschel avatar Sep 07 '23 11:09 BastianHentschel