Files
Lyrebird/Sources/LyrebirdCLI/Resources/XcodeTemplates/LyrebirdMultiChannelGenerator.xctemplate/___FILEBASENAME___.swift
T
2026-06-01 18:27:40 -07:00

86 lines
3.0 KiB
Swift

//___FILEHEADER___
import Foundation
@_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - ___FILEBASENAMEASIDENTIFIER___
/// One-line description of ___FILEBASENAMEASIDENTIFIER___.
///
/// Returns N output channels per render block. The channel count is fixed at
/// init time via `super.init(channels: N)` — `GeneratorBase` pre-allocates the
/// `_result` buffer with N rows. `outputChannelCount` returns the same N so
/// downstream operators (`+`, `-`, `*`, `/`) treat this as multi-channel rather
/// than mono.
///
/// Channel count must be derivable from a `let` at init — never runtime-mutable.
/// To support a user-chosen channel count, accept it as an `Int` init parameter
/// (see Pan4 etc).
///
/// ## Usage
/// ```swift
/// override func build() {
/// let src = OscSin.Audio(freq: 440.0)
/// // Out writes channel k to bus+k:
/// output(bus: 0, source: ___FILEBASENAMEASIDENTIFIER___(input: src))
/// }
/// ```
public final class ___FILEBASENAMEASIDENTIFIER___: GeneratorBase, AudioGenerator {
// MARK: - Channel count (fixed at init)
/// Output channel count. Hard-coded here as a starting value — replace with an
/// init parameter if your generator needs user-chosen channel counts.
private static let outChannels: Int = 2
public override var outputChannelCount: Int { Self.outChannels }
// MARK: - Inputs
private let _input: any ValidGeneratorInput
private let _mulAdd: MulAddSpec
// MARK: - Init
/// - Parameters:
/// - input: Source signal. Mono input fans out across all channels by
/// default; multi-row input maps element-wise (mismatched channel counts
/// should `precondition`-trap at init).
/// - mul: Output multiplier. Defaults to `1.0`. Channel-aware: scalar
/// broadcasts, equal-N multi-row applies element-wise.
/// - add: Output offset. Defaults to `0.0`. Same channel-aware policy.
public init(
input: any ValidGeneratorInput,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
_input = input
_mulAdd = MulAddSpec(mul: mul, add: add)
super.init(channels: Self.outChannels)
}
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
let inputSamples = _input.calculatedSamples(graph: graph, count: count)
guard let ins = inputSamples.first else { return _result }
// TODO: replace this body with whatever per-channel processing you want.
// The starter logic copies the first input row into every output row —
// i.e. fans a mono source out to N identical channels. Real generators
// typically produce different content per channel (panning, decoder
// matrices, multi-bus routing, etc.).
for ch: Int in 0..<Self.outChannels {
for i: Int in 0..<count {
_result[ch][i] = ins[i]
}
}
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
return _result
}
}