Files
ShoeGaze/Sources/LyrebirdShoeGaze/PedalSwitch.swift
T
josh 5090a9255f Looper reverse fix + header layout compaction + tempo toggle
Looper:
- Reverse now continues from the current play-head instead of jumping to
  duration-minus-position; anchor-based position tracking survives direction
  changes. Sync mode defers the flip to the next beat; Free applies immediately.
- Sync switch relabeled "Sync to Tempo".

Tempo/Tuner:
- Add a Tempo on/off toggle (ShoeGaze.setTempoEnabled) that hides the indicator
  and forces every tempo-dependent pedal switch (PedalSwitch.tempoDependent) off;
  PedalSwitchView disables such switches via the shoeGazeTempoEnabled environment.
- Tuner readout laid out inline; Tempo/Tuner pinned to fixed heights so toggling
  state no longer shifts surrounding layout.

Header:
- Collapsed to two rows: title + Tempo/Tuner (left, stacked) and engine options
  over input controls (right, trailing-aligned). TAP is a standard-height button.
- Removed the redundant Boards button; moved Clear into the board bar.
- Fixed the rig mute glyph size so toggling mute doesn't shift the volume row.

Help: added a Looper section documenting transport, Free/Sync, knobs, tracks,
and MIDI-assignable actions.

Tests: TempoEnableTests + looper reverse/deferral coverage.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 12:31:11 -07:00

89 lines
3.6 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]
/// `true` when this switch controls a tempo-synced behaviour (e.g. the looper's
/// Sync-to-Tempo mode). A generic UI can disable such switches — and the host can force them
/// to their first (off) option — while the tempo indicator is turned off. Defaults to `false`.
public let tempoDependent: Bool
/// 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.
/// - tempoDependent: `true` if the switch depends on the tempo/clock. Defaults to `false`.
public init(
label: String,
node: UIParamNode,
options: [Option],
tempoDependent: Bool = false
) {
precondition(options.count >= 2, "PedalSwitch requires at least two options")
self.label = label
self.node = node
self.options = options
self.tempoDependent = tempoDependent
}
/// 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
}
}