Audio
The audio driver mixes 8 output channels into a single stereo stream that plays at 44100 Hz. Programs reserve a channel with a fixed buffer length and format and then hand it 1 buffer of PCM samples at a time through sceAudio (see Library Functions).
Sample Format
Every sample is a signed 16 bit value. A channel's format decides how many values make up 1 sample frame.
| Value | Name | Sample frame |
|---|---|---|
0x00 |
PSP_AUDIO_FORMAT_STEREO |
2 values, left then right, 4 bytes per frame |
0x10 |
PSP_AUDIO_FORMAT_MONO |
1 value played on both sides, 2 bytes per frame |
A channel's buffer length is counted in sample frames and must be a multiple of 64 between 64 and 65472, so a stereo buffer occupies sample_count * 4 bytes and a mono buffer sample_count * 2 bytes.
Volume and Mixing
Each output call sets a left and a right volume between 0 and 0xFFFF, where 0x8000 plays the samples unchanged. The mixer scales each sample by its side's volume, adds up the scaled samples of all channels and saturates the sum to the signed 16 bit range.
let scaled = (sample as i32 * volume as i32) >> 15;
let output = mixed_sum.clamp(i16::MIN as i32, i16::MAX as i32) as i16;
Playing Buffers
A channel plays 1 buffer at a time and reads it from memory while playing, so a program must not overwrite a buffer until the channel has finished with it. A blocking output to an idle channel starts playback and returns at once. When the channel is still playing an earlier buffer, the calling thread enters WAIT until that buffer has finished and the new one has taken its place. Only 1 thread can wait on a channel. A blocking output from a 2nd thread while another thread is already waiting fails with SCE_AUDIO_ERROR_OUTPUT_BUSY (0x80260002).
Double buffering therefore keeps a channel playing without gaps. The program fills 1 buffer while the other plays and the blocking output of the filled buffer paces the thread to the playback rate.