## Loudness and peak measurements for the generated audio, and the checks every asset ## must pass (tools/audio/check.gd, test_audio_assets): ## ## - the peak stays at or under PEAK_CEILING_DB, so nothing clips; ## - each sound is within TOLERANCE_DB of its target loudness, or under it only because ## the peak ceiling held it down (by no more than LIMITED_SLACK_DB); ## - the mix order from docs/STYLE.md holds: the tide's sounds are the loudest in a ## match, ambience and music sit under the effects, menus are quietest. ## ## Loudness is the RMS of the loudest WINDOW_SECONDS window for one-shots, and of the ## whole file for loops, in dBFS. That is close enough to a momentary loudness meter for ## levelling sounds against each other without a K-weighting filter. extends RefCounted const Dsp = preload("res://tools/audio/dsp.gd") const WINDOW_SECONDS: float = 0.4 const PEAK_CEILING_DB: float = -1.0 const TOLERANCE_DB: float = 1.0 const LIMITED_SLACK_DB: float = 4.0 static func db(linear: float) -> float: return 20.0 * log(maxf(linear, 1e-9)) / log(10.0) static func peak_db(samples: PackedFloat32Array) -> float: var peak: float = 0.0 for value: float in samples: peak = maxf(peak, absf(value)) return db(peak) static func loudness_db(samples: PackedFloat32Array, loops: bool) -> float: if samples.is_empty(): return -180.0 if loops: var total: float = 0.0 for value: float in samples: total += value * value return db(sqrt(total / samples.size())) var window: int = mini(Dsp.n(WINDOW_SECONDS), samples.size()) var running: float = 0.0 for i: int in window: running += samples[i] * samples[i] var best: float = running for i: int in range(window, samples.size()): running += samples[i] * samples[i] - samples[i - window] * samples[i - window] best = maxf(best, running) return db(sqrt(maxf(best, 0.0) / window)) ## The gain that brings `samples` to `target` dBFS without the peak passing the ceiling. static func levelling_gain(samples: PackedFloat32Array, loops: bool, target: float) -> float: var gain_db: float = target - loudness_db(samples, loops) var headroom_db: float = PEAK_CEILING_DB - 0.05 - peak_db(samples) return pow(10.0, minf(gain_db, headroom_db) / 20.0) ## Problems with one measured sound, as sentences; empty when it passes. static func problems(sound: String, peak: float, loudness: float, target: float) -> Array[String]: var found: Array[String] = [] if peak > PEAK_CEILING_DB + 0.01: found.append("%s peaks at %.2f dBFS, over the %.1f ceiling" % [sound, peak, PEAK_CEILING_DB]) if loudness > target + TOLERANCE_DB: found.append("%s is %.1f dBFS, louder than its %.1f target" % [sound, loudness, target]) elif loudness < target - TOLERANCE_DB: var limited: bool = peak >= PEAK_CEILING_DB - 0.3 if not limited or loudness < target - LIMITED_SLACK_DB: found.append("%s is %.1f dBFS, quieter than its %.1f target" % [sound, loudness, target]) return found ## Problems with the mix as a whole, from {sound: {category, loudness}}. static func mix_problems(measured: Dictionary) -> Array[String]: var loudest: Dictionary = {} for sound: String in measured: var category: String = measured[sound]["category"] loudest[category] = maxf(float(loudest.get(category, -180.0)), float(measured[sound]["loudness"])) var found: Array[String] = [] var tide: float = float(loudest.get("tide", -180.0)) for category: String in loudest: if category != "tide" and float(loudest[category]) >= tide: found.append("%s (%.1f dBFS) is as loud as the tide (%.1f)" % [category, loudest[category], tide]) for under: String in ["ambience", "music", "ui"]: for over: String in ["weapon", "impact", "build"]: if loudest.has(under) and loudest.has(over) and float(loudest[under]) >= float(loudest[over]): found.append("%s should sit under %s" % [under, over]) return found