Skip to content

Recover automatically from sustained proxy audio delay - #78

Open
sergiou87 wants to merge 1 commit into
briankendall:masterfrom
sergiou87:fix-sustained-proxy-audio-delay
Open

sergiou87 wants to merge 1 commit into
briankendall:masterfrom
sergiou87:fix-sustained-proxy-audio-delay

Conversation

@sergiou87

Copy link
Copy Markdown

Problem

Occasionally, audio through the proxy develops noticeable delay relative to the source. Switching macOS output to another device and back clears it. The existing clock servo corrects small drift gradually, while the ring-buffer fallback only re-anchors after very large offsets; audible backlog can persist between those limits.

Changes

  • Detect queued audio exceeding the normal buffering budget by more than 50 ms or two input buffers, whichever is larger.
  • Re-anchor playback after at least 250 ms of newly observed input with sustained excess delay, reusing the existing timing and servo reset path.
  • Tolerate callback jitter and stalled input, and preserve final-audio draining when playback stops.
  • Log recovery events and document the behavior. Recovery can cause a brief audible skip rather than leaving playback persistently delayed.
  • Add simulated callback regression coverage across sample rates and input/output buffer sizes.

Validation

  • bash tests/run-audio-latency-tests.sh
  • Universal arm64/x86_64 Release driver build with Xcode 27, using a command-line MACOSX_DEPLOYMENT_TARGET=12.0 override because this Xcode no longer supports the project's 10.11 target. Repository deployment settings are unchanged.
  • I have been running a local build containing this fix and the separate wake-recovery fix for approximately one week. Neither the delay nor the wake-related silence has recurred during that time. This is combined-build field testing, not an isolated comparison of the two fixes.

This PR contains only the sustained-delay fix. The wake-recovery fix will be submitted independently against master.

Detect excessive queued audio above the normal buffering budget and re-anchor playback after at least 250 ms of newly observed input. Allow callback jitter, ignore stalled input, and preserve final-audio draining. Reset detection whenever playback timing is re-anchored.

Add simulated callback regression coverage across sample rates and buffer sizes, and document recovery behavior and the test runner.

Copilot-Session: b78a5bb5-6ba7-4a0a-86ac-d87fb6433026
@briankendall

Copy link
Copy Markdown
Owner

I'm happy to see you making an attempt at fixing an audio delay issue. However given that the main use cases of this driver includes people who are proxying to a TV and playing media, or pro audio producers proxying to an interface, it's important that there not be discontinuities in the audio that could create artifacts like pops and clicks if we can help it. I feel like there must be another approach for reducing the lag that doesn't require just making a jump in the audio buffer.

@sergiou87

Copy link
Copy Markdown
Author

That’s a completely fair concern! What do you think about using the existing clock servo to gradually drain the excess buffering instead of jumping the read position?

The idea would be to detect sustained excess latency as before, but adjust the servo’s phase target and gently slow the proxy clock so the output catches up. Queued samples would still play in order, without deliberately skipping any audio.

I'm trying this locally with correction capped at 0.5%, ramping by at most 0.1% per second and tapering near the target. The tradeoff is slower recovery—tens of seconds for 100 ms of excess delay—and potentially a slight speed/pitch change rather than a discontinuity. Simulated tests check sample continuity and convergence, but I haven’t tested this revision with real playback yet.

Would that be a better fit for the driver, or would you prefer a different approach or more conservative correction limits?

@briankendall

Copy link
Copy Markdown
Owner

I think that would work better, and I think with adjustments that small the change in pitch wouldn't generally be noticeable to anyone but the most attentive listeners with trained ears. But I'm wondering if there's a way to adjust the clock so that there isn't a delay in the first place. Do you have any ideas on how that might be accomplished?

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.

2 participants