From cfcedb58e2802c5cd38afd1cbde877ae9d71c2a5 Mon Sep 17 00:00:00 2001 From: andretuta Date: Tue, 24 Nov 2026 03:53:37 -0300 Subject: acphal: decode XMA contexts with runtime/xma_decoder when cargo is available Replace the hand-written C++ XMA2/WMA Pro decoder with the Rust decoder already on main. runtime/CMakeLists.txt builds it at configure time when cargo is on PATH and links it into gta6rt and acphal_test (LEONIDA_HAVE_XMA2_DECODER); without cargo, if that build fails, or with the cl.exe fallback, XMA voices output silence for each packet's frame count. The ring engine decodes whole 2048-byte packets: the read offset and the loop points are used per packet, so a loop plays its end packet in full and restarts with a fresh decoder at the packet holding the loop start. The C ABI gains leonida_xma2_decoder_create_hardware (no first-frame drop, no 64-sample start skip), since the context carries its own skip. Tested: acphal_test 129 checks (silence) and 125 checks (Rust decoder, gnullvm build), 0 failures, 3 runs each, clang 23 / mingw x64; cargo fmt and cargo test (gnullvm); pytest build/docs suites 204 passed. Co-Authored-By: Claude Opus 5.5 --- runtime/xma_decoder/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'runtime/xma_decoder/src') diff --git a/runtime/xma_decoder/src/lib.rs b/runtime/xma_decoder/src/lib.rs index 1b435a0ce..cda2721fb 100644 --- a/runtime/xma_decoder/src/lib.rs +++ b/runtime/xma_decoder/src/lib.rs @@ -58,6 +58,28 @@ pub unsafe extern "C" fn leonida_xma2_decoder_create( sample_rate: u32, channels: u32, out_decoder: *mut *mut LeonidaXma2Decoder, +) -> i32 { + unsafe { create_with(sample_rate, channels, out_decoder, Xma2Stream::new) } +} + +/// Like `leonida_xma2_decoder_create`, but decodes like the console's XMA hardware: the first +/// frame is kept and there is no 64-sample start skip; the caller applies the context's skip. +/// # Safety +/// Same as `leonida_xma2_decoder_create`. +#[no_mangle] +pub unsafe extern "C" fn leonida_xma2_decoder_create_hardware( + sample_rate: u32, + channels: u32, + out_decoder: *mut *mut LeonidaXma2Decoder, +) -> i32 { + unsafe { create_with(sample_rate, channels, out_decoder, Xma2Stream::new_hardware) } +} + +unsafe fn create_with( + sample_rate: u32, + channels: u32, + out_decoder: *mut *mut LeonidaXma2Decoder, + make: impl FnOnce(u32, usize) -> Result, ) -> i32 { if out_decoder.is_null() { return LEONIDA_XMA2_INVALID_ARGUMENT; @@ -68,8 +90,8 @@ pub unsafe extern "C" fn leonida_xma2_decoder_create( } match catch_unwind(AssertUnwindSafe(|| { - let stream = Xma2Stream::new(sample_rate, channels as usize) - .map_err(|_| LEONIDA_XMA2_INVALID_FORMAT)?; + let stream = + make(sample_rate, channels as usize).map_err(|_| LEONIDA_XMA2_INVALID_FORMAT)?; try_new_handle(LeonidaXma2Decoder { stream: Mutex::new(stream), channels: channels as usize, @@ -232,6 +254,24 @@ mod tests { unsafe { leonida_xma2_decoder_destroy(decoder) }; } + #[test] + fn create_hardware_accepts_stereo_and_rejects_three_channels() { + let mut decoder = ptr::null_mut(); + assert_eq!( + unsafe { leonida_xma2_decoder_create_hardware(48_000, 2, &mut decoder) }, + LEONIDA_XMA2_OK + ); + assert!(!decoder.is_null()); + unsafe { leonida_xma2_decoder_destroy(decoder) }; + + let mut decoder = ptr::dangling_mut(); + assert_eq!( + unsafe { leonida_xma2_decoder_create_hardware(48_000, 3, &mut decoder) }, + LEONIDA_XMA2_INVALID_FORMAT + ); + assert!(decoder.is_null()); + } + #[test] fn pcm_handle_allocation_failure_clears_output_and_allows_retry() { let mut decoder = ptr::null_mut(); -- cgit v1.2.3