58 lines
2.5 KiB
Swift
58 lines
2.5 KiB
Swift
// ShoeGaze+Engine.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import LyrebirdSynth
|
|
|
|
extension ShoeGaze {
|
|
|
|
/// Reconfigures the engine's sample rate and channel counts while preserving the rig.
|
|
///
|
|
/// The engine must be **stopped** (the underlying `SynthesisEngine` setters throw
|
|
/// `SynthesisEngineConfigError.engineIsRunning` otherwise). The current rig is captured,
|
|
/// the engine is reconfigured, and every board is rebuilt from the snapshot so that
|
|
/// sample-rate-dependent nodes — e.g. ``TestLoopSource``'s Karplus-Strong delay line, sized at
|
|
/// init from the engine rate — are recreated at the new rate. Reconstruction reuses the atomic
|
|
/// ``rebuildAllBoards(from:)`` (teardown-on-failure), so a failure leaves an empty rig rather
|
|
/// than a half-built one.
|
|
///
|
|
/// If the current `inputSource` is `.liveInput(channel:)` and `channel` is no longer valid
|
|
/// under `inputChannels`, the source is reset to `.testLoop` before rebuilding.
|
|
///
|
|
/// - Parameters:
|
|
/// - sampleRate: New engine sample rate in Hz.
|
|
/// - inputChannels: New hardware input-channel count (`>= 0`).
|
|
/// - outputChannels: New hardware output-channel count (`>= 1`).
|
|
/// - Throws: `SynthesisEngineConfigError.engineIsRunning` if the engine is running;
|
|
/// `SynthesisEngineConfigError.invalidValue` for out-of-range values; or any error from
|
|
/// board reconstruction.
|
|
public func reconfigureEngine(
|
|
sampleRate: Double,
|
|
inputChannels: Int,
|
|
outputChannels: Int
|
|
) throws {
|
|
guard !engine.isRunning else { throw SynthesisEngineConfigError.engineIsRunning }
|
|
|
|
let snapshot: ShoeGazeState = captureState()
|
|
|
|
// Tear the rig down BEFORE reconfiguring: changing the channel counts re-partitions the
|
|
// bus allocator's hardware reservation, which would invalidate (double-free) any rig buses
|
|
// still allocated under the old layout.
|
|
teardown()
|
|
|
|
try engine.setSampleRate(sampleRate)
|
|
try engine.setNumOutputChannels(outputChannels)
|
|
try engine.setNumInputChannels(inputChannels)
|
|
|
|
// A live-input source pointing at a channel that no longer exists would make the rebuild's
|
|
// addBoard() throw; fall back to the test loop.
|
|
if case .liveInput(let channel) = inputSource, channel >= inputChannels {
|
|
inputSource = .testLoop
|
|
}
|
|
|
|
try rebuildAllBoards(from: snapshot)
|
|
}
|
|
}
|