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,317 @@
|
||||
// AccelerateShim.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// `LyrebirdDSP` is the seam that lets the synthesis core build off Apple
|
||||
// platforms. Every Lyrebird file that previously wrote `import Accelerate`
|
||||
// writes `import LyrebirdDSP` instead:
|
||||
//
|
||||
// * On Apple, this module re-exports Accelerate wholesale
|
||||
// (`@_exported import Accelerate`), so `vDSP_*` / `vv*` / FFT / biquad all
|
||||
// resolve to the real, hardware-accelerated implementations. Call sites are
|
||||
// byte-identical to before — there is no behavioural or performance change.
|
||||
//
|
||||
// * On non-Apple platforms, Accelerate does not exist. This file (plus
|
||||
// `ScalarDSP.swift`, `PortableBiquad.swift`, `PortableFFT.swift`,
|
||||
// `ComplexFFTCore.swift`) supplies API-compatible replacements for every
|
||||
// vDSP/vForce primitive Lyrebird uses — vector ops and vForce here, the
|
||||
// biquad-setup family in `PortableBiquad.swift`, and the FFT family
|
||||
// (`vDSP_fft_zrip`, `vDSP_create_fftsetup`, `vDSP_ctoz`, …) in
|
||||
// `PortableFFT.swift`. All are parity-tested against real vDSP on Apple in
|
||||
// `Tests/LyrebirdDSPTests`.
|
||||
|
||||
// Re-export the platform C library so every consumer gets scalar math
|
||||
// (`sin`, `cos`, `pow`, `tanh`, `exp`, `log`, …) — and libc — through the single
|
||||
// `import LyrebirdDSP`, with no per-file platform `#if`. This is the scalar-math
|
||||
// counterpart to the vDSP re-export below.
|
||||
#if canImport(Darwin)
|
||||
@_exported import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
@_exported import Glibc
|
||||
#elseif canImport(Musl)
|
||||
@_exported import Musl
|
||||
#elseif canImport(ucrt)
|
||||
@_exported import ucrt
|
||||
#endif
|
||||
|
||||
#if canImport(Accelerate)
|
||||
|
||||
@_exported import Accelerate
|
||||
|
||||
#else
|
||||
|
||||
// (The platform C library — sin/cos/sqrt/… — is @_exported at the top of this
|
||||
// file, so the scalar cores below and every consumer already have it in scope.)
|
||||
|
||||
// MARK: - vDSP type shims
|
||||
|
||||
public typealias vDSP_Length = UInt
|
||||
public typealias vDSP_Stride = Int
|
||||
|
||||
// MARK: - Double vector ops
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vclrD(_ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbClear(c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vfillD(_ a: UnsafePointer<Double>, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbFill(a.pointee, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vsmulD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVScalarMul(a, ia, b.pointee, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vaddD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVAdd(a, ia, b, ib, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vmulD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVMul(a, ia, b, ib, c, ic, Int(n))
|
||||
}
|
||||
|
||||
/// `vDSP_vsubD` takes the subtrahend `b` first, then the minuend `a`; result is `a - b`.
|
||||
@inline(__always)
|
||||
public func vDSP_vsubD(_ b: UnsafePointer<Double>, _ ib: vDSP_Stride, _ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVSub(a, ia, b, ib, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vsaddD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVScalarAdd(a, ia, b.pointee, c, ic, Int(n))
|
||||
}
|
||||
|
||||
/// `d = a * b + c` where `b` and `c` are scalars.
|
||||
@inline(__always)
|
||||
public func vDSP_vsmsaD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ c: UnsafePointer<Double>, _ d: UnsafeMutablePointer<Double>, _ id: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVScalarMulScalarAdd(a, ia, b.pointee, c.pointee, d, id, Int(n))
|
||||
}
|
||||
|
||||
/// `d = a * b + c` where `b` is a scalar and `c` is a vector.
|
||||
@inline(__always)
|
||||
public func vDSP_vsmaD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ c: UnsafePointer<Double>, _ ic: vDSP_Stride, _ d: UnsafeMutablePointer<Double>, _ id: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVScalarMulVAdd(a, ia, b.pointee, c, ic, d, id, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vabsD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVAbs(a, ia, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vsdivD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVScalarDiv(a, ia, b.pointee, c, ic, Int(n))
|
||||
}
|
||||
|
||||
/// `vDSP_vdivD` takes the divisor `b` first, then the dividend `a`; result is `a / b`.
|
||||
@inline(__always)
|
||||
public func vDSP_vdivD(_ b: UnsafePointer<Double>, _ ib: vDSP_Stride, _ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVDiv(a, ia, b, ib, c, ic, Int(n))
|
||||
}
|
||||
|
||||
/// `c = a / b` where `a` is a scalar.
|
||||
@inline(__always)
|
||||
public func vDSP_svdivD(_ a: UnsafePointer<Double>, _ b: UnsafePointer<Double>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbScalarVDiv(a.pointee, b, ib, c, ic, Int(n))
|
||||
}
|
||||
|
||||
/// `c[i] = a + i * b` where `a` (start) and `b` (step) are scalars.
|
||||
@inline(__always)
|
||||
public func vDSP_vrampD(_ a: UnsafePointer<Double>, _ b: UnsafePointer<Double>, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVRamp(a.pointee, b.pointee, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_maxmgvD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ n: vDSP_Length) {
|
||||
c.pointee = _lbMaxMagnitude(a, ia, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_sveD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ n: vDSP_Length) {
|
||||
c.pointee = _lbSum(a, ia, Int(n))
|
||||
}
|
||||
|
||||
// MARK: - Float vector ops
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vclr(_ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbClear(c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vsmul(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVScalarMul(a, ia, b.pointee, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vadd(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVAdd(a, ia, b, ib, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vmul(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVMul(a, ia, b, ib, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vsadd(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVScalarAdd(a, ia, b.pointee, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vsma(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ c: UnsafePointer<Float>, _ ic: vDSP_Stride, _ d: UnsafeMutablePointer<Float>, _ id: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVScalarMulVAdd(a, ia, b.pointee, c, ic, d, id, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_vramp(_ a: UnsafePointer<Float>, _ b: UnsafePointer<Float>, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVRamp(a.pointee, b.pointee, c, ic, Int(n))
|
||||
}
|
||||
|
||||
/// `d[i] = a[i] + c * (b[i] - a[i])` where `c` is a scalar interpolation factor.
|
||||
@inline(__always)
|
||||
public func vDSP_vintb(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ ib: vDSP_Stride, _ c: UnsafePointer<Float>, _ d: UnsafeMutablePointer<Float>, _ id: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVInterp(a, ia, b, ib, c.pointee, d, id, Int(n))
|
||||
}
|
||||
|
||||
/// `c[i] = max(a[i], b)` where `b` is a scalar threshold (low-clamp).
|
||||
@inline(__always)
|
||||
public func vDSP_vthr(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbVThreshold(a, ia, b.pointee, c, ic, Int(n))
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vDSP_dotpr(_ a: UnsafePointer<Float>, _ ia: vDSP_Stride, _ b: UnsafePointer<Float>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Float>, _ n: vDSP_Length) {
|
||||
c.pointee = _lbDot(a, ia, b, ib, Int(n))
|
||||
}
|
||||
|
||||
/// `output = input · (start + i·step)`; `start` is advanced past the block.
|
||||
@inline(__always)
|
||||
public func vDSP_vrampmul(_ input: UnsafePointer<Float>, _ iStride: vDSP_Stride, _ start: UnsafeMutablePointer<Float>, _ step: UnsafePointer<Float>, _ output: UnsafeMutablePointer<Float>, _ oStride: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbRampMul(input, iStride, start, step.pointee, output, oStride, Int(n))
|
||||
}
|
||||
|
||||
/// Hann-window flags (only the standard denormalized window is used).
|
||||
public let vDSP_HANN_DENORM: Int = 0
|
||||
public let vDSP_HANN_NORM: Int = 1
|
||||
|
||||
/// Fills `c` with a symmetric Hann window of length `n` (flag ignored; the
|
||||
/// codebase only requests `vDSP_HANN_DENORM`).
|
||||
@inline(__always)
|
||||
public func vDSP_hann_window(_ c: UnsafeMutablePointer<Float>, _ n: vDSP_Length, _ flag: Int32) {
|
||||
_lbHannWindow(c, Int(n))
|
||||
}
|
||||
|
||||
// MARK: - Conversion & matrix
|
||||
|
||||
/// Double→Float conversion.
|
||||
@inline(__always)
|
||||
public func vDSP_vdpsp(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ c: UnsafeMutablePointer<Float>, _ ic: vDSP_Stride, _ n: vDSP_Length) {
|
||||
_lbConvertDoubleToFloat(a, ia, c, ic, Int(n))
|
||||
}
|
||||
|
||||
/// Row-major matrix multiply `C[M×N] = A[M×P] · B[P×N]`.
|
||||
@inline(__always)
|
||||
public func vDSP_mmulD(_ a: UnsafePointer<Double>, _ ia: vDSP_Stride, _ b: UnsafePointer<Double>, _ ib: vDSP_Stride, _ c: UnsafeMutablePointer<Double>, _ ic: vDSP_Stride, _ m: vDSP_Length, _ n: vDSP_Length, _ p: vDSP_Length) {
|
||||
_lbMatMul(a, ia, b, ib, c, ic, Int(m), Int(n), Int(p))
|
||||
}
|
||||
|
||||
// MARK: - vForce transcendentals (n is a pointer to the Int32 element count)
|
||||
|
||||
@inline(__always)
|
||||
public func vvsin(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = sin(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvcos(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = cos(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvsqrt(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = sqrt(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvexp(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = exp(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvlog(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = log(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvfloor(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = floor(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvceil(_ y: UnsafeMutablePointer<Double>, _ x: UnsafePointer<Double>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = ceil(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvsinf(_ y: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = sin(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvcosf(_ y: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = cos(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvsqrtf(_ y: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = sqrt(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvexpf(_ y: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = exp(x[i]) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func vvlogf(_ y: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { y[i] = log(x[i]) }
|
||||
}
|
||||
|
||||
/// `sinOut[i] = sin(x[i])`, `cosOut[i] = cos(x[i])`.
|
||||
@inline(__always)
|
||||
public func vvsincosf(_ sinOut: UnsafeMutablePointer<Float>, _ cosOut: UnsafeMutablePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count {
|
||||
sinOut[i] = sin(x[i])
|
||||
cosOut[i] = cos(x[i])
|
||||
}
|
||||
}
|
||||
|
||||
/// `z[i] = atan2(y[i], x[i])`.
|
||||
@inline(__always)
|
||||
public func vvatan2f(_ z: UnsafeMutablePointer<Float>, _ y: UnsafePointer<Float>, _ x: UnsafePointer<Float>, _ n: UnsafePointer<Int32>) {
|
||||
let count: Int = Int(n.pointee)
|
||||
for i: Int in 0..<count { z[i] = atan2(y[i], x[i]) }
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,122 @@
|
||||
// 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]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// DSPComplex.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Portable mirror of Accelerate's interleaved single-precision complex value.
|
||||
// Only defined off Apple (on Apple the real type is re-exported by
|
||||
// AccelerateShim.swift). Layout is two contiguous `Float`s (8 bytes, no
|
||||
// padding) so `vDSP_ctoz`/`vDSP_ztoc` may reinterpret a `DSPComplex*` as an
|
||||
// interleaved `Float*`.
|
||||
|
||||
#if !canImport(Accelerate)
|
||||
|
||||
public struct DSPComplex {
|
||||
public var real: Float
|
||||
public var imag: Float
|
||||
public init(
|
||||
real: Float = 0,
|
||||
imag: Float = 0
|
||||
) {
|
||||
self.real = real
|
||||
self.imag = imag
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
// DSPSplitComplex.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Portable mirror of Accelerate's split-complex descriptor: separate pointers
|
||||
// to the real and imaginary component arrays. Only defined off Apple (on Apple
|
||||
// the real type is re-exported by AccelerateShim.swift).
|
||||
|
||||
#if !canImport(Accelerate)
|
||||
|
||||
public struct DSPSplitComplex {
|
||||
public var realp: UnsafeMutablePointer<Float>
|
||||
public var imagp: UnsafeMutablePointer<Float>
|
||||
public init(
|
||||
realp: UnsafeMutablePointer<Float>,
|
||||
imagp: UnsafeMutablePointer<Float>
|
||||
) {
|
||||
self.realp = realp
|
||||
self.imagp = imagp
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
// PortableBiquad.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Portable stand-ins for Accelerate's opaque-setup biquad API, used off Apple
|
||||
// platforms (on Apple the real vDSP is re-exported by AccelerateShim.swift).
|
||||
// The setup object holds a copy of the packed coefficients; the per-block work
|
||||
// delegates to the parity-tested `_lbRunBiquad` core. Only the `Double`
|
||||
// (`…D`) variants are provided — the codebase uses no float biquad.
|
||||
|
||||
#if !canImport(Accelerate)
|
||||
|
||||
/// Opaque handle to a biquad coefficient setup, matching Accelerate's type.
|
||||
public typealias vDSP_biquad_SetupD = OpaquePointer
|
||||
|
||||
/// Heap-boxed coefficient store behind the opaque handle.
|
||||
///
|
||||
/// Coefficients live in a raw allocation rather than a Swift `Array` because
|
||||
/// the per-block path reads them on the render thread: reaching into an array
|
||||
/// there would retain and release it once per block, which is exactly the ARC
|
||||
/// traffic the RT rules exclude.
|
||||
private final class _LBBiquadSetup {
|
||||
let coeffs: UnsafeMutablePointer<Double>
|
||||
let sectionCount: Int
|
||||
|
||||
init(coefficients: UnsafePointer<Double>, sectionCount: Int) {
|
||||
let count: Int = sectionCount * coefficientsPerSection
|
||||
let storage: UnsafeMutablePointer<Double> = .allocate(capacity: count)
|
||||
storage.initialize(from: coefficients, count: count)
|
||||
self.coeffs = storage
|
||||
self.sectionCount = sectionCount
|
||||
}
|
||||
|
||||
deinit {
|
||||
coeffs.deinitialize(count: sectionCount * coefficientsPerSection)
|
||||
coeffs.deallocate()
|
||||
}
|
||||
}
|
||||
|
||||
/// A biquad section is `[b0, b1, b2, a1, a2]`.
|
||||
private let coefficientsPerSection: Int = 5
|
||||
|
||||
/// Builds a setup from `m` sections of `[b0, b1, b2, a1, a2]` coefficients.
|
||||
/// Allocates — call off the RT thread (as vDSP requires too).
|
||||
public func vDSP_biquad_CreateSetupD(
|
||||
_ coefficients: UnsafePointer<Double>,
|
||||
_ m: vDSP_Length
|
||||
) -> vDSP_biquad_SetupD? {
|
||||
let box: _LBBiquadSetup = _LBBiquadSetup(coefficients: coefficients, sectionCount: Int(m))
|
||||
return vDSP_biquad_SetupD(Unmanaged.passRetained(box).toOpaque())
|
||||
}
|
||||
|
||||
/// Releases a setup created by ``vDSP_biquad_CreateSetupD(_:_:)``.
|
||||
public func vDSP_biquad_DestroySetupD(_ setup: vDSP_biquad_SetupD) {
|
||||
Unmanaged<_LBBiquadSetup>.fromOpaque(UnsafeRawPointer(setup)).release()
|
||||
}
|
||||
|
||||
/// Runs the biquad cascade over `n` samples. `delays` must be caller-allocated
|
||||
/// (`2 * sectionCount + 2` doubles) and zero-initialised before first use.
|
||||
public func vDSP_biquadD(
|
||||
_ setup: vDSP_biquad_SetupD,
|
||||
_ delays: UnsafeMutablePointer<Double>,
|
||||
_ input: UnsafePointer<Double>,
|
||||
_ iStride: vDSP_Stride,
|
||||
_ output: UnsafeMutablePointer<Double>,
|
||||
_ oStride: vDSP_Stride,
|
||||
_ n: vDSP_Length
|
||||
) {
|
||||
let box: _LBBiquadSetup = Unmanaged<_LBBiquadSetup>.fromOpaque(UnsafeRawPointer(setup)).takeUnretainedValue()
|
||||
_lbRunBiquad(box.coeffs, box.sectionCount, delays, input, Int(iStride), output, Int(oStride), Int(n))
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -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
|
||||
@@ -0,0 +1,505 @@
|
||||
// ScalarDSP.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
|
||||
// Portable scalar implementations of the vector primitives Lyrebird uses from
|
||||
// Accelerate's vDSP. These are compiled on **every** platform (not behind an
|
||||
// `#if`) so they can be parity-tested against the real vDSP on Apple — the test
|
||||
// is what guarantees the non-Apple fallback matches Accelerate's semantics,
|
||||
// including the operand-order quirks (`vsub`/`vdiv` take the divisor/subtrahend
|
||||
// first). On Apple the public `vDSP_*` names resolve to Accelerate via
|
||||
// `@_exported import Accelerate`; on other platforms the shims in
|
||||
// `AccelerateShim.swift` delegate here.
|
||||
//
|
||||
// Each core is generic over `BinaryFloatingPoint` so the `Double` (`…D`) and
|
||||
// `Float` vDSP variants share one implementation and one tested code path.
|
||||
|
||||
// MARK: - Strided element cores
|
||||
|
||||
@inline(__always)
|
||||
func _lbClear<T: BinaryFloatingPoint>(
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = 0
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
func _lbFill<T: BinaryFloatingPoint>(
|
||||
_ scalar: T,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = scalar
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `c = a * scalar`
|
||||
@inline(__always)
|
||||
func _lbVScalarMul<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ scalar: T,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = a[ai] * scalar
|
||||
ai += ia
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `c = a + b` (elementwise)
|
||||
@inline(__always)
|
||||
func _lbVAdd<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ b: UnsafePointer<T>,
|
||||
_ ib: Int,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var bi: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = a[ai] + b[bi]
|
||||
ai += ia
|
||||
bi += ib
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `c = a * b` (elementwise)
|
||||
@inline(__always)
|
||||
func _lbVMul<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ b: UnsafePointer<T>,
|
||||
_ ib: Int,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var bi: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = a[ai] * b[bi]
|
||||
ai += ia
|
||||
bi += ib
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `c = a - b` (elementwise; minuend `a`, subtrahend `b`)
|
||||
@inline(__always)
|
||||
func _lbVSub<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ b: UnsafePointer<T>,
|
||||
_ ib: Int,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var bi: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = a[ai] - b[bi]
|
||||
ai += ia
|
||||
bi += ib
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `c = a + scalar`
|
||||
@inline(__always)
|
||||
func _lbVScalarAdd<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ scalar: T,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = a[ai] + scalar
|
||||
ai += ia
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `d = a * mul + add` (both scalars)
|
||||
@inline(__always)
|
||||
func _lbVScalarMulScalarAdd<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ mul: T,
|
||||
_ add: T,
|
||||
_ d: UnsafeMutablePointer<T>,
|
||||
_ id: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var di: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
d[di] = a[ai] * mul + add
|
||||
ai += ia
|
||||
di += id
|
||||
}
|
||||
}
|
||||
|
||||
/// `d = a * mul + c` (scalar `mul`, vector `c`)
|
||||
@inline(__always)
|
||||
func _lbVScalarMulVAdd<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ mul: T,
|
||||
_ c: UnsafePointer<T>,
|
||||
_ ic: Int,
|
||||
_ d: UnsafeMutablePointer<T>,
|
||||
_ id: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var ci: Int = 0
|
||||
var di: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
d[di] = a[ai] * mul + c[ci]
|
||||
ai += ia
|
||||
ci += ic
|
||||
di += id
|
||||
}
|
||||
}
|
||||
|
||||
/// `c = |a|`
|
||||
@inline(__always)
|
||||
func _lbVAbs<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = abs(a[ai])
|
||||
ai += ia
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `c = a / scalar`
|
||||
@inline(__always)
|
||||
func _lbVScalarDiv<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ scalar: T,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = a[ai] / scalar
|
||||
ai += ia
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `c = a / b` (elementwise)
|
||||
@inline(__always)
|
||||
func _lbVDiv<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ b: UnsafePointer<T>,
|
||||
_ ib: Int,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var bi: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = a[ai] / b[bi]
|
||||
ai += ia
|
||||
bi += ib
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `c = scalar / b`
|
||||
@inline(__always)
|
||||
func _lbScalarVDiv<T: BinaryFloatingPoint>(
|
||||
_ scalar: T,
|
||||
_ b: UnsafePointer<T>,
|
||||
_ ib: Int,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var bi: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = scalar / b[bi]
|
||||
bi += ib
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `c[i] = start + i * step`
|
||||
@inline(__always)
|
||||
func _lbVRamp<T: BinaryFloatingPoint>(
|
||||
_ start: T,
|
||||
_ step: T,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ci: Int = 0
|
||||
var value: T = start
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = value
|
||||
value += step
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// `d = a + t * (b - a)` (linear interpolation; scalar `t`)
|
||||
@inline(__always)
|
||||
func _lbVInterp<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ b: UnsafePointer<T>,
|
||||
_ ib: Int,
|
||||
_ t: T,
|
||||
_ d: UnsafeMutablePointer<T>,
|
||||
_ id: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var bi: Int = 0
|
||||
var di: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
d[di] = a[ai] + t * (b[bi] - a[ai])
|
||||
ai += ia
|
||||
bi += ib
|
||||
di += id
|
||||
}
|
||||
}
|
||||
|
||||
/// `c[i] = max(a[i], threshold)` (low-clamp; matches `vDSP_vthr`)
|
||||
@inline(__always)
|
||||
func _lbVThreshold<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ threshold: T,
|
||||
_ c: UnsafeMutablePointer<T>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
let v: T = a[ai]
|
||||
c[ci] = v > threshold ? v : threshold
|
||||
ai += ia
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `max(|a[i]|)`
|
||||
@inline(__always)
|
||||
func _lbMaxMagnitude<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ n: Int
|
||||
) -> T {
|
||||
var ai: Int = 0
|
||||
var result: T = 0
|
||||
for _: Int in 0..<n {
|
||||
let m: T = abs(a[ai])
|
||||
if m > result { result = m }
|
||||
ai += ia
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/// Returns `sum(a[i])`
|
||||
@inline(__always)
|
||||
func _lbSum<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ n: Int
|
||||
) -> T {
|
||||
var ai: Int = 0
|
||||
var result: T = 0
|
||||
for _: Int in 0..<n {
|
||||
result += a[ai]
|
||||
ai += ia
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/// Returns `sum(a[i] * b[i])` (dot product)
|
||||
@inline(__always)
|
||||
func _lbDot<T: BinaryFloatingPoint>(
|
||||
_ a: UnsafePointer<T>,
|
||||
_ ia: Int,
|
||||
_ b: UnsafePointer<T>,
|
||||
_ ib: Int,
|
||||
_ n: Int
|
||||
) -> T {
|
||||
var ai: Int = 0
|
||||
var bi: Int = 0
|
||||
var result: T = 0
|
||||
for _: Int in 0..<n {
|
||||
result += a[ai] * b[bi]
|
||||
ai += ia
|
||||
bi += ib
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/// `output[i] = input[i] · (start + i·step)` and advances `*start` to
|
||||
/// `start + n·step` (`vDSP_vrampmul`; callers rely on the start mutation).
|
||||
@inline(__always)
|
||||
func _lbRampMul(
|
||||
_ input: UnsafePointer<Float>,
|
||||
_ iStride: Int,
|
||||
_ start: UnsafeMutablePointer<Float>,
|
||||
_ step: Float,
|
||||
_ output: UnsafeMutablePointer<Float>,
|
||||
_ oStride: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var s: Float = start.pointee
|
||||
var ii: Int = 0
|
||||
var oi: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
output[oi] = input[ii] * s
|
||||
s += step
|
||||
ii += iStride
|
||||
oi += oStride
|
||||
}
|
||||
start.pointee = s
|
||||
}
|
||||
|
||||
/// Periodic Hann window `w[i] = 0.5·(1 − cos(2πi/n))` (`vDSP_hann_window` with
|
||||
/// `vDSP_HANN_DENORM`; vDSP divides by `n`, not `n−1`).
|
||||
@inline(__always)
|
||||
func _lbHannWindow(_ output: UnsafeMutablePointer<Float>, _ n: Int) {
|
||||
if n <= 0 { return }
|
||||
let denom: Float = Float(n)
|
||||
let twoPi: Float = 2 * Float.pi
|
||||
for i: Int in 0..<n {
|
||||
output[i] = 0.5 * (1 - cos(twoPi * Float(i) / denom))
|
||||
}
|
||||
}
|
||||
|
||||
/// `c[i] = Float(a[i])` — double→single-precision conversion (`vDSP_vdpsp`).
|
||||
@inline(__always)
|
||||
func _lbConvertDoubleToFloat(
|
||||
_ a: UnsafePointer<Double>,
|
||||
_ ia: Int,
|
||||
_ c: UnsafeMutablePointer<Float>,
|
||||
_ ic: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
var ai: Int = 0
|
||||
var ci: Int = 0
|
||||
for _: Int in 0..<n {
|
||||
c[ci] = Float(a[ai])
|
||||
ai += ia
|
||||
ci += ic
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs a cascade of `m` biquad sections over `n` samples (`vDSP_biquadD`).
|
||||
///
|
||||
/// Coefficients are packed `[b0, b1, b2, a1, a2]` per section; `delays` holds
|
||||
/// two state values per section at `[2s]`/`[2s+1]` (vDSP allocates `2m+2`, the
|
||||
/// trailing pair is unused here). Realises Direct Form II with vDSP's sign
|
||||
/// convention `y = b0·x + b1·x₁ + b2·x₂ − a1·y₁ − a2·y₂`, so from zero-initial
|
||||
/// state its output matches Accelerate's for identical coefficients.
|
||||
@inline(__always)
|
||||
func _lbRunBiquad(
|
||||
_ coeffs: UnsafePointer<Double>,
|
||||
_ m: Int,
|
||||
_ delays: UnsafeMutablePointer<Double>,
|
||||
_ input: UnsafePointer<Double>,
|
||||
_ iStride: Int,
|
||||
_ output: UnsafeMutablePointer<Double>,
|
||||
_ oStride: Int,
|
||||
_ n: Int
|
||||
) {
|
||||
for sample: Int in 0..<n {
|
||||
var x: Double = input[sample * iStride]
|
||||
for s: Int in 0..<m {
|
||||
let c: Int = s * 5
|
||||
let b0: Double = coeffs[c]
|
||||
let b1: Double = coeffs[c + 1]
|
||||
let b2: Double = coeffs[c + 2]
|
||||
let a1: Double = coeffs[c + 3]
|
||||
let a2: Double = coeffs[c + 4]
|
||||
let d0: Double = delays[2 * s]
|
||||
let d1: Double = delays[2 * s + 1]
|
||||
let w: Double = x - a1 * d0 - a2 * d1
|
||||
let y: Double = b0 * w + b1 * d0 + b2 * d1
|
||||
delays[2 * s + 1] = d0
|
||||
delays[2 * s] = w
|
||||
x = y
|
||||
}
|
||||
output[sample * oStride] = x
|
||||
}
|
||||
}
|
||||
|
||||
/// Row-major matrix multiply `C[M×N] = A[M×P] · B[P×N]` (`vDSP_mmulD`).
|
||||
/// `sa`/`sb`/`sc` are element strides applied to the flat row-major indices.
|
||||
@inline(__always)
|
||||
func _lbMatMul(
|
||||
_ a: UnsafePointer<Double>,
|
||||
_ sa: Int,
|
||||
_ b: UnsafePointer<Double>,
|
||||
_ sb: Int,
|
||||
_ c: UnsafeMutablePointer<Double>,
|
||||
_ sc: Int,
|
||||
_ m: Int,
|
||||
_ n: Int,
|
||||
_ p: Int
|
||||
) {
|
||||
for i: Int in 0..<m {
|
||||
for j: Int in 0..<n {
|
||||
var acc: Double = 0
|
||||
for k: Int in 0..<p {
|
||||
acc += a[(i * p + k) * sa] * b[(k * n + j) * sb]
|
||||
}
|
||||
c[(i * n + j) * sc] = acc
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user