dsp: pass speaker filters by their shape, not by size #457

Manually merged
github merged 2 commits from github-bot/gh-457 into gh-pr/457/base 2026-12-05 20:57:12 +00:00 AGit
Owner

@⁠niiniiniintje on GitHub · 2026-12-04 11:43 UTC · original
niiniiniintje:dsp-speaker-shape → main · +151 −6 · 2 files · 2 commits · merged 2026-12-05 20:50 UTC

Problem

Indoors, dialogue and footsteps sound doubled, like a short echo.

Convolutions aren't computed (the coprocessor's impulse-response layout is unknown). As a stand-in, any convolution whose response has at most 65536 floats passes its input through at 5x (b69806a, for the phone speaker), and all others are silent. But that size range also contains about a hundred short room reverbs. Passed through, each one plays a second, louder copy of the dry sound.

Change

Each response is laid out in 2048-float blocks, and each block holds the spectrum of one 1024-frame partition. By Parseval, a block's energy equals its partition's energy, whatever order the bins are in. So the response's energy over time can be measured without knowing the layout. The trailing block (first word 0xFFFFACE1) isn't a partition, so it's left out.

A convolution now passes only if at least 98% of its response's energy is in the first three blocks (~64 ms), which is what a speaker filter looks like. Rooms stay dry, as the other reverbs already were.

To check, I carved all 227 DSP impulse responses (codec 2) out of audio.rpf. They match the in-memory responses byte for byte.

  • 41 pass, all at ≥98%. All the others are under 95%, so the threshold has room on both sides.
  • The 40960-float size group, which the old rule passed entirely, is mixed. Most of its responses have 60–90% of their energy early: short rooms, not speakers.

Each stream shape is logged once at info level with its early share.

Testing

  • Unit tests: a new speaker_filters_pass_and_rooms_do_not, and convolution_stream_as_the_title_drives_it updated to use a speaker-shaped response. Both pass under Wine.
  • In game (RX 7900 XTX, Proton-CachyOS): phone callers are still audible, and speech and footsteps indoors are no longer doubled.
**@⁠niiniiniintje** on GitHub · 2026-12-04 11:43 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/457) `niiniiniintje:dsp-speaker-shape` → `main` · +151 −6 · 2 files · 2 commits · merged 2026-12-05 20:50 UTC ## Problem Indoors, dialogue and footsteps sound doubled, like a short echo. Convolutions aren't computed (the coprocessor's impulse-response layout is unknown). As a stand-in, any convolution whose response has at most 65536 floats passes its input through at 5x (b69806a, for the phone speaker), and all others are silent. But that size range also contains about a hundred short room reverbs. Passed through, each one plays a second, louder copy of the dry sound. ## Change Each response is laid out in 2048-float blocks, and each block holds the spectrum of one 1024-frame partition. By Parseval, a block's energy equals its partition's energy, whatever order the bins are in. So the response's energy over time can be measured without knowing the layout. The trailing block (first word `0xFFFFACE1`) isn't a partition, so it's left out. A convolution now passes only if at least 98% of its response's energy is in the first three blocks (~64 ms), which is what a speaker filter looks like. Rooms stay dry, as the other reverbs already were. To check, I carved all 227 DSP impulse responses (codec 2) out of `audio.rpf`. They match the in-memory responses byte for byte. - 41 pass, all at ≥98%. All the others are under 95%, so the threshold has room on both sides. - The 40960-float size group, which the old rule passed entirely, is mixed. Most of its responses have 60–90% of their energy early: short rooms, not speakers. Each stream shape is logged once at info level with its early share. ## Testing - Unit tests: a new `speaker_filters_pass_and_rooms_do_not`, and `convolution_stream_as_the_title_drives_it` updated to use a speaker-shaped response. Both pass under Wine. - In game (RX 7900 XTX, Proton-CachyOS): phone callers are still audible, and speech and footsteps indoors are no longer doubled.
Convolutions are not computed, so a stand-in decides which ones pass their
input through (louder) and which give silence. The size rule (responses up
to 65536 floats pass) let through about a hundred short room reverbs as
well as the phone speaker: indoors, speech and footsteps came back as a
second, louder copy of the dry sound, which sounded like an echo.

A response is laid out in 2048-float blocks, each holding the spectrum of
one 1024-frame partition, so its energy per block is that partition's
energy whatever the bin order (Parseval). The trailing block that starts
with 0xFFFFACE1 is not a partition and is left out. Pass a convolution
when at least 98% of its response's energy is in its first three blocks
(~64 ms). Of the title's 227 responses, 41 pass (the speaker filters) and
all others are under 95%, so the threshold has room on both sides.

Each stream shape is logged once at info with its early share.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 11:47:34 +00:00
Author
Owner

@⁠niiniiniintje on GitHub · 2026-12-04 11:46 UTC · original

NOte that this isn't a pure fix, just a small shape test after reading source info on audio more closer. The pure fix is to figure out the audio processor setup on xbox and implement it as the console setup. This approximation gets close enough though.

**@⁠niiniiniintje** on GitHub · 2026-12-04 11:46 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/457#issuecomment-7410546273) NOte that this isn't a pure fix, just a small shape test after reading source info on audio more closer. The pure fix is to figure out the audio processor setup on xbox and implement it as the console setup. This approximation gets close enough though.
Author
Owner

@⁠Luxuride on GitHub · 2026-12-04 12:20 UTC · original

The heuristic is well argued and validated against all 227 carved impulse responses. One thing must change before it lands, because it is the only thing here that can take the process down.

The unsafe read is unbounded. response_of builds std::slice::from_raw_parts(ir, ir_floats as usize) (crates/victory-hle/src/modules/dsp.rs:87-94), and the valid() gate that runs at activation checks kind/options/frames/channels but never ir or ir_floats (dsp.rs:60-75). ir_floats is bounded only by the caller's own descriptor — up to 2^32 floats. On main the same number was only compared, never dereferenced, which is why this was safe before and is not now. Clamp it in valid() before the read: ir_floats <= 0x1_0000 and ir_floats % BLOCK_FLOATS == 0, which is also the alignment the block splitter already assumes.

Two smaller notes:

  • early_share uses response.len() / BLOCK_FLOATS (dsp.rs:55), so a trailing partial block is dropped from numerator and denominator alike. A response shorter than one block gives blocks == 0 → None → no pass, and a single-block response whose first float happens to be the footer magic also gives None. Harmless, but worth a comment so the next person does not "fix" it by rounding up.
  • This changes audible output with no switch and no docs/SETTINGS.md entry. At minimum, document the transition and what the note_shape log line means, so a user whose room reverb changed has something to find.

The shape it replaces — an ir_floats <= 0x1_0000 size test — was a proxy for the same thing; the energy share is a better proxy, and the footer-magic check is the right way to drop the trailing block.

0 behind, no conflict.

**@⁠Luxuride** on GitHub · 2026-12-04 12:20 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/457#issuecomment-7411298372) The heuristic is well argued and validated against all 227 carved impulse responses. One thing must change before it lands, because it is the only thing here that can take the process down. **The `unsafe` read is unbounded.** `response_of` builds `std::slice::from_raw_parts(ir, ir_floats as usize)` (`crates/victory-hle/src/modules/dsp.rs:87-94`), and the `valid()` gate that runs at activation checks kind/options/frames/channels but never `ir` or `ir_floats` (`dsp.rs:60-75`). `ir_floats` is bounded only by the caller's own descriptor — up to 2^32 floats. On `main` the same number was only *compared*, never dereferenced, which is why this was safe before and is not now. Clamp it in `valid()` before the read: `ir_floats <= 0x1_0000` and `ir_floats % BLOCK_FLOATS == 0`, which is also the alignment the block splitter already assumes. Two smaller notes: - `early_share` uses `response.len() / BLOCK_FLOATS` (`dsp.rs:55`), so a trailing partial block is dropped from numerator and denominator alike. A response shorter than one block gives `blocks == 0 → None → no pass`, and a single-block response whose first float happens to be the footer magic also gives `None`. Harmless, but worth a comment so the next person does not "fix" it by rounding up. - This changes audible output with no switch and no `docs/SETTINGS.md` entry. At minimum, document the transition and what the `note_shape` log line means, so a user whose room reverb changed has something to find. The shape it replaces — an `ir_floats <= 0x1_0000` size test — was a proxy for the same thing; the energy share is a better proxy, and the footer-magic check is the right way to drop the trailing block. 0 behind, no conflict.
The size comes from the title's description, so the read of the response
is limited to whole blocks and at most MAX_IR_FLOATS (0x80000; the title's
largest has 243712). Anything else reads nothing and does not pass.
Activation still accepts it, as before: refusing sizes over 0x10000 in
valid() would refuse 40 of the title's room reverbs that main activates.

Note that early_share counts only whole blocks, and document the behaviour
and its log line under Input and audio.
Author
Owner

@⁠niiniiniintje on GitHub · 2026-12-04 12:53 UTC · original

Thanks. Bounded in cd1b7b5, though not in valid(): 48 of the title's 227 responses are over 0x10000 floats (up to 243712), all room reverbs that main activates, so refusing them would change activation. The read now happens only for whole 2048-float blocks up to 0x80000 floats; anything else activates as before, is read as empty and gives silence. Also added the whole-block comment and a paragraph in docs/SETTINGS.md. (The commit message says 40; it is 48.)

**@⁠niiniiniintje** on GitHub · 2026-12-04 12:53 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/457#issuecomment-7412073829) Thanks. Bounded in `cd1b7b5`, though not in `valid()`: 48 of the title's 227 responses are over 0x10000 floats (up to 243712), all room reverbs that main activates, so refusing them would change activation. The read now happens only for whole 2048-float blocks up to 0x80000 floats; anything else activates as before, is read as empty and gives silence. Also added the whole-block comment and a paragraph in `docs/SETTINGS.md`. (The commit message says 40; it is 48.)
Author
Owner

@⁠niiniiniintje on GitHub · 2026-12-04 14:05 UTC · original

MIght've misread on the SETTINGS comment, was the purpose to note a change in audio or to add a falg to switch between and record that in settings?

**@⁠niiniiniintje** on GitHub · 2026-12-04 14:05 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/457#issuecomment-7413814107) MIght've misread on the SETTINGS comment, was the purpose to note a change in audio or to add a falg to switch between and record that in settings?
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 14:10 UTC · original

Checked cd1b7b5 natively on Windows (MSVC build, not Wine).

cargo test -p victory-hle --lib with VICTORY_NATIVE_INPUT=0 gives 162 passed, 0 failed, 1 ignored.

Negative control. I put the old size rule back inside passes (a convolution passes if it has at most 0x10000 floats) and left the rest alone. speaker_filters_pass_and_rooms_do_not then fails at dsp.rs:390, since the 21-block room is under the old limit and passes with an early share of 0.65. The other two dsp tests pass under both rules. a_response_is_read_only_within_its_bounds tests a helper that main doesn't have, and convolution_stream_as_the_title_drives_it uses a one-block response that either rule passes.

The unbounded read from the earlier review is fixed. response_of reads only whole blocks, up to 0x80000 floats (dsp.rs:96-105), and valid() is unchanged, so activation accepts the same descriptors as main.

I also fuzzed passes and response_of locally (not pushed). 20000 random buffers of random length, some whole blocks and some not, filled with random bits, NaN, inf and footer words, each run with four kind values. Nothing panicked, the share always stayed between 0 and 1, and only kind 1 ever passed. Through response_of, every ir_floats I tried (0, 1, 2047, 2048, 2049, the cap, the cap plus one block, u32::MAX, plus random values) came back as whole blocks and never past the cap. Then I took a passing speaker shape and changed one thing at a time. These were all rejected: another kind, a 4th block with about 3% of the energy, a NaN or inf anywhere, all zeros, a null ir, ir_floats of 0, and ir_floats one short of a whole block. At about 1.3% late energy it still passes, and it also still passes with the footer word cleared.

What changes compared to main, in both directions:

  • A null ir, ir_floats of 0, or a size that isn't whole blocks used to pass at or under 0x10000 floats. Now these give silence (dsp.rs:96-101).
  • The 0x10000 limit is gone. A response between 0x10000 and 0x80000 floats with at least 98% of its energy early now passes, where main gave silence (dsp.rs:98).
  • Any finite, non-zero response of three blocks or fewer always passes, with a share of 1.0.

I don't have the game data, so I couldn't check whether any of the 41 passing responses are over 0x10000 floats, or whether all of the title's responses are whole blocks.

A small non-blocking note. note_shape keys on kind, options, frames, channels and ir_floats, not on the response itself (dsp.rs:111-120). So two different responses with the same size log only once, showing the first one's passes and early_share. The 40960-float group is mixed, according to the PR description, so for that group the log line and the new SETTINGS.md sentence can mislead.

Looks good to me.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 14:10 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/457#issuecomment-7413922455) Checked cd1b7b5 natively on Windows (MSVC build, not Wine). `cargo test -p victory-hle --lib` with VICTORY_NATIVE_INPUT=0 gives 162 passed, 0 failed, 1 ignored. Negative control. I put the old size rule back inside `passes` (a convolution passes if it has at most 0x10000 floats) and left the rest alone. `speaker_filters_pass_and_rooms_do_not` then fails at dsp.rs:390, since the 21-block room is under the old limit and passes with an early share of 0.65. The other two dsp tests pass under both rules. `a_response_is_read_only_within_its_bounds` tests a helper that main doesn't have, and `convolution_stream_as_the_title_drives_it` uses a one-block response that either rule passes. The unbounded read from the earlier review is fixed. `response_of` reads only whole blocks, up to 0x80000 floats (dsp.rs:96-105), and `valid()` is unchanged, so activation accepts the same descriptors as main. I also fuzzed `passes` and `response_of` locally (not pushed). 20000 random buffers of random length, some whole blocks and some not, filled with random bits, NaN, inf and footer words, each run with four kind values. Nothing panicked, the share always stayed between 0 and 1, and only kind 1 ever passed. Through `response_of`, every `ir_floats` I tried (0, 1, 2047, 2048, 2049, the cap, the cap plus one block, u32::MAX, plus random values) came back as whole blocks and never past the cap. Then I took a passing speaker shape and changed one thing at a time. These were all rejected: another kind, a 4th block with about 3% of the energy, a NaN or inf anywhere, all zeros, a null `ir`, `ir_floats` of 0, and `ir_floats` one short of a whole block. At about 1.3% late energy it still passes, and it also still passes with the footer word cleared. What changes compared to main, in both directions: - A null `ir`, `ir_floats` of 0, or a size that isn't whole blocks used to pass at or under 0x10000 floats. Now these give silence (dsp.rs:96-101). - The 0x10000 limit is gone. A response between 0x10000 and 0x80000 floats with at least 98% of its energy early now passes, where main gave silence (dsp.rs:98). - Any finite, non-zero response of three blocks or fewer always passes, with a share of 1.0. I don't have the game data, so I couldn't check whether any of the 41 passing responses are over 0x10000 floats, or whether all of the title's responses are whole blocks. A small non-blocking note. `note_shape` keys on kind, options, frames, channels and ir_floats, not on the response itself (dsp.rs:111-120). So two different responses with the same size log only once, showing the first one's `passes` and `early_share`. The 40960-float group is mixed, according to the PR description, so for that group the log line and the new SETTINGS.md sentence can mislead. Looks good to me. AI-assisted: yes
github-bot changed target branch from main to gh-pr/457/base 2026-12-05 20:54:35 +00:00
github manually merged commit cd1b7b5184 into gh-pr/457/base 2026-12-05 20:57:12 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.