Trouble with libmad

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

Moderators: cheriff, TyRaNiD

Post Reply
AllSystemGo
Posts: 26
Joined: Sat Jun 02, 2007 1:15 am

Trouble with libmad

Post by AllSystemGo »

Ok here is what I would like to accomplish and what I did right now to try to get there.

All I want to do is play a MP3 at a specified time, like and alarm, then stop and continue what I was doing.

Code: Select all

void AlarmManager::PlaySound()
{
	pspAudioInit();
	
	MP3_Init(1);
	MP3_Load("data/sound/Alarm.mp3");
	MP3_Play();
	
                while(int i = 1)
                {
                   if(MP3_EndOfStream() == 1)
	   {
		MP3_Stop();
		i = 0;				
	    }
	}
	MP3_FreeTune();
}
Right now all it does is play the sound then stops. The PSP doesn't go back and then it' shuts down.

I call that method from the main.

2- I Display a clock at the top of my screen

Code: Select all

   pspTime time;
   
   while(isrunning){
      sceRtcGetCurrentClockLocalTime(&time);
		
      char timeText[8];

      SDL_BlitSurface(background, NULL, ecran, &background_position);
      SDL_FreeSurface(textTime);
		
      snprintf(timeText, 8, "%02i : %02i", time.hour, time.minutes);
      SDL_Surface *textTime = TTF_RenderText_Shaded(timeFont, timeText, couleurblanc, couleurnoir);

       SDL_BlitSurface(textTime, NULL, ecran, &txttime_position);

    }
This is all in my main loop, for some reason the clock shows up for like 2 minutes then dissapear... Can't figure out why!

Cheers
AllSystemGo
Posts: 26
Joined: Sat Jun 02, 2007 1:15 am

Post by AllSystemGo »

Anyone??
Mihawk
Posts: 29
Joined: Tue Apr 03, 2007 2:04 am

Post by Mihawk »

What's the magic word?
AllSystemGo
Posts: 26
Joined: Sat Jun 02, 2007 1:15 am

Post by AllSystemGo »

abracadabra.. Please!
Mihawk
Posts: 29
Joined: Tue Apr 03, 2007 2:04 am

Post by Mihawk »

The thread sounds much friendlier now, but sorry, I don't know the answer.
Actually I just wanted to point out, that it's not very nice to bump a thread that soon (not even 1 day) after you've postet your question. If no one replies, then maybe no one knows.
AllSystemGo
Posts: 26
Joined: Sat Jun 02, 2007 1:15 am

Post by AllSystemGo »

Thank you for your answer Mihawk.

But I would like to know the "correct" and polite way to post over here, cause it seems every forums has his own way or rulke about that, and from now on I would like to be able to post the correct way here.

And I'm sorry if I bumped that early, but I am stuck and I wanted to move foward with that. Anyways sorry for all that!!

Thank anyways
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Are you running in psplink? If the PSP is shutting down, you are likely getting an exception and you can just find out where.
AllSystemGo
Posts: 26
Joined: Sat Jun 02, 2007 1:15 am

Post by AllSystemGo »

Nope I'm not using psplink. Can you tell me exactly what it does???
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

You have

Code: Select all

      SDL_FreeSurface(textTime); 
But textTime seems to be set AFTER this point. Where's the check to see if textTime has yet been initialized? Maybe it's in another part of the code, but we can't see that from the two snippets you posted.
AllSystemGo
Posts: 26
Joined: Sat Jun 02, 2007 1:15 am

Post by AllSystemGo »

Thank you JF I'll post more of my code then.

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include <pspdisplay.h>
#include <psppower.h>
#include <psprtc.h>

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string>
#include <list>
#include <iostream>

//#include "Calculator.h"
#include "alarm.h"
#include "alarmmanager.h"

using namespace std;

PSP_MODULE_INFO&#40;"iPDA", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;

static bool isrunning = true;
static int curWindow = 1;

// Exit callback
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
	isrunning = false;
	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;

int main&#40;&#41; &#123;
	//Initialize the controler and the PSP Speed
    SceCtrlData pad;
    scePowerSetClockFrequency&#40;333, 333, 166&#41;;
    sceCtrlSetSamplingCycle&#40;0&#41;;
    sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;
	
	char timeText&#91;8&#93;;
	int thread_id;
		
	//Initialize the PSP Time
	pspTime time;
    
	//Initialize the SDL Library &#40;audio and video&#41;
    if&#40;SDL_Init&#40;SDL_INIT_VIDEO&#41; < 0&#41;
		cout << stderr << "Couldn't initialize SDL&#58; " << SDL_GetError&#40;&#41; << endl;

    //Initialize the SDL True Type Font Library
    if&#40;TTF_Init&#40;&#41; < 0&#41;
		cout << stderr << "Couldn't initialize TTF&#58; " << SDL_GetError&#40;&#41; << endl;

    //Initialize the font color.
    SDL_Color couleurblanc = &#123;255, 255, 255&#125;;
	SDL_Color couleurnoir = &#123;0, 0, 0&#125;;
	
	//Initialize the position of the text.
    SDL_Rect txtcalendar_position;
    txtcalendar_position.x = 130;
    txtcalendar_position.y = 146;
	
    SDL_Rect txtconfig_position;
    txtconfig_position.x = 232;
    txtconfig_position.y = 146;

    SDL_Rect txtcalculator_position;
    txtcalculator_position.x = 307;
    txtcalculator_position.y = 146;

	SDL_Rect txtweather_position;
    txtweather_position.x = 119;
    txtweather_position.y = 240;

    SDL_Rect txtcontact_position;
    txtcontact_position.x = 228;
    txtcontact_position.y = 240;
	
	SDL_Rect txtalarm_position;
    txtalarm_position.x = 319;
    txtalarm_position.y = 240;
	
	SDL_Rect txttime_position;
	txttime_position.x = 186;
	txttime_position.y = 20;
	
	//Calculatrice
	SDL_Rect txtZero_position;
	txtZero_position.x = 19;
	txtZero_position.y = 236;
	
	SDL_Rect txtDot_position;
	txtDot_position.x = 66;
	txtDot_position.y = 210;

	SDL_Rect txtClear_position;
	txtClear_position.x = 113;
	txtClear_position.y = 231;
	
	
	//Initialize the position of the images.
    SDL_Rect background_position;
    background_position.x = 0;
    background_position.y = 0;

    SDL_Rect calendar_position;
    calendar_position.x = 125;
    calendar_position.y = 80;
	
	SDL_Rect tools_position;
    tools_position.x = 218;
    tools_position.y = 80;
	
	SDL_Rect calculator_position;
	calculator_position.x = 305;
	calculator_position.y = 80;

	SDL_Rect weather_position;
    weather_position.x = 125;
    weather_position.y = 170;

	SDL_Rect contact_position;
    contact_position.x = 218;
    contact_position.y = 170;
	
	SDL_Rect alarm_position;
	alarm_position.x = 305;
	alarm_position.y = 170;
	
	SDL_Rect cal_position;
	cal_position.x = 0;
	cal_position.y = 0;

    //Initialize the mouse.
    SDL_Rect souris;
    souris.x = 239;
    souris.y = 134;

    //Initialize the Joystick.
    SDL_Rect posJoy;
	posJoy.x = pad.Lx-128;
    posJoy.y = pad.Ly-128;

    //Chargement des images.
	SDL_Surface *background, *mousePointer, *iconCalendar, *iconConfig, *iconWeather, *iconContact, *iconCalculator, *iconAlarm,
	            *Calculatrice;
	
    SDL_Surface *ecran = SDL_SetVideoMode&#40;480, 272, 32, SDL_HWSURFACE | SDL_DOUBLEBUF&#41;;
    background = SDL_DisplayFormat&#40;IMG_Load&#40;"data/gfx/blackbackground.png"&#41;&#41;;
    mousePointer = SDL_DisplayFormatAlpha&#40;IMG_Load&#40;"data/gfx/mouse.png"&#41;&#41;;
    iconCalendar = SDL_DisplayFormatAlpha&#40;IMG_Load&#40;"data/gfx/calendar.png"&#41;&#41;;
    iconConfig = SDL_DisplayFormatAlpha&#40;IMG_Load&#40;"data/gfx/tools.png"&#41;&#41;;
    iconWeather = SDL_DisplayFormatAlpha&#40;IMG_Load&#40;"data/gfx/weather.png"&#41;&#41;;
    iconContact = SDL_DisplayFormatAlpha&#40;IMG_Load&#40;"data/gfx/contact.png"&#41;&#41;;
	iconCalculator = SDL_DisplayFormatAlpha&#40;IMG_Load&#40;"data/gfx/calculator.png"&#41;&#41;;
	iconAlarm = SDL_DisplayFormatAlpha&#40;IMG_Load&#40;"data/gfx/clock.png"&#41;&#41;;
	Calculatrice = SDL_DisplayFormatAlpha&#40;IMG_Load&#40;"data/gfx/calculatrice.png"&#41;&#41;;
	
    //Chargement des polices et du texte a afficher.
    TTF_Font *font = TTF_OpenFont&#40;"data/font/Rippen.ttf", 15&#41;;
	TTF_Font *timeFont = TTF_OpenFont&#40;"data/font/Rippen.ttf", 45&#41;;
	TTF_Font *twentyFont = TTF_OpenFont&#40;"data/font/Rippen.ttf", 20&#41;;
	TTF_Font *twenty5Font = TTF_OpenFont&#40;"data/font/Rippen.ttf", 25&#41;;
	TTF_Font *thirtyFont = TTF_OpenFont&#40;"data/font/Rippen.ttf", 30&#41;;
	TTF_Font *thirty5Font = TTF_OpenFont&#40;"data/font/Rippen.ttf", 35&#41;;
	TTF_Font *fortyFont = TTF_OpenFont&#40;"data/font/Rippen.ttf", 40&#41;;
	TTF_Font *fifthy5Font = TTF_OpenFont&#40;"data/font/Rippen.ttf", 55&#41;;
	
    SDL_Surface *textCalendar = TTF_RenderText_Shaded&#40;font, "Calendar", couleurblanc, couleurnoir&#41;;
    SDL_Surface *textConfig = TTF_RenderText_Shaded&#40;font, "Config", couleurblanc, couleurnoir&#41;;
    SDL_Surface *textWeather = TTF_RenderText_Shaded&#40;font, "Temperature", couleurblanc, couleurnoir&#41;;
    SDL_Surface *textContact = TTF_RenderText_Shaded&#40;font, "Contact", couleurblanc, couleurnoir&#41;;
    SDL_Surface *textCalculator = TTF_RenderText_Shaded&#40;font, "Calculator", couleurblanc, couleurnoir&#41;;
    SDL_Surface *textAlarm = TTF_RenderText_Shaded&#40;font, "Alarm", couleurblanc, couleurnoir&#41;;
	SDL_Surface *textZero = TTF_RenderText_Blended&#40;twentyFont, "0", couleurblanc&#41;;
	SDL_Surface *textDot = TTF_RenderText_Blended&#40;fifthy5Font, ".", couleurblanc&#41;;
	SDL_Surface *textClear = TTF_RenderText_Blended&#40;twenty5Font, "c", couleurblanc&#41;;
	SDL_Surface *textTime = NULL;

    //Initialisation du mode Debug de la PSP
	pspDebugScreenInit&#40;&#41;;
    SetupCallbacks&#40;&#41;;
	
    
    //Declare alarm
    /*pspTime alarmTime;
    sceRtcGetCurrentClockLocalTime&#40;&alarmTime&#41;;
    alarmTime.minutes++;

	AlarmManager Alarms;
    Alarms.Add&#40;new Alarm&#40;false, "Marimba", false, "alarme 1", alarmTime&#41;&#41;;
	alarmTime.minutes++;
	Alarms.Add&#40;new Alarm&#40;false, "Marimba2", false, "alarme 2", alarmTime&#41;&#41;;*/
	
	//Alarms.Save&#40;&#41;;
	//list<Alarm*> alarms = Alarms.Check&#40;time&#41;;
 
    //Start the loop
	
    while&#40;isrunning&#41;
    &#123;		
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
		sceRtcGetCurrentClockLocalTime&#40;&time&#41;;

		if&#40;&#40;pad.Lx < 35&#41; && &#40;souris.x > 0&#41;&#41;
		&#123;
			souris.x = souris.x-3;
		&#125;
		else if&#40;&#40;pad.Lx > 200&#41; && &#40;souris.x < 480&#41;&#41;
		&#123;
			souris.x = souris.x+3;
		&#125;
		
		if&#40;&#40;pad.Ly < 35&#41; && &#40;souris.y > 0&#41;&#41;
		&#123;
			souris.y = souris.y-3;
		&#125;
		else if&#40;&#40;pad.Ly > 200&#41; && &#40;souris.y < 480&#41;&#41;
		&#123;
			souris.y = souris.y+3;
		&#125;
		
		if&#40;souris.x >= 480&#41;&#40;souris.x = 480&#41;;
		if&#40;souris.y >= 272&#41;&#40;souris.y = 272&#41;;
		
		SDL_BlitSurface&#40;background, NULL, ecran, &background_position&#41;;
		//if &#40;textTime != NULL&#41; SDL_FreeSurface&#40;textTime&#41;;
		
		snprintf&#40;timeText, 8, "%02i &#58; %02i", time.hour, time.minutes&#41;;
		SDL_Surface *textTime = TTF_RenderText_Shaded&#40;timeFont, timeText, couleurblanc, couleurnoir&#41;;

		SDL_BlitSurface&#40;textTime, NULL, ecran, &txttime_position&#41;;
	
		if &#40;curWindow == 1&#41;
		&#123;
			if &#40;pad.Buttons & PSP_CTRL_CROSS&#41;
			&#123;
				if &#40;&#40;souris.x > calculator_position.x&#41; && &#40;souris.x < &#40;calculator_position.x + 64&#41;&#41; &&
				    &#40;souris.y > calculator_position.y&#41; && &#40;souris.y < &#40;calculator_position.y + 64&#41;&#41;&#41;
				&#123;
					curWindow = 2;
				&#125;
			&#125;
			
			SDL_BlitSurface&#40;iconCalendar, NULL, ecran, &calendar_position&#41;;
			SDL_BlitSurface&#40;textCalendar, NULL, ecran, &txtcalendar_position&#41;;
			SDL_BlitSurface&#40;iconConfig, NULL, ecran, &tools_position&#41;;
			SDL_BlitSurface&#40;textConfig, NULL, ecran, &txtconfig_position&#41;;
			SDL_BlitSurface&#40;iconWeather, NULL, ecran, &weather_position&#41;;
			SDL_BlitSurface&#40;textWeather, NULL, ecran, &txtweather_position&#41;;
			SDL_BlitSurface&#40;iconContact, NULL, ecran, &contact_position&#41;;
			SDL_BlitSurface&#40;textContact, NULL, ecran, &txtcontact_position&#41;;
			SDL_BlitSurface&#40;iconCalculator, NULL, ecran, &calculator_position&#41;;
			SDL_BlitSurface&#40;textCalculator, NULL, ecran, &txtcalculator_position&#41;;
			SDL_BlitSurface&#40;iconAlarm, NULL, ecran, &alarm_position&#41;;
			SDL_BlitSurface&#40;textAlarm, NULL, ecran, &txtalarm_position&#41;;
		&#125;
		else if &#40;curWindow == 2&#41;
		&#123;
			SDL_BlitSurface&#40;Calculatrice, NULL, ecran, &cal_position&#41;;
			SDL_BlitSurface&#40;textZero, NULL, ecran, &txtZero_position&#41;;
			SDL_BlitSurface&#40;textDot, NULL, ecran, &txtDot_position&#41;;
			SDL_BlitSurface&#40;textClear, NULL, ecran, &txtClear_position&#41;;
				
			if &#40;pad.Buttons & PSP_CTRL_SELECT&#41;&#40;curWindow = 1&#41;;
		&#125;
		
		SDL_BlitSurface&#40;mousePointer, NULL, ecran, &souris&#41;;		
		SDL_Flip&#40;ecran&#41;;
    &#125;
    
    //Free the memory.
	TTF_CloseFont&#40;font&#41;;
	TTF_CloseFont&#40;timeFont&#41;;
	TTF_CloseFont&#40;twentyFont&#41;;
	TTF_CloseFont&#40;twenty5Font&#41;;
	TTF_CloseFont&#40;thirtyFont&#41;;
	TTF_CloseFont&#40;thirty5Font&#41;;
	TTF_CloseFont&#40;fortyFont&#41;;
	TTF_CloseFont&#40;fifthy5Font&#41;;
	
    SDL_FreeSurface&#40;background&#41;;
    SDL_FreeSurface&#40;mousePointer&#41;;
    SDL_FreeSurface&#40;iconCalendar&#41;;
    SDL_FreeSurface&#40;iconConfig&#41;;
    SDL_FreeSurface&#40;iconWeather&#41;;
    SDL_FreeSurface&#40;iconContact&#41;;
    SDL_FreeSurface&#40;iconCalculator&#41;;
    SDL_FreeSurface&#40;iconAlarm&#41;;
    SDL_FreeSurface&#40;textCalendar&#41;;
    SDL_FreeSurface&#40;textConfig&#41;;
    SDL_FreeSurface&#40;textWeather&#41;;
    SDL_FreeSurface&#40;textContact&#41;;
    SDL_FreeSurface&#40;textCalculator&#41;;
    SDL_FreeSurface&#40;textAlarm&#41;;
	SDL_FreeSurface&#40;textTime&#41;;
	SDL_FreeSurface&#40;textZero&#41;;
	SDL_FreeSurface&#40;Calculatrice&#41;;
	
	TTF_Quit&#40;&#41;;
    SDL_Quit&#40;&#41;;
	
    sceKernelSleepThread&#40;&#41;;

    return 0;
&#125;
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Two things I'd do - first, turn the speed down. You don't need 333MHz for this. It'd be much better to crank the speed down as far as you could go and still play an mp3 so it saves battery life. Second, I'd put a delay after the flip. There's no reason to be going through the loop more than a few times per second.

Code: Select all

   sceKernelDelayThread&#40;250*1000&#41;;
That would delay the program so that you only did four loops per second. It's possible the unexpected quit is due to calling the flip too frequently.
AllSystemGo
Posts: 26
Joined: Sat Jun 02, 2007 1:15 am

Post by AllSystemGo »

Thank you for your advice J.F. I would like to point out one little small problem. Now with the reduce speed the mouse, that is used as a pointer device to select stuff, is really slow on the screen and doesn't run smoothly... Is there anything to do with that?

BTW It seems to have solved the problem I had with the clock disapearing.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

AllSystemGo wrote:Thank you for your advice J.F. I would like to point out one little small problem. Now with the reduce speed the mouse, that is used as a pointer device to select stuff, is really slow on the screen and doesn't run smoothly... Is there anything to do with that?

BTW It seems to have solved the problem I had with the clock disapearing.
Probably had to do with calling flip to often. I'm not familiar with SDL, but there's probably a function in it you could call before or after flip that would handle that issue.

As to the mouse speed, the delay time I gave was for 4 times per second. I didn't notice you had a mouse in there. You might bump that a bit higher - maybe 20 or 30 times per second. The delay value is microseconds.
AllSystemGo
Posts: 26
Joined: Sat Jun 02, 2007 1:15 am

Post by AllSystemGo »

Just to keep you posted on my problem J.F. I figured out a way to bypass the sceKernlDelayThread by adding a condition like so

Code: Select all

    while&#40;isrunning&#41;
    &#123;		
		
        sceRtcGetCurrentTick&#40;&CurTick&#41;;
		
        if &#40;sceRtcCompareTick&#40;&CurTick, &CmpTick&#41; >= 0&#41;
        &#123;
	                   SDL_FreeSurface&#40;textTime&#41;;
	                   snprintf&#40;timeText, 8, "%02i &#58; %02i", time.hour, time.minutes&#41;;
			
	                   textTime = TTF_RenderText_Shaded&#40;timeFont, timeText, white, black&#41;;

	                   sceRtcGetTick&#40;&time, &CmpTick&#41;;
	                   sceRtcTickAddMinutes&#40;&CmpTick, &CmpTick, 1&#41;;
	                   SDL_BlitSurface&#40;textTime, NULL, ecran, &txttime_position&#41;;
        &#125;

    &#125;
I have no clue if it's the perfect way to do it but right now it works.

Thank you for your help
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

AllSystemGo wrote:Just to keep you posted on my problem J.F. I figured out a way to bypass the sceKernlDelayThread by adding a condition like so

Code: Select all

    while&#40;isrunning&#41;
    &#123;		
		
        sceRtcGetCurrentTick&#40;&CurTick&#41;;
		
        if &#40;sceRtcCompareTick&#40;&CurTick, &CmpTick&#41; >= 0&#41;
        &#123;
	                   SDL_FreeSurface&#40;textTime&#41;;
	                   snprintf&#40;timeText, 8, "%02i &#58; %02i", time.hour, time.minutes&#41;;
			
	                   textTime = TTF_RenderText_Shaded&#40;timeFont, timeText, white, black&#41;;

	                   sceRtcGetTick&#40;&time, &CmpTick&#41;;
	                   sceRtcTickAddMinutes&#40;&CmpTick, &CmpTick, 1&#41;;
	                   SDL_BlitSurface&#40;textTime, NULL, ecran, &txttime_position&#41;;
        &#125;

    &#125;
I have no clue if it's the perfect way to do it but right now it works.

Thank you for your help
There's no such thing as the "perfect way". :) Sometimes, you're looking for an optimal solution, while other times you're looking for something more simple and maintainable. The above is simple to follow - each time through the loop, check if a minute has gone by. If so, add another minute and update the clock time. Waiting an odd number of microseconds doesn't lend itself to understanding why you were waiting without a comment to accompany it.

So programmers don't use terms like "perfect" - leave that to marketing. Programmers prefer terms like "better", "optimal", "intuitive", "simple", etc. Shades of gray...
Post Reply