From 6a229b56a57b0a4f19a58d7b389d483df2963352 Mon Sep 17 00:00:00 2001 From: niiniiniintje <364815431+niiniiniintje@users.noreply.github.com> Date: Fri, 4 Dec 2026 12:43:43 +0100 Subject: [PATCH 1/2] dsp: pass speaker filters by their shape, not by size 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. --- crates/victory-hle/src/modules/dsp.rs | 125 ++++++++++++++++++++++++-- 1 file changed, 119 insertions(+), 6 deletions(-) diff --git a/crates/victory-hle/src/modules/dsp.rs b/crates/victory-hle/src/modules/dsp.rs index e11c0746..bf639cef 100644 --- a/crates/victory-hle/src/modules/dsp.rs +++ b/crates/victory-hle/src/modules/dsp.rs @@ -11,10 +11,11 @@ //! //! The transforms themselves are not computed: the hardware takes impulse //! responses already in its own partitioned frequency-domain layout, which -//! is not known. Short convolutions, like the phone speaker that carries -//! callers' voices, pass their input through louder; every other output is +//! is not known. Convolutions with a speaker filter, like the phone speaker +//! that carries callers' voices (nearly all of the response's energy in its +//! first ~64 ms), pass their input through louder; every other output is //! silence, so reverb returns are dry. -// shortcut: no convolution or block transform; short convolutions pass +// shortcut: no convolution or block transform; speaker filters pass // through, the rest are zeros. Needs the coprocessor's impulse response and // spectrum layouts to do better. #![allow(non_snake_case)] @@ -34,9 +35,96 @@ const CONVOLUTION: u32 = 1; /// Option bits that need a stereo stream, and one convolution refuses. const STEREO_OPTIONS: u32 = 2 | 8; const NOT_FOR_CONVOLUTION: u32 = 4; -const PASS_IR_FLOATS: u32 = 0x1_0000; const PASS_GAIN: f32 = 5.0; +/// Share of a response's energy in its first [`EARLY_BLOCKS`] blocks at or +/// above which its convolution passes the input through (see [`passes`]). +const PASS_EARLY_SHARE: f32 = 0.98; +/// Blocks of the coprocessor's layout: 2048 floats each (one 1024-frame +/// partition); 3 of them are about 64 ms. +const BLOCK_FLOATS: usize = 2048; +const EARLY_BLOCKS: usize = 3; +/// First word of the trailing block that is not a partition. +const FOOTER_MAGIC: u32 = 0xFFFF_ACE1; + +/// The share of a response's energy in its first [`EARLY_BLOCKS`] blocks, +/// the trailing footer block left out. Each block holds the spectrum of one +/// partition, so its energy is that partition's (Parseval), whatever the +/// bin order. `None` when there is nothing to measure or it is not finite. +fn early_share(response: &[f32]) -> Option { + let mut blocks = response.len() / BLOCK_FLOATS; + if blocks > 0 && response[(blocks - 1) * BLOCK_FLOATS].to_bits() == FOOTER_MAGIC { + blocks -= 1; + } + let energy = |b: usize| -> f64 { + response[b * BLOCK_FLOATS..(b + 1) * BLOCK_FLOATS] + .iter() + .map(|&v| f64::from(v) * f64::from(v)) + .sum() + }; + let total: f64 = (0..blocks).map(energy).sum(); + let early: f64 = (0..blocks.min(EARLY_BLOCKS)).map(energy).sum(); + (total.is_finite() && total > 0.0).then(|| (early / total) as f32) +} + +/// Whether a stream's commands pass their input through louder instead of +/// giving silence. The transforms are not computed (see the module notes): +/// a speaker filter (a phone caller's voice), whose response is over within +/// a few blocks, is near enough to its input; a room's reverb is not, and +/// passed it would come back as a second, louder copy of the dry sound (an +/// echo on speech indoors). Of the title's 227 responses, 41 have at least +/// 98% of their energy in the first three blocks and the rest under 95%. +fn passes(kind: u32, response: &[f32]) -> (bool, Option) { + if kind != CONVOLUTION { + return (false, None); + } + let share = early_share(response); + (share.is_some_and(|s| s >= PASS_EARLY_SHARE), share) +} + +/// The response a description names, empty when it names none. +fn response_of(desc: &StreamDesc) -> &[f32] { + let (ir, ir_floats) = (desc.ir, desc.ir_floats); + if ir.is_null() || ir_floats == 0 { + &[] + } else { + // SAFETY: the title's description names `ir_floats` floats at `ir`, + // read during activation as the hardware would. + unsafe { std::slice::from_raw_parts(ir, ir_floats as usize) } + } +} + +/// Logs each stream shape the first time it is activated, at info level, so a +/// play log shows which impulse responses the title uses and which pass. +fn note_shape(desc: &StreamDesc, passes: bool, early_share: Option) { + static SEEN: Mutex> = Mutex::new(Vec::new()); + let (kind, options, frames, channels, ir_floats) = ( + desc.kind, + desc.options, + desc.frames, + desc.channels, + desc.ir_floats, + ); + let key = (kind, options, frames, channels, ir_floats); + { + let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner()); + if seen.contains(&key) || seen.len() >= 256 { + return; + } + seen.push(key); + } + tracing::info!( + kind, + options, + frames, + channels, + ir_floats, + passes, + early_share = early_share.unwrap_or(f32::NAN), + "audio DSP stream shape first activated" + ); +} + pub(crate) struct Dsp { max_streams: u32, active: Mutex, @@ -160,7 +248,8 @@ pub(crate) extern "system" fn XDspActivate( desc.channels, desc.ir_floats, ); - let passes = kind == CONVOLUTION && ir_floats <= PASS_IR_FLOATS; + let (passes, share) = passes(kind, response_of(desc)); + note_shape(desc, passes, share); tracing::debug!( kind, options, @@ -270,6 +359,29 @@ pub(crate) fn module() -> HleModule { mod tests { use super::*; + #[test] + fn speaker_filters_pass_and_rooms_do_not() { + let mut speaker = vec![0f32; 20 * BLOCK_FLOATS]; + speaker[..BLOCK_FLOATS].fill(1.0); + speaker[3 * BLOCK_FLOATS..4 * BLOCK_FLOATS].fill(0.001); + speaker[19 * BLOCK_FLOATS] = f32::from_bits(FOOTER_MAGIC); + let (pass, share) = passes(CONVOLUTION, &speaker); + assert!(pass && share.unwrap() > 0.99, "{share:?}"); + + // A room: energy halving block by block over 20 blocks. + let mut room = vec![0f32; 21 * BLOCK_FLOATS]; + for b in 0..20 { + room[b * BLOCK_FLOATS..(b + 1) * BLOCK_FLOATS].fill(0.5f32.powf(b as f32 / 4.0)); + } + room[20 * BLOCK_FLOATS] = f32::from_bits(FOOTER_MAGIC); + let (pass, share) = passes(CONVOLUTION, &room); + assert!(!pass && share.unwrap() < 0.9, "{share:?}"); + + // Nothing to measure, or not a convolution: no pass. + assert!(!passes(CONVOLUTION, &[]).0); + assert!(!passes(2, &speaker).0); + } + #[test] fn convolution_stream_as_the_title_drives_it() { let mut base = vec![0u8; 0x10_0000]; @@ -285,7 +397,8 @@ mod tests { ), S_OK ); - let ir = vec![0f32; 0x800]; + // One block whose energy is all early: a speaker filter, which passes. + let ir = vec![0.25f32; 0x800]; // Stereo convolution with option 2, as the title asks for. let desc = StreamDesc { kind: CONVOLUTION, From cd1b7b51843966bf3df5a597edb59517cc445a48 Mon Sep 17 00:00:00 2001 From: niiniiniintje <364815431+niiniiniintje@users.noreply.github.com> Date: Fri, 4 Dec 2026 13:50:25 +0100 Subject: [PATCH 2/2] dsp: bound the impulse response read; document the shape rule 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. --- crates/victory-hle/src/modules/dsp.rs | 34 +++++++++++++++++++++++++-- docs/SETTINGS.md | 2 ++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/crates/victory-hle/src/modules/dsp.rs b/crates/victory-hle/src/modules/dsp.rs index bf639cef..46c347b6 100644 --- a/crates/victory-hle/src/modules/dsp.rs +++ b/crates/victory-hle/src/modules/dsp.rs @@ -46,11 +46,16 @@ const BLOCK_FLOATS: usize = 2048; const EARLY_BLOCKS: usize = 3; /// First word of the trailing block that is not a partition. const FOOTER_MAGIC: u32 = 0xFFFF_ACE1; +/// The most floats of a response that are read: the title's largest has +/// 243712 (119 blocks). +const MAX_IR_FLOATS: u32 = 0x8_0000; /// The share of a response's energy in its first [`EARLY_BLOCKS`] blocks, /// the trailing footer block left out. Each block holds the spectrum of one /// partition, so its energy is that partition's (Parseval), whatever the /// bin order. `None` when there is nothing to measure or it is not finite. +/// Only whole blocks count: a response shorter than one block, or a lone +/// footer, gives `None` (no pass). [`response_of`] gives only whole blocks. fn early_share(response: &[f32]) -> Option { let mut blocks = response.len() / BLOCK_FLOATS; if blocks > 0 && response[(blocks - 1) * BLOCK_FLOATS].to_bits() == FOOTER_MAGIC { @@ -82,10 +87,17 @@ fn passes(kind: u32, response: &[f32]) -> (bool, Option) { (share.is_some_and(|s| s >= PASS_EARLY_SHARE), share) } -/// The response a description names, empty when it names none. +/// The response a description names, empty when it names none or one that +/// is not whole blocks or is larger than [`MAX_IR_FLOATS`]: the size comes +/// from the title's description, so it bounds the read. Activation does not +/// refuse such a response (it never did): it only does not pass. fn response_of(desc: &StreamDesc) -> &[f32] { let (ir, ir_floats) = (desc.ir, desc.ir_floats); - if ir.is_null() || ir_floats == 0 { + if ir.is_null() + || ir_floats == 0 + || ir_floats > MAX_IR_FLOATS + || ir_floats as usize % BLOCK_FLOATS != 0 + { &[] } else { // SAFETY: the title's description names `ir_floats` floats at `ir`, @@ -382,6 +394,24 @@ mod tests { assert!(!passes(2, &speaker).0); } + #[test] + fn a_response_is_read_only_within_its_bounds() { + let ir = vec![1f32; BLOCK_FLOATS]; + let desc = |ir_floats| StreamDesc { + kind: CONVOLUTION, + options: 0, + frames: 0x400, + channels: 1, + ir_floats, + ir: ir.as_ptr(), + }; + assert_eq!(response_of(&desc(BLOCK_FLOATS as u32)).len(), BLOCK_FLOATS); + // Past the bound, or not whole blocks: nothing is read. + assert!(response_of(&desc(u32::MAX)).is_empty()); + assert!(response_of(&desc(MAX_IR_FLOATS + BLOCK_FLOATS as u32)).is_empty()); + assert!(response_of(&desc(BLOCK_FLOATS as u32 - 1)).is_empty()); + } + #[test] fn convolution_stream_as_the_title_drives_it() { let mut base = vec![0u8; 0x10_0000]; diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index a624b99d..c3b68c29 100644 --- a/docs/SETTINGS.md +++ b/docs/SETTINGS.md @@ -44,6 +44,8 @@ choices or the accepted range. | `VICTORY_HAPTICS_GAIN_DB` | `8` | 0, 4, 8, 12, 14 | DualSense haptics gain in dB | | `MUTE` | unset | 0, 1 | `1`: no sound | +The audio coprocessor's convolutions (reverbs) are not computed. A convolution whose impulse response has at least 98% of its energy in its first ~64 ms (a speaker filter, such as the phone) passes its input through; room reverbs give silence, so rooms sound dry. The log line "audio DSP stream shape first activated" appears once for each response shape, with `passes` and `early_share` (that energy share). + ### Ray tracing, memory and paging | Setting | Default | Values | Effect |