// ShoeGazeError.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation /// Errors thrown by `ShoeGaze` during board and session management. public enum ShoeGazeError: Error, LocalizedError, Equatable, Sendable { /// Thrown by `ShoeGaze.addBoard()` when `inputSource == .liveInput` but the engine /// was created with `numInputChannels == 0`. /// /// Re-create the `SynthesisEngine` with `numInputChannels > 0` before using `.liveInput`. case liveInputRequiresInputChannels /// Thrown when `.liveInput(channel:)` requests a hardware input channel that is /// negative or `>= numInputChannels`. `requested` is the channel asked for; /// `available` is the engine's `numInputChannels`. case inputChannelOutOfRange(requested: Int, available: Int) /// Thrown by `ChainRouter.addToA(_:)` or `addToB(_:)` when called after /// ``ChainRouter/finalize()`` has already been invoked. /// /// Pedals must be added to the router's boards before `finalize()` so the merger /// is placed after the full evaluation tree. Adding pedals after finalization would /// leave them ordered before the merger in the node tree, causing stale-bus reads. case routerAlreadyFinalized /// Thrown by `ShoeGaze.startRecording()` when `recordingFolder` is `nil`. /// /// Set `ShoeGaze.recordingFolder` to a `URL` pointing to the desired output /// directory before calling `startRecording()`. case recordingFolderNotSet /// Thrown by `ShoeGaze.startRecording()` when a `RecordTap`'s `DiskOut.Audio` /// could not open the destination file (e.g. permissions, disk full). /// /// The `URL` identifies the file that could not be created. Any taps that /// were successfully added before the failure are removed and the recording /// session is not started. case recordTapFailedToOpen(URL) /// Thrown by `PedalBoard.add(_:)` when a widener pedal (``PedalChannelKind/widener``) /// is added while the board is already stereo (width 2). The chain width is capped at 2. /// /// Narrow the chain with `mergeToMono()` before adding another widener. case stereoWidthExceeded /// Thrown by the linear pedal-edit API (`appendPedal(_:to:)`) when the kind's `rawValue` is /// not a known pedal in `PedalRegistry.builtIn`. The associated value is the requested type string. case unknownPedalType(String) /// Thrown by `removePedal(at:from:)` / `movePedal(from:to:in:)` when a linear pedal index is /// negative or `>=` the board's linear pedal count. `index` is the offending index; `count` /// is the number of linear pedals on the board. case pedalIndexOutOfRange(index: Int, count: Int) /// Thrown by `appendStereoSplit(to:)` when the board's tail is already stereo (width 2). A /// stereo split fans a mono tail into two branches; narrow the chain with a `Merge to Mono` /// first. (See `PedalBoard.splitStereo()`.) case cannotSplitStereoChain /// Thrown by `appendMergeToMono(to:)` when the board's tail is already mono (width 1) — there /// is no stereo signal to collapse. case cannotMergeMonoChain /// Thrown by `unzipStereo()` when the tail is mono (width 1) — there is no stereo signal whose /// L/R channels could be split into independent lanes. case cannotUnzipMonoChain /// Thrown by the lane-addressed edit API when the segment at the given top-level index is not a /// branch region (stereo split or parallel section). `index` is the offending segment index. case notABranchRegion(index: Int) // MARK: - LocalizedError public var errorDescription: String? { switch self { case .liveInputRequiresInputChannels: return "ShoeGaze: .liveInput requires the engine to be created with numInputChannels > 0." case .inputChannelOutOfRange(let requested, let available): return "ShoeGaze: .liveInput channel \(requested) is out of range; engine has \(available) input channel(s)." case .routerAlreadyFinalized: return "ChainRouter: cannot add pedals after finalize() has been called." case .recordingFolderNotSet: return "ShoeGaze: set recordingFolder before calling startRecording()." case .recordTapFailedToOpen(let url): return "ShoeGaze: could not open recording file '\(url.lastPathComponent)'." case .stereoWidthExceeded: return "PedalBoard: chain is already stereo (width 2); cannot add another widener. Use mergeToMono() first." case .unknownPedalType(let type): return "ShoeGaze: '\(type)' is not a known pedal type; cannot add it to the board." case .pedalIndexOutOfRange(let index, let count): return "ShoeGaze: pedal index \(index) is out of range; the board has \(count) linear pedal(s)." case .cannotSplitStereoChain: return "ShoeGaze: cannot add a stereo split; the chain is already stereo. Merge to mono first." case .cannotMergeMonoChain: return "ShoeGaze: the chain is already mono; there is no stereo signal to merge." case .cannotUnzipMonoChain: return "ShoeGaze: the chain is mono; there is no stereo signal to split into L/R lanes." case .notABranchRegion(let index): return "ShoeGaze: segment \(index) is not a branch region (stereo split or parallel section)." } } }