Thread: Help with SDL and Sounds

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    120

    Help with SDL and Timing

    Hi there, i dont know if this is the right place to post this, but there is no SDL forum so...

    ok my problem is that im using this code to get a sound after another:

    Code:
            if ((SDL_GetTicks()-timing) >= (60000/y))
            {
                Mix_PlayChannel(-1,clap,0);
                timing=SDL_GetTicks();
            }
    the only problem is that the time between the sounds should be constant and it isnt, the sounds delay some time and thats not good, because i need TOTAL PRECISION.

    How can i solve this?? help pls.

    BTW: i believe the problem is infact the timing and not the sound... cause when i if ((SDL_GetTicks()-timing) == (60000/y)) do this the sound never plays, so i can i set the time delay (Im not talking about SDL_Delay() of course), so it would be exact??
    Last edited by shiroaisu; 06-11-2010 at 01:44 PM.

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Where is the loop?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    120
    here it is;

    Code:
        while (quit==0)
        {
            apply_surface(bg, screen,0,0);
    
            if (SDL_PollEvent(&event))
            {
    
                //Quit Metronome
                if (event.type==SDL_QUIT)
                {
                    quit=1;
                }
                //--------------------------------
    
                if (event.type==SDL_KEYDOWN)
                {
                    SDL_EnableUNICODE(SDL_ENABLE);
    
                    if (event.key.keysym.unicode>=(Uint16)'0' && event.key.keysym.unicode <=(Uint16)'9')
                    {
                        if (x<11)
                        {
                        time_str[x]=(char)event.key.keysym.unicode;
                        x++;
                        text=NULL;
                        }
                    }
    
                    if (event.key.keysym.sym==SDLK_RETURN)
                    {
                        x=0;
                        while (x<11)
                        {
                        time_str[x]=' ';
                        x++;
                        }
                        x=0;
                        text=NULL;
                    }
    
    
                }
                text=TTF_RenderText_Solid(font, time_str, textcolor);
    
            }
    
            y=atoi(time_str);
            if (y<=0) y=1;
    
            if ((SDL_GetTicks()-timing) == (60000/y))
            {
                Mix_PlayChannel(-1,clap,0);
                timing=SDL_GetTicks();
            }
    
            apply_surface(text, screen,0,0);
    
            SDL_Flip(screen);
        }
    the full loop
    Last edited by shiroaisu; 06-11-2010 at 03:31 PM.

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    And how do you expect your timing to be precise when there is a lot of stuff in your loop?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    120
    Ok tks for the help... -.-

    now i request REAL help, how can i fixx it pls??

    oh and btw... this is not too much stuff for a loop.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Why you both use an if block to see time is passed and SDL_Delay together? Surely when delay is called for 6000ms, 6000ms is passed.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    120
    I thought of that, but doesnt SDL_Delay stops the entire loop??

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Yes.
    Code:
    if ((SDL_GetTicks()-timing) == (60000/y))
    You meant
    Code:
    if ((SDL_GetTicks()-timing) > (60000/y))
    ?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    120
    if ((SDL_GetTicks()-timing) >= (60000/y))

    Yes, i meant that, sory lol. but i cant stop all the program cause while the sounds are playing i need imput too, is there any other way arround this??

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I don't get you. You already used SDL_Delay. Remove that or remove if block. You should remove if block or you will miss some events. Remove it and send new code with results.
    Code:
               x=0;
                        while (x<11)
                        {
                            time_str[x]=' ';
                            x++;
                        }
    You can make it a for loop. And what this loop supposed to do? Why putting space in time_str?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    120
    *facepalm* the code was wrong sory -.- that was a test i was doing,

    the right code is without the delay lol.

    oh and yes that fills up all the spaces in time_str with spaces so we can rewrite it.

    btw i updated the old code to the right version lol ^^^^ go check it out now xD

    oh and if i put the delay only it doesnt let me input nothing, because it is constantly delaying, wich stops the entire program
    Last edited by shiroaisu; 06-11-2010 at 03:33 PM.

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    This way I think you are missing events. You poll them, but time is not elapsed enough so event is discarded. Make it like
    Code:
      while (quit==0)
        {
            apply_surface(bg, screen,0,0);
    
            if (((SDL_GetTicks()-timing) == (60000/y)) && SDL_PollEvent(&event))
            {
    
                //Quit Metronome
                if (event.type==SDL_QUIT)
    ...
                Mix_PlayChannel(-1,clap,0);
                timing=SDL_GetTicks();
               //Without if
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    120
    Nop, it doesnt work, because to get the imput both conditions need to be positive, and thats very hard to happen.

    I tried changing the "and" to an "or" ( || ) but it didnt worked either, cause with a or every time i moved the mouse for example it would play the sound...

    help pls...

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Sorry make it >= instead of ==. I copied from wrong code. It should be &&. You may need to revise your whole loop (make it a do..while).

    This means that events should not be polled unless the enough time is elapsed. Sure it will work. If the second condition is false we have no input. f the first one is false, no event should be polled.
    Inputs are queued in event queue.

    Use
    Code:
    strcpy(str, "            ");
    to put space into a string. Anyway I think this part is redundant but to be sure I should see whole the code.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    120
    Code:
            if (((SDL_GetTicks()-timing) >= (60000/y)) && SDL_PollEvent(&event))
            {
    
                //Quit Metronome
                if (event.type==SDL_QUIT)
                {
                    quit=1;
                }
                //--------------------------------
    
                if (event.type==SDL_KEYDOWN)
                {
                    SDL_EnableUNICODE(SDL_ENABLE);
    
                    if (event.key.keysym.unicode>=(Uint16)'0' && event.key.keysym.unicode <=(Uint16)'9')
                    {
                        if (x<11)
                        {
                        time_str[x]=(char)event.key.keysym.unicode;
                        x++;
                        text=NULL;
                        }
                    }
    
                    if (event.key.keysym.sym==SDLK_RETURN)
                    {
                        x=0;
                        while (x<11)
                        {
                        time_str[x]=' ';
                        x++;
                        }
                        x=0;
                        text=NULL;
                    }
    
    
                }
                text=TTF_RenderText_Solid(font, time_str, textcolor);
                Mix_PlayChannel(-1,clap,0);
                timing=SDL_GetTicks();
    
            }
    u mean like this?? (btw ty for the string function lol, ill change it latter.. when i solve this major problem :\

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL sound.
    By antex in forum Game Programming
    Replies: 6
    Last Post: 11-08-2005, 04:07 AM