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>
193 lines
7.5 KiB
Swift
193 lines
7.5 KiB
Swift
// MIDIMap.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Foundation
|
||
|
||
/// A per-rig map of MIDI foot-controller bindings — which Program Change toggles which control, and
|
||
/// which Control Change drives which pedal knob.
|
||
///
|
||
/// Stored inside the rig (embedded in `ShoeGazeState`) so it travels with a saved rig. Pedals and
|
||
/// boards are addressed by **stable UUID** (`Pedal.id` / `PedalBoard.id`, persisted in
|
||
/// `PedalState`/`BoardState`), so a binding follows its pedal/board through insert, remove, move,
|
||
/// and reorder edits with no reindexing — the id survives the capture → rebuild round-trip. A
|
||
/// binding whose target no longer exists simply stops resolving (inert). The `*Hint` fields carry
|
||
/// the display name at learn time, for UI labels only (not resolution). Control-within-pedal is
|
||
/// still indexed into `Pedal.ccMappableControls`, which is stable for a given pedal type.
|
||
public struct MIDIMap: Codable, Equatable, Sendable {
|
||
|
||
/// Program-Change → action bindings (footswitch toggles). Populated in the switch phase.
|
||
public var switches: [SwitchBinding]
|
||
|
||
/// Control-Change → pedal-knob bindings (expression pedals).
|
||
public var ccs: [CCBinding]
|
||
|
||
/// An empty map (no bindings).
|
||
public static let empty: MIDIMap = MIDIMap()
|
||
|
||
/// Creates a MIDI map.
|
||
///
|
||
/// - Parameters:
|
||
/// - switches: Program-Change bindings. Defaults to empty.
|
||
/// - ccs: Control-Change bindings. Defaults to empty.
|
||
public init(
|
||
switches: [SwitchBinding] = [],
|
||
ccs: [CCBinding] = []
|
||
) {
|
||
self.switches = switches
|
||
self.ccs = ccs
|
||
}
|
||
|
||
// MARK: - Action
|
||
|
||
/// A toggle-style control a footswitch (Program Change) can drive. Program Change is momentary,
|
||
/// so boolean actions latch in software; `boardSolo` is exclusive (radio).
|
||
public enum Action: Codable, Equatable, Sendable {
|
||
case rigBypass
|
||
/// Mutes/unmutes the rig output (a hard silence, not a bypass).
|
||
case rigMute
|
||
/// Cancels any solo and makes every board audible again (the "All" button).
|
||
case allBoards
|
||
case boardSolo(boardID: UUID)
|
||
case boardAudible(boardID: UUID)
|
||
case boardBypass(boardID: UUID)
|
||
case pedalBypass(pedalID: UUID)
|
||
/// A pedal-specific momentary action beyond generic bypass (e.g. a looper's
|
||
/// record/stop or reset footswitch). `kind` is opaque to `MIDIMap` — `MIDIController`
|
||
/// resolves the target pedal (by id) and dispatches on `kind` itself.
|
||
case pedalAction(pedalID: UUID, kind: PedalActionKind)
|
||
/// Toggles the rig-level tuner on/off.
|
||
case tunerToggle
|
||
/// Registers one tap-tempo press (see `ShoeGaze.tapTempo()`).
|
||
case tapTempo
|
||
}
|
||
|
||
/// A pedal-specific momentary action addressable by ``Action/pedalAction(pedalID:kind:)``.
|
||
/// New pedals with more than a simple bypass toggle add cases here.
|
||
public enum PedalActionKind: Codable, Equatable, Sendable {
|
||
/// Looper: `.empty` → begin recording; `.recording` → stop and start looping.
|
||
case looperRecordStop
|
||
/// Looper: flips `overdubOn` between `0` and `1`.
|
||
case looperOverdubToggle
|
||
/// Looper: flips `loopOn` between `0` and `1`.
|
||
case looperLoopToggle
|
||
/// Looper: reset — clears the loop and returns to `.empty`.
|
||
case looperReset
|
||
}
|
||
|
||
// MARK: - Switch binding
|
||
|
||
/// A Program-Change number bound to a toggle ``Action``.
|
||
public struct SwitchBinding: Codable, Equatable, Sendable {
|
||
|
||
/// The Program Change number (0–127) that triggers the action.
|
||
public var program: UInt8
|
||
|
||
/// The MIDI channel (0–15), or `nil` to match any channel (omni).
|
||
public var channel: UInt8?
|
||
|
||
/// The control to toggle.
|
||
public var action: Action
|
||
|
||
/// The resolved control's display name at learn time, for stale-binding detection.
|
||
public var displayNameHint: String?
|
||
|
||
/// Creates a switch binding.
|
||
///
|
||
/// - Parameters:
|
||
/// - program: Program Change number (0–127).
|
||
/// - channel: MIDI channel (0–15), or `nil` for omni. Defaults to `nil`.
|
||
/// - action: The toggle action to perform.
|
||
/// - displayNameHint: Resolved control name at learn time. Defaults to `nil`.
|
||
public init(
|
||
program: UInt8,
|
||
channel: UInt8? = nil,
|
||
action: Action,
|
||
displayNameHint: String? = nil
|
||
) {
|
||
self.program = program
|
||
self.channel = channel
|
||
self.action = action
|
||
self.displayNameHint = displayNameHint
|
||
}
|
||
}
|
||
|
||
// MARK: - CC target
|
||
|
||
/// A continuous control an expression pedal can drive — either a specific pedal control (a knob
|
||
/// or slider, addressed by index into the pedal's ``Pedal/ccMappableControls``) or a board's
|
||
/// master level. The `*Hint` payloads capture the resolved display name at learn time so a
|
||
/// binding that no longer matches (chain edited) can be detected and skipped.
|
||
public enum CCTarget: Codable, Equatable, Sendable {
|
||
|
||
/// A knob or slider on a pedal, indexed into `pedal.ccMappableControls`.
|
||
case pedalControl(
|
||
pedalID: UUID,
|
||
controlIndex: Int,
|
||
pedalNameHint: String?,
|
||
controlLabelHint: String?
|
||
)
|
||
|
||
/// A board's master output level (the board fader).
|
||
case boardLevel(boardID: UUID, boardLabelHint: String?)
|
||
|
||
/// The rig-wide output volume (applies to the whole rig).
|
||
case rigVolume
|
||
|
||
/// Whether two targets address the same control, ignoring the display-name hints (used to
|
||
/// find and replace a binding regardless of the name captured at learn time).
|
||
public func addresses(_ other: CCTarget) -> Bool {
|
||
switch (self, other) {
|
||
case let (.pedalControl(p1, c1, _, _), .pedalControl(p2, c2, _, _)):
|
||
return p1 == p2 && c1 == c2
|
||
case let (.boardLevel(b1, _), .boardLevel(b2, _)):
|
||
return b1 == b2
|
||
case (.rigVolume, .rigVolume):
|
||
return true
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - CC binding
|
||
|
||
/// A Control-Change number bound to a pedal knob. The incoming value (0–127) is mapped across
|
||
/// the knob's own range; `inverted` swaps heel/toe for reversed expression pedals.
|
||
public struct CCBinding: Codable, Equatable, Sendable {
|
||
|
||
/// The Control Change number (0–127).
|
||
public var controller: UInt8
|
||
|
||
/// The MIDI channel (0–15), or `nil` to match any channel (omni).
|
||
public var channel: UInt8?
|
||
|
||
/// The pedal knob this CC drives.
|
||
public var target: CCTarget
|
||
|
||
/// When `true`, CC 0 maps to the knob's maximum and 127 to its minimum.
|
||
public var inverted: Bool
|
||
|
||
/// Creates a CC binding.
|
||
///
|
||
/// - Parameters:
|
||
/// - controller: Control Change number (0–127).
|
||
/// - channel: MIDI channel (0–15), or `nil` for omni. Defaults to `nil`.
|
||
/// - target: The pedal knob to drive.
|
||
/// - inverted: Reverse the sweep direction. Defaults to `false`.
|
||
public init(
|
||
controller: UInt8,
|
||
channel: UInt8? = nil,
|
||
target: CCTarget,
|
||
inverted: Bool = false
|
||
) {
|
||
self.controller = controller
|
||
self.channel = channel
|
||
self.target = target
|
||
self.inverted = inverted
|
||
}
|
||
}
|
||
}
|