Fix CC section: stop switch actions leaking in; add board-level target

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>
This commit is contained in:
2026-07-01 12:25:59 -07:00
parent ae8f370fa8
commit 1ba622a611
5 changed files with 263 additions and 214 deletions
@@ -15,7 +15,7 @@ import LyrebirdUI
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, Pedal, URL) {
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()
@@ -24,77 +24,75 @@ struct MIDIControllerCCTests {
try board.add(VolumePedal())
try sg.finalizeBoard(board)
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
return (controller, sg, board.pedals[0], url)
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, pedal, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig()
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 knob 0 on a Volume pedal.
controller.armCCLearn(boardIndex: 0, pedalIndex: 0, knobIndex: 0)
// 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(boardIndex: 0, pedalIndex: 0, knobIndex: 0)
let binding: MIDIMap.CCBinding? = controller.ccBinding(for: pedalControl(0, 0, 0))
#expect(binding?.controller == 20)
#expect(binding?.target.pedalNameHint == "Volume")
#expect(binding?.target.knobLabelHint == "Position")
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)
_ = pedal
}
@Test func boundCCDrivesKnobAcrossItsRange() throws {
@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, pedal, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig()
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armCCLearn(boardIndex: 0, pedalIndex: 0, knobIndex: 0)
controller.armCCLearn(pedalControl(0, 0, 0))
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
let node: UIParamNode = pedal.knobs[0].node // Position, range 0...1
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 knob max")
#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 knob min")
#expect(abs(node.value - 0.0) < 1e-9, "CC 0 should map to the control min")
}
}
@Test func unboundCCIsIgnored() throws {
let (controller, sg, pedal, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig()
@Test func ccCanTargetBoardMasterLevel() throws {
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
let node: UIParamNode = pedal.knobs[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, pedal, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
// Learn on channel 0 (the binding captures that channel).
controller.armCCLearn(boardIndex: 0, pedalIndex: 0, knobIndex: 0)
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
let node: UIParamNode = pedal.knobs[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")
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")
}
}
@@ -115,7 +113,7 @@ struct MIDIControllerCCTests {
let sliderIndex: Int = pedal.knobs.count // first control past the knobs is a slider
let control: PedalControl = pedal.ccMappableControls[sliderIndex]
controller.armCCLearn(boardIndex: 0, pedalIndex: 0, knobIndex: 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,
@@ -123,16 +121,46 @@ struct MIDIControllerCCTests {
}
}
@Test func clearRemovesBinding() throws {
let (controller, sg, _, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig()
@Test func unboundCCIsIgnored() throws {
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
controller.armCCLearn(boardIndex: 0, pedalIndex: 0, knobIndex: 0)
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(boardIndex: 0, pedalIndex: 0, knobIndex: 0)
#expect(controller.ccBinding(boardIndex: 0, pedalIndex: 0, knobIndex: 0) == nil)
controller.clearCCBinding(for: pedalControl(0, 0, 0))
#expect(controller.ccBinding(for: pedalControl(0, 0, 0)) == nil)
#expect(sg.midiMap?.ccs.isEmpty == true)
}
}
@@ -22,11 +22,16 @@ struct MIDIMapCodableTests {
MIDIMap.CCBinding(
controller: 7,
channel: 0,
target: MIDIMap.CCTarget(
boardIndex: 0, pedalIndex: 1, knobIndex: 0,
pedalNameHint: "Volume", knobLabelHint: "Position"
target: .pedalControl(
boardIndex: 0, pedalIndex: 1, controlIndex: 0,
pedalNameHint: "Volume", controlLabelHint: "Position"
)
),
MIDIMap.CCBinding(
controller: 11,
channel: 0,
target: .boardLevel(boardIndex: 0, boardLabelHint: "Board 1")
),
]
)
}