Google

Lesson 06

Adding Sound

How to use libmad to play MP3 files in your programs.

This page is the third page of a lesson written by guest writer Stephen Merity (aka Smerity). If you have stumbled upon this page without going through the first and second parts, go back and start from the beginning.

The next section shows some options you have, and also acts as a very simple user interface.

          while(1) {
                    sceCtrlReadBufferPositive(&pad, 1);
                    if(pad.Buttons & PSP_CTRL_CROSS) {
                              break;
                    } else if(pad.Buttons & PSP_CTRL_CIRCLE) {
                              MP3_Pause();
                              for(i=0; i<10; i++) {
                                        sceDisplayWaitVblankStart();
                              }
                    }

                    if (MP3_EndOfStream() == 1) {
                              MP3_Stop();
                    }
          }
We read the buttons like we have been doing since Lesson 03. If they push the [X] button, we will exit out of the loop (and effectively, exit out of our program). If the user pushes [O], then we will pause the music using the MP3_Stop() function. Finally, we check the MP3_EndOfStream() function to see if the MP3 file has finished playing. The function will return "1" if it is over and "0" if it's still playing. If it's over, we stop the playback simply so that we're not eating up CPU power when we don't need to.

Then we will enter some wrapup code for after the user exits the loop:
          MP3_Stop();
          MP3_FreeTune();

          sceKernelSleepThread();

          return 0;
}
To end our program, we will first stop the MP3, and then release the memory space of the music file we just used. Freeing up memory isn't incredibly important in this program (when the program terminates, the memory will be freed anyway), but it could be important in other homebrew programs. If you don't free the memory, it won't be available for other functions once the MP3 has finished.

So that's it for your C file. Simple, right? Now we move on to the all-important Makefile.

Remember - the Makefile has no suffix (no .exe, .doc, .txt, .tar, etc.)

The main change is the addition of the our new libraries. "lmad" is for linking libmad for decoding the Mp3. "pspaudiolib" and "pspaudio" give our program access to the PSP's audio hardware (the speakers, for instance).
TARGET = mp3
OBJS = mp3player.o main.o
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
LIBDIR =

LIBS = -lmad -lpspaudiolib -lpspaudio -lpsppower
LDFLAGS =
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = MP3 Player Example
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Now all that's left is to go into Cygwin, find your project folder, and type in the magic word, "make" (or for Firmware Version 1.50 type "make kxploit" to generate your two folders).

You will need to copy your "test.mp3" into the folder where your program will be located on the PSP in order for it to be loaded when your program runs. On Firmware Version 1.50, it goes in the folder without the "%" at the end. Be sure that your MP3's bitrate is less than or equal to 160 KBPS or you will get an error (if you do get a "recoverable frame level error (lost synchronization)" error and your sound file still plays, you probably don't need to worry about it).

You can now simply start the application and enjoy your MP3!

Be sure to add the feed to your RSS Aggregator (or Google Homepage, or Firefox Live Bookmark) to stay updated with the latest tutorials.

If you have enjoyed this tutorial and have a spare buck or two, please consider donating to the author. Or, if you have a website, link to this tutorial series (helping spread the word means more homebrew for all!).

Please contact me with your feedback on the tutorials and on what you'd like to see in the next lessons. My e-mail is smerity [at] smerity [dot] com.
Home | Lesson 01 | Lesson 02 | Lesson 03 | Lesson 04 | Lesson 05 | Lesson 06 [ 1, 2, 3 ]