Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/MIDIMapMigrationTests.swift
T
josh 947dfe427a Add looper pedal: beat-quantized transport, undo, reverse, timeline
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>
2026-07-03 09:22:20 -07:00

109 lines
5.1 KiB
Swift

// MIDIMapMigrationTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
@testable import LyrebirdShoeGaze
@MainActor
@Suite("Legacy MIDI map migration")
struct MIDIMapMigrationTests {
/// Two boards; board 0 has two pedals, board 1 has one — with fixed ids to assert against.
private func sampleBoards() -> [BoardState] {
let od: PedalState = PedalState(type: "Overdrive", knobValues: [], bypass: 0)
let rv: PedalState = PedalState(type: "Reverb", knobValues: [], bypass: 0)
let dl: PedalState = PedalState(type: "Delay", knobValues: [], bypass: 0)
return [
BoardState(label: "Board 1", pedals: [od, rv]),
BoardState(label: "Board 2", pedals: [dl]),
]
}
// MARK: - Unit: positional → id conversion
@Test func migratesPositionalBindingsToIds() {
let boards: [BoardState] = sampleBoards()
let legacy: LegacyMIDIMap = LegacyMIDIMap(
switches: [
LegacyMIDIMap.LegacySwitchBinding(program: 1, channel: nil, action: .rigBypass, displayNameHint: nil),
LegacyMIDIMap.LegacySwitchBinding(program: 2, channel: nil, action: .boardBypass(boardIndex: 1), displayNameHint: "Board 2"),
LegacyMIDIMap.LegacySwitchBinding(program: 3, channel: nil, action: .pedalBypass(boardIndex: 0, pedalIndex: 1), displayNameHint: "Reverb"),
],
ccs: [
LegacyMIDIMap.LegacyCCBinding(controller: 7, channel: nil,
target: .pedalControl(boardIndex: 0, pedalIndex: 0, controlIndex: 2, pedalNameHint: "Overdrive", controlLabelHint: "Level"),
inverted: false),
LegacyMIDIMap.LegacyCCBinding(controller: 11, channel: nil,
target: .boardLevel(boardIndex: 1, boardLabelHint: "Board 2"), inverted: false),
]
)
let migrated: MIDIMap = legacy.migrated(boards: boards)
#expect(migrated.switches[0].action == .rigBypass)
#expect(migrated.switches[1].action == .boardBypass(boardID: boards[1].id))
#expect(migrated.switches[2].action == .pedalBypass(pedalID: boards[0].pedals[1].id))
#expect(migrated.ccs[0].target == .pedalControl(
pedalID: boards[0].pedals[0].id, controlIndex: 2,
pedalNameHint: "Overdrive", controlLabelHint: "Level"))
#expect(migrated.ccs[1].target == .boardLevel(boardID: boards[1].id, boardLabelHint: "Board 2"))
}
@Test func dropsOutOfRangeBindings() {
let boards: [BoardState] = sampleBoards()
let legacy: LegacyMIDIMap = LegacyMIDIMap(
switches: [
LegacyMIDIMap.LegacySwitchBinding(program: 1, channel: nil, action: .pedalBypass(boardIndex: 0, pedalIndex: 9), displayNameHint: nil),
LegacyMIDIMap.LegacySwitchBinding(program: 2, channel: nil, action: .boardSolo(boardIndex: 5), displayNameHint: nil),
],
ccs: []
)
let migrated: MIDIMap = legacy.migrated(boards: boards)
#expect(migrated.switches.isEmpty, "bindings addressing a non-existent board/pedal are dropped")
}
// MARK: - Integration: legacy JSON decodes through ShoeGazeState
@Test func legacyFormatJSONDecodesAndMigrates() throws {
let boards: [BoardState] = sampleBoards()
let legacy: LegacyMIDIMap = LegacyMIDIMap(
switches: [
LegacyMIDIMap.LegacySwitchBinding(program: 3, channel: 0, action: .pedalBypass(boardIndex: 0, pedalIndex: 1), displayNameHint: "Reverb"),
],
ccs: []
)
// Build the raw rig JSON with the *legacy* midiMap encoding spliced in — exactly what an
// old saved rig on disk looks like (boards may or may not carry ids; midiMap is positional).
var dict: [String: Any] = [
"version": presetFormatVersion,
"boards": try jsonObject(boards),
"midiMap": try jsonObject(legacy),
]
// Sanity: the spliced midiMap is the positional shape, not the id shape.
#expect(((dict["midiMap"] as? [String: Any])?["switches"] as? [Any])?.isEmpty == false)
let data: Data = try JSONSerialization.data(withJSONObject: dict)
let state: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data)
let map: MIDIMap = try #require(state.midiMap, "legacy midiMap should migrate, not drop to nil")
#expect(map.switches.count == 1)
// The board ids in `state.boards` are what migration used — assert the binding points at
// the Reverb (board 0, pedal 1) by that board's decoded pedal id.
#expect(map.switches[0].action == .pedalBypass(pedalID: state.boards[0].pedals[1].id))
_ = dict.removeValue(forKey: "midiMap")
}
/// Encodes a `Codable` value and re-reads it as a JSON object for splicing.
private func jsonObject<T: Encodable>(_ value: T) throws -> Any {
let data: Data = try JSONEncoder().encode(value)
return try JSONSerialization.jsonObject(with: data)
}
}