Files
Lyrebird/Sources/Lyrebird/Generators/Panning/SpatialDistanceHelpers.swift
T
2026-06-01 18:27:40 -07:00

122 lines
4.4 KiB
Swift

// SpatialDistanceHelpers.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
// MARK: - Shared per-sample pipeline for PanXYDistance / PanAzDistance
/// Per-sample distance-shaping + delay-line + lowpass pipeline shared between
/// ``PanXYDistance`` (Cartesian) and ``PanAzDistance`` (polar). Both callers
/// pre-compute `(panX, panY)` and the Chebyshev distance metric `posD` from
/// their respective coordinate frames; the rest of the pipeline (amplitude
/// attenuation, cutoff coefficient caching, Doppler read-rate, delay-line
/// write/read/wrap/clamp, one-pole lowpass, equal-power 4-corner pan) is
/// identical and lives here.
///
/// Mutates the delay-line and one-pole state in place via `inout`. Writes one
/// sample of the 4-channel output into `result[ch][sampleIndex]` for each of
/// the four channels via the shared `writeEqualPowerSquarePanSample` helper.
///
/// `@inline(__always)` so the generated code is identical to inlined form.
@inline(__always)
internal func applySpatialDistanceFiltering(
into result: inout [[Double]],
sampleIndex: Int,
dry: Double,
velocity: Double,
posD: Double,
panX: Double,
panY: Double,
delayLine: inout [Double],
writePos: inout Int,
readPos: inout Double,
lpZ1: inout Double,
lastCutoff: inout Double,
lastLPCoeff: inout Double,
delayLineCount: Int,
wrapOffset: Int,
maxDelaySamples: Double,
airAbsorptionCutoff: Double,
maxCutoffHz: Double,
speedOfSoundRecip: Double,
sampleRateRecip: Double
) {
// 3. Amplitude attenuation: 1 / Chebyshev distance (clamped so distances
// inside the unit square yield amp = 1).
let posDClamped: Double = max(posD, 1.0)
let amp: Double = 1.0 / posDClamped
// 4. Air-absorption cutoff: falls as amp².
let cutoffNorm: Double = amp * amp
let rawCutoff: Double = airAbsorptionCutoff * cutoffNorm
let cutoff: Double = max(spatialDistanceMinCutoffHz, min(maxCutoffHz, rawCutoff))
// 5. Doppler read rate: 1 - velocity / speedOfSound, clamped.
let rawRate: Double = 1.0 - velocity * speedOfSoundRecip
let readRate: Double = max(spatialDistanceMinReadRate, min(spatialDistanceMaxReadRate, rawRate))
// 6. Write input into delay line and advance write pointer.
delayLine[writePos] = dry
writePos = (writePos + 1) % delayLineCount
// 7. Advance read pointer; wrap into [0, delayLineCount).
readPos += readRate
if readPos >= Double(delayLineCount) {
readPos = readPos.truncatingRemainder(dividingBy: Double(delayLineCount))
} else if readPos < 0 {
readPos = readPos + Double(wrapOffset)
readPos = readPos.truncatingRemainder(dividingBy: Double(delayLineCount))
}
// Compute current delay length and clamp to [1, delayLineCount - 2].
let delayLen: Double = Double(writePos) - readPos
let adjustedDelay: Double
if delayLen < 0 {
adjustedDelay = delayLen + Double(delayLineCount)
} else {
adjustedDelay = delayLen
}
if adjustedDelay < spatialDistanceMinDelaySamples {
readPos = Double(writePos) - spatialDistanceMinDelaySamples
if readPos < 0 { readPos += Double(delayLineCount) }
} else if adjustedDelay > maxDelaySamples {
readPos = Double(writePos) - maxDelaySamples
if readPos < 0 { readPos += Double(delayLineCount) }
}
// 8. Linear-interpolated delay read.
let basePos: Int = Int(readPos)
let frac: Double = readPos - Double(basePos)
let p0: Int = ((basePos % delayLineCount) + delayLineCount) % delayLineCount
let p1: Int = (p0 + 1) % delayLineCount
let delayed: Double = delayLine[p0] * (1.0 - frac) + delayLine[p1] * frac
// 9. One-pole lowpass with cutoff-coefficient caching.
let a: Double
if cutoff == lastCutoff {
a = lastLPCoeff
} else {
a = exp(-spatialDistanceTwoPi * cutoff * sampleRateRecip)
lastCutoff = cutoff
lastLPCoeff = a
}
let oneMinusA: Double = 1.0 - a
let lp: Double = oneMinusA * delayed + a * lpZ1
lpZ1 = denormalsToZero(lp)
// 10. Distance-shaped mono signal.
let wet: Double = lp * amp
// 11. Pan into four channels via the shared equal-power helper.
writeEqualPowerSquarePanSample(
into: &result,
sampleIndex: sampleIndex,
source: wet,
x: panX,
y: panY
)
}