upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/xma_decoder/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/xma_decoder/src/lib.rs')
-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!(