diff --git a/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift b/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift index e980337..cb5fad9 100644 --- a/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift +++ b/Sources/LyrebirdShoeGaze/MIDI/MIDIController.swift @@ -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 diff --git a/Sources/LyrebirdShoeGaze/Pedal.swift b/Sources/LyrebirdShoeGaze/Pedal.swift index 8f79c4c..b1be867 100644 --- a/Sources/LyrebirdShoeGaze/Pedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedal.swift @@ -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. diff --git a/Sources/LyrebirdShoeGaze/PedalControl.swift b/Sources/LyrebirdShoeGaze/PedalControl.swift new file mode 100644 index 0000000..f3353fb --- /dev/null +++ b/Sources/LyrebirdShoeGaze/PedalControl.swift @@ -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 + + /// 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) { + self.label = label + self.node = node + self.range = range + } +} diff --git a/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Edit.swift b/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Edit.swift index c415216..b82f8f1 100644 --- a/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Edit.swift +++ b/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Edit.swift @@ -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 diff --git a/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift b/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift index daaa348..f577acf 100644 --- a/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift +++ b/Sources/LyrebirdShoeGazeUI/MIDIMappingSheet.swift @@ -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) diff --git a/Tests/LyrebirdShoeGazeTests/BoardEditAPITests.swift b/Tests/LyrebirdShoeGazeTests/BoardEditAPITests.swift index c18c898..dd8b578 100644 --- a/Tests/LyrebirdShoeGazeTests/BoardEditAPITests.swift +++ b/Tests/LyrebirdShoeGazeTests/BoardEditAPITests.swift @@ -191,6 +191,23 @@ struct BoardEditAPITests { } } + @Test func insertIntoRegionOnlyBoardLandsBeforeRegion() throws { + // A board with no linear pedals but a trailing split region: inserting at index 0 must place + // the pedal at the head of the chain, BEFORE the region — not after it. + let (sg, _, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) = + try makeRig(pedals: []) + defer { try? FileManager.default.removeItem(at: url) } + + var live: PedalBoard = try sg.appendStereoSplit(to: board) // region-only board + live = try sg.insertPedal(.fuzz, at: 0, to: live, lane: .main) + + #expect(names(live) == ["Fuzz"], "the inserted pedal joins the linear chain") + guard case .pedal = live.captureState().segments.first else { + Issue.record("pedal should be the first segment, ahead of the split region") + return + } + } + @Test func insertIntoBranchLaneBeforeTarget() throws { let (sg, _, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) = try makeRig(pedals: ["Overdrive"]) diff --git a/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift b/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift index 35f6f09..a3410e0 100644 --- a/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift +++ b/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift @@ -54,7 +54,7 @@ struct MIDIControllerCCTests { let (controller, sg, pedal, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + withExtendedLifetime(sg) { controller.armCCLearn(boardIndex: 0, pedalIndex: 0, knobIndex: 0) controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0)) @@ -72,7 +72,7 @@ struct MIDIControllerCCTests { let (controller, sg, pedal, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + withExtendedLifetime(sg) { let node: UIParamNode = pedal.knobs[0].node let before: Double = node.value // No binding learned → an incoming CC must not touch anything. @@ -85,7 +85,7 @@ struct MIDIControllerCCTests { let (controller, sg, pedal, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + 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)) @@ -98,6 +98,31 @@ struct MIDIControllerCCTests { } } + @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(boardIndex: 0, pedalIndex: 0, knobIndex: 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 clearRemovesBinding() throws { let (controller, sg, _, url): (MIDIController, ShoeGaze, Pedal, URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } diff --git a/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift b/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift index cb1201f..41cddbe 100644 --- a/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift +++ b/Tests/LyrebirdShoeGazeTests/MIDIControllerPCTests.swift @@ -34,7 +34,7 @@ struct MIDIControllerPCTests { let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + withExtendedLifetime(sg) { var changed: Bool = false controller.onMapChanged = { changed = true } controller.armSwitchLearn(.rigBypass) @@ -51,7 +51,7 @@ struct MIDIControllerPCTests { let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + withExtendedLifetime(sg) { controller.armSwitchLearn(.rigBypass) controller.ingestForTesting(.programChange(channel: 0, program: 1)) // learn only #expect(sg.rigBypassed == false, "learn must not toggle") @@ -67,7 +67,7 @@ struct MIDIControllerPCTests { let (controller, sg, boards, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + withExtendedLifetime(sg) { let node: UIParamNode = boards[0].pedals[0].$bypass controller.armSwitchLearn(.pedalBypass(boardIndex: 0, pedalIndex: 0)) controller.ingestForTesting(.programChange(channel: 0, program: 5)) // learn @@ -83,7 +83,7 @@ struct MIDIControllerPCTests { let (controller, sg, boards, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + withExtendedLifetime(sg) { controller.armSwitchLearn(.boardBypass(boardIndex: 0)) controller.ingestForTesting(.programChange(channel: 0, program: 3)) // learn controller.ingestForTesting(.programChange(channel: 0, program: 3)) @@ -98,7 +98,7 @@ struct MIDIControllerPCTests { try makeRig(boardCount: 2) defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + withExtendedLifetime(sg) { controller.armSwitchLearn(.boardSolo(boardIndex: 1)) controller.ingestForTesting(.programChange(channel: 0, program: 2)) // learn controller.ingestForTesting(.programChange(channel: 0, program: 2)) // solo board 2 @@ -112,7 +112,7 @@ struct MIDIControllerPCTests { let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + withExtendedLifetime(sg) { controller.armSwitchLearn(.rigBypass) controller.ingestForTesting(.programChange(channel: 0, program: 1)) // learn on ch 0 controller.ingestForTesting(.programChange(channel: 4, program: 1)) // other channel @@ -124,7 +124,7 @@ struct MIDIControllerPCTests { let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + withExtendedLifetime(sg) { // Bind to a pedal index that doesn't exist; applying it must be a safe no-op. controller.armSwitchLearn(.pedalBypass(boardIndex: 0, pedalIndex: 9)) controller.ingestForTesting(.programChange(channel: 0, program: 6)) // learn @@ -137,7 +137,7 @@ struct MIDIControllerPCTests { let (controller, sg, _, url): (MIDIController, ShoeGaze, [PedalBoard], URL) = try makeRig() defer { try? FileManager.default.removeItem(at: url) } - try withExtendedLifetime(sg) { + withExtendedLifetime(sg) { controller.armSwitchLearn(.rigBypass) controller.ingestForTesting(.programChange(channel: 0, program: 8)) controller.armSwitchLearn(.boardBypass(boardIndex: 0))