844dc4d9cd
Typed throws across the closed-error-set APIs (OSC, MIDI, engine add/schedule, bus/config/parameter, and the WAV/RF64/Spectral file codecs — source-compatible narrowing, new .ioFailure cases), plus internal hardening: NSLock-guarded scheduling/MIDI state moved to Mutex<State>, and the Spectral/FFT scratch owners moved to a ~Copyable RTScratchBuffer RAII buffer. No API or behavioral change. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
98 lines
4.3 KiB
Swift
98 lines
4.3 KiB
Swift
// SpectralTilt.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import LyrebirdDSP
|
|
|
|
/// Per-bin spectral tilt EQ: applies a constant dB/octave slope around a pivot
|
|
/// frequency. Phase is untouched.
|
|
///
|
|
/// The gain table `gain[k] = 2^(slope/6 · log2(binFreq[k] / pivotHz))` is
|
|
/// pre-computed at init from `slopeDBPerOctave` and `pivotHz` and reused on
|
|
/// every hop — `process` is pure `vDSP_vmul` plus two scalar multiplies for
|
|
/// DC and Nyquist. `slope = 0` produces unity gain everywhere.
|
|
///
|
|
/// `slope` and `pivot` are init-only; create a new instance to change them.
|
|
/// No allocation in `process`.
|
|
///
|
|
/// - Parameters:
|
|
/// - fftSize: Must match the paired `SpectralChain.Audio.fftSize`. Power of two ≥ 4.
|
|
/// - sampleRate: Engine sample rate in Hz. Used to compute per-bin frequencies.
|
|
/// - slopeDBPerOctave: Slope in dB/octave. Positive = brightens (HF boost);
|
|
/// negative = darkens (HF cut). `0` = unity gain everywhere.
|
|
/// - pivotHz: Frequency at which gain = 1.0 (0 dB). Must be positive and below
|
|
/// Nyquist. Defaults to `1000.0` Hz. DC bin gain is forced to 1.0 since the
|
|
/// octave-relative slope is undefined at DC.
|
|
public final class SpectralTilt: SpectralProcessor {
|
|
|
|
private let _fftSize: Int
|
|
private let _halfSize: Int
|
|
private let _binCount: Int
|
|
|
|
/// Per-bin gain table sized `halfSize + 1`. Index 0 = DC, halfSize = Nyquist,
|
|
/// 1..halfSize-1 = complex bins. Pre-computed at init.
|
|
private let _gainTable: RTScratchBuffer<Float>
|
|
|
|
/// - Parameters:
|
|
/// - fftSize: Must match the paired `SpectralChain.Audio.fftSize`. Power of two ≥ 4.
|
|
/// - sampleRate: Engine sample rate. Used to compute per-bin frequencies.
|
|
/// - slopeDBPerOctave: Slope in dB/octave. Positive = brightens (HF boost),
|
|
/// negative = darkens (HF cut). Common values: ±3, ±6.
|
|
/// - pivotHz: Frequency at which gain = 1.0 (0 dB). Default 1 kHz. Must be
|
|
/// positive and ≤ Nyquist.
|
|
public init(
|
|
fftSize: Int,
|
|
sampleRate: Double,
|
|
slopeDBPerOctave: Double,
|
|
pivotHz: Double = 1_000.0
|
|
) {
|
|
precondition(fftSize >= 4 && (fftSize & (fftSize - 1)) == 0,
|
|
"SpectralTilt: fftSize must be a power of two >= 4, got \(fftSize)")
|
|
precondition(sampleRate > 0,
|
|
"SpectralTilt: sampleRate must be positive, got \(sampleRate)")
|
|
precondition(pivotHz > 0 && pivotHz < sampleRate * 0.5,
|
|
"SpectralTilt: pivotHz must be in (0, Nyquist), got \(pivotHz)")
|
|
_fftSize = fftSize
|
|
_halfSize = fftSize / 2
|
|
_binCount = _halfSize - 1
|
|
|
|
let tableCount: Int = _halfSize + 1
|
|
_gainTable = RTScratchBuffer<Float>(count: tableCount, repeating: 1)
|
|
|
|
// Build gain table.
|
|
// gain(f) = 2 ^ (slope/6 · log2(f / pivot)) = (f / pivot) ^ (slope/6).
|
|
// 6 dB/oct = doubling per octave; named for clarity.
|
|
let dBPerDoubling: Double = 6.0
|
|
let exponent: Double = slopeDBPerOctave / dBPerDoubling
|
|
let binHz: Double = sampleRate / Double(fftSize)
|
|
// DC bin (index 0): octave-relative slope is undefined at DC. Force
|
|
// unity gain so a steep negative slope does not accidentally amplify
|
|
// (or attenuate) DC by an arbitrary factor derived from a proxy bin.
|
|
_gainTable[0] = 1.0
|
|
for k: Int in 1..._halfSize {
|
|
let f: Double = Double(k) * binHz
|
|
_gainTable[k] = Float(pow(f / pivotHz, exponent))
|
|
}
|
|
}
|
|
|
|
// No deinit: each RTScratchBuffer frees its own allocation exactly once.
|
|
|
|
public func process(spectral: SpectralBuffer, engine: SynthesisEngine) {
|
|
guard spectral.fftSize == _fftSize else { return }
|
|
guard spectral.isValid else { return }
|
|
|
|
// Complex bins: vDSP_vmul gain[1..halfSize-1] * mag[1..halfSize-1].
|
|
if _binCount > 0 {
|
|
let gainComplex: UnsafeMutablePointer<Float> = _gainTable.baseAddress.advanced(by: 1)
|
|
spectral.withUnsafeMutableMagnitudePointer { mp in
|
|
vDSP_vmul(mp, 1, gainComplex, 1, mp, 1, vDSP_Length(_binCount))
|
|
}
|
|
}
|
|
// DC and Nyquist (signed scalars).
|
|
spectral.dcValue *= _gainTable[0]
|
|
spectral.nyquistValue *= _gainTable[_halfSize]
|
|
}
|
|
}
|