Address review; make all pedal controls (incl. sliders) CC-mappable

Review fixes:
- Remove stale phase-plan references from MIDIController / MIDIMappingSheet
  doc-comments (describe current behavior instead).
- insertPedal: a board with no linear pedals but a trailing routing region
  now inserts at the head (before the region) instead of appending after it.
- Drop the unused MIDIController.retarget (the app recreates the controller
  against a fresh engine rather than re-pointing it).
- CC steal now matches controller AND channel, so per-channel CC bindings
  can coexist.

Feature (requested): CC mapping now covers every continuous pedal control,
not just rotary knobs. New PedalControl descriptor + Pedal.ccMappableControls
(knobs followed by effect sliders); the Expression sheet lists them grouped
per pedal with a header, so slider params (e.g. EQ bands) are assignable and
labelled clearly. Knob-first ordering keeps existing CC bindings stable.

Tests: +2 (CC drives a slider control; insert into a region-only board lands
before the region). 318 total green; both app targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-01 08:34:30 -07:00
parent 1893576718
commit ae8f370fa8
8 changed files with 149 additions and 46 deletions
@@ -10,14 +10,14 @@ import LyrebirdUI
/// Owns the Core MIDI connection for a ShoeGaze session and routes incoming messages to the rig.
///
/// Constructed once by the app (it must outlive `ShoeGaze` rebuilds), started with ``start()``, and
/// re-pointed at a fresh `ShoeGaze` via ``retarget(_:)`` after an engine/rig rebuild. Handlers fire
/// on Lyrebird's background MIDI queue; this controller hops to the main actor before touching any
/// `@MainActor` rig state.
/// Constructed once by the app (it is recreated against a fresh engine when the engine is rebuilt),
/// and started with ``start()``. Handlers fire on Lyrebird's background MIDI queue; this controller
/// hops to the main actor before touching any `@MainActor` rig state.
///
/// This is the Phase 1 surface: it establishes the connection, publishes device-connection state
/// for the UI indicator, and installs a message handler whose body is filled in by later phases
/// (CC → pedal position, Program Change → toggle actions).
/// It establishes the Core MIDI connection, publishes device-connection state for the UI indicator,
/// and routes incoming messages to the rig: Control Change drives bound pedal controls, and Program
/// Change toggles bound pedal / board / rig actions. MIDI Learn arms a control so the next matching
/// message binds to it.
@MainActor
@Observable
public final class MIDIController {
@@ -111,11 +111,6 @@ public final class MIDIController {
midi.stop()
}
/// Re-points the controller at a fresh `ShoeGaze` after an engine/rig rebuild.
public func retarget(_ shoeGaze: ShoeGaze) {
self.shoeGaze = shoeGaze
}
// MARK: - Message handling
private func installHandler() {
@@ -131,7 +126,8 @@ public final class MIDIController {
}
/// Routes one incoming message to the rig: captures a binding when armed for Learn, otherwise
/// applies any matching binding. (Program Change handling is added in a later phase.)
/// applies any matching binding. Control Change drives bound controls; Program Change toggles
/// bound actions.
private func handle(_ message: MIDIMessage) {
switch message {
case .controlChange(let channel, let controller, let value):
@@ -209,14 +205,14 @@ public final class MIDIController {
learnTarget = nil
guard let shoeGaze: ShoeGaze = shoeGaze,
let pedal: Pedal = pedal(boardIndex: boardIndex, pedalIndex: pedalIndex),
knobIndex >= 0, knobIndex < pedal.knobs.count else { return }
knobIndex >= 0, knobIndex < pedal.ccMappableControls.count else { return }
let target: MIDIMap.CCTarget = MIDIMap.CCTarget(
boardIndex: boardIndex,
pedalIndex: pedalIndex,
knobIndex: knobIndex,
pedalNameHint: pedal.displayName,
knobLabelHint: pedal.knobs[knobIndex].label
knobLabelHint: pedal.ccMappableControls[knobIndex].label
)
let binding: MIDIMap.CCBinding = MIDIMap.CCBinding(
controller: controller,
@@ -225,8 +221,9 @@ public final class MIDIController {
)
var map: MIDIMap = shoeGaze.midiMap ?? MIDIMap.empty
// One knob → one CC, and reassigning a CC steals it from any other knob.
map.ccs.removeAll { $0.target == target || $0.controller == controller }
// One control → one CC, and reassigning a CC (same controller + channel) steals it from any
// other control so a given expression pedal drives exactly one thing.
map.ccs.removeAll { $0.target == target || ($0.controller == controller && $0.channel == channel) }
map.ccs.append(binding)
shoeGaze.midiMap = map
onMapChanged?()
@@ -245,18 +242,18 @@ public final class MIDIController {
private func applyCCBinding(_ binding: MIDIMap.CCBinding, value: UInt8) {
let target: MIDIMap.CCTarget = binding.target
guard let pedal: Pedal = pedal(boardIndex: target.boardIndex, pedalIndex: target.pedalIndex),
target.knobIndex >= 0, target.knobIndex < pedal.knobs.count else { return }
let knob: PedalKnob = pedal.knobs[target.knobIndex]
target.knobIndex >= 0, target.knobIndex < pedal.ccMappableControls.count else { return }
let control: PedalControl = pedal.ccMappableControls[target.knobIndex]
// Stale-binding guard: the chain may have been edited under the positional address.
if let nameHint: String = target.pedalNameHint, nameHint != pedal.displayName { return }
if let labelHint: String = target.knobLabelHint, labelHint != knob.label { return }
if let labelHint: String = target.knobLabelHint, labelHint != control.label { return }
let ccMax: Double = 127.0
var norm: Double = Double(value) / ccMax
if binding.inverted { norm = 1.0 - norm }
let mapped: Double = knob.range.lowerBound
+ norm * (knob.range.upperBound - knob.range.lowerBound)
knob.node.update(mapped)
let mapped: Double = control.range.lowerBound
+ norm * (control.range.upperBound - control.range.lowerBound)
control.node.update(mapped)
}
// MARK: - Switch (Program Change) learn + apply