host_posix: the spatial audio event ticks on a clock of its own #144

Merged
ItsMariusBC merged 1 commit from upstream-pr/144 into main 2026-11-26 22:38:11 +00:00
ItsMariusBC commented 2026-11-26 20:31:10 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by ItsMariusBC on 2026-11-26T20:31:10Z.


What this changes

The title's spatial audio event is set by a clock of the stream's own, instead of by the device's buffer requests. This is the crackling and the drop-outs in the boot movie and the menus reported in #110.

Cause. The AudioQueue does not ask for buffers evenly. I timed the render callback for 3 s on an M5 Pro at 48 kHz: 10 ms buffers are asked for 10.7 ms apart, and every 16th request comes right after the one before (under 2 ms). That is a device cycle of 512 frames against buffers of 480. The event was set once per request, so the title got one signal for those two periods, and one for any stretch it slept through. The queue held one period at most, so each of those played as 10 ms of silence.

Fix. Shared::tick is the event's clock: a period apart, half a period apart while fewer than three periods are queued, and no signal while five are. spatial.rs runs it on a thread that stops with the stream. The device callback only takes frames.

Why not make the lost pass up at once. I tried that first (signal again at the end of a pass while the queue is short). It left 7 gaps, 5 of them exactly 160 ms apart in the first second of the movie's sound, with the queue full and 100 passes a second. So the title returns a silent pass when it is given two back to back while a sound starts. With passes at least half a period apart those are gone.

Measurements

50 s runs, Series S, same Mac, the device output recorded with VICTORY_WASAPI_WAV and analysed offline. A gap is one or more periods of 480 frames that are all zero between two periods with sound.

49e7c64 with this change
Sound analysed 16.3 s 16.9 s
Gaps 39 (400 ms in all) 1 (40 ms)
Gaps per minute of sound 144 3.5
Jumps at a period boundary 16 0

The one gap left is 40 ms at 3.89 s into the movie's sound. It was at the same instant in the run before, and a temporary counter of frames the device got as silence because the queue was empty read 0 for the whole run, so it is in what the title mixes. That counter is not in this pull request.

By ear: no crackling left in the boot movie or the menus.

Not covered: the drop-outs during the world load. Those come with stalls of several seconds when memory runs out, and the title reopens its stream.

One caveat: the default output on this Mac is a virtual device at 48 kHz, and I did not time the built-in speakers.

How it was tested

  • cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings
  • Ran the game: Series S, 50 s (boot movie and its sound), on 49e7c64 plus this change. Not run again after the rebase on 57b7ebd.

Two new tests in wasapi.rs play the stream on a clock of microseconds, with a device that takes frames in cycles of 512:

  • the_event_clock_feeds_a_device_that_takes_frames_in_its_own_cycles: no silence, and no two passes closer than 5 ms. It fails with 1,250 µs if the short ticks are an eighth of a period.
  • a_title_that_wakes_late_catches_up_instead_of_leaving_silence: a title that oversleeps 15 ms every second. It fails if every tick is a full period.

cargo xtask policy passes.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none here; the silent second pass is an observation, noted in the comment of Shared::tick)
  • New diagnostics are behind a VICTORY_* variable and documented (none added)
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5516757659 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/144). Originally posted by [ItsMariusBC](https://github.com/ItsMariusBC) on 2026-11-26T20:31:10Z. --- ## What this changes The title's spatial audio event is set by a clock of the stream's own, instead of by the device's buffer requests. This is the crackling and the drop-outs in the boot movie and the menus reported in #110. **Cause.** The `AudioQueue` does not ask for buffers evenly. I timed the render callback for 3 s on an M5 Pro at 48 kHz: 10 ms buffers are asked for 10.7 ms apart, and every 16th request comes right after the one before (under 2 ms). That is a device cycle of 512 frames against buffers of 480. The event was set once per request, so the title got one signal for those two periods, and one for any stretch it slept through. The queue held one period at most, so each of those played as 10 ms of silence. **Fix.** `Shared::tick` is the event's clock: a period apart, half a period apart while fewer than three periods are queued, and no signal while five are. `spatial.rs` runs it on a thread that stops with the stream. The device callback only takes frames. **Why not make the lost pass up at once.** I tried that first (signal again at the end of a pass while the queue is short). It left 7 gaps, 5 of them exactly 160 ms apart in the first second of the movie's sound, with the queue full and 100 passes a second. So the title returns a silent pass when it is given two back to back while a sound starts. With passes at least half a period apart those are gone. ## Measurements 50 s runs, Series S, same Mac, the device output recorded with `VICTORY_WASAPI_WAV` and analysed offline. A gap is one or more periods of 480 frames that are all zero between two periods with sound. | | 49e7c64 | with this change | | --- | --- | --- | | Sound analysed | 16.3 s | 16.9 s | | Gaps | 39 (400 ms in all) | 1 (40 ms) | | Gaps per minute of sound | 144 | 3.5 | | Jumps at a period boundary | 16 | 0 | The one gap left is 40 ms at 3.89 s into the movie's sound. It was at the same instant in the run before, and a temporary counter of frames the device got as silence because the queue was empty read 0 for the whole run, so it is in what the title mixes. That counter is not in this pull request. By ear: no crackling left in the boot movie or the menus. Not covered: the drop-outs during the world load. Those come with stalls of several seconds when memory runs out, and the title reopens its stream. One caveat: the default output on this Mac is a virtual device at 48 kHz, and I did not time the built-in speakers. ## How it was tested - [x] `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory` - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings` - [x] Ran the game: Series S, 50 s (boot movie and its sound), on 49e7c64 plus this change. Not run again after the rebase on 57b7ebd. Two new tests in `wasapi.rs` play the stream on a clock of microseconds, with a device that takes frames in cycles of 512: - `the_event_clock_feeds_a_device_that_takes_frames_in_its_own_cycles`: no silence, and no two passes closer than 5 ms. It fails with 1,250 µs if the short ticks are an eighth of a period. - `a_title_that_wakes_late_catches_up_instead_of_leaving_silence`: a title that oversleeps 15 ms every second. It fails if every tick is a full period. `cargo xtask policy` passes. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md (none here; the silent second pass is an observation, noted in the comment of `Shared::tick`) - [x] New diagnostics are behind a `VICTORY_*` variable and documented (none added)
ItsMariusBC commented 2026-11-26 20:37:38 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by ItsMariusBC on 2026-11-26T20:37:38Z.


Run again after the rebase: main 0ae6cfc with this branch and #145 merged on top, Series S, 75 s, cold shader cache, same Mac.

  • No exception and no Metal error in the log.
  • "render client released frames in the last second": 100 or 101 passes in every full second, with 1,440 or 1,920 frames queued at each line.
  • By ear: no crackling in the boot movie.

I did not record the output this time, so there is no gap count for this run.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7202905945 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/144#issuecomment-7202905945). Originally posted by [ItsMariusBC](https://github.com/ItsMariusBC) on 2026-11-26T20:37:38Z. --- Run again after the rebase: main 0ae6cfc with this branch and #145 merged on top, Series S, 75 s, cold shader cache, same Mac. - No exception and no Metal error in the log. - "render client released frames in the last second": 100 or 101 passes in every full second, with 1,440 or 1,920 frames queued at each line. - By ear: no crackling in the boot movie. I did not record the output this time, so there is no gap count for this run.
LakshanSivakumar commented 2026-11-26 22:38:08 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T22:38:08Z.

Review: APPROVED


Reviewed and tested together with the other open PRs on main (macOS x86_64 workspace tests, the setup shell tests, and Windows clippy). Thanks!

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6081182327 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/144#pullrequestreview-6081182327). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T22:38:08Z. **Review:** APPROVED --- Reviewed and tested together with the other open PRs on main (macOS x86_64 workspace tests, the setup shell tests, and Windows clippy). Thanks!
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!144
No description provided.