52c676605f
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>
171 lines
7.9 KiB
Swift
171 lines
7.9 KiB
Swift
// PostBoardChainTests.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("Post-board chain")
|
|
struct PostBoardChainTests {
|
|
|
|
private func makeRig(
|
|
boardCount: Int,
|
|
duration: Double = 0.1
|
|
) throws -> (ShoeGaze, SynthesisEngine, URL) {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: duration, numAudioBuses: 256)
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
for _ in 0 ..< boardCount {
|
|
let board: PedalBoard = try sg.addBoard()
|
|
try board.add(OverdrivePedal())
|
|
try sg.finalizeBoard(board)
|
|
}
|
|
return (sg, engine, url)
|
|
}
|
|
|
|
private func render(_ engine: SynthesisEngine, _ url: URL) throws -> [Float] {
|
|
try engine.start()
|
|
return readStereoCAF(url: url)
|
|
}
|
|
|
|
@Test func postBoardChainProcessesTheMixAndRenders() throws {
|
|
let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 2)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
try sg.appendPostPedal(.reverb)
|
|
#expect(sg.postBoardStates.count == 1)
|
|
#expect(sg.postBoardPedals.count == 1, "the live post-chain holds the added pedal")
|
|
#expect(sg.boards.count == 2, "the boards are left in place")
|
|
|
|
let samples: [Float] = try render(engine, url)
|
|
#expect(hasNonSilentOutput(samples), "a post-board pedal keeps the summed mix flowing to output")
|
|
}
|
|
|
|
@Test func aClosedPostBoardVolumeSilencesTheOutput() throws {
|
|
let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 2)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
try sg.appendPostPedal(.volume)
|
|
// Close the post-board volume (position 0 = silent) before start → the final output is silent,
|
|
// proving the post-chain sits in the output path after the board mix.
|
|
let volume: Pedal = try #require(sg.postBoardPedals.first)
|
|
let state: PedalState = volume.captureState()
|
|
try #require(!state.knobValues.isEmpty)
|
|
volume.applyState(
|
|
PedalState(id: state.id, type: state.type, knobValues: [0.0],
|
|
switchValues: state.switchValues, bypass: state.bypass)
|
|
)
|
|
|
|
let samples: [Float] = try render(engine, url)
|
|
#expect(!hasNonSilentOutput(samples), "a closed post-board volume silences the whole rig output")
|
|
}
|
|
|
|
@Test func removingTheLastPostPedalRestoresDirectOutput() throws {
|
|
let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
try sg.appendPostPedal(.reverb)
|
|
#expect(sg.postBoardPedals.count == 1)
|
|
try sg.removePostPedal(at: 0)
|
|
#expect(sg.postBoardStates.isEmpty)
|
|
#expect(sg.postBoardPedals.isEmpty, "the post-chain is gone")
|
|
|
|
let samples: [Float] = try render(engine, url)
|
|
#expect(hasNonSilentOutput(samples), "the rig renders straight to hardware again")
|
|
}
|
|
|
|
@Test func addingABoardWhileAPostChainExistsKeepsOutputFlowing() throws {
|
|
let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
try sg.appendPostPedal(.reverb)
|
|
// Add a second board AFTER the post-chain exists. The post-chain sits at the tail of the main
|
|
// group, so the new master (spliced nearer the head) evaluates before it with no rebuild.
|
|
let second: PedalBoard = try sg.addBoard()
|
|
try second.add(DelayPedal())
|
|
try sg.finalizeBoard(second)
|
|
#expect(sg.boards.count == 2)
|
|
#expect(sg.postBoardPedals.count == 1, "the post-chain survives the board add")
|
|
|
|
let samples: [Float] = try render(engine, url)
|
|
#expect(hasNonSilentOutput(samples), "both boards reach output through the re-ordered post-chain")
|
|
}
|
|
|
|
@Test func postChainEvaluatesAfterEveryBoardMasterNotJustTheLast() throws {
|
|
// The naive anchor was `outputMasters.last` — the *last-finalized* board. But board taps are
|
|
// spliced `.after(feedNode)`, so a later board evaluates EARLIER; `outputMasters.last` is the
|
|
// first-evaluated master, and a post-chain placed after it reads the mix before the other
|
|
// boards write it. Here we silence the last-finalized board and keep the first one audible:
|
|
// if the post-chain ran before board 0's master, the mix would be empty and the output silent.
|
|
let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 2)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
try sg.appendPostPedal(.reverb)
|
|
// Close the LAST-finalized board's master; only the first board feeds the mix now.
|
|
let lastBoardGain: UIParamNode = try #require(sg.boards[1].masterGainNode)
|
|
lastBoardGain.update(0.0)
|
|
|
|
let samples: [Float] = try render(engine, url)
|
|
#expect(hasNonSilentOutput(samples),
|
|
"the first board must still reach the output through a tail-ordered post-chain")
|
|
}
|
|
|
|
@Test func insertMoveAndRemoveOrderThePostChain() throws {
|
|
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
try sg.appendPostPedal(.reverb)
|
|
try sg.insertPostPedal(.volume, at: 0)
|
|
#expect(sg.postBoardStates.map { $0.type } == ["Volume", "Reverb"], "insert lands at the index")
|
|
|
|
// SwiftUI `toOffset` semantics — move the Reverb (index 1) to the front.
|
|
try sg.movePostPedal(from: 1, to: 0)
|
|
#expect(sg.postBoardStates.map { $0.type } == ["Reverb", "Volume"], "move reorders the chain")
|
|
|
|
try sg.removePostPedal(at: 0)
|
|
#expect(sg.postBoardStates.map { $0.type } == ["Volume"], "remove drops the indexed pedal")
|
|
#expect(sg.postBoardPedals.map { $0.id } == sg.postBoardStates.map { $0.id },
|
|
"live pedals track the states in order")
|
|
}
|
|
|
|
@Test func postBoardChainRoundTripsThroughSaveLoad() throws {
|
|
let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
try sg.appendPostPedal(.reverb)
|
|
let knob: PedalKnob = try #require(sg.postBoardPedals.first?.knobs.first)
|
|
let sentinel: Double = knob.range.lowerBound + (knob.range.upperBound - knob.range.lowerBound) * 0.44
|
|
knob.node.update(sentinel)
|
|
|
|
let preset: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent(UUID().uuidString + ".json")
|
|
defer { try? FileManager.default.removeItem(at: preset) }
|
|
try sg.saveRig(to: preset)
|
|
|
|
let (engine2, url2): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 256)
|
|
defer { try? FileManager.default.removeItem(at: url2) }
|
|
let sg2: ShoeGaze = ShoeGaze(engine: engine2)
|
|
try sg2.loadRig(from: preset)
|
|
#expect(sg2.postBoardStates.map { $0.type } == ["Reverb"], "post-chain round-trips")
|
|
#expect(sg2.postBoardPedals.count == 1, "the reloaded post-chain is live")
|
|
let reloadedKnob: Double = try #require(sg2.postBoardPedals.first?.captureState().knobValues.first)
|
|
#expect(abs(reloadedKnob - sentinel) < 1e-9, "a post-pedal knob tweak survives save/load")
|
|
|
|
try engine2.start()
|
|
#expect(hasNonSilentOutput(readStereoCAF(url: url2)), "reloaded post-board rig renders")
|
|
}
|
|
|
|
@Test func oldRigWithoutPostChainDecodesToEmpty() throws {
|
|
let state: ShoeGazeState = ShoeGazeState(boards: [])
|
|
#expect(state.postChain == nil)
|
|
let data: Data = try JSONEncoder().encode(state)
|
|
let decoded: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data)
|
|
#expect(decoded.postChain == nil, "absent/nil postChain stays nil")
|
|
}
|
|
}
|