Feeding .mp3s into a buffer for playing in the MUSIC folder?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
sg57
Posts: 144
Joined: Fri Oct 14, 2005 2:26 pm

Feeding .mp3s into a buffer for playing in the MUSIC folder?

Post by sg57 »

I need some help. I (we if you count ShatterDome and Smerity) am/are trying to read all the .mp3's in the Music folder, and feed them into a buffer so I/we can call 'MP3_Load(mp3bufferhere);' when using libmad lib. So far this is what we have got:

Code: Select all

#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include "mp3player.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

PSP_MODULE_INFO&#40;"Mp3 Player Directory Example", 0, 1, 1&#41;;

#define printf pspDebugScreenPrintf

int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
          sceKernelExitGame&#40;&#41;;
          return 0;
&#125;
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
          int cbid;

          cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
          sceKernelRegisterExitCall  back&#40;cbid&#41;;

          sceKernelSleepThreadCB&#40;&#41;;  

          return 0;
&#125;
int SetupCallbacks&#40;void&#41; &#123;
          int thid = 0;

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
          if&#40;thid >= 0&#41; &#123;
                    sceKernelStartThread&#40;thid  , 0, 0&#41;;
          &#125;

          return thid;
&#125;

struct songinfo
&#123; char songname&#91;255&#93;;&#125; ;

struct songinfo playlist&#91;100&#93;;

int getsonglist&#40;void&#41;
&#123;
int dfd;
SceIoDirent songdir;
memset&#40;&songdir, 0, sizeof songdir&#41;;
char buffer&#91;255&#93;;

int songcount = 0, maxsongs = 0;

dfd = sceIoDopen&#40;"ms0&#58;/PSP/MUSIC/TEST/"&#41;;

while&#40;sceIoDread&#40;dfd, &songdir&#41; > 0&#41;
&#123;
//sceIoDread&#40;dfd,&songdir&#41;;

sprintf&#40;playlist&#91;songcount&#93;.songname,"%s",songdir.d_name&#41;;
maxsongs = songcount;
songcount++;

&#125; // while sceIODread
sceIoClose&#40;dfd&#41;;

return maxsongs;
&#125; // int

int main&#40;&#41;
&#123;
pspDebugScreenInit&#40;&#41;;
SetupCallbacks&#40;&#41;;
pspAudioInit&#40;&#41;;
SceCtrlData pad;

char songpathbuf&#91;255&#93;;
int songcounter = 1, change = 0, numsong = getsonglist&#40;&#41;;


MP3_Init&#40;1&#41;;

songcounter++;
sprintf&#40;songpathbuf,"ms0&#58;/PSP/MUSIC/TEST/%s",playlist&#91;songcounter&#93;.songname&#41;;

printf&#40;"songpathbuf == %s\n", songpathbuf&#41;;
MP3_Load&#40;songpathbuf&#41;;
MP3_Play&#40;&#41;;
while&#40;1&#41; &#123; //wait for X to be pressed
    sceCtrlReadBufferPositive  &#40;&pad, 1&#41;;
    if&#40;pad.Buttons & PSP_CTRL_CROSS&#41; break;

    if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; MP3_Pause&#40;&#41;;

    if&#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41; &#123; change = 1; songcounter++; &#125;

    if&#40;pad.Buttons & PSP_CTRL_LTRIGGER&#41; &#123; change = 1; songcounter--; &#125;

    if&#40;MP3_EndOfStream&#40;&#41; == 1&#41; &#123; change = 1; songcounter++; &#125;

if &#40;change == 1&#41;
&#123;
MP3_Stop&#40;&#41;;
MP3_FreeTune&#40;&#41;;
if &#40;songcounter > numsong&#41; songcounter = numsong;
if &#40;songcounter < 1&#41; songcounter = 1;

sprintf&#40;songpathbuf,"ms0&#58;/PSP/MUSIC/TEST/%s",playlist&#91;songcounter&#93;.songname&#41;;

printf&#40;"songpathbuf == %s\n", songpathbuf&#41;;

MP3_Load&#40;songpathbuf&#41;;
MP3_Play&#40;&#41;;
change = 0;
&#125; // if change == 1

&#125; // while
MP3_Stop&#40;&#41;;
//MP3_FreeTune&#40;&#41;;

sceKernelSleepThread&#40;&#41;;

return 0;
&#125;
Now, others have done this and it works, but I have edited ita little so it works with my code, and it complies alright, but i get this when running (using printf I believe):
http://forums.qj.net/attachment.php?att ... 2679&stc=1

Anyone know how I could read all the .mp3s from the MUSIC folder and load them using hte library libmad?[/code]
.: Smerity :.
Posts: 9
Joined: Sat Feb 04, 2006 11:43 pm
Contact:

Post by .: Smerity :. »

Ok, I got it to
  • Ignore non-Mp3 files
    Select previous and next files using L and R
What I haven't got it to do yet is -
  • Ability to select directories
    Reset the timer so it starts at the start of the next song
Any help on the latter two would be good...

Code: Select all

// Mp3 Directory Player, original code by Shatterdome, improved on by Smerity

#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdebug.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include "mp3player.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

PSP_MODULE_INFO&#40;"Mp3 Directory Example", 0, 1, 1&#41;; // name, other, major, minor

#define printf pspDebugScreenPrintf

int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
          sceKernelExitGame&#40;&#41;;
          return 0;
&#125;
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
          int cbid;

          cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
          sceKernelRegisterExitCallback&#40;cbid&#41;;

          sceKernelSleepThreadCB&#40;&#41;;

          return 0;
&#125;
int SetupCallbacks&#40;void&#41; &#123;
          int thid = 0;

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
          if&#40;thid >= 0&#41; &#123;
                    sceKernelStartThread&#40;thid, 0, 0&#41;;
          &#125;

          return thid;
&#125;

struct songinfo
&#123; char songname&#91;255&#93;;&#125; ;

struct songinfo playlist&#91;100&#93;; //an array of 100 structures of type songinfo &#40;which only holds one variable, but can hold as many as you want...
// This function reads all the songs in the psp/music dir and feeds them into that playlist&#91;100&#93; array, easier to work with...

int getsonglist&#40;void&#41;
&#123;
int dfd;
SceIoDirent songdir;
memset&#40;&songdir, 0, sizeof songdir&#41;;
char buffer&#91;255&#93;;

int songcount = 0, maxsongs = 0;

dfd = sceIoDopen&#40;"ms0&#58;/PSP/MUSIC/"&#41;;

while&#40;sceIoDread&#40;dfd, &songdir&#41; > 0&#41;
&#123;
//sceIoDread&#40;dfd,&songdir&#41;;

sprintf&#40;buffer,"%s",songdir.d_name&#41;;
// debug - printf&#40;"buffer == %s\n", buffer&#41;;
char *suffix = strrchr &#40;buffer, '.'&#41;;
if &#40;suffix != NULL&#41;
&#123;
if &#40;stricmp &#40;suffix, ".mp3"&#41; == 0&#41;
&#123;
sprintf&#40;playlist&#91;songcount&#93;.songname,"%s",buffer&#41;;
maxsongs = songcount;
songcount++;
&#125; // != NULL
&#125; // == 0

&#125; // while sceIODread
sceIoClose&#40;dfd&#41;;

return maxsongs;
&#125; // int

int main&#40;&#41;
&#123;
pspDebugScreenInit&#40;&#41;;
SetupCallbacks&#40;&#41;;
pspAudioInit&#40;&#41;;
SceCtrlData pad;

char songpathbuf&#91;255&#93;;
int songcounter = 0, change = 0, numsong = getsonglist&#40;&#41;;


MP3_Init&#40;1&#41;;

pspDebugScreenSetXY&#40;2, 4&#41;;
if &#40;numsong < 1&#41; printf&#40;"No songs in folder...\n\n"&#41;;

sprintf&#40;songpathbuf,"ms0&#58;/PSP/MUSIC/%s",playlist&#91;songcounter&#93;.songname&#41;;

printf&#40;"songpathbuf == %s\n", songpathbuf&#41;;
MP3_Load&#40;songpathbuf&#41;;
MP3_Play&#40;&#41;;
while&#40;1&#41; &#123;
    sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
    if&#40;pad.Buttons & PSP_CTRL_CROSS&#41; break;

    if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; MP3_Pause&#40;&#41;;

    if&#40;pad.Buttons & PSP_CTRL_RTRIGGER&#41; &#123; change = 1; songcounter++; &#125;

    if&#40;pad.Buttons & PSP_CTRL_LTRIGGER&#41; &#123; change = 1; songcounter--; &#125;

    if&#40;MP3_EndOfStream&#40;&#41; == 1&#41; &#123; change = 1; songcounter++; &#125;

if &#40;change == 1&#41;
&#123;
MP3_FreeTune&#40;&#41;;
MP3_Stop&#40;&#41;;
if &#40;songcounter > numsong&#41; songcounter = 0; // repeat from start
if &#40;songcounter < 0&#41; songcounter = 0; // compensate for . and .. but will be fixed when we can ignore non Mp3s

sprintf&#40;songpathbuf,"ms0&#58;/PSP/MUSIC/%s",playlist&#91;songcounter&#93;.songname&#41;;

// debug - printf&#40;"songpathbuf == %s\n", songpathbuf&#41;;
// debug - printf&#40;"songcounter == %i\n", songcounter&#41;;

MP3_Load&#40;songpathbuf&#41;;
MP3_Play&#40;&#41;;
change = 0;
&#125; // if change == 1

&#125; // while
MP3_Stop&#40;&#41;;

sceKernelSleepThread&#40;&#41;;

return 0;
&#125;
Post Reply