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>
76 lines
2.9 KiB
Swift
76 lines
2.9 KiB
Swift
// 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
|