Files
Lyrebird/Sources/Lyrebird/Generators/Analysis/LoudnessConstants.swift
T
2026-06-21 11:45:58 -07:00

170 lines
6.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// LoudnessConstants.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
// MARK: - General
/// Default dBFS→SPL calibration offset (dB) added to each bin's power level.
/// Chosen so a full-scale sinusoid lands near the top of the musical SPL range.
public let loudnessDefaultCalibrationDB: Double = 90.0
/// Floor added inside `log10` to guard the silence case (`log10(0)`).
public let loudnessTinyPower: Double = 1.0e-30
/// Decade factor for dB ↔ power/intensity conversions (`L = 10·log10(P)`,
/// `P = 10^(L/10)`).
public let loudnessDBDecadeFactor: Double = 10.0
/// Trapezoidal-rule factor (`½`) for the upper-slope area integration.
public let loudnessTrapezoidalFactor: Double = 0.5
/// Exponent for the third-octave half-bandwidth ratio `2^(1/6)`.
public let loudnessThirdOctaveHalfBandwidthExponent: Double = 1.0 / 6.0
/// Stevens' power-law exponent on intensity for the `.barkEstimate` model
/// (`N ∝ I^0.3` → +10 dB ≈ 2×).
public let loudnessStevensExponent: Double = 0.3
/// Output scaling for `.barkEstimate` so a 1 kHz tone near 40 dB SPL lands near
/// 1 sone (the model has no equal-loudness contour, so this is an anchor fit:
/// `(10^(40/10))^0.3 ≈ 15.85`, so the scale is `≈ 1/15.85`).
public let loudnessBarkEstimateScale: Double = 0.0631
/// Zwicker specific-loudness constant (`0.0635 = 0.0635`). Specific loudness
/// `N' = const · 10^(0.025·LTQ) · ((1 − s + s·10^(0.1·(L−LTQ)))^0.25 − 1)`.
public let loudnessZwickerConst: Double = 0.0635
/// Exponent in the Zwicker specific-loudness law (`^0.25`).
public let loudnessZwickerExponent: Double = 0.25
/// `10^(0.025·LTQ)` coefficient exponent factor.
public let loudnessZwickerThresholdCoeff: Double = 0.025
/// `10^(0.1·(L−LTQ))` level-to-power exponent factor (dB → power decade).
public let loudnessLevelToPowerCoeff: Double = 0.1
/// Default Zwicker spectral-masking constant `s` for `.realtimeZwicker` (the
/// standard's fixed value). SC `Loudness` `smask` default.
public let loudnessDefaultSMask: Double = 0.25
/// Default temporal-mask release time (seconds) for `.realtimeZwicker`.
public let loudnessDefaultTMask: Double = 1.0
// MARK: - Third-octave band layout (ISO 532 B input grid)
/// Number of third-octave bands (25 Hz … 12.5 kHz).
public let loudnessISOThirdOctaveCount: Int = 28
/// Third-octave band centre frequencies (Hz).
public let loudnessISOCenterFreqsHz: [Double] = [
25, 31.5, 40, 50, 63, 80, 100, 125, 160, 200, 250, 315,
400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150,
4000, 5000, 6300, 8000, 10000, 12500
]
/// Number of Zwicker critical bands used by the core-loudness stage (the upper
/// 20 third-octave bands, with the lowest three formed from sub-band sums).
public let loudnessCriticalBandCount: Int = 20
/// Number of low third-octave bands (25 … 250 Hz) that get the `RAP`/`DLL`
/// equal-loudness correction before being combined into the first three
/// critical bands.
public let loudnessISOLowBandCount: Int = 11
// MARK: - ISO 532 B core-loudness tables
// Transcribed from the published DIN 45631 / Zwicker (1991) algorithm via the
// Apache-2.0 MoSQITo implementation. Data only — facts of the standard.
/// Ranges of 1/3-octave band levels for low-frequency correction (dB).
public let loudnessISORAP: [Double] = [45, 55, 65, 71, 80, 90, 100, 120]
/// Reduction of the lowest 11 1/3-octave band levels within the eight `RAP`
/// ranges (8 rows × 11 columns), per the equal-loudness contours.
public let loudnessISODLL: [[Double]] = [
[-32, -24, -16, -10, -5, 0, -7, -3, 0, -2, 0],
[-29, -22, -15, -10, -4, 0, -7, -2, 0, -2, 0],
[-27, -19, -14, -9, -4, 0, -6, -2, 0, -2, 0],
[-25, -17, -12, -9, -3, 0, -5, -2, 0, -2, 0],
[-23, -16, -11, -7, -3, 0, -4, -1, 0, -1, 0],
[-20, -14, -10, -6, -3, 0, -4, -1, 0, -1, 0],
[-18, -12, -9, -6, -2, 0, -3, -1, 0, -1, 0],
[-15, -10, -8, -4, -2, 0, -3, -1, 0, -1, 0]
]
/// Critical-band level at absolute threshold without the ear transfer (dB), per
/// the 20 critical bands.
public let loudnessISOLTQ: [Double] = [
30, 18, 12, 8, 7, 6, 5, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
]
/// Ear-transmission level correction (dB), per critical band.
public let loudnessISOA0: [Double] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.5, -1.6, -3.2, -5.4, -5.6, -4, -1.5, 2, 5, 12
]
/// Free-field → diffuse-field level difference (dB), per critical band.
public let loudnessISODDF: [Double] = [
0, 0, 0.5, 0.9, 1.2, 1.6, 2.3, 2.8, 3, 2,
0, -1.4, -2, -1.9, -1, 0.5, 3, 4, 4.3, 4
]
/// Adaptation of 1/3-octave band levels to the critical-band level (dB).
public let loudnessISODCB: [Double] = [
-0.25, -0.6, -0.8, -0.8, -0.5, 0, 0.5, 1.1, 1.5, 1.7,
1.8, 1.8, 1.7, 1.6, 1.4, 1.2, 0.8, 0.5, 0, -0.5
]
/// Core-loudness correction `s` in the specific-loudness law.
public let loudnessISOCoreS: Double = 0.25
// MARK: - ISO 532 B upper-slope / integration tables
/// Upper limits of the approximated critical bands in Bark (21 values).
public let loudnessISOZUP: [Double] = [
0.9, 1.8, 2.8, 3.5, 4.4, 5.4, 6.6, 7.9, 9.2, 10.6, 12.3,
13.8, 15.2, 16.7, 18.1, 19.3, 20.6, 21.8, 22.7, 23.6, 24
]
/// Specific-loudness range thresholds for upper-slope steepness selection (18,
/// descending).
public let loudnessISORNS: [Double] = [
21.5, 18, 15.1, 11.5, 9, 6.1, 4.4, 3.1, 2.13, 1.36,
0.82, 0.42, 0.30, 0.22, 0.15, 0.10, 0.035, 0
]
/// Steepness of the upper slopes (18 ranges × 8 critical-band columns).
public let loudnessISOUSL: [[Double]] = [
[13, 8.2, 6.3, 5.5, 5.5, 5.5, 5.5, 5.5],
[9, 7.5, 6, 5.1, 4.5, 4.5, 4.5, 4.5],
[7.8, 6.7, 5.6, 4.9, 4.4, 3.9, 3.9, 3.9],
[6.2, 5.4, 4.6, 4.0, 3.5, 3.2, 3.2, 3.2],
[4.5, 3.8, 3.6, 3.2, 2.9, 2.7, 2.7, 2.7],
[3.7, 3.0, 2.8, 2.35, 2.2, 2.2, 2.2, 2.2],
[2.9, 2.3, 2.1, 1.9, 1.8, 1.7, 1.7, 1.7],
[2.4, 1.7, 1.5, 1.35, 1.3, 1.3, 1.3, 1.3],
[1.95, 1.45, 1.3, 1.15, 1.1, 1.1, 1.1, 1.1],
[1.5, 1.2, 0.94, 0.86, 0.82, 0.82, 0.82, 0.82],
[0.72, 0.67, 0.64, 0.63, 0.62, 0.62, 0.62, 0.62],
[0.59, 0.53, 0.51, 0.50, 0.42, 0.42, 0.42, 0.42],
[0.40, 0.33, 0.26, 0.24, 0.24, 0.22, 0.22, 0.22],
[0.27, 0.21, 0.20, 0.18, 0.17, 0.17, 0.17, 0.17],
[0.16, 0.15, 0.14, 0.12, 0.11, 0.11, 0.11, 0.11],
[0.12, 0.11, 0.10, 0.08, 0.08, 0.08, 0.08, 0.08],
[0.09, 0.08, 0.07, 0.06, 0.06, 0.06, 0.06, 0.05],
[0.06, 0.05, 0.03, 0.02, 0.02, 0.02, 0.02, 0.02]
]
/// `korry` low-band threshold-dependence correction constants
/// (`0.4 + 0.32·N^0.2`).
public let loudnessKorryBase: Double = 0.4
public let loudnessKorryScale: Double = 0.32
public let loudnessKorryExponent: Double = 0.2
/// Maximum third-octave band level (dB) above which the Zwicker method is
/// undefined for the low bands; input is clamped to this.
public let loudnessMaxBandLevelDB: Double = 120.0