Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI, REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive. See RELEASES.md and LINUX_README.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
// 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<Float>
|
||||
let workIm: UnsafeMutablePointer<Float>
|
||||
|
||||
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<DSPSplitComplex>,
|
||||
_ 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<DSPComplex>,
|
||||
_ strideC: vDSP_Stride,
|
||||
_ z: UnsafePointer<DSPSplitComplex>,
|
||||
_ strideZ: vDSP_Stride,
|
||||
_ n: vDSP_Length
|
||||
) {
|
||||
let split: DSPSplitComplex = z.pointee
|
||||
let cf: UnsafePointer<Float> = UnsafeRawPointer(c).assumingMemoryBound(to: Float.self)
|
||||
for k: Int in 0..<Int(n) {
|
||||
split.realp[k * strideZ] = cf[k * strideC]
|
||||
split.imagp[k * strideZ] = cf[k * strideC + 1]
|
||||
}
|
||||
}
|
||||
|
||||
/// Split → interleaved. `strideC` counts `Float`s (call sites pass 2).
|
||||
public func vDSP_ztoc(
|
||||
_ z: UnsafePointer<DSPSplitComplex>,
|
||||
_ strideZ: vDSP_Stride,
|
||||
_ c: UnsafeMutablePointer<DSPComplex>,
|
||||
_ strideC: vDSP_Stride,
|
||||
_ n: vDSP_Length
|
||||
) {
|
||||
let split: DSPSplitComplex = z.pointee
|
||||
let cf: UnsafeMutablePointer<Float> = UnsafeMutableRawPointer(c).assumingMemoryBound(to: Float.self)
|
||||
for k: Int in 0..<Int(n) {
|
||||
cf[k * strideC] = split.realp[k * strideZ]
|
||||
cf[k * strideC + 1] = split.imagp[k * strideZ]
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Complex-vector helpers
|
||||
|
||||
/// Squared magnitude per element: `c[k] = realp[k]² + imagp[k]²`.
|
||||
public func vDSP_zvmags(
|
||||
_ a: UnsafePointer<DSPSplitComplex>,
|
||||
_ strideA: vDSP_Stride,
|
||||
_ c: UnsafeMutablePointer<Float>,
|
||||
_ strideC: vDSP_Stride,
|
||||
_ n: vDSP_Length
|
||||
) {
|
||||
let s: DSPSplitComplex = a.pointee
|
||||
for k: Int in 0..<Int(n) {
|
||||
let re: Float = s.realp[k * strideA]
|
||||
let im: Float = s.imagp[k * strideA]
|
||||
c[k * strideC] = re * re + im * im
|
||||
}
|
||||
}
|
||||
|
||||
/// Complex vector multiply-add: `O = A · B + C` (elementwise).
|
||||
public func vDSP_zvma(
|
||||
_ a: UnsafePointer<DSPSplitComplex>,
|
||||
_ strideA: vDSP_Stride,
|
||||
_ b: UnsafePointer<DSPSplitComplex>,
|
||||
_ strideB: vDSP_Stride,
|
||||
_ c: UnsafePointer<DSPSplitComplex>,
|
||||
_ strideC: vDSP_Stride,
|
||||
_ o: UnsafePointer<DSPSplitComplex>,
|
||||
_ 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..<Int(n) {
|
||||
let ar: Float = A.realp[k * strideA]
|
||||
let ai: Float = A.imagp[k * strideA]
|
||||
let br: Float = B.realp[k * strideB]
|
||||
let bi: Float = B.imagp[k * strideB]
|
||||
O.realp[k * strideO] = ar * br - ai * bi + C.realp[k * strideC]
|
||||
O.imagp[k * strideO] = ar * bi + ai * br + C.imagp[k * strideC]
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user