Add MIDI foot-controller control; board/rig bypass; iOS fixes

Headline: MIDI control for a Behringer FCB1010 (four phases).
- MIDIController (Lyrebird MIDI client): connection indicator via source
  polling, background-queue->main-actor hop, MIDI Learn.
- Volume and Wah pedals (Wah reuses the SVF bandpass sweep, driven by
  pedal position instead of an envelope follower).
- CC -> any pedal knob (0-127 mapped across the knob range).
- Program Change -> pedal/board/rig toggles: software latch for booleans,
  exclusive radio for board solo. Positional addressing with display-name
  hints; stale/out-of-range bindings are skipped, not fired.
- Per-rig MIDIMap embedded in ShoeGazeState as an optional field (old rigs
  decode to nil; no preset version bump). Central MIDI Mapping sheet with
  Learn/Clear per control.

Also included (same working branch):
- Full board bypass and rig bypass (dry passthrough via cascaded
  crossfades in BoardOutputMaster), with UI toggles.
- Horizontal pedal picker grouped by category in filter order.
- Drag a palette pedal onto a chain card to insert before it
  (ShoeGaze.insertPedal); tap still appends.
- iOS: guard macOS-only .menuStyle(.borderlessButton) to silence the
  _UIReparentingView warning; iCloud-unavailable alert; keep the device
  awake while audio runs.

Tests: 24 added (Volume/Wah, MIDIMap Codable incl. legacy decode,
CC and PC controller behavior, insert/bypass API). Full suite green;
macOS and iOS app targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-01 08:18:09 -07:00
parent ec6c97c065
commit 1893576718
31 changed files with 2117 additions and 28 deletions
@@ -0,0 +1,113 @@
// 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, Pedal, 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.pedals[0], url)
}
@Test func learnCapturesBindingAndFiresCallback() throws {
let (controller, sg, pedal, url): (MIDIController, ShoeGaze, Pedal, 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)
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)
#expect(binding?.controller == 20)
#expect(binding?.target.pedalNameHint == "Volume")
#expect(binding?.target.knobLabelHint == "Position")
#expect(sg.midiMap?.ccs.count == 1)
_ = pedal
}
@Test func boundCCDrivesKnobAcrossItsRange() 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()
defer { try? FileManager.default.removeItem(at: url) }
try withExtendedLifetime(sg) {
controller.armCCLearn(boardIndex: 0, pedalIndex: 0, knobIndex: 0)
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
let node: UIParamNode = pedal.knobs[0].node // Position, range 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")
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")
}
}
@Test func unboundCCIsIgnored() throws {
let (controller, sg, pedal, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
try 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) }
try 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")
}
}
@Test func clearRemovesBinding() throws {
let (controller, sg, _, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
controller.armCCLearn(boardIndex: 0, pedalIndex: 0, knobIndex: 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)
#expect(sg.midiMap?.ccs.isEmpty == true)
}
}