// InputSource.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth /// Selects how the pedalboard's head bus is fed with audio. /// /// - `.testLoop`: A synthesized test tone (`TestLoopSource`) writes to the head bus. /// Suitable for testing the pedalboard with no hardware connected. /// - `.liveInput(channel:)`: A hardware input channel feeds the head bus. `channel` is /// the zero-based hardware input channel index (`0` = first input). It maps to engine /// audio-bus index `numOutputChannels + channel` (hardware input buses follow the /// output buses). Requires the engine to be created with `numInputChannels > channel`. public enum InputSource: Equatable { /// Synthesized test signal — no hardware required. case testLoop /// Live hardware input from the given zero-based input channel. The engine must be /// created with `numInputChannels > channel`. case liveInput(channel: Int) } // MARK: - Codable /// Persisted so a saved rig round-trips its input selection. Encoded as a `kind` discriminator /// plus an optional `channel`, so adding future cases stays forward-tolerant. extension InputSource: Codable { private enum Kind: String, Codable { case testLoop case liveInput } private enum CodingKeys: String, CodingKey { case kind case channel } public init(from decoder: Decoder) throws { let container: KeyedDecodingContainer = try decoder.container(keyedBy: CodingKeys.self) let kind: Kind = try container.decode(Kind.self, forKey: .kind) switch kind { case .testLoop: self = .testLoop case .liveInput: let channel: Int = try container.decode(Int.self, forKey: .channel) self = .liveInput(channel: channel) } } public func encode(to encoder: Encoder) throws { var container: KeyedEncodingContainer = encoder.container(keyedBy: CodingKeys.self) switch self { case .testLoop: try container.encode(Kind.testLoop, forKey: .kind) case .liveInput(let channel): try container.encode(Kind.liveInput, forKey: .kind) try container.encode(channel, forKey: .channel) } } } /// Validates a `.liveInput` channel against the engine and returns the engine audio-bus /// index of that hardware input channel. /// /// Hardware input buses occupy indices `numOutputChannels ..< (numOutputChannels + /// numInputChannels)`, so input channel `channel` lives at `numOutputChannels + channel`. /// /// - Parameters: /// - channel: Zero-based hardware input channel index. /// - engine: The engine whose bus layout is queried. /// - Returns: The audio-bus index to read the requested input channel from. /// - Throws: /// - `ShoeGazeError.liveInputRequiresInputChannels` if the engine has no input channels. /// - `ShoeGazeError.inputChannelOutOfRange` if `channel` is negative or `>= numInputChannels`. internal func liveInputBusIndex( channel: Int, engine: SynthesisEngine ) throws -> Int { guard engine.numInputChannels > 0 else { throw ShoeGazeError.liveInputRequiresInputChannels } guard channel >= 0, channel < engine.numInputChannels else { throw ShoeGazeError.inputChannelOutOfRange( requested: channel, available: engine.numInputChannels ) } return engine.numOutputChannels + channel }