Skip to content

fix(audio): stop a crossfade race from leaving the switched app silent - #487

Closed
NicoM701 wants to merge 1 commit into
ronitsingh10:mainfrom
NicoM701:fix/crossfade-idle-silence
Closed

NicoM701 wants to merge 1 commit into
ronitsingh10:mainfrom
NicoM701:fix/crossfade-idle-silence

Conversation

@NicoM701

@NicoM701 NicoM701 commented Nov 5, 2026 •

Copy link
Copy Markdown

Fixes the race described in #461: after a device switch, an app can keep playing (and its meter keeps moving) while FineTune outputs silence until the tap gets rebuilt.

CrossfadeState.updateProgress runs on the secondary IO callback. If that callback reads .crossfading just before the main thread runs promoteSecondaryToPrimary() + complete(), it can store progress = 1.0 after the reset (totalSamples is 0 by then). The phase is idle, nothing clears progress while idle, and the idle branch of primaryMultiplier returned 0 for progress >= 1.0. The promoted tap, now the primary, stays muted.

The fix makes the idle branch always return 1.0. The old guard had no job left to do. Before promotion the phase is .crossfading, so the cosine curve already takes the old primary to ~0. Callbacks from the old primary that enter after promotion have a stale callbackID, so the role guard in processAudioCallback zeros their output. An already-running callback retains its role and can finish a buffer at unity gain after the reset; that was already possible when complete() reset progress to 0, and this change does not introduce it. complete() already reset progress to 0 on every non-racy path. The change adds no work on the audio thread.

I replaced the existing idle test, which asserted the old behavior, with a regression test for the stale-progress state.

Proof: a standalone two-thread harness compiled against the real CrossfadeState.swift. It models the secondary callback (role check, peak scan, updateProgress) racing the promotion and complete(). On main it left the promoted tap silent in 40 to 125 of every 100k promotions, red in 5 of 5 runs. With this change it was 0 in 5 of 5 runs, even though the stale idle progress = 1.0 state still occurred 11 to 77 times per run. The new test fails on main and all 25 CrossfadeState tests pass with the fix. xcodebuild build-for-testing (Debug) succeeds. I did not run the full xcodebuild test locally, because the test host launches the full app next to my installed FineTune.

This fixes the race in #461. I can't show it was the cause of every silent-output report there. The other changes in the patch attached to #461 (health-check eligibility, destructive-switch volume restore) are not part of this PR.

Reviewed with sol-loop: 2 independent reviews with gpt-6.1-sol. Sol verdict: approved. The second review clarified the in-flight callback limitation above; no further code changes were needed.

Created with Claude Opus 5.5 in Claude Code.

🤖 Generated with Claude Code

The secondary IO callback can read `.crossfading` in updateProgress just
before the main thread runs complete(), then store progress = 1.0 after
the reset (totalSamples is now 0). Once the secondary is promoted to
primary, nothing clears progress while idle, and the idle branch of
primaryMultiplier returned 0 for progress >= 1.0. The app kept playing
and the meter kept moving, but its output stayed silent until the tap
was rebuilt.

The idle guard had no remaining job: before promotion the phase is
`.crossfading` (cos -> 0), and after promotion the old primary callback
has a stale callbackID and writes zeros. Idle now always returns 1.0.

Refs ronitsingh10#461

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
temporaryna added a commit to temporaryna/FineTune that referenced this pull request Nov 12, 2026
Port of upstream PR ronitsingh10#487 (fixes ronitsingh10#461). A secondary IO callback reading
.crossfading just before complete() can store progress = 1.0 after the
reset; nothing clears it while idle, and the idle branch of
primaryMultiplier returned 0 for progress >= 1.0 — the promoted tap
stayed muted until rebuilt. Idle now always returns 1.0: before
promotion the phase is .crossfading, so the cosine curve already takes
the old primary to ~0, and post-promotion stale-role callbacks are
zeroed by the role guard in processAudioCallback.

Observed live: after the monitor unplug, every device switch forced
through '[CROSSFADE] Timeout at 0.000000%' — apps ended with dead taps
(one app's audio bypassing the tap raw, another consumed silently).
@NicoM701 NicoM701 closed this Nov 16, 2026
RobinLandy added a commit to RobinLandy/FineTune that referenced this pull request Nov 16, 2026
Keep a lock-free gain target and give each IOProc its own ramp. Route the popup, media keys and HUD through the master setting. Order switch cleanup, guard reconnect promotion and preserve input preference and persisted levels.

The initial master-volume DSP was adapted from wait2050/FineTune-Sync (GPLv3), be99199. The targeted reconnect and idle crossfade fixes follow upstream FineTune PRs ronitsingh10#314 (clem-git) and ronitsingh10#487 (NicoM701).
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.

1 participant