diff --git a/examples/audio/.gitignore b/examples/audio/.gitignore new file mode 100644 index 00000000..eb5a316c --- /dev/null +++ b/examples/audio/.gitignore @@ -0,0 +1 @@ +target diff --git a/examples/audio/Cargo.toml b/examples/audio/Cargo.toml new file mode 100644 index 00000000..75f14b4e --- /dev/null +++ b/examples/audio/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "psp-audio-example" +version = "0.1.0" +authors = ["Paul Sajna "] +edition = "2018" + +[dependencies] +psp = { path = "../../psp" } diff --git a/examples/audio/assets/showtime.rawpcm16 b/examples/audio/assets/showtime.rawpcm16 new file mode 100644 index 00000000..59f649d8 Binary files /dev/null and b/examples/audio/assets/showtime.rawpcm16 differ diff --git a/examples/audio/src/main.rs b/examples/audio/src/main.rs new file mode 100644 index 00000000..1c39947a --- /dev/null +++ b/examples/audio/src/main.rs @@ -0,0 +1,52 @@ +#![no_std] +#![no_main] + +use psp::sys::{ + sceAudioChReserve, sceAudioOutput, sceAudioOutputBlocking, + sceAudioSetChannelDataLen, sceAudioGetChannelRestLen, +}; + +psp::module!("sound_sample", 1, 1); + +// In a real scenario you would probably load this out of a file at runtime +// I leave that as an exercise to the user ;) +// 44.1KHz signed 16-bit PCM in RAW (no header) format +static AUDIO_CLIP: [u8; 718828] = *include_bytes!("../assets/showtime.rawpcm16"); + +const MAX_VOL: i32 = 0x8000; +const MAX_SAMPLES: usize = 65472; +const CHANNEL: i32 = 0; + +fn psp_main() { + psp::enable_home_button(); + unsafe { + sceAudioChReserve(CHANNEL, MAX_SAMPLES as i32, psp::sys::AudioFormat::Stereo) + }; + let mut start_pos: usize = 0; + let mut restlen = 0; + while (start_pos+MAX_SAMPLES*4) < AUDIO_CLIP.len() { + if restlen > 0 { + psp::dprintln!("Check it out, I can do other stuff while audio plays!"); + } else { + unsafe { + sceAudioOutput( + CHANNEL, + MAX_VOL, + AUDIO_CLIP.as_ptr().add(start_pos) as *mut _ + ) + }; + start_pos += MAX_SAMPLES*4; + } + restlen = unsafe { sceAudioGetChannelRestLen(CHANNEL) }; + } + let remainder: i32 = (((AUDIO_CLIP.len() % (MAX_SAMPLES*4)/4)+63) & !63) as i32; + unsafe { sceAudioSetChannelDataLen(CHANNEL, remainder)}; + unsafe { + // Blocking here so the program doesn't exit while we're finishing + // up outputting audio + sceAudioOutputBlocking( + CHANNEL, + MAX_VOL, + AUDIO_CLIP.as_ptr().add(start_pos) as *mut _) + }; +}