audio: opt-in loudness equalization of the title's Spatial Audio output (VICTORY_LOUDNESS_EQ) #95

Open
github-bot wants to merge 7 commits from github-bot/gh-94 into main AGit
Owner

@⁠rgroenewoudt on GitHub · 2026-12-05 11:08 UTC · original
rgroenewoudt:loudness-equalization → main · +1213 −16 · 7 files · 7 commits · open

Title has lower audio volume compared to other PC applications/games. Add loudness equalization to add small gain without clipping louder sounds like explosions.

Summary

Opt-in loudness equalization for the title's Spatial Audio output. Quiet sound is raised by up to +2 / +4 / +6 dB (low / medium / high). Loud sound is not raised, and peaks stay under -1 dBFS. It is off by default.

Where it hooks in (only when VICTORY_LOUDNESS_EQ is set):

CoCreateInstance(MMDeviceEnumerator)          # title import, swapped for ours
  IMMDevice::Activate(ISpatialAudioClient)
    ActivateSpatialAudioStream                # F32 / I16 stream → one Leveler
      BeginUpdatingAudioObjects               # this thread starts an update
      ISpatialAudioObject::GetBuffer          # buffers collected, each once
      EndUpdatingAudioObjects                 # Leveler.update(buffers), then the host's End
      Release → 0                             # Leveler dropped

What the leveler does on each update:

update(buffers)
  power, peak = measure every buffer          # non-finite samples skipped
  level  = smooth(power, up 10 ms, down 300 ms)
  wanted = clamp(target - level, 0, max_boost) # held below -60 dBFS
  gain   = smooth(wanted, down 50 ms, up 1.5 s)
  gain   = max(min(gain, ceiling / peak), 0 dB) # ceiling = -1 dBFS
  scale every buffer by that gain             # down at once, up as a ramp

Every object in an update gets the same gain, so the spatial balance between objects stays the same.

Launcher and script:

 Settings → Game
   [Skip starting intro]  [Auto-warm before playing]
   [Button prompts]       [Phone web apps]
+  [Loudness equalization: Off / Low / Medium / High]
   SHADERS & PIPELINE CACHE         # moved down, cache boxes 122 → 94 px

scripts/play.ps1 -LoudnessEq low|medium|high sets the same env var.

Evidence

  • Before (an earlier commit of this branch, fixed gain ×2 = +6 dB, ~150 s of play): in 5 of those seconds samples went over full scale, with peaks up to +6.02 dBFS. The title's own mix was already at 0 dBFS there, and the gain doubled it. Clipping was audible.
    After (-LoudnessEq medium, ~170 s of play including a loud scene of about a minute):
    • 0 samples over full scale, highest peak -2.9 dBFS
    • gain about +4 dB in quiet scenes and about 0 dB in loud ones
    • no audible pumping
    • with the setting off, no hooks are installed and the log has no loudness eq lines
  • cargo test -p victory-hle loudness_eq: 14 pass, including:
    • quiet sound is raised by at most the boost
    • loud sound is not raised, and a loud peak lowers the gain at once
    • sound under the gate is not raised
    • all buffers of one update get the same gain
    • stand-in COM objects: a buffer outside an update is not scaled, a buffer handed out twice is scaled once, an update left without its End scales nothing, and a final Release drops the leveler
    • NaN and Inf samples do not stop the leveler
  • cargo test -p victory launcher: 25 pass. This includes the_settings_are_those_of_the_script, which ties the launcher to play.ps1.

Merge Danger

Door: two-way

Off by default: with VICTORY_LOUDNESS_EQ unset, the CoCreateInstance import is left alone. The only lasting trace is a loudness_eq key in launcher.txt, which older launchers ignore.

Blast Radius: audio

When it is on, it patches vtable slots of process-wide COM classes (IMMDevice::Activate, the Spatial Audio stream and object vtables). Only Spatial Audio streams of F32 or 16-bit PCM are scaled, and the haptics path (IAudioClient) is not touched. I tested it on one machine only (AMD, a 12-object F32 stream). The Settings → Game layout also changed: the cache section moved down and the boxes are shorter. I checked it by eye in the launcher.

The branch history starts with the fixed gain and then replaces it. Squash on merge if you want one commit.

**@⁠rgroenewoudt** on GitHub · 2026-12-05 11:08 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/94) `rgroenewoudt:loudness-equalization` → `main` · +1213 −16 · 7 files · 7 commits · open Title has lower audio volume compared to other PC applications/games. Add loudness equalization to add small gain without clipping louder sounds like explosions. ## Summary Opt-in loudness equalization for the title's Spatial Audio output. Quiet sound is raised by up to +2 / +4 / +6 dB (`low` / `medium` / `high`). Loud sound is not raised, and peaks stay under -1 dBFS. It is off by default. Where it hooks in (only when `VICTORY_LOUDNESS_EQ` is set): ```text CoCreateInstance(MMDeviceEnumerator) # title import, swapped for ours IMMDevice::Activate(ISpatialAudioClient) ActivateSpatialAudioStream # F32 / I16 stream → one Leveler BeginUpdatingAudioObjects # this thread starts an update ISpatialAudioObject::GetBuffer # buffers collected, each once EndUpdatingAudioObjects # Leveler.update(buffers), then the host's End Release → 0 # Leveler dropped ``` What the leveler does on each update: ```text update(buffers) power, peak = measure every buffer # non-finite samples skipped level = smooth(power, up 10 ms, down 300 ms) wanted = clamp(target - level, 0, max_boost) # held below -60 dBFS gain = smooth(wanted, down 50 ms, up 1.5 s) gain = max(min(gain, ceiling / peak), 0 dB) # ceiling = -1 dBFS scale every buffer by that gain # down at once, up as a ramp ``` Every object in an update gets the same gain, so the spatial balance between objects stays the same. Launcher and script: ```diff Settings → Game [Skip starting intro] [Auto-warm before playing] [Button prompts] [Phone web apps] + [Loudness equalization: Off / Low / Medium / High] SHADERS & PIPELINE CACHE # moved down, cache boxes 122 → 94 px ``` `scripts/play.ps1 -LoudnessEq low|medium|high` sets the same env var. ## Evidence - **Before** (an earlier commit of this branch, fixed gain ×2 = +6 dB, ~150 s of play): in 5 of those seconds samples went over full scale, with peaks up to **+6.02 dBFS**. The title's own mix was already at 0 dBFS there, and the gain doubled it. Clipping was audible. **After** (`-LoudnessEq medium`, ~170 s of play including a loud scene of about a minute): - **0** samples over full scale, highest peak **-2.9 dBFS** - gain about +4 dB in quiet scenes and about 0 dB in loud ones - no audible pumping - with the setting off, no hooks are installed and the log has no `loudness eq` lines - `cargo test -p victory-hle loudness_eq`: 14 pass, including: - quiet sound is raised by at most the boost - loud sound is not raised, and a loud peak lowers the gain at once - sound under the gate is not raised - all buffers of one update get the same gain - stand-in COM objects: a buffer outside an update is not scaled, a buffer handed out twice is scaled once, an update left without its End scales nothing, and a final `Release` drops the leveler - NaN and Inf samples do not stop the leveler - `cargo test -p victory launcher`: 25 pass. This includes `the_settings_are_those_of_the_script`, which ties the launcher to `play.ps1`. ## Merge Danger **Door:** two-way Off by default: with `VICTORY_LOUDNESS_EQ` unset, the `CoCreateInstance` import is left alone. The only lasting trace is a `loudness_eq` key in `launcher.txt`, which older launchers ignore. **Blast Radius:** audio When it is on, it patches vtable slots of process-wide COM classes (`IMMDevice::Activate`, the Spatial Audio stream and object vtables). Only Spatial Audio streams of F32 or 16-bit PCM are scaled, and the haptics path (`IAudioClient`) is not touched. I tested it on one machine only (AMD, a 12-object F32 stream). The Settings → Game layout also changed: the cache section moved down and the boxes are shorter. I checked it by eye in the launcher. The branch history starts with the fixed gain and then replaces it. Squash on merge if you want one commit.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 11:13:03 +00:00
Author
Owner

@⁠ReTerryDavis on GitHub · 2026-12-05 17:39 UTC · original

Really clean opt-in implementation and nice in-game verification! Marking as draft for one small thread-safety fix before merge:

  • Concurrent VirtualProtect race (crates/victory-hle/src/loudness_eq.rs, swap): Hook::install locks only its own Hook, not a shared page-protect lock. Since STREAM_BEGIN_HOOK, STREAM_END_HOOK, STREAM_RELEASE_HOOK, and STREAM_OBJECT_HOOK patch vtable slots on the same .rdata page, concurrent installs can have one thread restore PAGE_READONLY right before another's AtomicUsize::swap, causing an access violation. Guarding swap() with a single static Mutex<()> closes that cleanly.

Once that's fixed, mark ready and it's good to merge!

**@⁠ReTerryDavis** on GitHub · 2026-12-05 17:39 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/94#issuecomment-7442370666) Really clean opt-in implementation and nice in-game verification! Marking as draft for one small thread-safety fix before merge: - **Concurrent `VirtualProtect` race (`crates/victory-hle/src/loudness_eq.rs`, `swap`):** `Hook::install` locks only its own `Hook`, not a shared page-protect lock. Since `STREAM_BEGIN_HOOK`, `STREAM_END_HOOK`, `STREAM_RELEASE_HOOK`, and `STREAM_OBJECT_HOOK` patch vtable slots on the same `.rdata` page, concurrent installs can have one thread restore `PAGE_READONLY` right before another's `AtomicUsize::swap`, causing an access violation. Guarding `swap()` with a single static `Mutex<()>` closes that cleanly. Once that's fixed, mark ready and it's good to merge!
github-bot force-pushed github-bot/gh-94 from 9e087662ef to d867437137 2026-12-05 19:24:33 +00:00 Compare
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/95/head:github-bot/gh-94'
git switch 'github-bot/gh-94'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-94'
git switch 'github-bot/gh-94'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-94'
git switch 'github-bot/gh-94'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-94'
git switch 'main'
git merge --squash 'github-bot/gh-94'
git switch 'main'
git merge --ff-only 'github-bot/gh-94'
git switch 'main'
git merge 'github-bot/gh-94'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.