Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/PedalView.swift
T
josh 1893576718 Add MIDI foot-controller control; board/rig bypass; iOS fixes
Headline: MIDI control for a Behringer FCB1010 (four phases).
- MIDIController (Lyrebird MIDI client): connection indicator via source
  polling, background-queue->main-actor hop, MIDI Learn.
- Volume and Wah pedals (Wah reuses the SVF bandpass sweep, driven by
  pedal position instead of an envelope follower).
- CC -> any pedal knob (0-127 mapped across the knob range).
- Program Change -> pedal/board/rig toggles: software latch for booleans,
  exclusive radio for board solo. Positional addressing with display-name
  hints; stale/out-of-range bindings are skipped, not fired.
- Per-rig MIDIMap embedded in ShoeGazeState as an optional field (old rigs
  decode to nil; no preset version bump). Central MIDI Mapping sheet with
  Learn/Clear per control.

Also included (same working branch):
- Full board bypass and rig bypass (dry passthrough via cascaded
  crossfades in BoardOutputMaster), with UI toggles.
- Horizontal pedal picker grouped by category in filter order.
- Drag a palette pedal onto a chain card to insert before it
  (ShoeGaze.insertPedal); tap still appends.
- iOS: guard macOS-only .menuStyle(.borderlessButton) to silence the
  _UIReparentingView warning; iCloud-unavailable alert; keep the device
  awake while audio runs.

Tests: 24 added (Volume/Wah, MIDIMap Codable incl. legacy decode,
CC and PC controller behavior, insert/bypass API). Full suite green;
macOS and iOS app targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 08:18:09 -07:00

512 lines
22 KiB
Swift

// PedalView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdUI
import LyrebirdShoeGaze
// MARK: - Accent-color mapping (UI-only, never in the core target)
/// Returns a stompbox enclosure tint colour keyed on the pedal's display name.
///
/// The core `Pedal` type cannot carry a SwiftUI `Color`, so the mapping lives
/// here in the UI target. Unrecognised names fall through to `.gray`.
private func enclosureColor(for displayName: String) -> Color {
switch displayName {
case "Overdrive": return Color(red: 0.85, green: 0.42, blue: 0.08) // warm orange
case "Distortion": return Color(red: 0.80, green: 0.26, blue: 0.12) // hot rust
case "Delay": return Color(red: 0.10, green: 0.55, blue: 0.55) // teal
case "Reverb": return Color(red: 0.30, green: 0.18, blue: 0.65) // indigo
case "Shimmer Reverb": return Color(red: 0.46, green: 0.52, blue: 0.78) // icy periwinkle
case "Modulation": return Color(red: 0.12, green: 0.58, blue: 0.22) // green
case "Fuzz": return Color(red: 0.55, green: 0.10, blue: 0.10) // deep crimson
case "Tremolo": return Color(red: 0.20, green: 0.38, blue: 0.72) // cobalt blue
case "Phaser": return Color(red: 0.60, green: 0.22, blue: 0.62) // violet
case "Compressor": return Color(red: 0.18, green: 0.42, blue: 0.32) // forest green
case "Mutant": return Color(red: 0.70, green: 0.12, blue: 0.48) // magenta / plum
case "Auto-Wah": return Color(red: 0.80, green: 0.62, blue: 0.10) // gold
case "Wah": return Color(red: 0.72, green: 0.50, blue: 0.06) // amber gold
case "Volume": return Color(red: 0.30, green: 0.30, blue: 0.34) // graphite
case "Feedback Delay": return Color(red: 0.48, green: 0.30, blue: 0.14) // tape sepia
case "Multi-Tap Delay": return Color(red: 0.08, green: 0.48, blue: 0.68) // cyan
case "Reverse Delay": return Color(red: 0.10, green: 0.44, blue: 0.60) // deep cyan-teal
case "Limiter": return Color(red: 0.62, green: 0.16, blue: 0.20) // brick red
case "Noise Gate": return Color(red: 0.24, green: 0.26, blue: 0.30) // gunmetal
case "Graphic EQ": return Color(red: 0.14, green: 0.40, blue: 0.50) // deep teal
case "Chorus": return Color(red: 0.16, green: 0.50, blue: 0.64) // aqua
case "Flanger": return Color(red: 0.28, green: 0.52, blue: 0.78) // steel blue
case "Vibrato": return Color(red: 0.42, green: 0.30, blue: 0.70) // violet-blue
case "Rotary": return Color(red: 0.45, green: 0.34, blue: 0.20) // Leslie cabinet walnut
case "Sustainer": return Color(red: 0.22, green: 0.46, blue: 0.30) // pine green
case "Ring Modulator": return Color(red: 0.64, green: 0.30, blue: 0.10) // burnt amber
case "Diffuser": return Color(red: 0.34, green: 0.30, blue: 0.62) // periwinkle
case "Harmonizer": return Color(red: 0.52, green: 0.20, blue: 0.40) // wine / mauve
case "Bitcrusher": return Color(red: 0.30, green: 0.34, blue: 0.40) // slate / digital gray
case "Granulator": return Color(red: 0.20, green: 0.55, blue: 0.50) // sea green
case "Auto Granulator": return Color(red: 0.24, green: 0.50, blue: 0.56) // teal-aqua
default: return Color(red: 0.36, green: 0.36, blue: 0.38) // steel gray
}
}
// MARK: - Screw decoration
/// A small circular "screw head" decoration placed in each corner of the enclosure.
private struct ScrewDot: View {
private static let size: CGFloat = 8
private static let strokeWidth: CGFloat = 1
private static let slotWidth: CGFloat = 1
private static let slotHeight: CGFloat = 4
private static let fillOpacity: Double = 0.25
private static let rimOpacity: Double = 0.18
private static let slotOpacity: Double = 0.50
var body: some View {
ZStack {
Circle()
.fill(Color.black.opacity(ScrewDot.fillOpacity))
.frame(width: ScrewDot.size, height: ScrewDot.size)
Circle()
.stroke(Color.white.opacity(ScrewDot.rimOpacity), lineWidth: ScrewDot.strokeWidth)
.frame(width: ScrewDot.size, height: ScrewDot.size)
// Slot line — Phillips-style cross
Rectangle()
.fill(Color.black.opacity(ScrewDot.slotOpacity))
.frame(width: ScrewDot.slotWidth, height: ScrewDot.slotHeight)
Rectangle()
.fill(Color.black.opacity(ScrewDot.slotOpacity))
.frame(width: ScrewDot.slotHeight, height: ScrewDot.slotWidth)
}
}
}
// MARK: - LED
/// A small circular LED that glows green when the effect is engaged.
///
/// Bypass polarity: `0` = engaged (effect ON), `1` = bypassed.
/// The LED is lit when `bypassValue < 0.5`.
private struct LEDIndicator: View {
private static let diameter: CGFloat = 10
private static let glowRadius: CGFloat = 6
private static let glowOpacity: Double = 0.75
private static let rimLineWidth: CGFloat = 0.5
private static let dimBrightness: Double = 0.15
private static let rimStrokeOpacity: Double = 0.30
let bypassValue: Double
private var isEngaged: Bool { bypassValue < 0.5 }
var body: some View {
ZStack {
if isEngaged {
Circle()
.fill(Color.green.opacity(LEDIndicator.glowOpacity))
.frame(
width: LEDIndicator.diameter + LEDIndicator.glowRadius,
height: LEDIndicator.diameter + LEDIndicator.glowRadius
)
.blur(radius: LEDIndicator.glowRadius / 2)
}
Circle()
.fill(isEngaged ? Color.green : Color(white: LEDIndicator.dimBrightness))
.frame(width: LEDIndicator.diameter, height: LEDIndicator.diameter)
.overlay(
Circle()
.stroke(Color.white.opacity(LEDIndicator.rimStrokeOpacity), lineWidth: LEDIndicator.rimLineWidth)
.frame(width: LEDIndicator.diameter, height: LEDIndicator.diameter)
)
}
.frame(width: LEDIndicator.diameter + LEDIndicator.glowRadius,
height: LEDIndicator.diameter + LEDIndicator.glowRadius)
}
}
// MARK: - Footswitch
/// A round footswitch-style button that toggles bypass on/off.
private struct FootswitchButton: View {
private static let diameter: CGFloat = 44
private static let strokeWidth: CGFloat = 2.5
private static let innerDiameter: CGFloat = 36
private static let outerFillBrightness: Double = 0.18
private static let ringBrightness: Double = 0.50
private static let innerFillBrightness: Double = 0.25
let node: UIParamNode
var body: some View {
Button(action: {
let engaged: Bool = node.value < 0.5
// Toggle: engaged (0) → bypassed (1) and vice-versa.
node.update(engaged ? 1.0 : 0.0)
}) {
ZStack {
Circle()
.fill(Color(white: FootswitchButton.outerFillBrightness))
.frame(width: FootswitchButton.diameter, height: FootswitchButton.diameter)
Circle()
.stroke(Color(white: FootswitchButton.ringBrightness),
lineWidth: FootswitchButton.strokeWidth)
.frame(width: FootswitchButton.diameter, height: FootswitchButton.diameter)
Circle()
.fill(Color(white: FootswitchButton.innerFillBrightness))
.frame(width: FootswitchButton.innerDiameter, height: FootswitchButton.innerDiameter)
}
}
.buttonStyle(.plain)
.frame(width: FootswitchButton.diameter, height: FootswitchButton.diameter)
}
}
// MARK: - PedalView
/// A stompbox-style enclosure view that renders all controls for a single `Pedal`.
///
/// The enclosure shows:
/// - Corner screw decorations.
/// - A contrasting name-plate capsule near the top with `displayName`.
/// - A grid of ``ParameterKnob`` controls (up to ``knobColumnCount`` per row) drawn from
/// the effect + `Mix` knobs of ``Pedal/knobs``.
/// - A centred bottom row pairing the `Level` knob with the bypass switch (LED + footswitch).
///
/// ## Bypass LED polarity
///
/// `Pedal.bypass` is `0` = engaged (effect active), `1` = bypassed.
/// The LED is **green/lit** when `bypass < 0.5` (effect is on) and dark when bypassed.
///
/// ## Accent color
///
/// The enclosure tint is assigned by `enclosureColor(for:)` — a private helper in
/// this file keyed on `displayName`. Core `Pedal` never imports SwiftUI.
///
/// ## Usage
///
/// ```swift
/// PedalView(pedal: overdrivePedal)
/// ```
public struct PedalView: View {
// MARK: - Layout constants
/// Wide enough for three ~55 pt knobs (dial + label + value) plus padding in a
/// single row, so the effect controls never overlap.
private static let enclosureWidth: CGFloat = 220
private static let enclosureCornerRadius: CGFloat = 12
private static let enclosurePadding: CGFloat = 14
private static let screwInset: CGFloat = 6
private static let knobColumnCount: Int = 3
private static let knobSpacing: CGFloat = 10
/// Horizontal gap between knobs within a row. Wider than `knobSpacing` so centred
/// rows keep a comfortable, touch-friendly gap between adjacent knobs.
private static let knobRowItemSpacing: CGFloat = 24
/// Horizontal gap between adjacent vertical faders in the slider bank.
private static let sliderBankSpacing: CGFloat = 4
/// Approximate per-fader footprint (matches `VerticalParameterSlider.controlWidth`),
/// used to widen the enclosure so a wide bank (e.g. a 6-band EQ) is never clipped.
private static let sliderBankItemWidth: CGFloat = 28
/// Horizontal gap between the Level knob and the bypass control in the bottom row.
private static let bottomControlsSpacing: CGFloat = 18
private static let sectionSpacing: CGFloat = 14
private static let namePlateCornerRadius: CGFloat = 8
private static let namePlatePaddingH: CGFloat = 10
private static let namePlatePaddingV: CGFloat = 4
private static let outerBorderOpacity: Double = 0.60
private static let innerHighlightOpacity: Double = 0.22
private static let shadowRadius: CGFloat = 8
private static let shadowYOffset: CGFloat = 4
private static let shadowOpacity: Double = 0.60
private static let enclosureGradientDarkFraction: Double = 0.30
private static let bypassSectionSpacing: CGFloat = 6
/// Half the screw-dot size — used to centre screws on the corner position.
private static let screwHalfSize: CGFloat = 4
private static let namePlateBackgroundOpacity: Double = 0.35
private static let fontSizeNamePlate: CGFloat = 13
/// Opacity of the ⓘ info button.
private static let infoButtonOpacity: Double = 0.7
/// Insets placing the ⓘ info button just under the top-left corner screw.
private static let infoButtonLeading: CGFloat = 5
private static let infoButtonTop: CGFloat = 20
private static let outerBorderLineWidth: CGFloat = 1.5
private static let innerHighlightLineWidth: CGFloat = 1
private static let innerHighlightPadding: CGFloat = 1
private static let shadowXOffset: CGFloat = 0
// MARK: - Properties
private let pedal: Pedal
/// Presentation flag for the pedal info panel (description + control ranges).
@State private var showInfo: Bool = false
// MARK: - Init
/// Creates a stompbox pedal view for the given pedal.
///
/// - Parameter pedal: The `Pedal` whose parameters will be displayed.
public init(pedal: Pedal) {
self.pedal = pedal
}
// MARK: - Body
public var body: some View {
let accent: Color = enclosureColor(for: pedal.displayName)
let darkAccent: Color = accent.opacity(PedalView.enclosureGradientDarkFraction)
// Evaluate the (computed) slider descriptors once per render — both the bank and
// the enclosure-width calculation consume the same array.
let sliders: [PedalSlider] = pedal.effectSliders
ZStack(alignment: .topLeading) {
// Enclosure fill with top-to-bottom gradient
RoundedRectangle(cornerRadius: PedalView.enclosureCornerRadius)
.fill(
LinearGradient(
gradient: Gradient(colors: [accent, darkAccent]),
startPoint: .top,
endPoint: .bottom
)
)
// Outer border
RoundedRectangle(cornerRadius: PedalView.enclosureCornerRadius)
.stroke(Color.black.opacity(PedalView.outerBorderOpacity),
lineWidth: PedalView.outerBorderLineWidth)
// Inner highlight rim (subtle bevel)
RoundedRectangle(cornerRadius: PedalView.enclosureCornerRadius - PedalView.innerHighlightPadding)
.stroke(Color.white.opacity(PedalView.innerHighlightOpacity),
lineWidth: PedalView.innerHighlightLineWidth)
.padding(PedalView.innerHighlightPadding)
// Content stack
VStack(spacing: PedalView.sectionSpacing) {
namePlate
knobGrid
selectorRow
switchRow
sliderBank(sliders)
Spacer(minLength: 0)
bottomControls
}
.padding(PedalView.enclosurePadding)
// Corner screws — overlaid at the four corners
screwCorners
// Info button, tucked just under the top-left corner screw.
infoButton
.padding(.leading, PedalView.infoButtonLeading)
.padding(.top, PedalView.infoButtonTop)
}
.frame(width: enclosureWidth(for: sliders))
.shadow(
color: Color.black.opacity(PedalView.shadowOpacity),
radius: PedalView.shadowRadius,
x: PedalView.shadowXOffset,
y: PedalView.shadowYOffset
)
.sheet(isPresented: $showInfo) {
PedalInfoView(pedal: pedal)
.presentationDetents([.medium, .large])
}
}
// MARK: - Subviews
/// Capsule name plate near the top of the enclosure.
private var namePlate: some View {
Text(pedal.displayName)
.font(.system(size: PedalView.fontSizeNamePlate, weight: .bold, design: .rounded))
.foregroundStyle(Color.white)
.padding(.horizontal, PedalView.namePlatePaddingH)
.padding(.vertical, PedalView.namePlatePaddingV)
.background(Color.black.opacity(PedalView.namePlateBackgroundOpacity))
.clipShape(RoundedRectangle(cornerRadius: PedalView.namePlateCornerRadius))
}
/// Info button tucked just under the top-left corner screw.
private var infoButton: some View {
Button {
showInfo = true
} label: {
Image(systemName: "info.circle")
.font(.system(size: PedalView.fontSizeNamePlate))
.foregroundStyle(Color.white.opacity(PedalView.infoButtonOpacity))
}
.buttonStyle(.plain)
.help("About this pedal")
}
/// Grid of the effect knobs, in rows of up to `knobColumnCount`.
///
/// The trailing `Mix` and `Level` knobs (the last two entries of `pedal.knobs`) are
/// dropped here and rendered in the bottom control row flanking the bypass switch —
/// `Level` on the left, `Mix` on the right — leaving more room for the effect controls
/// at the top and giving every pedal a symmetric footer.
private var knobGrid: some View {
let knobs: [PedalKnob] = Array(pedal.knobs.dropLast(2))
let rows: [[PedalKnob]] = stride(
from: 0,
to: knobs.count,
by: PedalView.knobColumnCount
).map { startIndex in
Array(knobs[startIndex ..< min(startIndex + PedalView.knobColumnCount, knobs.count)])
}
return VStack(spacing: PedalView.knobSpacing) {
ForEach(Array(rows.enumerated()), id: \.offset) { rowPair in
let row: [PedalKnob] = rowPair.element
// Centre each row: a short row (one or two knobs) sits centred in the
// enclosure rather than left-aligned. Symmetric spacers also centre a full
// three-knob row, so every row stays horizontally centred.
// A full three-knob row keeps the tight spacing so it fits the enclosure;
// shorter centred rows use a wider, touch-friendly gap.
let rowItemSpacing: CGFloat = row.count >= PedalView.knobColumnCount
? PedalView.knobSpacing
: PedalView.knobRowItemSpacing
HStack(spacing: rowItemSpacing) {
Spacer(minLength: 0)
ForEach(Array(row.enumerated()), id: \.offset) { knobPair in
let knob: PedalKnob = knobPair.element
ParameterKnob(knob.label, node: knob.node, range: knob.range)
.foregroundStyle(Color.white)
}
Spacer(minLength: 0)
}
}
}
}
/// Row of stepped selector knobs drawn from `pedal.effectSelectors`, beneath the knob grid.
/// Renders nothing when the pedal publishes no selectors.
@ViewBuilder
private var selectorRow: some View {
let selectors: [PedalSelectorKnob] = pedal.effectSelectors
if !selectors.isEmpty {
HStack(alignment: .top, spacing: PedalView.knobRowItemSpacing) {
Spacer(minLength: 0)
ForEach(Array(selectors.enumerated()), id: \.offset) { pair in
PedalSelectorKnobView(pair.element)
}
Spacer(minLength: 0)
}
}
}
/// Row of segmented switches drawn from `pedal.effectSwitches`, one per row, beneath
/// the knob grid. Renders nothing when the pedal publishes no switches.
@ViewBuilder
private var switchRow: some View {
let switches: [PedalSwitch] = pedal.effectSwitches
if !switches.isEmpty {
VStack(spacing: PedalView.knobSpacing) {
ForEach(Array(switches.enumerated()), id: \.offset) { pair in
PedalSwitchView(pair.element)
}
}
}
}
/// Row of vertical faders drawn from `pedal.effectSliders`, beneath the switch row.
/// Renders nothing when the pedal publishes no sliders.
@ViewBuilder
private func sliderBank(_ sliders: [PedalSlider]) -> some View {
if !sliders.isEmpty {
HStack(alignment: .top, spacing: PedalView.sliderBankSpacing) {
ForEach(Array(sliders.enumerated()), id: \.offset) { pair in
let slider: PedalSlider = pair.element
VerticalParameterSlider(slider.label, node: slider.node, range: slider.range)
}
}
}
}
/// Enclosure width — the base stompbox width, widened to fit a wide vertical-fader bank
/// (e.g. a multi-band graphic EQ) so its faders are never clipped.
private func enclosureWidth(for sliders: [PedalSlider]) -> CGFloat {
let sliderCount: Int = sliders.count
guard sliderCount > 0 else { return PedalView.enclosureWidth }
let bankContentWidth: CGFloat =
CGFloat(sliderCount) * PedalView.sliderBankItemWidth
+ CGFloat(max(0, sliderCount - 1)) * PedalView.sliderBankSpacing
let needed: CGFloat = bankContentWidth + 2 * PedalView.enclosurePadding
return max(PedalView.enclosureWidth, needed)
}
/// Bottom control row: the `Level` knob, the bypass switch, and the `Mix` knob,
/// side-by-side and centred — a symmetric knob-switch-knob footer shared by all pedals.
private var bottomControls: some View {
HStack(alignment: .center, spacing: PedalView.bottomControlsSpacing) {
levelControl
bypassControl
mixControl
}
.frame(maxWidth: .infinity)
}
/// The `Level` knob — the last entry of `pedal.knobs` — placed left of the bypass switch.
@ViewBuilder
private var levelControl: some View {
if let level: PedalKnob = pedal.knobs.last {
ParameterKnob(level.label, node: level.node, range: level.range)
.foregroundStyle(Color.white)
}
}
/// The `Mix` knob — the second-to-last entry of `pedal.knobs` — placed right of the
/// bypass switch to mirror `Level`.
@ViewBuilder
private var mixControl: some View {
if let mix: PedalKnob = pedal.knobs.dropLast().last {
ParameterKnob(mix.label, node: mix.node, range: mix.range)
.foregroundStyle(Color.white)
}
}
/// LED + footswitch bypass control, paired beside the Level knob.
private var bypassControl: some View {
// Reading `pedal.$bypass.value` here registers this view for automatic
// re-renders whenever the bypass parameter changes on the main thread
// (UIParamNode is @Observable).
let bypassValue: Double = pedal.$bypass.value
return VStack(spacing: PedalView.bypassSectionSpacing) {
LEDIndicator(bypassValue: bypassValue)
FootswitchButton(node: pedal.$bypass)
}
}
/// Four screw dots overlaid at the enclosure corners.
private var screwCorners: some View {
GeometryReader { geometry in
let w: CGFloat = geometry.size.width
let h: CGFloat = geometry.size.height
let inset: CGFloat = PedalView.screwInset
let half: CGFloat = PedalView.screwHalfSize
ZStack(alignment: .topLeading) {
// Top-left
ScrewDot()
.position(x: inset + half, y: inset + half)
// Top-right
ScrewDot()
.position(x: w - inset - half, y: inset + half)
// Bottom-left
ScrewDot()
.position(x: inset + half, y: h - inset - half)
// Bottom-right
ScrewDot()
.position(x: w - inset - half, y: h - inset - half)
}
}
}
}