Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion FineTune/Audio/Engine/CrossfadeState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ nonisolated struct CrossfadeState: @unchecked Sendable {
var primaryMultiplier: Float {
switch phase {
case .idle:
return progress >= 1.0 ? 0.0 : 1.0
// Never gate on `progress` here. A secondary callback that read `.crossfading`
// just before `complete()` can store progress = 1.0 after the reset, and nothing
// clears it while idle — the promoted tap would stay silent indefinitely.
return 1.0
case .warmingUp:
return 1.0
case .crossfading:
Expand Down
4 changes: 1 addition & 3 deletions FineTune/Audio/Engine/ProcessTapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1592,9 +1592,7 @@ final class ProcessTapController: ProcessTapControlling {

if isPrimary {
currentVol = _primaryCurrentVolume
// Equal-power crossfade: primary uses cosine curve (1→0).
// CrossfadeState.primaryMultiplier handles all phase logic including the
// race condition guard (returns 0.0 when progress >= 1.0 in idle phase).
// Equal-power crossfade: primary uses cosine curve (1→0), 1.0 when idle.
crossfadeMultiplier = crossfadeState.primaryMultiplier
rampCoeff = rampCoefficient
stereoLeft = _primaryPreferredStereoLeftChannel
Expand Down
23 changes: 13 additions & 10 deletions FineTuneTests/CrossfadeStateAdversarialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,22 @@ struct CrossfadeProgressTests {
@Suite("CrossfadeState — Idle Phase Multipliers (Adversarial)")
struct CrossfadeIdleTests {

@Test("Idle with progress >= 1.0: primary=0, secondary=1 (post-crossfade window)")
func idlePostCrossfadeWindow() {
// This state occurs between the audio thread detecting progress >= 1.0
// and the main thread calling complete(). The multipliers should reflect
// "crossfade done, secondary is promoted."
@Test("Idle with stale progress >= 1.0: promoted primary stays audible (#461)")
func idleStaleProgressKeepsPrimaryAudible() {
// The secondary callback can read .crossfading, then store progress = 1.0 after
// complete() has reset it. After promotion that callback is the primary, and
// nothing resets progress while idle.
var state = CrossfadeState()
state.beginWarmup()
state.totalSamples = 48000
state.beginCrossfading()
state.complete()
state.progress = 1.0
// Phase is idle (default), but progress indicates crossfade was completed

#expect(state.primaryMultiplier == 0.0,
"Post-crossfade idle with progress=1: primary should be 0")
#expect(state.secondaryMultiplier == 1.0,
"Post-crossfade idle with progress=1: secondary should be 1")
#expect(state.phase == .idle)
#expect(state.primaryMultiplier == 1.0,
"Idle primary must not be silenced by stale progress")
#expect(state.secondaryMultiplier == 1.0)
}

@Test("Idle with progress < 1.0: primary=1, secondary=1 (normal idle)")
Expand Down