947dfe427a
Extend the looper into a double-width tempo-aware pedal: - Beat-quantized transport: record start and loop close both snap to the next beat via a LyrebirdRoutine waiting on the shared clock; loop length is an exact whole-beat span. Falls back to immediate/unquantized when no clock is present. - Loop-length read-outs (frames/seconds/beats), transport state, and a sample-clock-derived play-head position for the UI timeline. - Overdub undo (pre-dub snapshot + restore), reverse playback (live rate flip), and feedback-decay overdub (overdub preLevel via a Feedback knob). - Inject the shared LyrebirdClock + SynthesisEngine into Pedal via PedalBoard/ChainRouter so tempo-aware pedals can reach them. - New double-width LooperTimelineView (state badge, length read-out, sweeping play-head) wired into PedalView via Pedal.widthUnits. Concurrency hardening from RT-safety review: a transportGeneration counter drops superseded deferred beat/undo callbacks that LyrebirdRoutine.stop() cannot recall once enqueued; the undo restore is deferred past the RT overdub gate-close; transport routines use non-blocking stop(). Note: this commit also snapshots pre-existing uncommitted work-in-progress already in the tree (MIDI control refactor, Tuner, Tempo, preset changes), which is interleaved with the looper changes through shared files and could not be split into a separately-building commit. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
227 lines
12 KiB
Swift
227 lines
12 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 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)
|
|
}
|
|
}
|