Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/MIDIControllerCCTests.swift
T
josh 4d8464282e Add per-binding CC invert toggle
Each CC binding gains an "Invert" toggle in the mapping sheet (shown once the
control is bound). Inverting swaps the sweep — CC 0 drives the control to its
max, 127 to its min — so a single expression pedal can crossfade two controls:
map both to the same CC and invert one.

The MIDICCBinding.inverted field and applyCC already honored inversion; this
adds isCCInverted / setCCInverted plus the sheet toggle, and the binding label
now notes "· inverted".

Test: an inverted binding reverses the mapped output. 321 green; both app
targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 13:09:18 -07:00

185 lines
9.2 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 (hints filled at capture time).
private func pedalControl(_ board: Int, _ pedal: Int, _ control: Int) -> MIDIMap.CCTarget {
.pedalControl(boardIndex: board, pedalIndex: pedal, controlIndex: control,
pedalNameHint: nil, controlLabelHint: nil)
}
@Test func learnCapturesBindingAndFiresCallback() throws {
let (controller, sg, _, 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(0, 0, 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(0, 0, 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(0, 0, 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(boardIndex: 0, 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(0, 0, 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(0, 0, 0))
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
controller.setCCInverted(for: pedalControl(0, 0, 0), true)
#expect(controller.isCCInverted(for: pedalControl(0, 0, 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 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(0, 0, 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, _, url): (MIDIController, ShoeGaze, PedalBoard, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
controller.armCCLearn(pedalControl(0, 0, 0))
controller.ingestForTesting(.controlChange(channel: 0, controller: 20, value: 0))
#expect(sg.midiMap?.ccs.isEmpty == false)
controller.clearCCBinding(for: pedalControl(0, 0, 0))
#expect(controller.ccBinding(for: pedalControl(0, 0, 0)) == nil)
#expect(sg.midiMap?.ccs.isEmpty == true)
}
}