forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Trouble with libmad

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
AllSystemGo



Joined: 02 Jun 2007
Posts: 26

PostPosted: Thu Aug 09, 2007 10:36 am    Post subject: Trouble with libmad Reply with quote

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:

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:

   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
Back to top
View user's profile Send private message
AllSystemGo



Joined: 02 Jun 2007
Posts: 26

PostPosted: Thu Aug 09, 2007 10:13 pm    Post subject: Reply with quote

Anyone??
Back to top
View user's profile Send private message
Mihawk



Joined: 03 Apr 2007
Posts: 29

PostPosted: Thu Aug 09, 2007 11:09 pm    Post subject: Reply with quote

What's the magic word?
Back to top
View user's profile Send private message
AllSystemGo



Joined: 02 Jun 2007
Posts: 26

PostPosted: Thu Aug 09, 2007 11:30 pm    Post subject: Reply with quote

abracadabra.. Please!
Back to top
View user's profile Send private message
Mihawk



Joined: 03 Apr 2007
Posts: 29

PostPosted: Fri Aug 10, 2007 1:21 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
AllSystemGo



Joined: 02 Jun 2007
Posts: 26

PostPosted: Fri Aug 10, 2007 1:43 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Fri Aug 10, 2007 2:37 am    Post subject: Reply with quote

Are you running in psplink? If the PSP is shutting down, you are likely getting an exception and you can just find out where.
Back to top
View user's profile Send private message
AllSystemGo



Joined: 02 Jun 2007
Posts: 26

PostPosted: Fri Aug 10, 2007 2:43 am    Post subject: Reply with quote

Nope I'm not using psplink. Can you tell me exactly what it does???
Back to top
View user's profile Send private message
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Fri Aug 10, 2007 2:52 am    Post subject: Reply with quote

http://ps2dev.org/psp/Tools/PspLink
http://www.google.com/search?q=psplink
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Fri Aug 10, 2007 5:29 am    Post subject: Reply with quote

You have

Code:
      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.
Back to top
View user's profile Send private message AIM Address
AllSystemGo



Joined: 02 Jun 2007
Posts: 26

PostPosted: Fri Aug 10, 2007 6:13 am    Post subject: Reply with quote

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

Code:

#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("iPDA", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

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

// Exit callback
int exit_callback(int arg1, int arg2, void *common) {
   isrunning = false;
   sceKernelExitGame();
   return 0;
}

// Callback thread
int CallbackThread(SceSize args, void *argp) {
   int cbid;
   
   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);
   
   sceKernelSleepThreadCB();
    
   return 0;
}

// Sets up the callback thread and returns its thread id
int SetupCallbacks(void) {
   int thid = 0;
    
   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }
    
   return thid;
}

int main() {
   //Initialize the controler and the PSP Speed
    SceCtrlData pad;
    scePowerSetClockFrequency(333, 333, 166);
    sceCtrlSetSamplingCycle(0);
    sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
   
   char timeText[8];
   int thread_id;
      
   //Initialize the PSP Time
   pspTime time;
   
   //Initialize the SDL Library (audio and video)
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
      cout << stderr << "Couldn't initialize SDL: " << SDL_GetError() << endl;

    //Initialize the SDL True Type Font Library
    if(TTF_Init() < 0)
      cout << stderr << "Couldn't initialize TTF: " << SDL_GetError() << endl;

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

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

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

      if((pad.Lx < 35) && (souris.x > 0))
      {
         souris.x = souris.x-3;
      }
      else if((pad.Lx > 200) && (souris.x < 480))
      {
         souris.x = souris.x+3;
      }
      
      if((pad.Ly < 35) && (souris.y > 0))
      {
         souris.y = souris.y-3;
      }
      else if((pad.Ly > 200) && (souris.y < 480))
      {
         souris.y = souris.y+3;
      }
      
      if(souris.x >= 480)(souris.x = 480);
      if(souris.y >= 272)(souris.y = 272);
      
      SDL_BlitSurface(background, NULL, ecran, &background_position);
      //if (textTime != NULL) 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);
   
      if (curWindow == 1)
      {
         if (pad.Buttons & PSP_CTRL_CROSS)
         {
            if ((souris.x > calculator_position.x) && (souris.x < (calculator_position.x + 64)) &&
                (souris.y > calculator_position.y) && (souris.y < (calculator_position.y + 64)))
            {
               curWindow = 2;
            }
         }
         
         SDL_BlitSurface(iconCalendar, NULL, ecran, &calendar_position);
         SDL_BlitSurface(textCalendar, NULL, ecran, &txtcalendar_position);
         SDL_BlitSurface(iconConfig, NULL, ecran, &tools_position);
         SDL_BlitSurface(textConfig, NULL, ecran, &txtconfig_position);
         SDL_BlitSurface(iconWeather, NULL, ecran, &weather_position);
         SDL_BlitSurface(textWeather, NULL, ecran, &txtweather_position);
         SDL_BlitSurface(iconContact, NULL, ecran, &contact_position);
         SDL_BlitSurface(textContact, NULL, ecran, &txtcontact_position);
         SDL_BlitSurface(iconCalculator, NULL, ecran, &calculator_position);
         SDL_BlitSurface(textCalculator, NULL, ecran, &txtcalculator_position);
         SDL_BlitSurface(iconAlarm, NULL, ecran, &alarm_position);
         SDL_BlitSurface(textAlarm, NULL, ecran, &txtalarm_position);
      }
      else if (curWindow == 2)
      {
         SDL_BlitSurface(Calculatrice, NULL, ecran, &cal_position);
         SDL_BlitSurface(textZero, NULL, ecran, &txtZero_position);
         SDL_BlitSurface(textDot, NULL, ecran, &txtDot_position);
         SDL_BlitSurface(textClear, NULL, ecran, &txtClear_position);
            
         if (pad.Buttons & PSP_CTRL_SELECT)(curWindow = 1);
      }
      
      SDL_BlitSurface(mousePointer, NULL, ecran, &souris);      
      SDL_Flip(ecran);
    }
   
    //Free the memory.
   TTF_CloseFont(font);
   TTF_CloseFont(timeFont);
   TTF_CloseFont(twentyFont);
   TTF_CloseFont(twenty5Font);
   TTF_CloseFont(thirtyFont);
   TTF_CloseFont(thirty5Font);
   TTF_CloseFont(fortyFont);
   TTF_CloseFont(fifthy5Font);
   
    SDL_FreeSurface(background);
    SDL_FreeSurface(mousePointer);
    SDL_FreeSurface(iconCalendar);
    SDL_FreeSurface(iconConfig);
    SDL_FreeSurface(iconWeather);
    SDL_FreeSurface(iconContact);
    SDL_FreeSurface(iconCalculator);
    SDL_FreeSurface(iconAlarm);
    SDL_FreeSurface(textCalendar);
    SDL_FreeSurface(textConfig);
    SDL_FreeSurface(textWeather);
    SDL_FreeSurface(textContact);
    SDL_FreeSurface(textCalculator);
    SDL_FreeSurface(textAlarm);
   SDL_FreeSurface(textTime);
   SDL_FreeSurface(textZero);
   SDL_FreeSurface(Calculatrice);
   
   TTF_Quit();
    SDL_Quit();
   
    sceKernelSleepThread();

    return 0;
}
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Sat Aug 11, 2007 6:10 am    Post subject: Reply with quote

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:
   sceKernelDelayThread(250*1000);


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.
Back to top
View user's profile Send private message AIM Address
AllSystemGo



Joined: 02 Jun 2007
Posts: 26

PostPosted: Mon Aug 13, 2007 11:29 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Mon Aug 13, 2007 2:16 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
AllSystemGo



Joined: 02 Jun 2007
Posts: 26

PostPosted: Thu Aug 23, 2007 4:51 am    Post subject: Reply with quote

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:

    while(isrunning)
    {      
      
        sceRtcGetCurrentTick(&CurTick);
      
        if (sceRtcCompareTick(&CurTick, &CmpTick) >= 0)
        {
                      SDL_FreeSurface(textTime);
                      snprintf(timeText, 8, "%02i : %02i", time.hour, time.minutes);
         
                      textTime = TTF_RenderText_Shaded(timeFont, timeText, white, black);

                      sceRtcGetTick(&time, &CmpTick);
                      sceRtcTickAddMinutes(&CmpTick, &CmpTick, 1);
                      SDL_BlitSurface(textTime, NULL, ecran, &txttime_position);
        }

    }


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

Thank you for your help
Back to top
View user's profile Send private message
J.F.



Joined: 22 Feb 2004
Posts: 2906

PostPosted: Thu Aug 23, 2007 9:03 am    Post subject: Reply with quote

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:

    while(isrunning)
    {      
      
        sceRtcGetCurrentTick(&CurTick);
      
        if (sceRtcCompareTick(&CurTick, &CmpTick) >= 0)
        {
                      SDL_FreeSurface(textTime);
                      snprintf(timeText, 8, "%02i : %02i", time.hour, time.minutes);
         
                      textTime = TTF_RenderText_Shaded(timeFont, timeText, white, black);

                      sceRtcGetTick(&time, &CmpTick);
                      sceRtcTickAddMinutes(&CmpTick, &CmpTick, 1);
                      SDL_BlitSurface(textTime, NULL, ecran, &txttime_position);
        }

    }


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...
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group