Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift
T
josh 52c676605f feat(postboard): shared post-board chain after the board mix
Add a dedicated linear stereo post-board chain that processes the summed
board mix once, after the board split, before the hardware output — for
global reverb, a bus limiter, a final EQ, and the like.

Routing: when a post-chain exists, every BoardOutputMaster writes a shared
stereo mix bus (parameterized output bus indices) instead of hardware; the
post-chain (PostChain + PostChainOutput) reads that mix, runs its pedals in
order, and copies the tail to the hardware outputs. Always stereo-headed
(dual-mono for a mono rig) so mono pedals auto-stereoize per channel.

The chain's head node is inserted at .tail(of: mainGroup), so it evaluates
after every board master regardless of board add order — no re-order on
board add, and no board dropped from the mix. buildPostChain tracks the
chain before wiring it so a mid-build throw stays reclaimable by teardown.

State: additive ShoeGazeState.postChain (nil-when-empty, decodeIfPresent,
byte-identical old-rig round-trip). Edit API (append/insert/remove/move)
mirrors the pre-board chain: capture-from-live, stop/teardown/rebuild. Undo
treats a post-topology change as a full rebuild via id-sequence compare.
UI: a hide-when-empty post-board strip below the boards, symmetric to the
pre-board strip, with palette routing and per-pedal remove through undo.

Bumps the iOS demo's Lyrebird pin to 2.3.0 (onEditingEnded).

Tests: PostBoardChainTests (render, closed-volume silences output,
post-chain-after-every-master ordering, board-add ordering, insert/move/
remove, save/load round-trip with knob, old-rig decode) + post-pedal undo.
855 tests pass. Reviewed (RT-safety + line sweep); the ordering bug both
reviewers caught is fixed at the root and covered by a deterministic test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-26 00:22:39 -08:00

387 lines
19 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")
}
}
@Test func tunedVerbAndFreezVerbExposeAPrimaryTransport() {
// Unconditional conformance — the `as` is a compile-time proof the pedals are transport-mappable.
let tunedVerb: any MIDITransportPedal = TunedVerbPedal()
let freezVerb: any MIDITransportPedal = FreezVerbPedal()
#expect(tunedVerb.midiTransportName == "Capture")
#expect(freezVerb.midiTransportName == "Freeze")
}
@Test func mappedProgramChangeTogglesTunedVerbCapture() 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)
let board: PedalBoard = try sg.addBoard()
let tuned: TunedVerbPedal = TunedVerbPedal()
try board.add(tuned)
try sg.finalizeBoard(board)
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
withExtendedLifetime(sg) {
controller.armSwitchLearn(.pedalAction(pedalID: tuned.id, kind: .pedalPrimaryTransport))
controller.ingestForTesting(.programChange(channel: 0, program: 3)) // learn only
#expect(tuned.captured < 0.5, "learn must not toggle Capture")
controller.ingestForTesting(.programChange(channel: 0, program: 3)) // fire
#expect(tuned.captured > 0.5, "a mapped Program Change toggles Capture on")
controller.ingestForTesting(.programChange(channel: 0, program: 3)) // fire again
#expect(tuned.captured < 0.5, "a second press toggles Capture off")
}
}
}