Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/InputMuteTests.swift
T
josh e207728a12 Review nits: name pulse-settle constant, fix stale comments
Non-blocking cleanups from the clean review pass:
- EngineConfigView.startEngine: collapse the redundant early-return in the
  sample-rate-mismatch branch into the shared tail.
- InputMuteButton: hoist the 0.2s settle duration to a named pulseSettleDuration.
- InputMuteTests: correct a stale "input defaults muted" comment (library default
  is audible; the test mutes explicitly).
- Plan doc: setInputMuted(true) runs before engine.start(), not after; note the
  addBoard priming.

464 tests pass; macOS app builds.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-11 21:41:57 -07:00

190 lines
7.8 KiB
Swift

// InputMuteTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
/// Tests for the input mute — the hard gate on the signal feeding every board, distinct from the
/// rig-output mute.
@MainActor
@Suite("InputMute")
struct InputMuteTests {
/// A fresh session defaults audible at the library level (the app mutes on engine Start), and
/// muting drives every board's input writer to the silent gain.
@Test func defaultsAudibleThenMuteDrivesInputNodes() throws {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine)
try withExtendedLifetime(sg) {
let board: PedalBoard = try sg.addBoard()
try board.add(VolumePedal())
try sg.finalizeBoard(board)
#expect(!sg.inputMuted)
let audible: Double = 0.0
#expect(sg.inputMuteNodeValues.allSatisfy { $0 == audible })
#expect(!sg.inputMuteNodeValues.isEmpty)
sg.setInputMuted(true)
let muted: Double = 1.0
#expect(sg.inputMuteNodeValues.allSatisfy { $0 == muted })
}
}
/// `setInputMuted(false)` un-mutes every input node and clears the flag; re-muting restores it.
@Test func setInputMutedDrivesAllNodes() throws {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine)
try withExtendedLifetime(sg) {
let board: PedalBoard = try sg.addBoard()
try board.add(VolumePedal())
try sg.finalizeBoard(board)
sg.setInputMuted(false)
#expect(!sg.inputMuted)
let audible: Double = 0.0
#expect(sg.inputMuteNodeValues.allSatisfy { $0 == audible })
sg.toggleInputMuted()
#expect(sg.inputMuted)
let muted: Double = 1.0
#expect(sg.inputMuteNodeValues.allSatisfy { $0 == muted })
}
}
/// A board added while the input is un-muted comes up audible — the current mute state is
/// applied to newly-wired input nodes, not just the ones present when the toggle flipped.
@Test func newBoardAdoptsCurrentMuteState() throws {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine)
try withExtendedLifetime(sg) {
let first: PedalBoard = try sg.addBoard()
try first.add(VolumePedal())
try sg.finalizeBoard(first)
sg.setInputMuted(false)
let second: PedalBoard = try sg.addBoard()
try second.add(VolumePedal())
try sg.finalizeBoard(second)
let audible: Double = 0.0
#expect(sg.inputMuteNodeValues.count == 2)
#expect(sg.inputMuteNodeValues.allSatisfy { $0 == audible })
}
}
/// A muted input renders **exactly** silent through a full render — the kill-switch gate reaches
/// zero with no residual. A lagged gate leaked a faint transient on its first blocks; the hard
/// gate does not.
@Test func mutedInputRendersSilent() throws {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.25, numAudioBuses: 256)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine)
try withExtendedLifetime(sg) {
let board: PedalBoard = try sg.addBoard()
try board.add(VolumePedal())
try sg.finalizeBoard(board)
sg.setInputMuted(true) // mute explicitly before Start (library default is audible)
try engine.start()
let samples: [Float] = readStereoCAF(url: url)
#expect(!hasNonSilentOutput(samples),
"a muted input must render silent — the kill-switch gate leaves no residual")
}
}
/// Muting while the engine is stopped **primes** every input node — its ramp start (`previous
/// OutputValue`) equals the muted target — so the next Start renders muted from block zero with no
/// audible→muted ramp. Guards against the `UIParamNode` one-block ramp leaking a fresh node's
/// prior (audible) value. A freshly-added board is primed the same way.
@Test func stoppedMutePrimesNodesSoTheresNoStartRamp() throws {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine)
try withExtendedLifetime(sg) {
let board: PedalBoard = try sg.addBoard()
try board.add(VolumePedal())
try sg.finalizeBoard(board)
// Default audible: node value and ramp start both at 0 (no pending ramp).
let audible: Double = 0.0
#expect(sg.inputMuteNodeValues.allSatisfy { $0 == audible })
#expect(sg.inputMutePreviousValues.allSatisfy { $0 == audible })
// Mute while stopped: both the target and the ramp start move to 1 → no leak on Start.
sg.setInputMuted(true)
let muted: Double = 1.0
#expect(sg.inputMuteNodeValues.allSatisfy { $0 == muted })
#expect(sg.inputMutePreviousValues.allSatisfy { $0 == muted },
"a stopped-engine mute must prime the ramp start so the first render can't ramp")
// A board added while muted comes up primed muted too.
let second: PedalBoard = try sg.addBoard()
try second.add(VolumePedal())
try sg.finalizeBoard(second)
#expect(sg.inputMutePreviousValues.allSatisfy { $0 == muted })
#expect(sg.inputMuteNodeValues.count == 2)
}
}
/// Un-muting the input lets the test-loop signal through — the gate opens fully.
@Test func unmutedInputRendersSignal() throws {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.25, numAudioBuses: 256)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine)
try withExtendedLifetime(sg) {
let board: PedalBoard = try sg.addBoard()
try board.add(VolumePedal())
try sg.finalizeBoard(board)
sg.setInputMuted(false)
try engine.start()
let samples: [Float] = readStereoCAF(url: url)
#expect(hasNonSilentOutput(samples),
"an un-muted input must pass the test-loop signal through")
}
}
/// The live-input pass-through path is driven by `setInputMuted` identically to the test-loop tap.
@Test func liveInputPassThroughIsMuted() throws {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.05,
numOutputChannels: 2,
numInputChannels: 1,
numAudioBuses: 256
)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine, inputSource: .liveInput(channel: 0))
try withExtendedLifetime(sg) {
let board: PedalBoard = try sg.addBoard()
try board.add(VolumePedal())
try sg.finalizeBoard(board)
let audible: Double = 0.0
#expect(!sg.inputMuted)
#expect(sg.inputMuteNodeValues == [audible])
sg.setInputMuted(true)
let muted: Double = 1.0
#expect(sg.inputMuteNodeValues == [muted])
}
}
}