ae8f370fa8
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>
139 lines
6.7 KiB
Swift
139 lines
6.7 KiB
Swift
// 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) }
|
|
|
|
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) }
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
@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) }
|
|
|
|
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)
|
|
}
|
|
}
|