Editing Lesson 06 - Adding Sound

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 8: Line 8:


To do this, open Cygwin, and type
To do this, open Cygwin, and type
<pre>svn checkout svn://svn.ps2dev.org/psp/trunk/libmad</pre>
svn checkout svn://svn.ps2dev.org/psp/trunk/libmad
You should see a long list of filenames scroll by as the package is downloaded.
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:
Now we will switch into the libmad directory and compile the library:
<pre>cd libmad  
cd libmad  
make</pre>
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.
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.
<pre>cp -Rf ./include /usr/local/pspdev/psp/
cp -Rf ./include /usr/local/pspdev/psp/
cp -Rf ./lib/libmad.a /usr/local/pspdev/psp/lib</pre>
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..."
NOTE: There is a space between "./include" and "/usr..." and between "libmad.a" and "/usr..."


Line 22: Line 22:


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):
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):
<pre>//DO NOT ADD THIS TO YOUR PROGRAM
//DO NOT ADD THIS TO YOUR PROGRAM
//IT IS EXAMPLE CODE
//IT IS EXAMPLE CODE
mp3player.c</pre>
mp3player.c
Line 76 - <code>/* void void MP3setStubs to end of function */</code> - specific to stubs again, see mp3player.h
Line 76 - /* void void MP3setStubs to end of function */ - specific to stubs again, see mp3player.h


===== mp3player.h =====
mp3player.h
Line 10 - <code>#include "../../codec.h"</code> - specific to PSPMediaCenter<br>
Line 10 - #include "../../codec.h" - specific to PSPMediaCenter
Line 17 - <code>void MP3setStubs(codecStubs * stubs);</code> - specific to PSPMediaCenter<br>
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.
Other than that, it was very clean. John_K has done a great job in porting it.


Now, on to the fun part. Create main.c in your editor of choice, and begin with your comments:
Now, on to the fun part. Create main.c in your editor of choice, and begin with your comments:
<pre>// Mp3 Player Sample on PSP
// Mp3 Player Sample on PSP
// By *YOUR NAME HERE*</pre>
// 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.
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.
<pre>#include <pspkernel.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspdebug.h>
Line 46: Line 46:


PSP_MODULE_INFO("Mp3 Player Example", 0, 1, 1);
PSP_MODULE_INFO("Mp3 Player Example", 0, 1, 1);
#define printf pspDebugScreenPrintf</pre>
#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.
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.


nd now on to what I like to call the twilight zone. I call it that because at this moment, I have no clue how it works or why, but I do intend finding out some day. Anyway, these functions are necessary for your code.
nd now on to what I like to call the twilight zone. I call it that because at this moment, I have no clue how it works or why, but I do intend finding out some day. Anyway, these functions are necessary for your code.


<pre>// TWILIGHT ZONE! <do doo do doo>
// TWILIGHT ZONE! <do doo do doo>
/* Exit callback */
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
int exit_callback(int arg1, int arg2, void *common) {
Line 81: Line 81:
           return thid;
           return thid;
}
}
// END OF TWILIGHT ZONE! <do doo do do></pre>
// END OF TWILIGHT ZONE! <do doo do do>
Now onto the main section of your code. First thing we will do is set the PSP's clock speed to full. Please note that this will not damage your PSP in any way, as it has only been underclocked to this speed by Sony, the reason was most likely battery life (for more information, see Lesson 05. Another notable addition is the "pspAudioInit()" function. This is similar to the "pspDebugScreenInit()" function that we have always called. Basically, it sets up the speakers and everything to be ready to handle our sound output.
Now onto the main section of your code. First thing we will do is set the PSP's clock speed to full. Please note that this will not damage your PSP in any way, as it has only been underclocked to this speed by Sony, the reason was most likely battery life (for more information, see Lesson 05. Another notable addition is the "pspAudioInit()" function. This is similar to the "pspDebugScreenInit()" function that we have always called. Basically, it sets up the speakers and everything to be ready to handle our sound output.
<pre>int main() {
int main() {
           scePowerSetClockFrequency(333, 333, 166);
           scePowerSetClockFrequency(333, 333, 166);


Line 91: Line 91:
           pspAudioInit();
           pspAudioInit();
           SceCtrlData pad;
           SceCtrlData pad;
           int i;</pre>
           int i;
Now for the shiny new stuff!
Now for the shiny new stuff!
           <pre>MP3_Init(1);
           MP3_Init(1);
           MP3_Load("test.mp3");
           MP3_Load("test.mp3");
           MP3_Play();</pre>
           MP3_Play();
MP3_Init() initialises the structures used by libmad and sets which channel the PSP will be playing back on (in this case, Channel 1). MP3_Load() is pretty self descriptive, it loads your MP3 file (either change this to your MP3 filename or change your MP3 to be called "test.mp3"). MP3_Play() starts the sound playing through the speakers.
MP3_Init() initialises the structures used by libmad and sets which channel the PSP will be playing back on (in this case, Channel 1). MP3_Load() is pretty self descriptive, it loads your MP3 file (either change this to your MP3 filename or change your MP3 to be called "test.mp3"). MP3_Play() starts the sound playing through the speakers.


he next section shows some options you have, and also acts as a very simple user interface.
he next section shows some options you have, and also acts as a very simple user interface.
<pre>
 
           while(1) {
           while(1) {
                     sceCtrlReadBufferPositive(&pad, 1);
                     sceCtrlReadBufferPositive(&pad, 1);
Line 114: Line 114:
                               MP3_Stop();
                               MP3_Stop();
                     }
                     }
           }</pre>
           }
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.
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:
Then we will enter some wrapup code for after the user exits the loop:
           <pre>MP3_Stop();
           MP3_Stop();
           MP3_FreeTune();
           MP3_FreeTune();


Line 124: Line 124:


           return 0;
           return 0;
}</pre>
}
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.
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.


Line 132: Line 132:


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).
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).
<pre>TARGET = mp3
TARGET = mp3
OBJS = mp3player.o main.o
OBJS = mp3player.o main.o
CFLAGS = -O2 -G0 -Wall
CFLAGS = -O2 -G0 -Wall
Line 144: Line 144:
PSP_EBOOT_TITLE = MP3 Player Example
PSP_EBOOT_TITLE = MP3 Player Example
PSPSDK=$(shell psp-config --pspsdk-path)
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak</pre>
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).
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).


Please note that all contributions to PSP Developer wiki are considered to be released under the GNU Free Documentation License 1.2 (see PSP Developer wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following hCaptcha:

Cancel Editing help (opens in new window)