feat(ui): amp-head layout + Neural Amp model loader (S4)

- AmpPedal: double-width (widthUnits 2) amp-head card so the four tone knobs +
  Amp/Cab selectors + master aren't cramped. Uses the generic single-column
  content (no greedy Spacers → no flow-canvas height issue).
- Neural Amp model loader: PedalView renders, for a NeuralAmpPedal, a "Load
  Model…" picker (.fileImporter for .json) plus the loaded model's filename.
  Picking sets Pedal.resourcePath and asks the host to rebuild the board so the
  model applies live. Picker + AppKit stay in the UI layer (core imports none).
- New `pedalRebuildAction` environment value (@MainActor @Sendable), installed by
  ShoeGazeBuilderView; it resolves the pedal's board via a new branch-aware
  `ShoeGaze.board(containing:)` (finds pedals inside parallel / stereo-split
  branches, not just the main chain) and rebuilds it via the live-safe
  `rebuildBoard(_:from:)`.

Tests: AmpPedal widthUnits == 2; board(containing:) finds main + branch pedals
and returns nil for a stranger. Full suite 502. (SwiftUI loader flow needs
on-device visual QA.)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-08-01 22:17:53 -07:00
parent 40fa590196
commit 23d607b9fc
7 changed files with 127 additions and 0 deletions
@@ -49,6 +49,10 @@ public final class AmpPedal: Pedal {
/// The amp is fully wet (it replaces the dry signal); hide the Mix control.
public override var showsMixControl: Bool { false }
/// Amp-head-style double-width card so the four tone knobs plus the Amp / Cab selectors and
/// master lay out with room, rather than cramping into a single-width stompbox.
public override var widthUnits: Int { 2 }
// MARK: - Parameters
/// Preamp drive into the first tube stage. Range [1, 30].
+10
View File
@@ -248,6 +248,16 @@ public final class ShoeGaze {
return result
}
/// The top-level board that contains `pedal`, searching the main chain **and** parallel /
/// stereo-split branch sections. Returns `nil` if the pedal isn't in a main board (e.g. it
/// lives in a chain-router branch, which `rebuildBoard(_:from:)` doesn't operate on).
///
/// Used by the UI to rebuild the right board after a non-`@UIParam` pedal change (e.g. the
/// Neural Amp loading a model file).
public func board(containing pedal: Pedal) -> PedalBoard? {
boards.first { board in pedals(in: board).contains { $0 === pedal } }
}
/// Every live `Pedal` in one board, including pedals inside its parallel and stereo-split
/// branch sections (branch pedals live in their section objects, not as top-level segments).
private func pedals(in board: PedalBoard) -> [Pedal] {
@@ -0,0 +1,24 @@
// PedalRebuildEnvironment.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdShoeGaze
/// Environment action a leaf pedal control can call to ask the host to rebuild the board that
/// contains the given pedal — needed when a pedal setting that is *not* a live `@UIParam` (e.g.
/// the Neural Amp's model file, stored in ``Pedal/resourcePath``) changes and only takes effect
/// after the pedal's graph is rebuilt. Set once near the builder root; defaults to a no-op.
private struct PedalRebuildActionKey: EnvironmentKey {
static let defaultValue: @MainActor @Sendable (Pedal) -> Void = { _ in }
}
extension EnvironmentValues {
/// Rebuilds the board containing the passed pedal. No-op unless the host installs it.
var pedalRebuildAction: @MainActor @Sendable (Pedal) -> Void {
get { self[PedalRebuildActionKey.self] }
set { self[PedalRebuildActionKey.self] = newValue }
}
}
@@ -5,6 +5,7 @@
// SPDX-License-Identifier: MIT
import SwiftUI
import UniformTypeIdentifiers
import LyrebirdUI
import LyrebirdShoeGaze
@@ -417,6 +418,10 @@ public struct PedalView: View {
/// 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
/// Neural Amp model row: label/button spacing, caption size, and caption opacity.
private static let neuralRowSpacing: CGFloat = 6
private static let neuralRowFontSize: CGFloat = 11
private static let neuralRowLabelOpacity: Double = 0.85
private static let namePlateCornerRadius: CGFloat = 8
private static let namePlatePaddingH: CGFloat = 10
private static let namePlatePaddingV: CGFloat = 4
@@ -452,6 +457,13 @@ public struct PedalView: View {
/// Presentation flag for the pedal info panel (description + control ranges).
@State private var showInfo: Bool = false
/// Presentation flag for the Neural Amp model-file importer.
@State private var showModelImporter: Bool = false
/// Rebuilds this pedal's board after a non-`@UIParam` change (Neural Amp model file). Injected
/// by the builder; no-op elsewhere.
@Environment(\.pedalRebuildAction) private var pedalRebuildAction: @MainActor @Sendable (Pedal) -> Void
/// The rig's control map, injected by the builder so the pedal can draw a mapping strip. `nil`
/// when the rig has no bindings (or in contexts that don't set it) — the strip renders nothing.
@Environment(\.controlMappingMap) private var controlMappingMap: MIDIMap?
@@ -541,6 +553,7 @@ public struct PedalView: View {
VStack(spacing: PedalView.sectionSpacing) {
namePlate
knobGrid
neuralModelRow
selectorRow
switchRow
// Keyed on the pedal instance so the buttons' live captions (which poll the pedal
@@ -848,6 +861,42 @@ public struct PedalView: View {
/// 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.
/// Neural Amp only: shows the loaded model file name and a picker to load a `.json` capture.
/// Setting the model changes the graph, so after a pick it asks the host to rebuild the board.
@ViewBuilder
private var neuralModelRow: some View {
if let neural: NeuralAmpPedal = pedal as? NeuralAmpPedal {
VStack(spacing: PedalView.neuralRowSpacing) {
Text(neuralModelLabel(neural))
.font(.system(size: PedalView.neuralRowFontSize, weight: .medium))
.foregroundStyle(Color.white.opacity(PedalView.neuralRowLabelOpacity))
.lineLimit(1)
.truncationMode(.middle)
Button("Load Model…") { showModelImporter = true }
.buttonStyle(.bordered)
.controlSize(.small)
}
.frame(maxWidth: .infinity)
.fileImporter(
isPresented: $showModelImporter,
allowedContentTypes: [.json]
) { (result: Result<URL, Error>) in
guard case .success(let url) = result else { return }
// The app is unsandboxed, so a plain path read works; still bracket access defensively.
let accessed: Bool = url.startAccessingSecurityScopedResource()
neural.resourcePath = url.path
if accessed { url.stopAccessingSecurityScopedResource() }
pedalRebuildAction(neural)
}
}
}
/// Loaded-model caption for the Neural Amp row — the model file's base name, or a prompt.
private func neuralModelLabel(_ neural: NeuralAmpPedal) -> String {
guard let path: String = neural.resourcePath, !path.isEmpty else { return "No model loaded" }
return (path as NSString).lastPathComponent
}
private var bottomControls: some View {
HStack(alignment: .center, spacing: PedalView.bottomControlsSpacing) {
levelControl
@@ -670,6 +670,18 @@ public struct ShoeGazeBuilderView: View {
// Feed the rig's control map into the board tree so each pedal can draw its mapping strip.
// Reading `shoeGaze.midiMap` here (observable) refreshes every strip when a binding changes.
.environment(\.controlMappingMap, shoeGaze.midiMap)
// Let a pedal (e.g. Neural Amp on a model-file change) request a rebuild of its board.
// Captures only `shoeGaze` (a @MainActor session); `rebuildBoard` is safe on a running
// engine (at most one block of glitch).
.environment(\.pedalRebuildAction, { @MainActor [shoeGaze] (pedal: Pedal) in
// Branch-aware lookup — the pedal may live inside a parallel / stereo-split section.
guard let board: PedalBoard = shoeGaze.board(containing: pedal) else { return }
do {
_ = try shoeGaze.rebuildBoard(board, from: board.captureState())
} catch {
print("[ShoeGaze] board rebuild after model change failed: \(error)")
}
})
}
// MARK: - Compact layout (iPhone / narrow iPad)
@@ -89,6 +89,7 @@ struct AmpPedalTests {
let pedal: AmpPedal = AmpPedal()
#expect(pedal.effectKnobs.map { $0.label } == ["Gain", "Bass", "Mid", "Treble"])
#expect(pedal.effectSelectors.map { $0.label } == ["Amp", "Cab"])
#expect(pedal.widthUnits == 2, "amp head should use a double-width card")
}
@Test func selectionsRoundTripThroughState() throws {
@@ -461,6 +461,33 @@ struct BoardEditAPITests {
#expect(hasNonSilentOutput(samples), "stereo-split board should render audio")
}
@Test func boardContainingFindsMainAndBranchPedals() throws {
let (sg, _, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Overdrive"])
defer { try? FileManager.default.removeItem(at: url) }
// Main-chain pedal is found.
guard let mainPedal: Pedal = board.pedals.first else {
Issue.record("no main pedal"); return
}
#expect(sg.board(containing: mainPedal) === board)
// A pedal inside a stereo-split branch is also found (the fix: not just the main chain).
var live: PedalBoard = try sg.appendStereoSplit(to: board)
live = try sg.appendPedal(.delay, to: live, lane: .left(region: 1))
var branchPedal: Pedal?
for segment: BoardSegment in live.orderedSegments {
if case .stereoSplit(let section) = segment { branchPedal = section.left.pedals.first }
}
guard let branch: Pedal = branchPedal else {
Issue.record("no branch pedal"); return
}
#expect(sg.board(containing: branch) === live, "branch pedal should resolve to its board")
// A pedal not in any board resolves to nil.
#expect(sg.board(containing: OverdrivePedal()) == nil)
}
@Test func removePedalFromBranchLane() throws {
let (sg, _, url, board): (ShoeGaze, SynthesisEngine, URL, PedalBoard) =
try makeRig(pedals: ["Overdrive"])