Skip to content

acphal: audio frame deadlines are exact 1/375 s per frame index - #468

Open
DavidM1911 wants to merge 1 commit into
xcl-dev:mainfrom
DavidM1911:acphal-frame-clock
Open

DavidM1911 wants to merge 1 commit into
xcl-dev:mainfrom
DavidM1911:acphal-frame-clock

Conversation

@DavidM1911

@DavidM1911 DavidM1911 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Part of #329 (item 3).

FRAME was 2667 µs where one audio frame (128 samples at 48 kHz) is exactly 1/375 s = 2666.6 µs, and each pop scheduled the next frame by adding it — so the cadence drifted +0.333 µs per frame (+125 ppm, ~0.45 s per hour of audio). Deadlines are now computed per 1-based frame index as epoch + k/375 s in integer nanoseconds (frame_deadline), so there is no accumulation. The "a frame more than 8 late is not made up for" rule is kept by fast-forwarding the index to the same floor (now - 8·FRAME).

What is in it

  • crates/xcl/src/hle/acphal.rs: Channel stores epoch + next_frame index instead of Option<Instant>; frame_due/pop/connect reworked; frame_deadline helper; FRAME_CATCHUP constant; a test that walks 375 frames (exactly 1 s) checking each is due at epoch + k/375 s and not 1 ns earlier.

Tested

  • Windows 11, Ryzen 9 7900X (Radeon iGPU + NVIDIA RTX 3060 Ti 8 GB, driver 610.88), Rust 1.100.0 x86_64-pc-windows-msvc:
    • cargo test -p xcl --lib acphal: 8 passed, 0 failed
    • cargo test --workspace -- --test-threads=4: 677 passed, 0 failed, 41 ignored
  • Regression run on the retail title (1.0.2.1, own copy), RTX 3060 Ti, scripts/smoke.ps1 -Seconds 60, cargo build --release -p xcl:

main a6f5f7a:

xcl-smoke v1
commit: a6f5f7a74cc620c1f88a9e3669a633d10c3c8212
gpu: NVIDIA GeForce RTX 3060 Ti
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1503 (PresentX 1503)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14437 MB

this PR's head 0d407cf:

xcl-smoke v1
commit: 0d407cfcf81756012401c85f27462e41747b7165
gpu: NVIDIA GeForce RTX 3060 Ti
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1498 (PresentX 1498)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14449 MB

Not tested

  • Long-run audible drift (the fix is arithmetic; the unit test pins the grid for 1 s of frames)
  • AMD adapter smoke run

AI use

  • Devin (SWE-2 Flash Medium): implemented the fix and test, ran the suites and smoke runs.

FRAME was 2667 us per frame where 128 samples at 48 kHz is 2666.6 us,
so every scheduled frame drifted +0.333 us (+125 ppm, ~0.45 s/hour).
Deadlines are now computed per 1-based frame index as epoch + k/375 s
in integer nanoseconds; the >8-frames-late catch-up rule is kept by
fast-forwarding the index to the same floor. Part of xcl-dev#329.

Assisted-by: Devin (SWE-2 Flash Medium)
@mastrr9

mastrr9 commented Nov 24, 2026

Copy link
Copy Markdown

Code audit + NVIDIA RTX 5080 test run on 0d407cfcf81756012401c85f27462e41747b7165 (part of #329):

Review

Looks clean — frame_deadline(epoch, k) computes epoch + floor(k * 10^9 / 375) in u128 nanoseconds with zero cumulative drift, and the catch-up floor ceiling (wait_ns * 375 + 999_999_999) / 1_000_000_000 preserves the FRAME_CATCHUP = 8 cap cleanly. (Small note: this and #471 both touch Channel::connect one line apart, so whichever merges second will need a trivial rebase.)

Tested

  • Hardware / OS / toolchain: NVIDIA GeForce RTX 5080 (driver 617.42), AMD Ryzen 7 9800X3D, Windows 11 25H2 (build 26200.9711), rustc 1.100.0 (MSVC), debug build.
  • cargo build --workspace && cargo test --workspace -- --test-threads=4: 677 passed, 0 failed, 41 ignored.
  • New test hle::acphal::tests::frames_stay_on_the_48k_grid_without_drift: passed.
  • SKIPPED GPU tests (2, wave32-only adapter): gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask: no wave64, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware: no wave64.

Not tested

  • Retail title / scripts/smoke.ps1 run (test suite only).
  • AMD / Intel GPUs.

AI use: code review and test run orchestrated with Antigravity (Gemini 4 Neon) on the hardware listed above.

@alpz1000

Copy link
Copy Markdown

Independent check of audio frame scheduling at 0d407cfcf81756012401c85f27462e41747b7165 on Windows 11 Home 26200.9974, Intel Core i5-8400, Rust 1.100.0 x86_64-pc-windows-msvc, debug.

Tested: cargo test --locked -p xcl --lib acphal on the unmodified PR head: 8 passed, 0 failed. Then I temporarily extended only the loop bound in frames_stay_on_the_48k_grid_without_drift from 375 to 1,350,000 and reran that test. It passed across one simulated hour at 375 frames/s, checking at every frame that it was not due 1 ns before its deadline and was due at the deadline. The local edit was reverted.

Not tested: audible drift, actual audio playback, retail titles, or the full workspace suite. This checks the scheduler against simulated timestamps.

AI use: OpenAI Codex (GPT-6) ran the tests and drafted this report.

@ryosen0

ryosen0 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Data point from the same NVIDIA card the PR was tested on, different CPU/driver: RTX 3060 Ti 8 GB, Ryzen 7 9800X3D, driver 32.0.16.1742, Windows 11. Head 0d407cf merged onto main 508a0c7 (clean), together with #456 and #508 (adjacent paths).

  • cargo test -p xcl --lib -- --test-threads=1 title_textures xaudio2 acphal: 27 passed, 0 failed, including the 375-frame deadline walk.
  • The integer-ns epoch + k/375 s construction reads right: no accumulation, and the fast-forward floor keeps the >8-late rule without a second drift source.

Not tested: audible retail audio on this stack (the run ended early on its own at t≈34 s, exit 0x0 - unrelated early-exit behaviour of the title); no long-run drift measurement.

AI use: GPT-6.2 Astra (API model selector) in DSH ran the tests and wrote this comment; the human contributor reviewed it.

@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 0d407cfcf81756012401c85f27462e41747b7165 on AMD RDNA4. I merged it onto current main f4ce27c locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 702 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 661 passed, 0 failed, 21 ignored
  • 0 SKIPPED; frames_stay_on_the_48k_grid_without_drift passes.

Review: Deadlines are epoch + k/375 s in integer nanoseconds per frame index, so there is no accumulated rounding, and FRAME_CATCHUP bounds how many frames a late pop releases at once. I found nothing to change.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the PR's head on its old base was not run, only its merge onto main.

Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@Alpha325

Copy link
Copy Markdown

I added a synthetic Channel::pop test that leaves the channel unpolled for one hour, then drains the frames due at that instant. I ran it at the exact one-hour boundary and 1 ms after it.

First poll Parent 2997c9a PR 0d407cf
1 hour 10 frames drained 10 frames drained
1 hour + 1 ms 10 frames drained 9 frames drained

The totals include the first frame emitted before the catch-up clamp. After draining, neither revision emitted a frame 1 ns before the next deadline on the original 375 Hz grid. At that exact deadline, 2,666,666 ns after the hour boundary, the PR emitted MSG_AUDIO_FRAME in both cases. The parent emitted nothing and failed the fixture as expected.

On Windows 11 with Rust 1.100.0 MSVC, cargo test --offline --locked -p xcl --lib acphal -- --test-threads=1 --nocapture returned 9 passed, 0 failed on the PR. This checks scheduling with synthetic timestamps. It does not measure audible playback.

AI use — OpenAI Codex (GPT-6) wrote and ran the fixture and drafted this report.

@DavidM1911

Copy link
Copy Markdown
Contributor Author

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

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.

6 participants