mirror of
https://github.com/lobomfz/VIctory-xmem.git
synced 2026-12-06 05:48:38 +00:00
Merge pull request #457 from niiniiniintje/dsp-speaker-shape
dsp: pass speaker filters by their shape, not by size
This commit is contained in:
@ -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,108 @@ 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 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<f32> {
|
||||
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<f32>) {
|
||||
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 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
|
||||
|| ir_floats > MAX_IR_FLOATS
|
||||
|| ir_floats as usize % BLOCK_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<f32>) {
|
||||
static SEEN: Mutex<Vec<(u32, u32, u32, u32, u32)>> = 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<u32>,
|
||||
@ -160,7 +260,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 +371,47 @@ 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 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];
|
||||
@ -285,7 +427,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,
|
||||
|
||||
@ -51,6 +51,8 @@ Set `VICTORY_GAMERTAG` in `run.conf` to change the local display name returned b
|
||||
| `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 |
|
||||
|
||||
Reference in New Issue
Block a user