View Single Post
 
Old 02-14-2007, 08:25 PM
PSdonkey's Avatar
PSdonkey PSdonkey is offline
Squirrel Admin
PSP Titan
 
Join Date: Mar 2006
Posts: 9,117
PSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond repute
Default

Here is Lesson 5 - How to load and play background music and sound effects.

I'm just going to create a simple program for this lesson which will show you how to covert music into the OSlib standard, load and play music into your game, and assign sound effects to the PSP's buttons.


Preparing/converting your audio

In OSlib, there are 2 formats that you can use for your music/sound files. The first one is .WAV (PCM) files. The second is .BGM files (BackGround Muisc). Now BGM files are some what similar to compressed WAV files. BGM files are actually 4 to 5 times smaller then the same WAV file equivalent. This can be useful as the PSP;S memory is limited in both VRAM and standard RAM. BGM format is usually used for longer background music and WAV format is usually used for the smaller simpler sound effect. You can use WAV format for all of your music and sound files in your game but it is easier to convert a large WAV file to BGM format with almost the same quality.

In your OSlib folder, you will notice that there is also a Tools folder. Inside the tools folder you will see a program called wav2bgm.exe. What this program does is convert any WAV file to BGM format. The WAV file that you want to convert must be in either 44KHz or 22KHz or 11KHz. Any other format will not convert correctly.

Now to convert a file, simply goto your tools folder and place the WAV file in that folder that you would like to convert to BGM format. Then double click your pspdev.bat file and type:
Code:
cd oslib
cd tools
wav2bgm music.wav music.bgm
Now this is assuming OSlib is in the same directory as your pspdev.bat file and that your WAV file is named music.wav. You can change it to any name you would like and just type
wav2bgm name.wav name.bgm
You should then see a new file in the tools folder called music.bgm or whatever you named it.




This lesson I made will load a small part of the music from Super Mario 64 as the background music and then continuously loop the music until you stop it. You can start the music by pressing the triangle button, pause it by pressing the square button or stop the music completely by pressing the circle button. It will also play a small sound effect "It's a me Mario!" when you press the cross button.
(The music is kind of cheesy and not done or looped well because my Cool Edit program crashed on me and I had to use the cheap free program Audacity to record the music rip from the game and export it into a WAV file)
You can download the music file and sound effect from this lesson and the source and the wav2bgm program plus the EBOOT binaries from the link at the end of this post.

One thing to note is that the PSP is very powerful by having 8 audio channels (0-7). That means that you can have 8 different sounds and music going on a the same time on each separate channel if you would like. In this lesson, we will just be using 2 channels, 0 and 1.

Here is the Makefile:
Code:
TARGET = Donkey_Lesson_5
OBJS = main.o 	

INCDIR = 
CFLAGS = -G4 -Wall -O2 
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

LIBDIR =
LDFLAGS =
STDLIBS= -losl -lpng -lz \
		-lpspsdk -lpspctrl -lpspumd -lpsprtc -lpsppower -lpspgu -lpspaudiolib -lpspaudio -lm
LIBS=$(STDLIBS)$(YOURLIBS)


EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Donkey_Lesson_5

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Here is the source code:
Code:
//OSlib header file
#include <oslib/oslib.h>

//Necessary to create eboot
PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

//declaration of the pointers of our sounds
OSL_SOUND *music, *sound;
	
int main()
{
	//Initialization of the Oslib library
	oslInit(0);

	//Initialization of the graphics mode
	oslInitGfx(OSL_PF_8888, 1);

	//Initialization of text mode
	oslInitConsole();

	//Initialization of the Audio library
	oslInitAudio();

	//loads our sounds into memory
	music = oslLoadSoundFile("music.bgm", OSL_FMT_STREAM);
	sound = oslLoadSoundFile("sound.wav", OSL_FMT_NONE);
	
	//verification that all files are present
	if (!music || !sound)
		oslDebug("It is impossible to load one or more of the images that are required in this program. Please make sure you have all the files required in the eboot folder");

	//main while loop
	while (!osl_quit)
	{
		//To be able to draw on the screen
		oslStartDrawing();

		//To be able to use button input
		oslReadKeys();
		
		//Button commands
		if(osl_keys->pressed.triangle)
{
         oslPlaySound(music, 0);
         oslSetSoundLoop(music, 1);        
}
		if (osl_keys->pressed.square) oslPauseSound(music, -1);
		if (osl_keys->pressed.circle) oslStopSound(music);		
		if (osl_keys->pressed.cross) oslPlaySound(sound, 1);		

		//Prints text on the screen at specific coordinates
		oslPrintf_xy(0,0,"Press Triangle to listen to the Background music.");
		oslPrintf_xy(0,10,"Press Square to pause the Background music.");
		oslPrintf_xy(0,20,"Pressez Circle to stop the Background music.");
		oslPrintf_xy(0,30,"Press Cross to hear the sound effect.");

		//Ends drawing mode
		oslEndDrawing();

		//Synchronizes the screen
		oslSyncFrame();	

		//Synchronizes the Audio
		oslAudioVSync();
	}
	
	//Terminate the program
	oslEndGfx();
	oslQuit();
	return 0;
}
Source Code Explanation:

1) OSL_SOUND *music, *sound;
This line is just used to create the pointers for our sound files. It is just the same as when we created pointers for our image files in the previous lessons.

2) oslInitAudio();
This initializes the PSP's audio system.

3) music = oslLoadSoundFile("music.bgm", OSL_FMT_STREAM);
sound = oslLoadSoundFile("sound.wav", OSL_FMT_NONE);

Take a look at this code. Notice how music.bgm has OSL_FMT_STREAM next to it and sound.wav has OSL_FMT_NONE next to it. This means that music.bgm will be streamed from the memory stick and NOT loaded into the PSP's memory. Sound.wav will not be streamed from the memory stick and will be loaded into the PSP's memory. The reason for this is because background music for games are usually a lot larger in size then small WAV sounds used for effects. The advantages of having your music streamed from the memory stick is that it has no loading time as it is being streamed from the stick and it takes no space in the PSP's memory. The disadvantage of this is it does require higher CPU usage especially if your music has a high sampling rate.

4) oslPlaySound(music, 0);
This will start playing your music.bgm when the triangle button is pressed on channel 0 of your PSP's audio system.

5) oslSetSoundLoop(music, 1);
This will loop your music.bgm file unless it is given orders to stop or pause the music. The 1 just means that loop=true

6) oslPlaySound(sound, 1);
This will play your sound.wav file when the cross button is pressed on channel 1 of your PSP's audio system.

7) oslAudioVSync();
This code is just used to sync the Audio.


Here is the link to download all the music and sound files along with the wav2bgm, source and binary files you need to run the lesson.

http://www.mediafire.com/?5nmj0qmqzyz

In the next lesson, I will either show you how to create special effects with your sprites such as zooming, rotation and transparency or I will go into showing you how to create maps for your game.
__________________
Want to become a PSP Dev the easy way? Check out my tutorial for the PSP here www.psp-hacks.com/forums/viewtopic.php?id=65403
Want to learn how to create your own games on the PSP? Check out my tutorial here http://www.psp-hacks.com/forums/view...693884#p693884
Want to learn how to program C++ ? Check out that tutorial here www.psp-hacks.com/forums/viewtopic.php?id=28694
Reply With Quote