Skip to content

non-blocking audio example #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/audio/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
8 changes: 8 additions & 0 deletions examples/audio/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "psp-audio-example"
version = "0.1.0"
authors = ["Paul Sajna <[email protected]>"]
edition = "2018"

[dependencies]
psp = { path = "../../psp" }
Binary file added examples/audio/assets/showtime.rawpcm16
Binary file not shown.
52 changes: 52 additions & 0 deletions examples/audio/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 _)
};
}