Files
ShoeGaze/Sources/LyrebirdShoeGaze/PedalSwitch.swift
T
josh 3ef39a7a0d Looper: MIDI-mappable Sync, cycling through the switch options
Add a looperSyncToggle PedalActionKind (dispatched to LooperPedal.cycleSyncMode)
and a Sync row in the MIDI mapping sheet. A mapped switch/Program Change
oscillates the control through its positions on each press, via a reusable
PedalSwitch.nextValue(after:) that advances to the next option and wraps — so
this generalises to any multi-position switch, not just the two-state Sync.

Tests: cycleSyncModeOscillatesThroughOptions, pedalActionCyclesLooperSync.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 17:24:50 -07:00

81 lines
3.1 KiB
Swift

// PedalSwitch.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdUI
/// A UI-agnostic description of one multi-position pedal switch.
///
/// Where ``PedalKnob`` describes a continuous rotary control, `PedalSwitch` describes a
/// discrete segmented switch with a fixed set of labelled positions — a two-state toggle
/// (e.g. sweep Up / Down) or a three-state selector (e.g. filter LP / BP / HP). A `Pedal`
/// publishes an array of these via ``Pedal/effectSwitches`` so a generic pedalboard UI can
/// render a segmented control per discrete parameter without knowing the concrete type.
///
/// Each option pairs a short caption with the exact `Double` written to the live
/// ``UIParamNode`` when that position is selected; the currently-lit segment is the option
/// whose `value` is nearest the node's current value.
public struct PedalSwitch {
/// One labelled position of a ``PedalSwitch``.
public struct Option {
/// Short caption shown inside the segment (e.g. "Up", "BP").
public let title: String
/// Value written to the switch's node when this position is selected.
public let value: Double
/// Creates a switch position.
///
/// - Parameters:
/// - title: Short caption shown inside the segment.
/// - value: Value written to the node when this position is selected.
public init(title: String, value: Double) {
self.title = title
self.value = value
}
}
/// Short label shown beneath the segmented control (e.g. "Mode", "Sweep").
public let label: String
/// The live parameter node the switch reads and writes. Pass `pedal.$paramName`.
public let node: UIParamNode
/// Ordered positions, left to right. Two entries make a two-state switch, three a
/// three-state switch. Must contain at least two options.
public let options: [Option]
/// Creates a switch descriptor.
///
/// - Parameters:
/// - label: Short label shown beneath the segmented control.
/// - node: The `UIParamNode` projected value of an `@UIParam` property
/// (`pedal.$mode`, `pedal.$direction`, …).
/// - options: Ordered switch positions, left to right. At least two.
public init(
label: String,
node: UIParamNode,
options: [Option]
) {
precondition(options.count >= 2, "PedalSwitch requires at least two options")
self.label = label
self.node = node
self.options = options
}
/// The value of the next option after the one nearest `current`, wrapping to the first — so a
/// momentary trigger (e.g. a MIDI switch) can oscillate through the positions on each press.
public func nextValue(after current: Double) -> Double {
guard !options.isEmpty else { return current }
let currentIndex: Int = options.indices.min(by: {
abs(options[$0].value - current) < abs(options[$1].value - current)
}) ?? 0
let nextIndex: Int = (currentIndex + 1) % options.count
return options[nextIndex].value
}
}