Skip to content

Make Player::get_pos and try_seek independent of playback speed - #934

Open
UnknownSuperficialNight wants to merge 3 commits into
RustAudio:masterfrom
UnknownSuperficialNight:fix-get-pos
Open

UnknownSuperficialNight wants to merge 3 commits into
RustAudio:masterfrom
UnknownSuperficialNight:fix-get-pos

Conversation

@UnknownSuperficialNight

Copy link
Copy Markdown
Contributor

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.

Speed does not change the samples, it only reports a scaled sample rate.
TrackPosition sat after Speed in the chain and divided its sample count
by 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_seek was also affected: Speed::try_seek multiplies the
target by the speed factor, so try_seek(30s) at 0.5x landed at 15s of
the media.

Changes

  • Player::append: track_position() now comes before speed(), so
    samples 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 to
    cancel the multiplication in Speed::try_seek. The division uses f64 and
    falls back to the unscaled position if the result is out of range.
  • Updated the .inner() chains in the periodic_access closure for the
    new adapter order and documented the order.
  • Docs: rewrote TrackPosition::get_pos, updated the speed() and
    set_speed docs and the get_pos/try_seek docs, replaced an outdated
    example in SpatialPlayer::get_pos, and linked the related items.
  • Added a test for the track_position_before_speed ordering.

Behaviour

  • get_pos() is the position in the media, independent of speed.
  • try_seek(pos) seeks to pos in the media at any speed.
  • At speed 1.0 nothing changes.

Testing

  • Played a track at 0.5x and 1.5x: get_pos() advances at half and 1.5x
    the 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 test and cargo doc

Crude 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.

diagram

Additional notes

  1. This branch is forked from my old AGC branch as the main branch at the moment still has Clicking/glitching sound when set_speed is higher or lower than 1.0 using opus #917 and or something else after that branch added the issue, so I just based it off my own for working AGC branch.
  2. I also made this fix simply because I needed it fixed for my program as I'm releasing soon just figured I would submit a PR in case.

closes #837

- 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 kapok-4o left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@UnknownSuperficialNight

UnknownSuperficialNight commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

Thank you for catching this :)

Player now seeks the TrackPosition adapter directly, so the timestamp reaches the source exactly as given, at any speed.

I added test_seek_target_does_not_depend_on_speed. It seeks to 2 ms on a 48 kHz buffer at 1.0, 1.5, 2.0 and 0.75, and checks both the timestamp the source receives and the next frame (96).

I also fixed the cargo doc --no-deps warning and reworded the 2× example in position.rs.

If you feel up for it, please check that test_seek_target_does_not_depend_on_speed is correct. I've attached a patch that makes the test print its values, so you can see the output:

    git apply seek_test_output.patch
    cargo test test_seek_target_does_not_depend_on_speed -- --nocapture

Output on my machine:

image

seek_test_output.patch

If you spot anything else, or anything I got wrong, please let me know.

@UnknownSuperficialNight

Copy link
Copy Markdown
Contributor Author

Bonus finding: I checked how far get_pos drifts with f32 on long positions. Seeks to a position + 2 ms, reads 1 s of audio, then compares get_pos to the exact value. With --features 64bit the error is 0 everywhere.

image

I don't think this is a bug, it's just how f32 works. But I hadn't seen get_pos tested at these lengths, so I'm posting the numbers. Maybe worth a note in the docs, happy to discuss.

If you want to try it:

git apply get_pos_drift_diagnostic.patch
cargo test diagnostic_get_pos_drift_on_long_positions -- --nocapture

And with f64:

cargo test --features 64bit diagnostic_get_pos_drift_on_long_positions -- --nocapture

get_pos_drift_diagnostic.patch

@kapok-4o kapok-4o left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@UnknownSuperficialNight

Copy link
Copy Markdown
Contributor Author

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.

Thanks for catching that :)

Should be fixed in the latest commit: I added player.pause(); before the append, as you suggested.

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))
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_pos does not seem to account for the current speed

2 participants