1.0.0 release
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
// SpectralTilt.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Accelerate
|
||||
|
||||
/// 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: UnsafeMutablePointer<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 = UnsafeMutablePointer<Float>.allocate(capacity: tableCount)
|
||||
_gainTable.initialize(repeating: 1, count: tableCount)
|
||||
|
||||
// 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))
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
_gainTable.deinitialize(count: _halfSize + 1)
|
||||
_gainTable.deallocate()
|
||||
}
|
||||
|
||||
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.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]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user