3ef39a7a0d
Add a looperSyncToggle PedalActionKind (dispatched to LooperPedal.cycleSyncMode) and a Sync row in the MIDI mapping sheet. A mapped switch/Program Change oscillates the control through its positions on each press, via a reusable PedalSwitch.nextValue(after:) that advances to the next option and wraps — so this generalises to any multi-position switch, not just the two-state Sync. Tests: cycleSyncModeOscillatesThroughOptions, pedalActionCyclesLooperSync. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
310 lines
15 KiB
Swift
310 lines
15 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(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")
|
|
}
|
|
}
|
|
}
|