// PortableFFT.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT // Portable stand-ins for Accelerate's single-precision real-FFT API, used off // Apple platforms (on Apple the real vDSP is re-exported by AccelerateShim.swift). // The FFT setup boxes pre-allocated scratch so the transform allocates nothing; // the transform and complex-vector helpers delegate to the parity-tested cores // in ComplexFFTCore.swift. The FFT setup is single-consumer (one per instance, // used from one thread at a time), like Lyrebird's spectral generators use it. #if !canImport(Accelerate) // MARK: - FFT type / constant shims public typealias FFTSetup = OpaquePointer public typealias FFTDirection = Int32 public typealias FFTRadix = Int32 public let kFFTDirection_Forward: FFTDirection = 1 public let kFFTDirection_Inverse: FFTDirection = -1 public let kFFTRadix2: FFTRadix = 0 /// Heap-boxed FFT setup: dimensions + pre-allocated length-`n` scratch. /// /// The scratch is a raw allocation rather than a Swift `Array` because the /// transform runs on the render thread, where reaching into an array would /// retain and release it once per call — the ARC traffic the RT rules exclude. private final class _LBFFTSetup { /// Half the maximum transform length this setup was built for. let half: Int let workRe: UnsafeMutablePointer let workIm: UnsafeMutablePointer init(log2n: Int) { let n: Int = 1 << log2n self.half = n / 2 self.workRe = .allocate(capacity: n) self.workIm = .allocate(capacity: n) self.workRe.initialize(repeating: 0, count: n) self.workIm.initialize(repeating: 0, count: n) } deinit { let n: Int = half * 2 workRe.deinitialize(count: n) workIm.deinitialize(count: n) workRe.deallocate() workIm.deallocate() } } // MARK: - FFT setup lifecycle /// Allocates an FFT setup for length `2^log2n`. Only `kFFTRadix2` is supported. /// /// A radix this implementation does not have traps rather than silently /// producing a radix-2 transform of data that is not radix-2 — a wrong spectrum /// is far harder to notice than a crash. public func vDSP_create_fftsetup(_ log2n: vDSP_Length, _ radix: FFTRadix) -> FFTSetup? { precondition( radix == kFFTRadix2, "PortableFFT implements radix 2 only; got radix \(radix)" ) let box: _LBFFTSetup = _LBFFTSetup(log2n: Int(log2n)) return FFTSetup(Unmanaged.passRetained(box).toOpaque()) } /// Releases a setup from ``vDSP_create_fftsetup(_:_:)``. public func vDSP_destroy_fftsetup(_ setup: FFTSetup) { Unmanaged<_LBFFTSetup>.fromOpaque(UnsafeRawPointer(setup)).release() } // MARK: - Real FFT /// In-place packed real FFT. /// /// `stride` must be 1 — every Lyrebird call site passes 1, and a strided /// transform is not implemented. Passing anything else traps instead of /// quietly transforming the wrong samples. public func vDSP_fft_zrip( _ setup: FFTSetup, _ ioData: UnsafePointer, _ stride: vDSP_Stride, _ log2n: vDSP_Length, _ direction: FFTDirection ) { precondition(stride == 1, "PortableFFT supports stride 1 only; got \(stride)") let box: _LBFFTSetup = Unmanaged<_LBFFTSetup>.fromOpaque(UnsafeRawPointer(setup)).takeUnretainedValue() // vDSP's contract is that one setup serves any transform *up to* its size, // so the length comes from `log2n`, not from the setup. Only asking for // more than the setup was built for is an error — the scratch would not // fit. let half: Int = (1 << Int(log2n)) / 2 precondition( half <= box.half, "PortableFFT setup holds \(box.half * 2) points; got log2n \(log2n) (\(half * 2) points)" ) let split: DSPSplitComplex = ioData.pointee if direction == kFFTDirection_Forward { _lbRealFFTForward(split.realp, split.imagp, half, box.workRe, box.workIm) } else { _lbRealFFTInverse(split.realp, split.imagp, half, box.workRe, box.workIm) } } // MARK: - Interleaved ↔ split conversion /// Interleaved → split. `strideC` counts `Float`s (call sites pass 2). public func vDSP_ctoz( _ c: UnsafePointer, _ strideC: vDSP_Stride, _ z: UnsafePointer, _ strideZ: vDSP_Stride, _ n: vDSP_Length ) { let split: DSPSplitComplex = z.pointee let cf: UnsafePointer = UnsafeRawPointer(c).assumingMemoryBound(to: Float.self) for k: Int in 0.., _ strideZ: vDSP_Stride, _ c: UnsafeMutablePointer, _ strideC: vDSP_Stride, _ n: vDSP_Length ) { let split: DSPSplitComplex = z.pointee let cf: UnsafeMutablePointer = UnsafeMutableRawPointer(c).assumingMemoryBound(to: Float.self) for k: Int in 0.., _ strideA: vDSP_Stride, _ c: UnsafeMutablePointer, _ strideC: vDSP_Stride, _ n: vDSP_Length ) { let s: DSPSplitComplex = a.pointee for k: Int in 0.., _ strideA: vDSP_Stride, _ b: UnsafePointer, _ strideB: vDSP_Stride, _ c: UnsafePointer, _ strideC: vDSP_Stride, _ o: UnsafePointer, _ strideO: vDSP_Stride, _ n: vDSP_Length ) { let A: DSPSplitComplex = a.pointee let B: DSPSplitComplex = b.pointee let C: DSPSplitComplex = c.pointee let O: DSPSplitComplex = o.pointee for k: Int in 0..