MP3 Loop, more 'attach-strings' problem

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

Moderators: cheriff, TyRaNiD

Post Reply
Miki
Posts: 4
Joined: Fri Aug 29, 2008 8:59 pm

MP3 Loop, more 'attach-strings' problem

Post by Miki »

Its a stupid question, i guess, but I just can't find the answer :)

Okey, i'm going to make a casino game. The problem here is sound. I want that there are 2 songs playing in a loop.

The problem in my code is where i added :// PROBLEM IS HERE!!!!!

There should be something like this :

MP3_Load("sound" AND ADD THIS VAR songtel AND ADD THIS STRING".mp3");
so the output would be : sound2.mp3, for example, but I don't know how to 'attach' them.

Code: Select all

//Casino!
//By Miklas 'Miki' hoet
//Started on 29 Aug 2008

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspctrl.h> 
#include <pspgu.h>
#include <png.h>
#include <stdio.h>
#include "graphics.h"
#include <psppower.h>
#include "mp3player.h"
#include <pspaudio.h>


PSP_MODULE_INFO&#40;"Casino",0,1,1&#41;;

#define printf pspDebugScreenPrintf
#define MAX&#40;X,Y&#41; &#40;&#40;X&#41; > &#40;Y&#41; ? &#40;X&#41; &#58; &#40;Y&#41;&#41;
#define RGB&#40;r, g, b&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<8&#41;|&#40;&#40;b&#41;<<16&#41;&#41;
 
//  BEGIN BLOCK
/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
          sceKernelExitGame&#40;&#41;;
          return 0;
&#125;

/* Callback thread */
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;

/* Sets up the callback thread and returns its thread id */
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;

// MAIN BLOCK

int main&#40;&#41;&#123;
	SetupCallbacks&#40;&#41;;
	scePowerSetClockFrequency&#40;333,333,166&#41;;
	initGraphics&#40;&#41;;
	pspAudioInit&#40;&#41;;
	SceCtrlData pad;
	MP3_Init&#40;1&#41;;
	MP3_Load&#40;"sound1.mp3"&#41;;
	
	MP3_Play&#40;&#41;;
	int j;
	int i;
	int songs;
	int songtel;
	songtel = 1;
	songs = 2;
	int selComponent = 0;
	char filler&#91;10&#93;;
	int bgR = 0;
	int bgG = 0;
	int bgB = 0;
	Color highlightColor = RGB&#40;200,200,200&#41;;
	Color dimmedColor = RGB&#40;100,100,100&#41;;
	Color shadowColorH = RGB&#40;55,55,55&#41;;
	Color shadowColorD = RGB&#40;55,55,55&#41;;
	//Begin loop &#40;Button_press&#41;
	while &#40;0&#41;&#123;
		// MUZIEK
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
		if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; &#123;
			MP3_Pause&#40;&#41;;
			for&#40;j=0; j<10; j++&#41; &#123;
				sceDisplayWaitVblankStart&#40;&#41;;
			&#125;
		&#125;
		if &#40;MP3_EndOfStream&#40;&#41; == 1&#41; &#123;
			MP3_Stop&#40;&#41;;
			songtel++;
			if &#40;songtel > songs&#41;&#123;
				songtel = 1;
			&#125;
			// PROBLEM IS HERE!!!!!
			MP3_Play&#40;&#41;;
		&#125;
		// MUZIEK
		//sceCtrlReadBufferPositive&#40;&pad, 1&#41;;  //in commentaar, bij muziek eerder gebruikt
		if&#40;pad.Buttons & PSP_CTRL_UP&#41; &#123;
			if&#40;selComponent > 0&#41; &#123;
				selComponent--;
			&#125;
			for&#40;i=0; i<10; i++&#41; &#123;
				sceDisplayWaitVblankStart&#40;&#41;;
			&#125;
		&#125; else if&#40;pad.Buttons & PSP_CTRL_DOWN&#41; &#123;
			if&#40;selComponent < 2&#41; &#123;
				selComponent++;
			&#125;
			for&#40;i=0; i<10; i++&#41; &#123;
				sceDisplayWaitVblankStart&#40;&#41;;
			&#125;
		&#125; 
		if&#40;pad.Buttons & PSP_CTRL_RIGHT&#41; &#123;
			switch&#40;selComponent&#41; &#123;
				case 0&#58;
					bgR++;
				break;
				case 1&#58;
					bgG++;
				break;
				case 2&#58;
					bgB++;
				break;
				default&#58;
					//SHOULD NEVER EXECUTE
				break;
			&#125;
		&#125; else if&#40;pad.Buttons & PSP_CTRL_LEFT&#41; &#123;
			switch&#40;selComponent&#41; &#123;
				case 0&#58;
					bgR--;
				break;
				case 1&#58;
					bgG--;
				break;
				case 2&#58;
					bgB--;
				break;
				default&#58;
					//SHOULD NEVER EXECUTE
				break;
			&#125;
		&#125; 		
		if&#40;bgR < 0&#41; &#123;
			bgR = 0;
		&#125; else if&#40;bgR > 255&#41; &#123;
			bgR = 255;
		&#125;
		if&#40;bgG < 0&#41; &#123;
			bgG = 0;
		&#125; else if&#40;bgG > 255&#41; &#123;
			bgG = 255;
		&#125;
		if&#40;bgB < 0&#41; &#123;
			bgB = 0;
		&#125; else if&#40;bgB > 255&#41; &#123;
			bgB = 255;
		&#125; 
		fillScreenRect&#40;RGB&#40;bgR, bgG, bgB&#41;,0,0,SCREEN_WIDTH, SCREEN_HEIGHT&#41;;
		sprintf&#40;filler, "RED&#58; %i", bgR&#41;;
		if&#40;selComponent == 0&#41; &#123;
			printTextScreen&#40;11, 10, filler, shadowColorH&#41;;
			printTextScreen&#40;10, 10, filler, highlightColor&#41;;
		&#125; else &#123;
			printTextScreen&#40;11, 10, filler, shadowColorD&#41;;
			printTextScreen&#40;10, 10, filler, dimmedColor&#41;;
		&#125;                
		sprintf&#40;filler, "GREEN&#58; %i", bgG&#41;;
		if&#40;selComponent == 1&#41; &#123;
			printTextScreen&#40;11, 20, filler, shadowColorH&#41;;
			printTextScreen&#40;10, 20, filler, highlightColor&#41;;
		&#125; else &#123;
			printTextScreen&#40;11, 20, filler, shadowColorD&#41;;
			printTextScreen&#40;10, 20, filler, dimmedColor&#41;;
		&#125;
		sprintf&#40;filler, "BLUE&#58; %i", bgB&#41;;
		if&#40;selComponent == 2&#41; &#123;
			printTextScreen&#40;11, 30, filler, shadowColorH&#41;;
			printTextScreen&#40;10, 30, filler, highlightColor&#41;;
		&#125; else &#123;
			printTextScreen&#40;11, 30, filler, shadowColorD&#41;;
			printTextScreen&#40;10, 30, filler, dimmedColor&#41;;
		&#125;  
		flipScreen&#40;&#41;;
		for&#40;i=0; i<1; i++&#41; &#123;
			sceDisplayWaitVblankStart&#40;&#41;;
		&#125;
	&#125;
	return 0;
&#125; 

// MAIN BLOCK
Thx for help :)
User avatar
jean
Posts: 489
Joined: Sat Jan 05, 2008 2:44 am

Post by jean »

not a PSP related question. Go learn basics. As this is your first post, take this as a bonus:
google for strcat, sprintf
Miki
Posts: 4
Joined: Fri Aug 29, 2008 8:59 pm

Post by Miki »

I have tried that before, but it doesn't seem to work. Sorry if its in the wrong section ;)

Code: Select all

			char toload&#91;80&#93;;
			strcat &#40;toload,"sound"&#41;;
			strcat &#40;toload,&#40;char&#41;songtel&#41;;
			strcat &#40;toload,".mp3"&#41;;
			MP3_Load&#40;toload&#41;;
Then i get the following error :

main.c:96: let op: passing argument 2 of ‘strcat’ makes pointer from integer without a cast

edit : and running the EBOOT.PBP file on my psp results that my PSP keeps blank screen, and then goes out.
kralyk
Posts: 114
Joined: Sun Apr 06, 2008 8:18 pm
Location: Czech Republic, central EU

Post by kralyk »

huh, try this:

Code: Select all

char toload&#91;80&#93;;
//or you might as well want to use char * toload = ""; for unlimited string
sprintf&#40;toload, "sound%d.mp3", songtel&#41;;
MP3_Load&#40;toload&#41;;
for more information:
http://www.cplusplus.com/reference/clib ... rintf.html

have a look at some c/c++ reference, tutorials or books...
...sorry for my english...
whistler
Posts: 39
Joined: Tue Mar 04, 2008 7:08 am

Post by whistler »

Code: Select all

 //Begin loop &#40;Button_press&#41;
   while &#40;0&#41;&#123;
      // MUZIEK
      sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
      if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; &#123;
         MP3_Pause&#40;&#41;;
         for&#40;j=0; j<10; j++&#41; &#123;
            sceDisplayWaitVblankStart&#40;&#41;;
         &#125;
      .... 
the code inside that loop will never be executed
Miki
Posts: 4
Joined: Fri Aug 29, 2008 8:59 pm

Post by Miki »

kralyk wrote:huh, try this:

Code: Select all

char toload&#91;80&#93;;
//or you might as well want to use char * toload = ""; for unlimited string
sprintf&#40;toload, "sound%d.mp3", songtel&#41;;
MP3_Load&#40;toload&#41;;
for more information:
http://www.cplusplus.com/reference/clib ... rintf.html

have a look at some c/c++ reference, tutorials or books...
This works, thanks :)
whistler wrote:

Code: Select all

 //Begin loop &#40;Button_press&#41;
   while &#40;0&#41;&#123;
      // MUZIEK
      sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
      if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; &#123;
         MP3_Pause&#40;&#41;;
         for&#40;j=0; j<10; j++&#41; &#123;
            sceDisplayWaitVblankStart&#40;&#41;;
         &#125;
      .... 
the code inside that loop will never be executed
Why not, and in what loop do you mean. The for loop?

the 'pauze' function works perfectly...
hibbyware
Posts: 78
Joined: Wed Mar 28, 2007 10:29 am

Post by hibbyware »

Miki wrote:
whistler wrote:

Code: Select all

 //Begin loop &#40;Button_press&#41;
   while &#40;0&#41;&#123;
      // MUZIEK
      sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
      if&#40;pad.Buttons & PSP_CTRL_CIRCLE&#41; &#123;
         MP3_Pause&#40;&#41;;
         for&#40;j=0; j<10; j++&#41; &#123;
            sceDisplayWaitVblankStart&#40;&#41;;
         &#125;
      .... 
the code inside that loop will never be executed
Why not, and in what loop do you mean. The for loop?

the 'pauze' function works perfectly...
Are you sure?

Have a quick look at http://www.cprogramming.com/reference/while.html and all should become clear,
kralyk
Posts: 114
Joined: Sun Apr 06, 2008 8:18 pm
Location: Czech Republic, central EU

Post by kralyk »

Miki, if you dont mind this advise:
dont just put toghether pieces of code which somehow might or might not work, make sure you understand what it actually means and what it actually does. Well, you dont have to be a c/c++ expert really, but you should know what youre doing at least on a basic level...
...sorry for my english...
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

kralyk wrote:huh, try this:

Code: Select all

char toload&#91;80&#93;;
//or you might as well want to use char * toload = ""; for unlimited string
sprintf&#40;toload, "sound%d.mp3", songtel&#41;;
MP3_Load&#40;toload&#41;;
Regarding the comment:
using char* toload = ""; will just make sprintf to it crash eventually.
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
Miki
Posts: 4
Joined: Fri Aug 29, 2008 8:59 pm

Post by Miki »

About the while loop, you were right, it had to be a while (1) instead of (0),


I'll first go buy some book about C to learn the basics of the language.
hibbyware
Posts: 78
Joined: Wed Mar 28, 2007 10:29 am

Post by hibbyware »

No need to go buy a book as there is plenty of free resources to learn from around the net but saying that you do get what you pay for when buying a book,
kralyk
Posts: 114
Joined: Sun Apr 06, 2008 8:18 pm
Location: Czech Republic, central EU

Post by kralyk »

Raphael wrote: Regarding the comment:
using char* toload = ""; will just make sprintf to it crash eventually.
Umm ... it didnt crash for me...
What do you recommend?
...sorry for my english...
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

Then you were just lucky you didn't overwrite important memory.
I just recommend sticking with your first solution and making sure that the string doesn't get longer than 79 chars.
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
kralyk
Posts: 114
Joined: Sun Apr 06, 2008 8:18 pm
Location: Czech Republic, central EU

Post by kralyk »

Ok.
Otherwise I'd have to count the length of the future string and use malloc wouldnt I?
But yes, the static solution is much better for this purpose...
...sorry for my english...
Post Reply