Repository navigation
Make Player::get_pos and try_seek independent of playback speed - #934
UnknownSuperficialNight wants to merge 3 commits into
Conversation
- Move `track_position()` before `speed()` in `Player::append` so samples are counted at the source's real sample rate. Previously it counted against the scaled rate that Speed reports, so `get_pos()` returned elapsed playback time instead of the position in the media (at 0.5x it advanced 1s per second instead of 0.5s) - Divide the seek target by the current speed in `SeekOrder::attempt` to cancel the scaling in `Speed::try_seek`, so `try_seek(pos)` lands at pos in the media at any speed - Do the division in `f64` and fall back to the unscaled position instead of panicking on an out of range duration - Update the .inner() chains in the periodic_access closure for the new adapter order and document the order - Rewrite the `TrackPosition::get_pos` docs, which recommended placing track_position after speed (the opposite of what Player now does), and describe what try_seek reports in each ordering - Add a test for the `track_position_before_speed` ordering - Update the docs of Player::get_pos, Player::try_seek, Player::set_speed and Source::speed: playback duration instead of "total duration", seek targets are scaled by the speed, and positions do not depend on it - Fix the SpatialPlayer::speed getter doc, which said "Changes", and replace the outdated example in SpatialPlayer::get_pos - Link the docs of Speed, TrackPosition and Player to each other
kapok-4o
left a comment
There was a problem hiding this comment.
Could Player pass the media timestamp directly to TrackPosition? With a 48 kHz SamplesBuffer, seeking to 2 ms lands on frame 96 at 1× and frame 95 at 1.5×. The divide/multiply round trip sends 1,999,999 ns to the source at 1.5×. I reproduced the timestamp difference with a seekable WAV decoder too. A Player::try_seek test at both speeds could check the timestamp the source receives and the next frame.
cargo doc --no-deps warns about the super::TrackPosition::get_pos link in source/mod.rs. The 2× example in position.rs should say half a second per second of source audio.
- Seek the `TrackPosition` adapter directly from `Player` instead of through `Speed`, so the target reaches the source exactly as given - Remove the `speed` argument from `SeekOrder::attempt` - Add `Player` test that seeks to `2 ms` at `1.0, 1.5, 2.0` and `0.75` speed and checks the timestamp the source receives and the next frame - Fix the `2x` example in `TrackPosition::get_pos` docs - Fix the `get_pos` link in `Source::speed` docs (`cargo doc` warning) - Rewrite the docs of `Player::try_seek`, `Player::get_pos` and `Source::track_position` to match
kapok-4o
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround. All three points are fixed. Seeking the TrackPosition directly is a clean fix. cargo doc --no-deps is now clean (one warning on 0aaf78f), and the 2× wording reads well. I also checked the new test against the old divide/multiply path: it fails at 1.5 with (1.999999ms, Some(95.0)), so it catches the bug.
One problem: test_seek_target_does_not_depend_on_speed is flaky under load. The loop in seek_to_2ms_at_speed (src/player.rs:480) can drain the whole 1 s buffer before the seeker thread runs. The player is then empty, try_seek returns Ok(()) without seeking, and the recorder stays empty (left: []). Running the full cargo test --lib 25 times, it failed 7 times, and 3 times with --features 64bit. Run alone, it passed 50 out of 50. Adding player.pause(); before the append at src/player.rs:471 keeps the source from being consumed: 0 failures in 25 full-suite runs in each mode. The seek still happens, and the frame-96 check still holds.
On the f32 drift: I think your seek case is fine without a note (under 0.05 ms up to 2 h). A larger drift exists already on master. After try_seek(Duration::ZERO), TrackPosition counts spans and adds each one to an f32 offset_duration (src/source/position.rs:118). With a 10-minute 44.1 kHz WAV through Decoder, get_pos was 11.9 ms ahead at 540 s, and 0 with 64bit. A synthetic source with the same 2304-sample spans was 82 ms off at 60 min and about 29 s off at 120 min. That probably deserves its own issue rather than a docs note in this PR.
- Pause the `Player` before appending in `seek_to_2ms_at_speed`, so the source is not consumed while the seek is pending - Before, the test thread could drain the whole buffer before the seeker thread ran, leaving the `Player` empty so `try_seek` returned `Ok(())` without seeking
Thanks for catching that :) Should be fixed in the latest commit: I added I didn't catch it earlier because it never triggered on my machine. However forcing a delay in the seeker thread did reproduce it every time (without the pause): let seeker = s.spawn(|| {
std::thread::sleep(Duration::from_millis(500));
player.try_seek(Duration::from_millis(2))
}); |


I'm not sure if this is something we want to even commit while the rewrite is happening in #901 but ill add the PR in case feel free to close if it's irrelevant in the future or if we need updating ping me here
Problem
Player::get_pos()is wrong whenever the speed is not 1.0.Speeddoes not change the samples, it only reports a scaled sample rate.TrackPositionsat afterSpeedin the chain and divided its sample countby that scaled rate, so it measured elapsed playback time instead of the
position in the media. At 0.5x the position advanced 1s per second while
the audio advanced 0.5s per second, and changing the speed during
playback made it wrong in other ways too.
Player::try_seekwas also affected:Speed::try_seekmultiplies thetarget by the speed factor, so
try_seek(30s)at 0.5x landed at 15s ofthe media.
Changes
Player::append:track_position()now comes beforespeed(), sosamples are counted at the source's real sample rate. It still sits
inside
pausable(), so silence played while paused is not counted.SeekOrder::attempt: the target is divided by the current speed tocancel the multiplication in
Speed::try_seek. The division uses f64 andfalls back to the unscaled position if the result is out of range.
.inner()chains in theperiodic_accessclosure for thenew adapter order and documented the order.
TrackPosition::get_pos, updated thespeed()andset_speeddocs and theget_pos/try_seekdocs, replaced an outdatedexample in
SpatialPlayer::get_pos, and linked the related items.track_position_before_speedordering.Behaviour
get_pos()is the position in the media, independent of speed.try_seek(pos)seeks toposin the media at any speed.Testing
get_pos()advances at half and 1.5xthe rate of real time, and stays continuous when the speed changes
during playback.
try_seek(30s)lands at 30s at 0.5x, 1.0x and 2.0x.cargo testandcargo docCrude Diagram
This is a crude diagram showing what changed I was going to use the mermaid diagram renderer on GitHub, but its sucks so here is a PNG instead.
This does not account for pause and all that in the diagram It's simply just the core idea basically.
Additional notes
1.0using opus #917 and or something else after that branch added the issue, so I just based it off my own for working AGC branch.closes #837