1ba622a611
The MIDI Mapping sheet's two sections both keyed their ForEach rows on the enumeration offset (0,1,2…) and the Expression section mixed a bare header Text with a ForEach. SwiftUI's List reused structurally identical row views across the sections, so Program-Change switch rows (e.g. "Board 1 · Solo", "Board 1 · Overdrive · Bypass") leaked into the Expression (CC) section and their Learn button armed a Program Change instead of a Control Change. - Rebuild both sections from precomputed row models with globally-unique, section-namespaced string ids and a single ForEach each; no bare headers. - CC target is now an enum: `.pedalControl` (a knob/slider) or `.boardLevel` (the board fader). Board master level is now CC-assignable, shown as "<Board> · Level". - CC rows are labelled "<Pedal> · <Control>" (e.g. "Overdrive · Drive"). - Stale-binding + steal-on-reassign now key off an address matcher on the target enum. Tests updated for the enum target; added a board-level CC test. 319 green; both app targets build. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
167 lines
8.2 KiB
Swift
167 lines
8.2 KiB
Swift
// MIDIControllerCCTests.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 CC")
|
|
struct MIDIControllerCCTests {
|
|
|
|
/// Builds an engine + a one-board rig whose first pedal is a Volume pedal, plus a controller.
|
|
private func makeRig() throws -> (MIDIController, ShoeGaze, PedalBoard, URL) {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 128)
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
let board: PedalBoard = try sg.addBoard()
|
|
// Add the pedal directly (not via appendPedal, which rebuilds and replaces the board and
|
|
// would leave this `board` reference stale).
|
|
try board.add(VolumePedal())
|
|
try sg.finalizeBoard(board)
|
|
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
|
|
return (controller, sg, board, url)
|
|
}
|
|
|
|
/// An address-only pedal-control CC target (hints filled at capture time).
|
|
private func pedalControl(_ board: Int, _ pedal: Int, _ control: Int) -> MIDIMap.CCTarget {
|
|
.pedalControl(boardIndex: board, pedalIndex: pedal, controlIndex: control,
|
|
pedalNameHint: nil, controlLabelHint: nil)
|
|
}
|
|
|
|
@Test func learnCapturesBindingAndFiresCallback() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
var mapChanged: Bool = false
|
|
controller.onMapChanged = { mapChanged = true }
|
|
|
|
// Position is control 0 on a Volume pedal.
|
|
controller.armCCLearn(pedalControl(0, 0, 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 64))
|
|
|
|
#expect(controller.learnTarget == nil, "learn should clear after capture")
|
|
#expect(mapChanged, "onMapChanged should fire on capture")
|
|
let binding: MIDIMap.CCBinding? = controller.ccBinding(for: pedalControl(0, 0, 0))
|
|
#expect(binding?.controller == 20)
|
|
if case .pedalControl(_, _, _, let nameHint, let labelHint)? = binding?.target {
|
|
#expect(nameHint == "Volume")
|
|
#expect(labelHint == "Position")
|
|
} else {
|
|
Issue.record("expected a pedalControl target with captured hints")
|
|
}
|
|
#expect(sg.midiMap?.ccs.count == 1)
|
|
}
|
|
|
|
@Test func boundCCDrivesControlAcrossItsRange() throws {
|
|
// `sg` must be retained: MIDIController holds it weakly, so binding it to `_` would let the
|
|
// rig deallocate and every message no-op.
|
|
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armCCLearn(pedalControl(0, 0, 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
|
|
let node: UIParamNode = board.pedals[0].ccMappableControls[0].node // Position, 0...1
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 127))
|
|
#expect(abs(node.value - 1.0) < 1e-9, "CC 127 should map to the control max")
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 64))
|
|
#expect(abs(node.value - 64.0 / 127.0) < 1e-9, "CC 64 should map mid-range")
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
#expect(abs(node.value - 0.0) < 1e-9, "CC 0 should map to the control min")
|
|
}
|
|
}
|
|
|
|
@Test func ccCanTargetBoardMasterLevel() throws {
|
|
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
guard let levelNode: UIParamNode = board.masterGainNode else {
|
|
Issue.record("board should have a master-gain node after finalize")
|
|
return
|
|
}
|
|
let target: MIDIMap.CCTarget = .boardLevel(boardIndex: 0, boardLabelHint: nil)
|
|
controller.armCCLearn(target)
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 11, value: 0)) // learn
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 11, value: 127))
|
|
#expect(abs(levelNode.value - 1.0) < 1e-9, "CC 127 should drive board level to max")
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 11, value: 0))
|
|
#expect(abs(levelNode.value - 0.0) < 1e-9, "CC 0 should drive board level to min")
|
|
}
|
|
}
|
|
|
|
@Test func ccCanTargetASliderControl() throws {
|
|
// A pedal whose controls include vertical-fader sliders (e.g. Graphic EQ bands) must expose
|
|
// those sliders as CC targets, not only its rotary knobs.
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 128)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
let board: PedalBoard = try sg.addBoard()
|
|
try board.add(GraphicEQPedal())
|
|
try sg.finalizeBoard(board)
|
|
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
|
|
|
|
withExtendedLifetime(sg) {
|
|
let pedal: Pedal = board.pedals[0]
|
|
#expect(pedal.ccMappableControls.count > pedal.knobs.count, "sliders should be mappable")
|
|
|
|
let sliderIndex: Int = pedal.knobs.count // first control past the knobs is a slider
|
|
let control: PedalControl = pedal.ccMappableControls[sliderIndex]
|
|
controller.armCCLearn(pedalControl(0, 0, sliderIndex))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 30, value: 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 30, value: 127))
|
|
#expect(abs(control.node.value - control.range.upperBound) < 1e-9,
|
|
"CC 127 should drive the slider to its max")
|
|
}
|
|
}
|
|
|
|
@Test func unboundCCIsIgnored() throws {
|
|
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
let node: UIParamNode = board.pedals[0].ccMappableControls[0].node
|
|
let before: Double = node.value
|
|
// No binding learned → an incoming CC must not touch anything.
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 127))
|
|
#expect(node.value == before)
|
|
}
|
|
}
|
|
|
|
@Test func channelFilterBlocksOtherChannels() throws {
|
|
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
// Learn on channel 0 (the binding captures that channel).
|
|
controller.armCCLearn(pedalControl(0, 0, 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
|
|
let node: UIParamNode = board.pedals[0].ccMappableControls[0].node
|
|
node.update(0.5)
|
|
// A CC on a different channel must not drive the channel-0 binding.
|
|
controller.ingestForTesting(.controlChange(channel: 5, controller: 20, value: 127))
|
|
#expect(abs(node.value - 0.5) < 1e-9, "CC on a non-matching channel should be ignored")
|
|
}
|
|
}
|
|
|
|
@Test func clearRemovesBinding() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
controller.armCCLearn(pedalControl(0, 0, 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
#expect(sg.midiMap?.ccs.isEmpty == false)
|
|
|
|
controller.clearCCBinding(for: pedalControl(0, 0, 0))
|
|
#expect(controller.ccBinding(for: pedalControl(0, 0, 0)) == nil)
|
|
#expect(sg.midiMap?.ccs.isEmpty == true)
|
|
}
|
|
}
|