Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Using sound effects in homebrew (OSlib)

Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
User avatar
NightStar3
VIP
Posts: 364
Joined: Mon Sep 27, 2010 8:22 pm

[Tutorial] Using sound effects in homebrew (OSlib)

Post by NightStar3 » Mon Sep 27, 2010 8:54 pm

Welcome to my tutorial on using sound effects in your
PSP homebrew applications.


First, In order to use this tutorial, you need to download
the unofficial PSP graphics library name OSlib which
can be found here:

http://www.qj.net/qjnet/psp/psp-homebre ... -v111.html


(If you have windows and don't have Cygwin,
there's a way for you guys to install it as well. Just PM and I'll
take you through the steps :) . )


Firstly your code should look like this:

Code: Select all

//The main library OSLib
#include <oslib/oslib.h>
In order to use oslib in your programs,
you need to include this line. Those are your
basic header files, which allow you to use
oslib's functions.

NOTE: Those who try to include "oslib/audio.h"
will just get an error when compiling (functions aren't
fully linked).


Next add the lines:

Code: Select all

PSP_MODULE_INFO("SFX_tutorial", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
 
Those lines tell us that we are running
the files in usermode. Those lines are necessary to
create an EBOOT.PBP file.


Follow that code with:

Code: Select all

//definition of the pointers towards our sounds
OSL_SOUND *click;

Those lines tell us that we are using the variables
music and click in our program.
You can change those if you'd like but you
would have to change them in the preceding lines as well.

So now, we are entering our main thread:

Code: Select all

int main()
{
	//Initialization of the library
	oslInit(0);
 
	//Initialization of the graphic mode
	oslInitGfx(OSL_PF_8888, 1);
 
	//Initialization of the text console
	oslInitConsole();
 
	//Initialize audio
	oslInitAudio();
Those lines initialize the graphics mode
and the audio mode used in oslib.


Now for our variables:

Code: Select all

click = oslLoadSoundFile("file.wav", OSL_FMT_NONE);

In order to load the sound defined in the click
variable, we must first configure the path to the wav file.
Just change the filename to the one that you are using.


Now we enter our main loop:

Code: Select all

while (!osl_quit)
	{
		//Allow to draw
		oslStartDrawing();
 
		//Read the keys
		oslReadKeys();

		
		//Sounds commands
		
		if (osl_keys->pressed.cross) oslPlaySound(click, 1);	
                
                // It loads the sound file defined in the click variable  when the
                  //  X button is pressed

                /* The "click" line in oslPlaySound calls the variable, in your case
                     the sound effect defined in the "click" variable.
                       
                    The next pointer "1" means that it is streamed through the audio 
                    channel 1. The psp has 8 audio channels that we can use 
                    (0, 1, 2, 3, 4, 5, 6, 7). Each sound effect should use it's own channel.
                
         	*/
 
		//Display the instructions		
		oslPrintf_xy(0,30,"Press Cross to play the sound effect.");
 
		//End of the draw
		oslEndDrawing();
 
		//Synchronize the screen
		oslSyncFrame();	
 
		//For the sleep
		oslAudioVSync();
	}
With that we end our main control loop but the
code isn't finished yet.

Our last lines are:


Code: Select all


//Leave the program
	oslEndGfx();
	oslQuit();
	return 0;
}
This allows us to shut down our program.
If the command was successful,
the program would return a Zero.

The full code should look like:

Code: Select all

//The main library OSLib
#include <oslib/oslib.h>
 

PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
 
//definition of the pointers towards our sounds
OSL_SOUND *click;
	
int main()
{
	//Initialization of the library
	oslInit(0);
 
	//Initialization of the graphic mode
	oslInitGfx(OSL_PF_8888, 1);
 
	//Initialization of the text console
	oslInitConsole();
 
	//Initialize audio
	oslInitAudio();
 
	//loading the sounds
	click = oslLoadSoundFile("file.wav", OSL_FMT_NONE);
	
	
	//main loop
	while (!osl_quit)
	{
		//Allow to draw
		oslStartDrawing();
 
		//Read the keys
		oslReadKeys();
		
		//Sounds commands
		
		if (osl_keys->pressed.cross) 
                        {
                                oslPlaySound(click, 1);
                  }		
   
                // It loads the sound file defined in the click variable  when the
                  //  X button is pressed

                /* The "click" line in oslPlaySound calls the variable, in your case
                     the sound effect defined in the "click" variable.
                       
                    The next pointer "1" means that it is streamed through the audio 
                    channel 1. The psp has 8 audio channels that we can use 
                    (0, 1, 2, 3, 4, 5, 6, 7). Each sound effect should use it's own channel.
                
         	*/
 
 
		//Display the instructions
		
		oslPrintf_xy(0,30,"Press Cross to play the sound effect.");
 
		//End of the draw
		oslEndDrawing();
 
		//Synchronize the screen
		oslSyncFrame();	
 
		//For the sleep
		oslAudioVSync();
	}
	
	//Leave the program
	oslEndGfx();
	oslQuit();
	return 0;
}


In order to compile the whole code so that the psp can
read it and execute it, we must create a makefile.

Your makefile should look like this:

Code: Select all

TARGET = SFX_lesson
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 = SFX_test

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
That will generate our EBOOT file.

Be sure to place the sound effect file with the eboot.
;)


You can find sound effects here:

http://www.soundjay.com/button-sounds-1.html
(be sure to download the .wav file, not the .mp3 :P )

Once downloaded, if you didn't modify the code I posted
then rename the wav file to file.wav.
If you used your own name (in the variable) change it to that.




If you have any problems with this tutorial on how to use
sound effects in your PSP homebrew (It's useful for menu's)
Feel free to discuss it here.

If you find any bugs in the code that I have posted,
feel free to correct me. ;)



Hope it all helped,


NightStar3
Advertising

cscash241
Banned
Posts: 105
Joined: Mon Sep 27, 2010 6:52 pm

Re: [Tutorial] Using sound effects in homebrew (OSlib)

Post by cscash241 » Mon Sep 27, 2010 9:37 pm

very nice it's much more detailed than the one i made for making a hello world homebrew i made an app like this using also OSlib it lets you press different buttons to make diffrent sounds i use gun/war sounds here is the source hopefull it will be able to help someone

Code: Select all

//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 *fire, *load, *bomb, *pickup, *greenade;
	
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
                                    bomb = oslLoadSoundFile("bom36.wav", OSL_FMT_NONE);
	                  fire = oslLoadSoundFile("gun30_r.wav", OSL_FMT_NONE);
	                  load = oslLoadSoundFile("weapon02.wav", OSL_FMT_NONE);
                                    greenade = oslLoadSoundFile("greenade.wav", OSL_FMT_NONE);
                                    pickup = oslLoadSoundFile("pickup.wav", OSL_FMT_NONE);
	//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)
{
 }                                          
		if (osl_keys->pressed.down) oslPlaySound(greenade, 1);
		if (osl_keys->pressed.cross) oslPlaySound(bomb, 1);
                                    if (osl_keys->pressed.triangle) oslPlaySound(fire, 1);
                                    if (osl_keys->pressed.square) oslPlaySound(load, 1);		
                                    if (osl_keys->pressed.up) oslPlaySound(pickup, 1);
		if (osl_keys->pressed.right) oslQuit(); 
                                    //Prints text on the screen at specific coordinates

		oslPrintf_xy(0,0,"Press x to set off the bomb");
                                    oslPrintf_xy(0,10,"Press ^ to shoot");
                                    oslPrintf_xy(0,20,"Press square to reload");
                                    oslPrintf_xy(0,60,"made by cscash241 - pspgohax.co.cc");
                                    oslPrintf_xy(0,50,"boom r4");
                                    oslPrintf_xy(0,40,"press up to pickup a weapon");
                                    oslPrintf_xy(0,30,"press down to throw a grenade");
                                    oslPrintf_xy(0,70,"press right to exit");

		//Ends drawing mode
		oslEndDrawing();

		//Synchronizes the screen
		oslSyncFrame();	

		//Synchronizes the Audio
		oslAudioVSync();
	}
	
	//Terminate the program
	oslEndGfx();
	oslQuit();
	return 0;
}
Advertising

Post Reply

Return to “Programming and Security”