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

63 lines
2.0 KiB
Swift

//___FILEHEADER___
import Foundation
import LyrebirdSynth
// MARK: - ___FILEBASENAMEASIDENTIFIER___
/// One-line description of ___FILEBASENAMEASIDENTIFIER___.
///
/// A `SpectralProcessor` transforms the FFT-domain `SpectralBuffer`
/// in-place after each forward-FFT hop. Wire it into a `SpectralChain.Audio`
/// as a `.mono` stage:
///
/// ```swift
/// let proc = ___FILEBASENAMEASIDENTIFIER___()
/// let chain = SpectralChain.Audio(
/// input: source,
/// fftSize: 2048,
/// stages: [.mono(proc)]
/// )
/// output(bus: 0, source: chain)
/// ```
///
/// ## RT contract
///
/// `process(spectral:)` runs on the RT thread. It must:
/// - Not allocate
/// - Not lock
/// - Not call into Foundation / Swift stdlib methods that may allocate
///
/// Read parameters from `@LiveParam` slots on a Synthesizer that owns this
/// processor — those reads are atomic and RT-safe.
public final class ___FILEBASENAMEASIDENTIFIER___: SpectralProcessor {
// MARK: - Configuration
/// Internal state that controls the spectral transform. Mutate from non-RT
/// threads via atomic operations or `@LiveParam`-style projections.
private var _someParameter: Double = 1.0
// MARK: - Init
public init() {
// No allocation paths should run after this point on the RT thread.
}
// MARK: - SpectralProcessor
public func process(spectral: SpectralBuffer, engine: SynthesisEngine) {
// Walk every bin in the half-spectrum [0, binCount). DC and Nyquist
// are real-only — phase at those bins is undefined.
//
// The starter body below applies a uniform magnitude scale to every
// bin. Replace with your transform (spectral filtering, formant shift,
// freezing, cross-synthesis, etc.). The subscripts compile to direct
// memory reads/writes through the buffer's pre-allocated storage.
for bin in 0..<spectral.binCount {
let mag = spectral[magnitude: bin]
spectral[magnitude: bin] = mag * Float(_someParameter)
}
}
}