diff --git a/Demo/ShoeGazeDemo/AppDelegate.swift b/Demo/ShoeGazeDemo/AppDelegate.swift index ab08347..aa96e26 100644 --- a/Demo/ShoeGazeDemo/AppDelegate.swift +++ b/Demo/ShoeGazeDemo/AppDelegate.swift @@ -29,7 +29,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate { private static let outputChannels: Int = 2 // Built-in mics are mono; default to one input channel so live input works out of the box // (CoreAudio can't satisfy more input channels than the device provides). Raise in the UI. - private static let inputChannels: Int = 1 + private static let inputChannels: Int = 6 private static let numAudioBuses: Int = 256 // MARK: - Window geometry constants diff --git a/Demo/ShoeGazeDemo/DemoRootView.swift b/Demo/ShoeGazeDemo/DemoRootView.swift index 09cd360..765d86f 100644 --- a/Demo/ShoeGazeDemo/DemoRootView.swift +++ b/Demo/ShoeGazeDemo/DemoRootView.swift @@ -79,6 +79,10 @@ struct DemoRootView: View { .overlay(Color.white.opacity(DemoRootView.dividerOpacity)) contentArea .padding(DemoRootView.contentPadding) + Divider() + .overlay(Color.white.opacity(DemoRootView.dividerOpacity)) + ShoeGazeScopeView(shoeGaze: controller.shoeGaze) + .padding(DemoRootView.contentPadding) } .frame( minWidth: DemoRootView.windowMinWidth, diff --git a/Demo/ShoeGazeDemo/ShoeGazeDemoController.swift b/Demo/ShoeGazeDemo/ShoeGazeDemoController.swift index 6d9a86d..01771ed 100644 --- a/Demo/ShoeGazeDemo/ShoeGazeDemoController.swift +++ b/Demo/ShoeGazeDemo/ShoeGazeDemoController.swift @@ -182,6 +182,8 @@ final class ShoeGazeDemoController { inputSource = .testLoop } buildSession() + // Input buses follow the output buses, so the tapped indices change with the layout. + shoeGaze.installScopeTap() if wasRunning { start() } } diff --git a/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Engine.swift b/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Engine.swift index 64408c2..4a8e774 100644 --- a/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Engine.swift +++ b/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Engine.swift @@ -53,5 +53,56 @@ extension ShoeGaze { } try rebuildAllBoards(from: snapshot) + + // The tapped bus indices depend on the channel layout (input buses follow the output + // buses), so rebuild the scope tap for the new configuration. + installScopeTap() + } + + /// Default samples-per-frame captured by the scope tap. + public static let defaultScopeBufferSize: Int = 2_048 + + /// (Re)builds the ``scopeTap`` over the current hardware input + output buses and assigns it + /// to the engine, so the debug scope view can visualise both the live input signal and the + /// board output. + /// + /// Lane order is **inputs first** (`In 0`, `In 1`, …) then **outputs** (`Out L`, `Out R`, …), + /// so the input waveform sits at the top of the scope. Must be called while the engine is + /// stopped — `SynthesisEngine` caches its scope tap at `start()`. Rebuilt automatically by + /// ``reconfigureEngine(sampleRate:inputChannels:outputChannels:)`` and at init. + /// + /// - Parameter bufferSize: Samples per channel per display frame. Defaults to 2048. + public func installScopeTap(bufferSize: Int = ShoeGaze.defaultScopeBufferSize) { + let numOut: Int = engine.numOutputChannels + let numIn: Int = engine.numInputChannels + + var buses: [Int] = [] + for ch: Int in 0.. String { + let numOut: Int = engine.numOutputChannels + guard bus >= numOut else { + let stereoNames: [String] = ["Out L", "Out R"] + return bus < stereoNames.count ? stereoNames[bus] : "Out \(bus)" + } + return "In \(bus - numOut)" } } diff --git a/Sources/LyrebirdShoeGaze/ShoeGaze.swift b/Sources/LyrebirdShoeGaze/ShoeGaze.swift index d7bb035..a70ff1b 100644 --- a/Sources/LyrebirdShoeGaze/ShoeGaze.swift +++ b/Sources/LyrebirdShoeGaze/ShoeGaze.swift @@ -84,6 +84,16 @@ public final class ShoeGaze { /// (same module, different file) can mutate it. internal var recordTaps: [RecordTap] = [] + // MARK: - Scope state + + /// Scope tap over the hardware input + output buses, feeding the debug scope view. + /// + /// Rebuilt by ``installScopeTap(bufferSize:)`` whenever the engine layout changes (so the + /// tapped bus indices follow the current input/output channel counts). `nil` until first + /// installed. The setter is `internal` so ``installScopeTap(bufferSize:)`` (same module, + /// different file) can assign it; external callers observe it through this `public` getter. + public internal(set) var scopeTap: ScopeCapture? + // MARK: - Private private var testLoopSources: [TestLoopSource] = [] @@ -110,6 +120,7 @@ public final class ShoeGaze { ) { self.engine = engine self.inputSource = inputSource + installScopeTap() } // MARK: - Board management diff --git a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift index dc56cf1..98b2d46 100644 --- a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift +++ b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift @@ -105,6 +105,7 @@ public struct ShoeGazeBuilderView: View { boardArea } .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) + ShoeGazeScopeView(shoeGaze: shoeGaze) LyrebirdStatusView(engine: shoeGaze.engine) } .padding(ShoeGazeBuilderView.outerPadding) diff --git a/Sources/LyrebirdShoeGazeUI/ShoeGazeScopeView.swift b/Sources/LyrebirdShoeGazeUI/ShoeGazeScopeView.swift new file mode 100644 index 0000000..3bbc170 --- /dev/null +++ b/Sources/LyrebirdShoeGazeUI/ShoeGazeScopeView.swift @@ -0,0 +1,83 @@ +// ShoeGazeScopeView.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import SwiftUI +import LyrebirdSynth +import LyrebirdUI +import LyrebirdShoeGaze + +/// Oscilloscope panel showing the live input and board-output waveforms. +/// +/// Wraps Lyrebird's `LyrebirdScopeView` bound to ``ShoeGaze/scopeTap`` and adds a labelled +/// legend so each lane is identifiable (`In 0`, `Out L`, `Out R`, …) — input lanes on top, +/// outputs below. Use it to confirm signal flow: a flat input lane with audible test-tone +/// output means the live input never reaches the board head bus; a live input lane with a flat +/// output lane points downstream. +/// +/// The lane colours match `LyrebirdScopeView` (cyan, orange, green, yellow, cycling). The view +/// keys itself on the tap's object identity so a reconfigure that rebuilds the tap recreates the +/// underlying scope. +public struct ShoeGazeScopeView: View { + + // MARK: - Constants + + /// Colours used by `LyrebirdScopeView`, mirrored here for the legend swatches. + private static let laneColors: [Color] = [.cyan, .orange, .green, .yellow] + private static let scopeHeight: CGFloat = 120 + private static let legendSpacing: CGFloat = 14 + private static let swatchSize: CGFloat = 9 + private static let panelSpacing: CGFloat = 6 + private static let labelOpacity: Double = 0.75 + + // MARK: - Properties + + private let shoeGaze: ShoeGaze + + // MARK: - Init + + /// - Parameter shoeGaze: The session whose ``ShoeGaze/scopeTap`` is visualised. + public init(shoeGaze: ShoeGaze) { + self.shoeGaze = shoeGaze + } + + // MARK: - Body + + public var body: some View { + VStack(alignment: .leading, spacing: ShoeGazeScopeView.panelSpacing) { + if let capture: ScopeCapture = shoeGaze.scopeTap { + legend(for: capture) + LyrebirdScopeView(capture: capture) + .frame(height: ShoeGazeScopeView.scopeHeight) + .id(ObjectIdentifier(capture)) + } else { + Text("Scope unavailable") + .font(.caption) + .foregroundStyle(Color.white.opacity(ShoeGazeScopeView.labelOpacity)) + } + } + } + + // MARK: - Legend + + /// A horizontal legend mapping each lane colour to its bus label. + private func legend(for capture: ScopeCapture) -> some View { + HStack(spacing: ShoeGazeScopeView.legendSpacing) { + ForEach(Array(capture.buses.enumerated()), id: \.offset) { index, bus in + HStack(spacing: 4) { + Circle() + .fill(ShoeGazeScopeView.laneColors[index % ShoeGazeScopeView.laneColors.count]) + .frame( + width: ShoeGazeScopeView.swatchSize, + height: ShoeGazeScopeView.swatchSize + ) + Text(shoeGaze.scopeLaneLabel(forBus: bus)) + .font(.caption2) + .foregroundStyle(Color.white.opacity(ShoeGazeScopeView.labelOpacity)) + } + } + } + } +} diff --git a/Sources/LyrebirdShoeGazeUI/ShoeGazeView.swift b/Sources/LyrebirdShoeGazeUI/ShoeGazeView.swift index 5824303..eed07c4 100644 --- a/Sources/LyrebirdShoeGazeUI/ShoeGazeView.swift +++ b/Sources/LyrebirdShoeGazeUI/ShoeGazeView.swift @@ -112,6 +112,9 @@ public struct ShoeGazeView: View { Divider() .overlay(Color.white.opacity(ShoeGazeView.dividerOpacity)) boardList + Divider() + .overlay(Color.white.opacity(ShoeGazeView.dividerOpacity)) + ShoeGazeScopeView(shoeGaze: shoeGaze) Divider() .overlay(Color.white.opacity(ShoeGazeView.dividerOpacity)) LyrebirdStatusView(engine: shoeGaze.engine)