75edf44b64
Volume is a CC-mappable [0,1] gain applied after every board's output; mute is a separate hard gate (not the existing dry bypass), togglable via Program Change. Both are lagged to avoid clicks and fan out across all boards from a single ShoeGaze-level setter. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
213 lines
9.8 KiB
Swift
213 lines
9.8 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 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(boardIndex: 0, pedalIndex: 0))
|
|
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(boardIndex: 0))
|
|
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(boardIndex: 1))
|
|
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 index that doesn't exist; applying it must be a safe no-op.
|
|
controller.armSwitchLearn(.pedalBypass(boardIndex: 0, pedalIndex: 9))
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 6)) // learn
|
|
controller.ingestForTesting(.programChange(channel: 0, program: 6)) // apply — no crash
|
|
#expect(controller.switchBinding(for: .pedalBypass(boardIndex: 0, pedalIndex: 9)) != 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(boardIndex: 0, pedalIndex: 0))
|
|
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(boardIndex: 0, pedalIndex: 0))?.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)
|
|
}
|
|
}
|
|
}
|