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>
115 lines
4.2 KiB
Swift
115 lines
4.2 KiB
Swift
// SpectralBlur.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import LyrebirdDSP
|
|
import Synchronization
|
|
|
|
/// Magnitude moving-average ("box blur") across complex bins. Smears spectral
|
|
/// peaks into plateaus of width `2 · radius + 1`. Phase is untouched.
|
|
///
|
|
/// Edge bins shrink the window rather than wrap or pad: bin `k` averages
|
|
/// `_mag[max(1, k - r) ... min(halfSize - 1, k + r)]`. This avoids artefacts
|
|
/// from wrapping low frequencies into high or vice versa.
|
|
///
|
|
/// `radius = 0` is bypass. Computation uses a running sum, so cost is O(halfSize)
|
|
/// per hop regardless of `r`.
|
|
///
|
|
/// `radius` may be retuned from any thread; the RT thread reads the latest value
|
|
/// (clamped to `[0, halfSize / 2]`) on the next FFT hop. Storage `_blurOut` is
|
|
/// pre-allocated; no allocation in `process`.
|
|
///
|
|
/// - Parameters:
|
|
/// - fftSize: Must match the paired `SpectralChain.Audio.fftSize`. Power of two ≥ 4.
|
|
/// - radius: Initial half-window radius in bins. Clamped to `[0, halfSize / 2]`.
|
|
/// Defaults to `0` (bypass).
|
|
public final class SpectralBlur: SpectralProcessor {
|
|
|
|
private let _fftSize: Int
|
|
private let _halfSize: Int
|
|
private let _binCount: Int
|
|
|
|
private let _radius: Atomic<Int>
|
|
private let _blurOut: UnsafeMutablePointer<Float> // [_binCount]
|
|
|
|
/// Half-window radius in bins. Updates take effect on the next hop. Clamped
|
|
/// to `[0, halfSize / 2]`.
|
|
public var radius: Int {
|
|
get { _radius.load(ordering: .acquiring) }
|
|
set {
|
|
let clamped: Int = max(0, min(_halfSize / 2, newValue))
|
|
_radius.store(clamped, ordering: .releasing)
|
|
}
|
|
}
|
|
|
|
/// - Parameters:
|
|
/// - fftSize: Must match the paired `SpectralChain.Audio.fftSize`. Power of two ≥ 4.
|
|
/// - radius: Initial half-window radius in bins. Clamped to `[0, halfSize / 2]`.
|
|
/// Defaults to `0` (bypass).
|
|
public init(
|
|
fftSize: Int,
|
|
radius: Int = 0
|
|
) {
|
|
precondition(fftSize >= 4 && (fftSize & (fftSize - 1)) == 0,
|
|
"SpectralBlur: fftSize must be a power of two >= 4, got \(fftSize)")
|
|
_fftSize = fftSize
|
|
_halfSize = fftSize / 2
|
|
_binCount = max(0, _halfSize - 1)
|
|
|
|
let clamped: Int = max(0, min(_halfSize / 2, radius))
|
|
_radius = Atomic(clamped)
|
|
|
|
let allocCount: Int = max(_binCount, 1)
|
|
_blurOut = UnsafeMutablePointer<Float>.allocate(capacity: allocCount)
|
|
_blurOut.initialize(repeating: 0, count: allocCount)
|
|
}
|
|
|
|
deinit {
|
|
let allocCount: Int = max(_binCount, 1)
|
|
_blurOut.deinitialize(count: allocCount)
|
|
_blurOut.deallocate()
|
|
}
|
|
|
|
public func process(spectral: SpectralBuffer, engine: SynthesisEngine) {
|
|
guard spectral.fftSize == _fftSize else { return }
|
|
guard spectral.isValid else { return }
|
|
|
|
let r: Int = _radius.load(ordering: .acquiring)
|
|
if r == 0 || _binCount == 0 { return }
|
|
|
|
let n: Int = _binCount
|
|
// Compute moving average over _mag[0 ..< n] (which is bins 1 ..< halfSize)
|
|
// into _blurOut[0 ..< n], then copy back.
|
|
spectral.withUnsafeMutableMagnitudePointer { mp in
|
|
// Build prefix sum incrementally; for each output i, window is
|
|
// [max(0, i - r), min(n - 1, i + r)] inclusive.
|
|
// Use straightforward two-pointer running sum.
|
|
var lo: Int = 0
|
|
var hi: Int = -1
|
|
var sum: Float = 0
|
|
for i: Int in 0..<n {
|
|
let wantLo: Int = Swift.max(0, i - r)
|
|
let wantHi: Int = Swift.min(n - 1, i + r)
|
|
// Extend hi.
|
|
while hi < wantHi {
|
|
hi += 1
|
|
sum += mp[hi]
|
|
}
|
|
// Advance lo.
|
|
while lo < wantLo {
|
|
sum -= mp[lo]
|
|
lo += 1
|
|
}
|
|
let count: Int = wantHi - wantLo + 1
|
|
_blurOut[i] = sum / Float(count)
|
|
}
|
|
// Copy averaged result back.
|
|
mp.update(from: _blurOut, count: n)
|
|
}
|
|
|
|
// DC and Nyquist are not blurred — they have no neighbours in this layout.
|
|
}
|
|
}
|