b29c8848ef
External review found the lagged mute gate leaks a faint transient (~-54 dB) on its first blocks: Lag seeds its state at 0, so gating on a lagged value never starts at exactly zero on a rig that comes up muted — the moment the mute is meant to keep things quiet. Measured directly via an NRT render. - Gate the input with a hard, un-lagged kill switch `(inputMute - 1) * -1` in both HardwareInputPassThrough and TestLoopTap. A muted node is then exactly silent from the first sample; a kill-switch mute is expected to be abrupt. Drop the now-unused muteLagTime constant. - Flip the library default to `inputMuted = false` (audible). Muting is the app's Start-time policy (EngineConfigView already re-asserts muted on user Start), so headless/offline library use isn't forced silent and render tests that route the test-loop through a board no longer depend on a leak to see signal. The pulsing glow now also starts at engine Start rather than before it. - Tests: add render-level mutedInputRendersSilent (exact silence) and unmutedInputRendersSignal; update the default-state and MIDI-latch expectations; drop the StereoRouting un-mutes that the leak had made necessary. - Plan doc: correct the mute default / gate description and the UserDefaults note. 463 tests pass; both app targets build. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
357 lines
17 KiB
Swift
357 lines
17 KiB
Swift
// MIDIControllerPCTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
@testable import LyrebirdShoeGaze
|
|
|
|
@MainActor
|
|
@Suite("MIDIController Program Change")
|
|
struct MIDIControllerPCTests {
|
|
|
|
/// Builds an engine + a rig of `boardCount` boards (each with one Volume pedal) + a controller.
|
|
private func makeRig(boardCount: Int = 1) throws -> (MIDIController, ShoeGaze, [PedalBoard], URL) {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
var boards: [PedalBoard] = []
|
|
for index: Int in 0 ..< boardCount {
|
|
let board: PedalBoard = try sg.addBoard()
|
|
board.label = "Board \(index + 1)"
|
|
try board.add(VolumePedal())
|
|
try sg.finalizeBoard(board)
|
|
boards.append(board)
|
|
}
|
|
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
|
|
return (controller, sg, boards, url)
|
|
}
|
|
|
|
@Test func learnCapturesSwitchAndFiresCallback() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
var changed: Bool = false
|
|
controller.onMapChanged = { changed = true }
|
|
controller.armSwitchLearn(.rigBypass)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 7))
|
|
|
|
#expect(controller.learnTarget == nil)
|
|
#expect(changed)
|
|
#expect(controller.switchBinding(for: .rigBypass)?.program == 7)
|
|
#expect(sg.midiMap?.switches.count == 1)
|
|
}
|
|
}
|
|
|
|
@Test func rigBypassLatchesOnEachPress() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.rigBypass)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 1)) // learn only
|
|
#expect(sg.rigBypassed == false, "learn must not toggle")
|
|
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 1))
|
|
#expect(sg.rigBypassed == true, "first press engages")
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 1))
|
|
#expect(sg.rigBypassed == false, "second press releases")
|
|
}
|
|
}
|
|
|
|
@Test func rigMuteLatchesOnEachPressAndIsSeparateFromBypass() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.rigMute)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 1)) // learn only
|
|
#expect(sg.rigMuted == false, "learn must not toggle")
|
|
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 1))
|
|
#expect(sg.rigMuted == true, "first press mutes")
|
|
#expect(sg.rigBypassed == false, "mute must not engage bypass")
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 1))
|
|
#expect(sg.rigMuted == false, "second press unmutes")
|
|
}
|
|
}
|
|
|
|
@Test func inputMuteLatchesOnEachPressAndIsSeparateFromRigMute() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
// A fresh library session starts audible (the app mutes on engine Start).
|
|
#expect(sg.inputMuted == false, "input defaults audible at the library level")
|
|
|
|
controller.armSwitchLearn(.inputMute)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 2)) // learn only
|
|
#expect(sg.inputMuted == false, "learn must not toggle")
|
|
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 2))
|
|
#expect(sg.inputMuted == true, "first press mutes the input")
|
|
#expect(sg.rigMuted == false, "input mute must not touch the rig-output mute")
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 2))
|
|
#expect(sg.inputMuted == false, "second press un-mutes the input")
|
|
}
|
|
}
|
|
|
|
@Test func pedalBypassLatches() throws {
|
|
let (controller, sg, boards, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
let node: UIParamNode = boards[0].pedals[0].$bypass
|
|
controller.armSwitchLearn(.pedalBypass(pedalID: boards[0].pedals[0].id))
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 5)) // learn
|
|
#expect(node.value == 0.0)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 5))
|
|
#expect(abs(node.value - 1.0) < 1e-9, "first press bypasses")
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 5))
|
|
#expect(abs(node.value - 0.0) < 1e-9, "second press re-engages")
|
|
}
|
|
}
|
|
|
|
@Test func boardBypassLatches() throws {
|
|
let (controller, sg, boards, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.boardBypass(boardID: boards[0].id))
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 3)) // learn
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 3))
|
|
#expect(sg.isBoardBypassed(boards[0]) == true)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 3))
|
|
#expect(sg.isBoardBypassed(boards[0]) == false)
|
|
}
|
|
}
|
|
|
|
@Test func boardSoloIsExclusiveRadio() throws {
|
|
let (controller, sg, boards, url): (MIDIController, ShoeGaze, [PedalBoard], URL) =
|
|
try makeRig(boardCount: 2)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.boardSolo(boardID: boards[1].id))
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 2)) // learn
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 2)) // solo board 2
|
|
#expect(sg.activeBoardIndex == 1)
|
|
#expect(sg.isBoardAudible(boards[1]) == true)
|
|
#expect(sg.isBoardAudible(boards[0]) == false)
|
|
}
|
|
}
|
|
|
|
@Test func allBoardsActionCancelsSolo() throws {
|
|
let (controller, sg, boards, url): (MIDIController, ShoeGaze, [PedalBoard], URL) =
|
|
try makeRig(boardCount: 2)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
// Solo board 2 first, then map + fire the All-Boards action.
|
|
sg.soloBoard(boards[1], crossfadeSeconds: 0)
|
|
#expect(sg.activeBoardIndex == 1)
|
|
|
|
controller.armSwitchLearn(.allBoards)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 4)) // learn
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 4)) // fire
|
|
|
|
#expect(sg.activeBoardIndex == nil, "All Boards should clear the solo")
|
|
#expect(sg.isBoardAudible(boards[0]) == true)
|
|
#expect(sg.isBoardAudible(boards[1]) == true)
|
|
}
|
|
}
|
|
|
|
@Test func channelFilterBlocksOtherChannels() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.rigBypass)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 1)) // learn on ch 0
|
|
controller.ingestForTesting(.programChange(channel: 4, program: 1)) // other channel
|
|
#expect(sg.rigBypassed == false, "PC on a non-matching channel should be ignored")
|
|
}
|
|
}
|
|
|
|
@Test func staleOutOfRangeBindingIsIgnored() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
// Bind to a pedal id that doesn't exist in the rig; applying it must be a safe no-op.
|
|
let ghostID: UUID = UUID()
|
|
controller.armSwitchLearn(.pedalBypass(pedalID: ghostID))
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 6)) // learn
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 6)) // apply — no crash
|
|
#expect(controller.switchBinding(for: .pedalBypass(pedalID: ghostID)) != nil)
|
|
}
|
|
}
|
|
|
|
@Test func oneProgramCanDriveMultipleActions() throws {
|
|
// A single footswitch (PC) may drive several toggles at once — a "scene".
|
|
let (controller, sg, boards, url): (MIDIController, ShoeGaze, [PedalBoard], URL) =
|
|
try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.rigBypass)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 8))
|
|
controller.armSwitchLearn(.pedalBypass(pedalID: boards[0].pedals[0].id))
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 8)) // same PC, kept
|
|
|
|
// Both bindings survive — one PC drives two actions.
|
|
#expect(controller.switchBinding(for: .rigBypass)?.program == 8)
|
|
#expect(controller.switchBinding(for: .pedalBypass(pedalID: boards[0].pedals[0].id))?.program == 8)
|
|
#expect(sg.midiMap?.switches.count == 2)
|
|
|
|
// Firing the PC toggles both at once.
|
|
let pedalBypass: UIParamNode = boards[0].pedals[0].$bypass
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 8))
|
|
#expect(sg.rigBypassed == true)
|
|
#expect(abs(pedalBypass.value - 1.0) < 1e-9)
|
|
}
|
|
}
|
|
|
|
@Test func reLearningAnActionReplacesItsProgram() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.rigBypass)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 8))
|
|
controller.armSwitchLearn(.rigBypass)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 9)) // same action, new PC
|
|
|
|
#expect(controller.switchBinding(for: .rigBypass)?.program == 9, "re-learn replaces")
|
|
#expect(sg.midiMap?.switches.count == 1)
|
|
}
|
|
}
|
|
|
|
@Test func tunerToggleFlipsEngagedState() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.tunerToggle)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 10)) // learn only
|
|
#expect(!sg.tunerEngaged)
|
|
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 10))
|
|
#expect(sg.tunerEngaged, "first press engages")
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 10))
|
|
#expect(!sg.tunerEngaged, "second press disengages")
|
|
}
|
|
}
|
|
|
|
@Test func tapTempoRegistersEachPress() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.tapTempo)
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 11)) // learn only
|
|
#expect(sg.tapTimestamps.isEmpty)
|
|
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 11))
|
|
#expect(sg.tapTimestamps.count == 1, "first press registers a tap")
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 11))
|
|
#expect(sg.tapTimestamps.count == 2, "second press registers a second tap")
|
|
}
|
|
}
|
|
|
|
@Test func pedalActionDispatchesLooperRecordStop() throws {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
let board: PedalBoard = try sg.addBoard()
|
|
let looper: LooperPedal = LooperPedal()
|
|
try board.add(looper)
|
|
try sg.finalizeBoard(board)
|
|
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.pedalAction(pedalID: looper.id, kind: .looperRecordStop))
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 12)) // learn only
|
|
#expect(looper.transportState == .clear)
|
|
|
|
// The looper was added via the board, so it carries the shared tempo clock and its
|
|
// transport is beat-quantized: the mapped press arms recording for the next beat.
|
|
// Asserting the arm proves the Program Change dispatched to the pedal's action.
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 12))
|
|
#expect(looper.transportState == .armedRecord,
|
|
"mapped Program Change should dispatch the looper record/stop action")
|
|
}
|
|
}
|
|
|
|
@Test func pedalActionDispatchesLooperReverse() throws {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
let board: PedalBoard = try sg.addBoard()
|
|
let looper: LooperPedal = LooperPedal()
|
|
try board.add(looper)
|
|
try sg.finalizeBoard(board)
|
|
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.pedalAction(pedalID: looper.id, kind: .looperReverse))
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 20)) // learn only
|
|
#expect(looper.reverse == 0.0)
|
|
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 20)) // fire → toggleReverse
|
|
#expect(looper.reverse == 1.0, "mapped Program Change should flip the looper's reverse")
|
|
}
|
|
}
|
|
|
|
@Test func pedalActionCyclesLooperSync() throws {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
let board: PedalBoard = try sg.addBoard()
|
|
let looper: LooperPedal = LooperPedal()
|
|
try board.add(looper)
|
|
try sg.finalizeBoard(board)
|
|
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armSwitchLearn(.pedalAction(pedalID: looper.id, kind: .looperSyncToggle))
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 21)) // learn only
|
|
#expect(looper.syncToBeat == 1.0)
|
|
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 21)) // fire → cycle
|
|
#expect(looper.syncToBeat == 0.0, "mapped Program Change should oscillate Sync through its options")
|
|
}
|
|
}
|
|
|
|
@Test func pedalActionTapsLooperTrack() throws {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
let board: PedalBoard = try sg.addBoard()
|
|
let looper: LooperPedal = LooperPedal()
|
|
try board.add(looper)
|
|
try sg.finalizeBoard(board)
|
|
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
// Get a recorded, selected track A (immediate transport).
|
|
looper.clock = nil
|
|
looper.pressLoop()
|
|
looper.pressLoop()
|
|
#expect(looper.trackHasContent(0))
|
|
#expect(!looper.isTrackMuted(0))
|
|
|
|
controller.armSwitchLearn(.pedalAction(pedalID: looper.id, kind: .looperTrack(index: 0)))
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 22)) // learn only
|
|
#expect(!looper.isTrackMuted(0))
|
|
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 22)) // fire → tapTrack(0) → mute A
|
|
#expect(looper.isTrackMuted(0), "mapped Program Change should tap (mute) the selected recorded track")
|
|
}
|
|
}
|
|
}
|