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,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