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
+16
View File
@@ -187,6 +187,22 @@ open class Pedal: SynthesizerBase {
]
}
/// Every continuous control on this pedal that a MIDI Control Change can drive — the pedal's
/// knobs (effect knobs + Mix + Level) followed by its vertical-fader sliders. Discrete controls
/// (switches, stepped selectors) are excluded; they suit Program Change toggles, not CC sweeps.
///
/// Ordering is stable — knobs first, then sliders — so a persisted CC binding addressed by index
/// keeps resolving to the same control across launches. Used by MIDI mapping.
public var ccMappableControls: [PedalControl] {
let knobControls: [PedalControl] = knobs.map {
PedalControl(label: $0.label, node: $0.node, range: $0.range)
}
let sliderControls: [PedalControl] = effectSliders.map {
PedalControl(label: $0.label, node: $0.node, range: $0.range)
}
return knobControls + sliderControls
}
// MARK: - Subclass hook
/// Returns the processed (wet) mono signal for this pedal.
@@ -0,0 +1,38 @@
// PedalControl.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdUI
/// A continuous pedal control that can be driven by a MIDI Control Change — the common shape of a
/// ``PedalKnob`` and a ``PedalSlider`` (a labelled `UIParamNode` with a value range).
///
/// Used by MIDI mapping to enumerate every CC-assignable control on a pedal uniformly, regardless of
/// how the pedal chose to present it (rotary knob vs vertical fader). Discrete controls (switches,
/// stepped selectors) are intentionally excluded — they suit momentary Program Change bindings, not
/// continuous expression control.
public struct PedalControl {
/// Short display label (e.g. `"Rate"`, `"Position"`).
public let label: String
/// The live parameter node the CC drives.
public let node: UIParamNode
/// Inclusive value range the control spans, in the parameter's own units.
public let range: ClosedRange<Double>
/// Creates a pedal control descriptor.
///
/// - Parameters:
/// - label: Short display label.
/// - node: The live parameter node.
/// - range: Inclusive value range in the parameter's own units.
public init(label: String, node: UIParamNode, range: ClosedRange<Double>) {
self.label = label
self.node = node
self.range = range
}
}
@@ -105,7 +105,9 @@ extension ShoeGaze {
// routing node — mirrors `appendPedal`'s placement rule.
segments.insert(.pedal(newState), at: lastPedalIndex + 1)
} else {
segments.append(.pedal(newState))
// No linear pedals at all: insert-before means the head of the chain, ahead of any
// trailing routing region (e.g. a board that is only a stereo split).
segments.insert(.pedal(newState), at: 0)
}
case .left, .right:
try mutateBranch(&segments, lane: lane) { branch in
@@ -12,8 +12,9 @@ import LyrebirdUI
/// pedal knobs via MIDI Learn.
///
/// Presented from the transport-row ``MIDIIndicatorView``. Rows are generated by walking the live
/// `ShoeGaze.boards` / `board.pedals` / `pedal.knobs`, so bindings address controls positionally.
/// (The Program-Change switch section is added in a later phase.)
/// `ShoeGaze.boards` / `board.pedals` and each pedal's controls, so bindings address controls
/// positionally. A Switches section binds Program Change to toggle actions; an Expression section
/// binds Control Change to continuous pedal controls.
public struct MIDIMappingSheet: View {
// MARK: - Constants
@@ -145,21 +146,29 @@ public struct MIDIMappingSheet: View {
let boardIndex: Int = boardPair.offset
let board: PedalBoard = boardPair.element
ForEach(Array(board.pedals.enumerated()), id: \.offset) { pedalPair in
pedalRows(boardIndex: boardIndex, pedalIndex: pedalPair.offset, pedal: pedalPair.element)
pedalRows(
boardIndex: boardIndex,
pedalIndex: pedalPair.offset,
pedal: pedalPair.element,
boardLabel: board.label
)
}
}
}
}
@ViewBuilder
private func pedalRows(boardIndex: Int, pedalIndex: Int, pedal: Pedal) -> some View {
ForEach(Array(pedal.knobs.enumerated()), id: \.offset) { knobPair in
private func pedalRows(boardIndex: Int, pedalIndex: Int, pedal: Pedal, boardLabel: String) -> some View {
// Per-pedal header so each control row can show just its own name.
Text("\(boardLabel) · \(pedal.displayName)")
.font(.subheadline.weight(.semibold))
.foregroundStyle(.secondary)
ForEach(Array(pedal.ccMappableControls.enumerated()), id: \.offset) { controlPair in
knobRow(
boardIndex: boardIndex,
pedalIndex: pedalIndex,
knobIndex: knobPair.offset,
pedalName: pedal.displayName,
knobLabel: knobPair.element.label
knobIndex: controlPair.offset,
controlLabel: controlPair.element.label
)
}
}
@@ -168,8 +177,7 @@ public struct MIDIMappingSheet: View {
boardIndex: Int,
pedalIndex: Int,
knobIndex: Int,
pedalName: String,
knobLabel: String
controlLabel: String
) -> some View {
let isArmed: Bool = controller.learnTarget == .cc(
boardIndex: boardIndex, pedalIndex: pedalIndex, knobIndex: knobIndex
@@ -179,7 +187,7 @@ public struct MIDIMappingSheet: View {
)
return HStack {
VStack(alignment: .leading, spacing: 1) {
Text("\(pedalName) · \(knobLabel)")
Text(controlLabel)
Text(bindingLabel(isArmed: isArmed, binding: binding))
.font(.caption)
.foregroundStyle(isArmed ? Color.orange : .secondary)