173 lines
6.7 KiB
Swift
173 lines
6.7 KiB
Swift
// Loudness.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
|
|
/// Psychoacoustic loudness meter — perceived loudness in **sones** from the
|
|
/// magnitude spectrum of a paired analyze-only ``SpectralChain/Audio``.
|
|
///
|
|
/// Three fidelity models are folded into the one generator behind a
|
|
/// ``LoudnessModel`` flag (mirroring the `Balance` `logDomain:` and `BeatTrack`
|
|
/// `BeatTrackODF` folds):
|
|
///
|
|
/// - ``LoudnessModel/barkEstimate`` — cheap Stevens power-law band estimate.
|
|
/// - ``LoudnessModel/realtimeZwicker(smask:tmask:)`` — SC `Loudness`-style
|
|
/// critical-band Zwicker loudness with spectral + temporal masking.
|
|
/// - ``LoudnessModel/iso532b(field:)`` — full ISO 532 B / DIN 45631 stationary
|
|
/// Zwicker loudness.
|
|
///
|
|
/// Control-rate, single-row output (sones). See ``Loudness/Control``.
|
|
///
|
|
/// > **Clean-room note.** SuperCollider's `Loudness` (Nick Collins) is GPL. This
|
|
/// > is an independent implementation from the published Zwicker model and the
|
|
/// > ISO 532 B / DIN 45631 standard; output is in sones, not bit-exact with SC.
|
|
public enum Loudness {
|
|
|
|
/// Control-rate loudness meter. Model selected at init via ``LoudnessModel``.
|
|
///
|
|
/// - Parameters:
|
|
/// - model: Loudness model + its parameters. Defaults to
|
|
/// ``LoudnessModel/realtimeZwicker(smask:tmask:)`` with the standard
|
|
/// `smask`/`tmask`.
|
|
/// - analysis: Paired ``SpectralChain/Audio`` (analyze-only). Its
|
|
/// `spectralBuffer` is read once per audio block.
|
|
/// - sampleRate: Sample rate in Hz (bin→frequency mapping + block timing).
|
|
/// - calibrationDB: dBFS→SPL offset. Defaults to ``loudnessDefaultCalibrationDB``.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset (sones). Defaults to `0.0`.
|
|
public final class Control: GeneratorBase, ControlGenerator {
|
|
|
|
private let _analysis: SpectralChain.Audio?
|
|
private let _spectral: SpectralBuffer
|
|
private let _model: LoudnessModel
|
|
private let _core: LoudnessBandCore
|
|
private let _sampleRate: Double
|
|
private var _mulAdd: MulAddSpec
|
|
public var previousOutputValue: Double = 0.0
|
|
private var _rampTarget: Double = 0.0
|
|
|
|
/// - Parameters:
|
|
/// - model: Loudness model + parameters. Defaults to
|
|
/// ``LoudnessModel/realtimeZwicker(smask:tmask:)``.
|
|
/// - analysis: Paired ``SpectralChain/Audio`` (analyze-only).
|
|
/// - sampleRate: Sample rate in Hz.
|
|
/// - calibrationDB: dBFS→SPL offset. Defaults to ``loudnessDefaultCalibrationDB``.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset (sones). Defaults to `0.0`.
|
|
public convenience init(
|
|
model: LoudnessModel = .realtimeZwicker(
|
|
smask: loudnessDefaultSMask, tmask: loudnessDefaultTMask),
|
|
analysis: SpectralChain.Audio,
|
|
sampleRate: Double,
|
|
calibrationDB: Double = loudnessDefaultCalibrationDB,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
self.init(
|
|
model: model,
|
|
analysis: analysis,
|
|
spectral: analysis.spectralBuffer,
|
|
sampleRate: sampleRate,
|
|
calibrationDB: calibrationDB,
|
|
mul: mul,
|
|
add: add
|
|
)
|
|
}
|
|
|
|
/// Buffer-only init. For tests / advanced use; no analysis auto-pull.
|
|
///
|
|
/// - Parameters:
|
|
/// - model: Loudness model + parameters.
|
|
/// - spectral: Spectral frame buffer read each block.
|
|
/// - sampleRate: Sample rate in Hz.
|
|
/// - calibrationDB: dBFS→SPL offset. Defaults to ``loudnessDefaultCalibrationDB``.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset (sones). Defaults to `0.0`.
|
|
public convenience init(
|
|
model: LoudnessModel = .realtimeZwicker(
|
|
smask: loudnessDefaultSMask, tmask: loudnessDefaultTMask),
|
|
spectral: SpectralBuffer,
|
|
sampleRate: Double,
|
|
calibrationDB: Double = loudnessDefaultCalibrationDB,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
self.init(
|
|
model: model,
|
|
analysis: nil,
|
|
spectral: spectral,
|
|
sampleRate: sampleRate,
|
|
calibrationDB: calibrationDB,
|
|
mul: mul,
|
|
add: add
|
|
)
|
|
}
|
|
|
|
private init(
|
|
model: LoudnessModel,
|
|
analysis: SpectralChain.Audio?,
|
|
spectral: SpectralBuffer,
|
|
sampleRate: Double,
|
|
calibrationDB: Double,
|
|
mul: any ValidGeneratorInput,
|
|
add: any ValidGeneratorInput
|
|
) {
|
|
self._analysis = analysis
|
|
self._spectral = spectral
|
|
self._model = model
|
|
self._sampleRate = max(1.0, sampleRate)
|
|
self._core = LoudnessBandCore(
|
|
fftSize: spectral.fftSize,
|
|
sampleRate: sampleRate,
|
|
calibrationDB: calibrationDB,
|
|
binCount: spectral.binCount
|
|
)
|
|
self._mulAdd = MulAddSpec(mul: mul, add: add)
|
|
super.init()
|
|
_setupSubBlockBuffer()
|
|
}
|
|
|
|
// Bypass the sub-block loop: the spectral buffer advances once per FFT
|
|
// hop and the loudness state (temporal mask) advances once per block.
|
|
public func calculatedSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_calculatedSamplesBlockRate(graph: graph, count: count)
|
|
}
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
|
|
_ = _analysis?.calculatedSamples(graph: graph, count: count)
|
|
|
|
let blockSeconds: Double = Double(count) / _sampleRate
|
|
let sones: Double = _core.loudness(
|
|
spectral: _spectral, model: _model, blockSeconds: blockSeconds)
|
|
|
|
let target: Double = sones
|
|
let rampStart: Double = _rampTarget
|
|
_rampTarget = target
|
|
guard count > 0 else { return _result }
|
|
previousOutputValue = fillRampedResult(
|
|
into: &_result,
|
|
from: rampStart,
|
|
to: target,
|
|
mulAdd: _mulAdd,
|
|
graph: graph,
|
|
count: count
|
|
)
|
|
return _result
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - GeneratorEnum
|
|
// GeneratorEnum conformance omitted: Loudness is Control-rate only.
|