Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/PedalSelectorKnobView.swift
T
josh 59370ee5dd perf(cab): instantiate only the selected cabinet convolver
The cab selector was a Select over one ConvolutionReverb per CabinetVoice, and
Select evaluates every input each block — so all 8 voices' partitioned-FFT
convolvers ran continuously (7/8 discarded), per CabinetPedal and per amp voice.

Build only the selected voice's convolver. The cab selector is now
rebuildsOnChange: committing a new stop asks the host to rebuild the board
(same mechanism the Neural Amp model loader uses), which re-enters effect()
with the new voice. One convolver runs instead of N; cost moves to a brief
rebuild on cab switch (cabs are set-and-forget).

- PedalSelectorKnob: add rebuildsOnChange flag.
- PedalSelectorKnobView: fire onCommit on drag-end when the stop changed.
- PedalView: wire onCommit → pedalRebuildAction for rebuildsOnChange selectors.
- CabinetVoice.voice(atSelectorValue:): rounded/clamped index → voice.
- CabinetPedal + AmpVoicePedal: single ConvolutionReverb for the selected voice.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-02 20:16:42 -07:00

135 lines
4.7 KiB
Swift

// PedalSelectorKnobView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdShoeGaze
/// Renders a ``PedalSelectorKnob`` — a rotary knob that snaps to a fixed set of labelled stops.
///
/// Visually matches `ParameterKnob` (same dial + indicator), but drag quantises to discrete
/// positions and the caption shows the current stop's title rather than a numeric value. Drag up to
/// advance clockwise through the stops, down to go back.
struct PedalSelectorKnobView: View {
// MARK: - Layout constants
private static let dialSize: CGFloat = 50
private static let indicatorWidth: CGFloat = 2
private static let indicatorHeight: CGFloat = 14
private static let indicatorInset: CGFloat = 3
private static let startAngle: Double = -135.0
private static let totalArc: Double = 270.0
/// Pixels of vertical drag per stop step.
private static let pixelsPerStep: CGFloat = 22
private static let labelSpacing: CGFloat = 4
// MARK: - Input
private let selector: PedalSelectorKnob
/// Called once when a drag ends on a different stop than it began — used to trigger a board
/// rebuild for `rebuildsOnChange` selectors. `nil` for plain live-value selectors.
private let onCommit: (() -> Void)?
// MARK: - Drag state
@State private var dragStartIndex: Int = 0
@State private var dragInProgress: Bool = false
// MARK: - Init
init(_ selector: PedalSelectorKnob, onCommit: (() -> Void)? = nil) {
self.selector = selector
self.onCommit = onCommit
}
// MARK: - Body
var body: some View {
VStack(spacing: PedalSelectorKnobView.labelSpacing) {
dial
.frame(width: PedalSelectorKnobView.dialSize, height: PedalSelectorKnobView.dialSize)
.gesture(dragGesture)
Text(selector.label)
.font(.caption)
.foregroundStyle(Color.white)
Text(currentStop.title)
.font(.caption)
.foregroundStyle(Color.white)
}
}
// MARK: - Dial
private var dial: some View {
let count: Int = selector.stops.count
let normalised: Double = count > 1 ? Double(currentIndex) / Double(count - 1) : 0
let rotation: Double = PedalSelectorKnobView.startAngle + normalised * PedalSelectorKnobView.totalArc
let offset: CGFloat = -(PedalSelectorKnobView.dialSize / 2
- PedalSelectorKnobView.indicatorHeight / 2
- PedalSelectorKnobView.indicatorInset)
return ZStack {
Circle()
.fill(Color.lyrebirdControlBackground)
.overlay(Circle().stroke(Color.lyrebirdSeparator, lineWidth: 1))
Rectangle()
.fill(Color.accentColor)
.frame(
width: PedalSelectorKnobView.indicatorWidth,
height: PedalSelectorKnobView.indicatorHeight
)
.offset(y: offset)
.rotationEffect(.degrees(rotation))
}
}
// MARK: - Drag
private var dragGesture: some Gesture {
DragGesture(minimumDistance: 1)
.onChanged { gesture in
if !dragInProgress {
dragInProgress = true
dragStartIndex = currentIndex
}
let delta: CGFloat = gesture.translation.height // down = positive
// Up should advance (increase index), so subtract the downward delta.
let steps: Int = Int((-delta / PedalSelectorKnobView.pixelsPerStep).rounded())
let target: Int = min(max(dragStartIndex + steps, 0), selector.stops.count - 1)
let value: Double = selector.stops[target].value
if value != selector.node.value {
selector.node.update(value)
}
}
.onEnded { _ in
let changed: Bool = currentIndex != dragStartIndex
dragInProgress = false
if changed { onCommit?() }
}
}
// MARK: - Helpers
/// Index of the stop whose value is nearest the node's current value.
private var currentIndex: Int {
let value: Double = selector.node.value
var bestIndex: Int = 0
var bestDistance: Double = .greatestFiniteMagnitude
for (index, stop) in selector.stops.enumerated() {
let distance: Double = abs(stop.value - value)
if distance < bestDistance {
bestDistance = distance
bestIndex = index
}
}
return bestIndex
}
private var currentStop: PedalSelectorKnob.Stop {
selector.stops[currentIndex]
}
}