7e39842eb2
A CC bound to a pedal parameter locked to a note division is now ignored while synced — the value is tempo-derived and a CC write would be overwritten on the next tempo change. Index-stable: ccMappableControls is unchanged (persisted bindings resolve by position), and only the CC *write* is skipped. MIDIController.applyCC checks Pedal.isNodeTempoSynced(_:) (node-identity match against tempoSyncableParams) and skips the update; the binding stays intact and resumes control once set back to Free. Test: MIDIControllerCCTests.ccIsIgnoredWhileParameterIsTempoSynced. 455 tests pass. Docs: CHANGELOG.md, RELEASES.md, plan MIDI section updated to "implemented". Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
263 lines
14 KiB
Swift
263 lines
14 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, PedalBoard, 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, url)
|
|
}
|
|
|
|
/// An address-only pedal-control CC target by pedal id (hints filled at capture time).
|
|
private func pedalControl(_ pedalID: UUID, _ control: Int) -> MIDIMap.CCTarget {
|
|
.pedalControl(pedalID: pedalID, controlIndex: control,
|
|
pedalNameHint: nil, controlLabelHint: nil)
|
|
}
|
|
|
|
@Test func learnCapturesBindingAndFiresCallback() throws {
|
|
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
var mapChanged: Bool = false
|
|
controller.onMapChanged = { mapChanged = true }
|
|
|
|
// Position is control 0 on a Volume pedal.
|
|
controller.armCCLearn(pedalControl(board.pedals[0].id, 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(for: pedalControl(board.pedals[0].id, 0))
|
|
#expect(binding?.controller == 20)
|
|
if case .pedalControl(_, _, let nameHint, let labelHint)? = binding?.target {
|
|
#expect(nameHint == "Volume")
|
|
#expect(labelHint == "Position")
|
|
} else {
|
|
Issue.record("expected a pedalControl target with captured hints")
|
|
}
|
|
#expect(sg.midiMap?.ccs.count == 1)
|
|
}
|
|
|
|
@Test func boundCCDrivesControlAcrossItsRange() 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, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armCCLearn(pedalControl(board.pedals[0].id, 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
|
|
let node: UIParamNode = board.pedals[0].ccMappableControls[0].node // Position, 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 control 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 control min")
|
|
}
|
|
}
|
|
|
|
@Test func ccCanTargetBoardMasterLevel() throws {
|
|
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
guard let levelNode: UIParamNode = board.masterGainNode else {
|
|
Issue.record("board should have a master-gain node after finalize")
|
|
return
|
|
}
|
|
let target: MIDIMap.CCTarget = .boardLevel(boardID: board.id, boardLabelHint: nil)
|
|
controller.armCCLearn(target)
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 11, value: 0)) // learn
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 11, value: 127))
|
|
#expect(abs(levelNode.value - 1.0) < 1e-9, "CC 127 should drive board level to max")
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 11, value: 0))
|
|
#expect(abs(levelNode.value - 0.0) < 1e-9, "CC 0 should drive board level to min")
|
|
}
|
|
}
|
|
|
|
@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(pedalControl(pedal.id, 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 invertedCCReversesTheSweep() throws {
|
|
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armCCLearn(pedalControl(board.pedals[0].id, 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
controller.setCCInverted(for: pedalControl(board.pedals[0].id, 0), true)
|
|
#expect(controller.isCCInverted(for: pedalControl(board.pedals[0].id, 0)))
|
|
|
|
let node: UIParamNode = board.pedals[0].ccMappableControls[0].node
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 127))
|
|
#expect(abs(node.value - 0.0) < 1e-9, "inverted CC 127 should map to the control min")
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
#expect(abs(node.value - 1.0) < 1e-9, "inverted CC 0 should map to the control max")
|
|
}
|
|
}
|
|
|
|
@Test func oneCCDrivesMultipleControlsForCrossfade() throws {
|
|
// Assigning a second control to a CC must NOT steal it from the first — a single expression
|
|
// pedal can crossfade by driving two controls (one inverted).
|
|
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
// Control 0 = Position, control 1 = Mix on a Volume pedal.
|
|
controller.armCCLearn(pedalControl(board.pedals[0].id, 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
controller.armCCLearn(pedalControl(board.pedals[0].id, 1))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
controller.setCCInverted(for: pedalControl(board.pedals[0].id, 1), true)
|
|
|
|
// Both bindings survive — the same CC drives both controls.
|
|
#expect(sg.midiMap?.ccs.count == 2, "assigning a 2nd control must not steal the 1st")
|
|
#expect(controller.ccBinding(for: pedalControl(board.pedals[0].id, 0)) != nil)
|
|
|
|
let position: UIParamNode = board.pedals[0].ccMappableControls[0].node
|
|
let mix: UIParamNode = board.pedals[0].ccMappableControls[1].node
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 127))
|
|
#expect(abs(position.value - 1.0) < 1e-9, "position sweeps up")
|
|
#expect(abs(mix.value - 0.0) < 1e-9, "inverted mix sweeps down — a crossfade")
|
|
}
|
|
}
|
|
|
|
@Test func ccCanTargetRigVolume() throws {
|
|
let (controller, sg, _, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
controller.armCCLearn(.rigVolume)
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 40, value: 0)) // learn
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 40, value: 127))
|
|
#expect(abs(sg.rigVolume - 1.0) < 1e-9, "CC 127 should drive rig volume to max")
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 40, value: 0))
|
|
#expect(abs(sg.rigVolume - 0.0) < 1e-9, "CC 0 should drive rig volume to min")
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 40, value: 64))
|
|
#expect(abs(sg.rigVolume - 64.0 / 127.0) < 1e-9, "CC 64 should map mid-range")
|
|
}
|
|
}
|
|
|
|
@Test func ccIsIgnoredWhileParameterIsTempoSynced() throws {
|
|
// A CC bound to a parameter that is locked to a note division must be ignored — the value is
|
|
// tempo-derived and a CC write would be overwritten on the next tempo change. The binding
|
|
// stays intact and resumes control once the parameter is set back to Free.
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 128)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
let sg: ShoeGaze = ShoeGaze(engine: engine)
|
|
sg.clock.bpm = 120.0
|
|
let board: PedalBoard = try sg.addBoard()
|
|
let delay: DelayPedal = DelayPedal()
|
|
try board.add(delay)
|
|
try sg.finalizeBoard(board)
|
|
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
|
|
|
|
withExtendedLifetime(sg) {
|
|
// Bind CC 21 to Time (control 0 on a Delay pedal).
|
|
controller.armCCLearn(pedalControl(delay.id, 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 21, value: 0))
|
|
|
|
// Lock Time to a quarter note — 0.5 s at 120 BPM.
|
|
delay.setSync(.division(.quarter), for: "time")
|
|
let syncedValue: Double = delay.$time.value
|
|
#expect(abs(syncedValue - 0.5) < 1e-9)
|
|
|
|
// A CC while synced must not move the value.
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 21, value: 127))
|
|
#expect(abs(delay.$time.value - syncedValue) < 1e-9, "CC must not drive a tempo-synced parameter")
|
|
|
|
// Back to Free — the same binding drives the control again.
|
|
delay.setSync(.free, for: "time")
|
|
let timeMax: Double = delay.ccMappableControls[0].range.upperBound
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 21, value: 127))
|
|
#expect(abs(delay.$time.value - timeMax) < 1e-9, "CC resumes control once the parameter is Free")
|
|
}
|
|
}
|
|
|
|
@Test func unboundCCIsIgnored() throws {
|
|
let (controller, sg, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
let node: UIParamNode = board.pedals[0].ccMappableControls[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, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
withExtendedLifetime(sg) {
|
|
// Learn on channel 0 (the binding captures that channel).
|
|
controller.armCCLearn(pedalControl(board.pedals[0].id, 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
|
|
let node: UIParamNode = board.pedals[0].ccMappableControls[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, board, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
controller.armCCLearn(pedalControl(board.pedals[0].id, 0))
|
|
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
|
|
#expect(sg.midiMap?.ccs.isEmpty == false)
|
|
|
|
controller.clearCCBinding(for: pedalControl(board.pedals[0].id, 0))
|
|
#expect(controller.ccBinding(for: pedalControl(board.pedals[0].id, 0)) == nil)
|
|
#expect(sg.midiMap?.ccs.isEmpty == true)
|
|
}
|
|
}
|