Lesson 06
Adding Sound
How to use libmad to play MP3 files in your programs.
This lesson was written by guest writer Stephen Merity (aka Smerity), so thanks to him for his great contribution.
First of all, thanks goes to John_K for porting libmad to the PSP and for developing PSPMediaCenter (which I used some of the code from). Also, a special thank you to seventoes, who posted a while back on PS2Dev.org asking for help with libmad with code that was so close to working!
This tutorial is a simple walkthrough concerning playing music in your programs. Sound FX and background music are two aspects that give an application a polished feeling, but they are often
overlooked. Whether that is because a developer is rushing to get a release out of the door or whether it is because they simply don't know how to implement sound (or have any fitting music to
to with their application) is unclear. Hopefully this tutorial will help some people get sound into their programs.
The first thing we are going to have to do is grab libmad from SVN. libmad is an MPEG audio decoder released under the GPL.
It has been ported to the PSP by John_K.
To do this, open Cygwin, and type
Now we will switch into the libmad directory and compile the library:
This will copy all of the files into their appropriate place. The "-Rf" tag stands for "Recursive" and "final" -- it will just make sure that you copy everything and that you won't get any permission errors (which some users have reported as a problem without these flags).
Next, download and extract some necessary files into a new project folder. Inside are two files, mp3player.c and mp3player.h which were obtained from John_K's PSPMediaCenter, I made some slight changes to the files as follows (you don't need to do anything with this, it's just in case you were wondering):
Now, on to the fun part. Create main.c in your editor of choice, and begin with your comments:
Continue this tutorial with part two.
First of all, thanks goes to John_K for porting libmad to the PSP and for developing PSPMediaCenter (which I used some of the code from). Also, a special thank you to seventoes, who posted a while back on PS2Dev.org asking for help with libmad with code that was so close to working!
To do this, open Cygwin, and type
svn checkout svn://svn.ps2dev.org/psp/trunk/libmad
You should see a long list of filenames scroll by as the package is downloaded.Now we will switch into the libmad directory and compile the library:
cd libmad
make
The next bit is a little deviation from the routine we used to install our libraries in Lesson 04. Usually, you could type "make install"
and it would automatically install the files to their appropriate directories. Unfortunately, libmad's install script appears to be broken at the moment. Not to worry though, we can install
it manually ourselves.
make
cp -Rf ./include /usr/local/pspdev/psp/
cp -Rf ./lib/libmad.a /usr/local/pspdev/psp/lib
NOTE: There is a space between "./include" and "/usr..." and between "libmad.a" and "/usr..."cp -Rf ./lib/libmad.a /usr/local/pspdev/psp/lib
This will copy all of the files into their appropriate place. The "-Rf" tag stands for "Recursive" and "final" -- it will just make sure that you copy everything and that you won't get any permission errors (which some users have reported as a problem without these flags).
Next, download and extract some necessary files into a new project folder. Inside are two files, mp3player.c and mp3player.h which were obtained from John_K's PSPMediaCenter, I made some slight changes to the files as follows (you don't need to do anything with this, it's just in case you were wondering):
//DO NOT ADD THIS TO YOUR PROGRAM
//IT IS EXAMPLE CODE
mp3player.c
Line 76 - /* void void MP3setStubs to end of function */ - specific to stubs again, see mp3player.h
mp3player.h
Line 10 - #include "../../codec.h" - specific to PSPMediaCenter
Line 17 - void MP3setStubs(codecStubs * stubs); - specific to PSPMediaCenter
Other than that, it was very clean. John_K has done a great job in porting it.//IT IS EXAMPLE CODE
mp3player.c
Line 76 - /* void void MP3setStubs to end of function */ - specific to stubs again, see mp3player.h
mp3player.h
Line 10 - #include "../../codec.h" - specific to PSPMediaCenter
Line 17 - void MP3setStubs(codecStubs * stubs); - specific to PSPMediaCenter
Now, on to the fun part. Create main.c in your editor of choice, and begin with your comments:
// Mp3 Player Sample on PSP
// By *YOUR NAME HERE*
The next little section should be standard to you. The only new includes are the "pspaudio.h" and "pspaudiolib.h." These headers are needed for the audio-specific functions that we will
use in our program.
// By *YOUR NAME HERE*
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <psppower.h>
#include "mp3player.h"
PSP_MODULE_INFO("Mp3 Player Example", 0, 1, 1);
#define printf pspDebugScreenPrintf
You will also see that we included one of the files that you downloaded, "mp3player.h" (make sure that it's in the same directory as our source file). We also defined printf and did our
PSP_MODULE_INFO.#include <pspctrl.h>
#include <pspdebug.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include <psppower.h>
#include "mp3player.h"
PSP_MODULE_INFO("Mp3 Player Example", 0, 1, 1);
#define printf pspDebugScreenPrintf
Continue this tutorial with part two.
