b6fdee8bfc
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>
123 lines
4.1 KiB
Swift
123 lines
4.1 KiB
Swift
// ComplexFFTCore.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Foundation
|
||
|
||
// Portable radix-2 FFT cores backing the non-Apple `vDSP_fft_zrip` shim. These
|
||
// are compiled on every platform (not behind an `#if`) so they can be parity-
|
||
// tested against the real Accelerate FFT on Apple — the test is what proves the
|
||
// fallback reproduces vDSP's real-FFT packing (DC in realp[0], Nyquist in
|
||
// imagp[0]) and its scaling (forward ×2 per bin; forward→inverse ×2N), which
|
||
// consumers depend on.
|
||
|
||
/// In-place iterative Cooley–Tukey complex FFT, length `n = 2^log2n`.
|
||
/// `forward == true` uses the `e^{-2πi}` sign; both directions are
|
||
/// **unnormalized** (no `1/n`), matching vDSP.
|
||
@inline(__always)
|
||
func _lbComplexFFT(
|
||
_ re: UnsafeMutablePointer<Float>,
|
||
_ im: UnsafeMutablePointer<Float>,
|
||
_ n: Int,
|
||
_ forward: Bool
|
||
) {
|
||
// Bit-reversal permutation.
|
||
var j: Int = 0
|
||
for i: Int in 0..<(n - 1) {
|
||
if i < j {
|
||
let tr: Float = re[i]; re[i] = re[j]; re[j] = tr
|
||
let ti: Float = im[i]; im[i] = im[j]; im[j] = ti
|
||
}
|
||
var m: Int = n >> 1
|
||
while m >= 1 && j >= m { j -= m; m >>= 1 }
|
||
j += m
|
||
}
|
||
|
||
let sign: Float = forward ? -1 : 1
|
||
var len: Int = 2
|
||
while len <= n {
|
||
let half: Int = len >> 1
|
||
let base: Float = sign * 2 * Float.pi / Float(len)
|
||
var group: Int = 0
|
||
while group < n {
|
||
for k: Int in 0..<half {
|
||
let angle: Float = base * Float(k)
|
||
let wr: Float = cos(angle)
|
||
let wi: Float = sin(angle)
|
||
let a: Int = group + k
|
||
let b: Int = group + k + half
|
||
let tr: Float = wr * re[b] - wi * im[b]
|
||
let ti: Float = wr * im[b] + wi * re[b]
|
||
re[b] = re[a] - tr
|
||
im[b] = im[a] - ti
|
||
re[a] = re[a] + tr
|
||
im[a] = im[a] + ti
|
||
}
|
||
group += len
|
||
}
|
||
len <<= 1
|
||
}
|
||
}
|
||
|
||
/// Forward real FFT in vDSP `zrip` packed layout. `realp`/`imagp` (length
|
||
/// `half = n/2`) arrive holding the real signal split even/odd (as `vDSP_ctoz`
|
||
/// produces) and are overwritten with the packed spectrum: `realp[0]` = 2·DC,
|
||
/// `imagp[0]` = 2·Nyquist, and `realp[k] + i·imagp[k]` = 2·bin k for
|
||
/// `1 ≤ k < half`. `workRe`/`workIm` are scratch of length `n`.
|
||
@inline(__always)
|
||
func _lbRealFFTForward(
|
||
_ realp: UnsafeMutablePointer<Float>,
|
||
_ imagp: UnsafeMutablePointer<Float>,
|
||
_ half: Int,
|
||
_ workRe: UnsafeMutablePointer<Float>,
|
||
_ workIm: UnsafeMutablePointer<Float>
|
||
) {
|
||
let n: Int = half * 2
|
||
for jIdx: Int in 0..<half {
|
||
workRe[2 * jIdx] = realp[jIdx]
|
||
workRe[2 * jIdx + 1] = imagp[jIdx]
|
||
}
|
||
for i: Int in 0..<n { workIm[i] = 0 }
|
||
|
||
_lbComplexFFT(workRe, workIm, n, true)
|
||
|
||
realp[0] = 2 * workRe[0] // DC (real)
|
||
imagp[0] = 2 * workRe[half] // Nyquist (real) packed into imagp[0]
|
||
for k: Int in 1..<half {
|
||
realp[k] = 2 * workRe[k]
|
||
imagp[k] = 2 * workIm[k]
|
||
}
|
||
}
|
||
|
||
/// Inverse real FFT in vDSP `zrip` packed layout — the exact inverse of
|
||
/// ``_lbRealFFTForward``. Consumes the packed spectrum in `realp`/`imagp`
|
||
/// (length `half`) and overwrites them with the real time signal split
|
||
/// even/odd (`realp[j]` = sample 2j, `imagp[j]` = sample 2j+1). Unnormalized:
|
||
/// forward then inverse scales the signal by `2n`, matching vDSP.
|
||
@inline(__always)
|
||
func _lbRealFFTInverse(
|
||
_ realp: UnsafeMutablePointer<Float>,
|
||
_ imagp: UnsafeMutablePointer<Float>,
|
||
_ half: Int,
|
||
_ workRe: UnsafeMutablePointer<Float>,
|
||
_ workIm: UnsafeMutablePointer<Float>
|
||
) {
|
||
let n: Int = half * 2
|
||
// Rebuild the full Hermitian-symmetric spectrum from the packed half.
|
||
workRe[0] = realp[0]; workIm[0] = 0
|
||
workRe[half] = imagp[0]; workIm[half] = 0
|
||
for k: Int in 1..<half {
|
||
workRe[k] = realp[k]; workIm[k] = imagp[k]
|
||
workRe[n - k] = realp[k]; workIm[n - k] = -imagp[k]
|
||
}
|
||
|
||
_lbComplexFFT(workRe, workIm, n, false)
|
||
|
||
for jIdx: Int in 0..<half {
|
||
realp[jIdx] = workRe[2 * jIdx]
|
||
imagp[jIdx] = workRe[2 * jIdx + 1]
|
||
}
|
||
}
|