upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/xma_decoder/src
diff options
context:
space:
mode:
authorandretuta <andreviniciusmerlia@gmail.com>2026-11-24 03:53:37 -0300
committerandretuta <andreviniciusmerlia@gmail.com>2026-11-24 03:53:37 -0300
commitcfcedb58e2802c5cd38afd1cbde877ae9d71c2a5 (patch)
treeaa73abcbd3f2fb1685127392ad8716717831c725 /runtime/xma_decoder/src
parent12536c2ab33072b8f49a8d012c970093c90c0866 (diff)
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 <noreply@anthropic.com>
Diffstat (limited to 'runtime/xma_decoder/src')
-rw-r--r--runtime/xma_decoder/src/lib.rs44
1 files changed, 42 insertions, 2 deletions
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
@@ -59,6 +59,28 @@ pub unsafe extern "C" fn leonida_xma2_decoder_create(
59 channels: u32, 59 channels: u32,
60 out_decoder: *mut *mut LeonidaXma2Decoder, 60 out_decoder: *mut *mut LeonidaXma2Decoder,
61) -> i32 { 61) -> i32 {
62 unsafe { create_with(sample_rate, channels, out_decoder, Xma2Stream::new) }
63}
64
65/// Like `leonida_xma2_decoder_create`, but decodes like the console's XMA hardware: the first
66/// frame is kept and there is no 64-sample start skip; the caller applies the context's skip.
67/// # Safety
68/// Same as `leonida_xma2_decoder_create`.
69#[no_mangle]
70pub unsafe extern "C" fn leonida_xma2_decoder_create_hardware(
71 sample_rate: u32,
72 channels: u32,
73 out_decoder: *mut *mut LeonidaXma2Decoder,
74) -> i32 {
75 unsafe { create_with(sample_rate, channels, out_decoder, Xma2Stream::new_hardware) }
76}
77
78unsafe fn create_with<E>(
79 sample_rate: u32,
80 channels: u32,
81 out_decoder: *mut *mut LeonidaXma2Decoder,
82 make: impl FnOnce(u32, usize) -> Result<Xma2Stream, E>,
83) -> i32 {
62 if out_decoder.is_null() { 84 if out_decoder.is_null() {
63 return LEONIDA_XMA2_INVALID_ARGUMENT; 85 return LEONIDA_XMA2_INVALID_ARGUMENT;
64 } 86 }
@@ -68,8 +90,8 @@ pub unsafe extern "C" fn leonida_xma2_decoder_create(
68 } 90 }
69 91
70 match catch_unwind(AssertUnwindSafe(|| { 92 match catch_unwind(AssertUnwindSafe(|| {
71 let stream = Xma2Stream::new(sample_rate, channels as usize) 93 let stream =
72 .map_err(|_| LEONIDA_XMA2_INVALID_FORMAT)?; 94 make(sample_rate, channels as usize).map_err(|_| LEONIDA_XMA2_INVALID_FORMAT)?;
73 try_new_handle(LeonidaXma2Decoder { 95 try_new_handle(LeonidaXma2Decoder {
74 stream: Mutex::new(stream), 96 stream: Mutex::new(stream),
75 channels: channels as usize, 97 channels: channels as usize,
@@ -233,6 +255,24 @@ mod tests {
233 } 255 }
234 256
235 #[test] 257 #[test]
258 fn create_hardware_accepts_stereo_and_rejects_three_channels() {
259 let mut decoder = ptr::null_mut();
260 assert_eq!(
261 unsafe { leonida_xma2_decoder_create_hardware(48_000, 2, &mut decoder) },
262 LEONIDA_XMA2_OK
263 );
264 assert!(!decoder.is_null());
265 unsafe { leonida_xma2_decoder_destroy(decoder) };
266
267 let mut decoder = ptr::dangling_mut();
268 assert_eq!(
269 unsafe { leonida_xma2_decoder_create_hardware(48_000, 3, &mut decoder) },
270 LEONIDA_XMA2_INVALID_FORMAT
271 );
272 assert!(decoder.is_null());
273 }
274
275 #[test]
236 fn pcm_handle_allocation_failure_clears_output_and_allows_retry() { 276 fn pcm_handle_allocation_failure_clears_output_and_allows_retry() {
237 let mut decoder = ptr::null_mut(); 277 let mut decoder = ptr::null_mut();
238 assert_eq!( 278 assert_eq!(