OSLib has some sound functions. You can play musics (.bgm, 'proprietary' ADPCM-like format) and wav sound files. The .bgm format takes less space but has a slightly worse quality and doesn't support stereo (for the moment, at least). It can be used for in-game musics, if you have a small Memory Stick. One minute of Mega Drive style music can take on a little less than 400 kB with still an average quality. Sounds (.wav) take a lot more space and are not really faster to play than .bgm. When they are streamed, .bgm can even be faster due to the little amount of data read from the MS.
Let's see how to play a music file. First of all it's necessary to call oslInitAudio() to initialize the audio system. Then, we can load and play our sounds. Example:
OSL_SOUND *music, *sfx; oslInit(0); oslInitAudio(); music = oslLoadSoundFile("test.bgm", OSL_FMT_STREAM); oslAssert(music); //Debug: verify that it could be loaded sfx = oslLoadSoundFile("test.wav", OSL_FMT_NONE); oslAssert(sfx); //Debug: verify that it could be loaded oslPlaySound(music, 0); oslPlaySound(sfx, 1);
Here are the functions we will use:
int oslInitAudio();
Initialize the OSLib audio system. Obviously, you need to call it to be able to play a sound.
OSL_SOUND *oslLoadSoundFile(const char *filename, int stream);
Loads a .wav or .bgm file. You just need to indicate the file name as well as if you wish to stream it from the Memory Stick. If you are streaming a sound, it will not be loaded in memory, that makes you spare loading time but especially many RAM space, because the PSP as "only" 20 MB, so imagine that if you load sound effects of 1 MB each, it will be quickly saturated (memory is also shared for your code, variables, images and sound). However, streaming affects performance, especially for a high sample rate. For better performances, I advise you to use small .wav loaded for the sound effects and streamed .bgm for musics. You can also stream sound effects, but only those which are very rarely played at normal time.
void oslAudioVSync();
Call it in your main loop if you stream some sounds. Why? When you put your PSP in stand-by mode, the kernel closes all opened files! Then, obviously the audio system was crashing. Now OSLib manages that, but it is necessary to reopen files later to continue, and it's not possible directly when the PSP wakes up because the MS is not initialized yet. This function will verify that the MS is ready, and reload files and continue where they stopped if they were. If you do not call this function, any sound currently played in streaming will be stopped when the PSP wakes up. However, it will be reloaded automatically (but restart at the beginning) when you call oslPlaySound. Exactly the same for sounds which were opened in streaming but which were not playing at this moment.
Structures
OSL_SOUND
Stores data about sounds loaded in memory. Most audio functions will need it.