// TestLoopTap.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Internal synthesizer that copies the shared global test-loop bus to a board head bus. /// /// In `.testLoop` mode every board reads the **same** global ``TestLoopSource`` (so all boards /// stay phase-locked instead of each running an independently-phased arpeggio). A `TestLoopTap` /// per board reads the global test bus at `inputBusIndex` and writes it to the board's head bus /// at `outputBusIndex`, so the first pedal still reads from a board-owned bus. /// /// Added `.after(_:)` the global source so it evaluates once the source has written the test bus /// for the current block. /// /// - Note: Must be removed from the engine before the board's `teardown()` releases the head bus. final class TestLoopTap: SynthesizerBase { // MARK: - Parameters /// Input mute — `0` = audible, `1` = muted. Driven across every board input node by /// `ShoeGaze.setInputMuted(_:)`, which sets a newly-added node to the session's current mute /// state. Defaults audible so direct users that don't drive the mute pass the signal through. @UIParam var inputMute: Double = 0.0 /// The live `UIParamNode` backing `inputMute`, driven by `ShoeGaze.setInputMuted(_:)`. var inputMuteNode: UIParamNode { $inputMute } // MARK: - Routing /// Index of the shared global test-loop bus to read from. private let inputBusIndex: Int /// Index of the board head bus to write to. private let outputBusIndex: Int // MARK: - Init /// Creates a tap that copies `inputBusIndex` (the global test bus) to `outputBusIndex` /// (a board head bus). /// /// - Parameters: /// - inputBusIndex: Shared global test-loop bus index to read from. /// - outputBusIndex: Board head bus index to write to. init(inputBusIndex: Int, outputBusIndex: Int) { self.inputBusIndex = inputBusIndex self.outputBusIndex = outputBusIndex super.init() } // MARK: - Build override func build() { // Hard kill-switch gate: `1 − inputMute`, so mute 0 → gain 1 (audible), 1 → gain 0 (exactly // silent). Unlagged on purpose — a lagged gate never settles to exactly zero on its first // blocks and leaks a faint transient right when the mute is meant to keep things quiet; a // kill-switch mute is expected to be abrupt anyway. let muteGain: any ValidGeneratorInput = ($inputMute - 1.0) * -1.0 output(bus: outputBusIndex, source: input(bus: inputBusIndex) * muteGain) } }