4

So I am trying to learn SDL and creating a small game. When the user presses the space bar, it should play a sound. This works, but the sound takes about .5 seconds to play... How would I fix this? I've checked the actual file to see if the delay was just part of it, and that turned out to not be the case.

SDL with c++ on win vista.

Here's some relevant code:

...
Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 )
...

    main() 
    {

    ...

    Mix_PlayMusic( BG_Music, 0 );   //background music

        while( quit == false )
        {
            fps.start();
            fire.handleInput( event, ship.get_x_pos() );
    ...
    }

the handle input function:

void lasersC::handleInput( SDL_Event &event, int x )
{
    Uint8 *keystates = SDL_GetKeyState( NULL );

    if ( keystates[ SDLK_SPACE ] && delay == 0)
    {
        Mix_PlayChannel(-1, laser, 0);
        fired * F = new fired( NULL, S_HEIGHT - 50, x + 20 );

        F->shot  = lasers;
        F->y_pos = S_HEIGHT - 50;
        F->x_pos = x + 20;

        weps.push_back( F );
        delay = 10;
    }
}

If more is needed, let me know.

Justen
  • 4,859
  • 9
  • 44
  • 68

1 Answers1

6

There is usually a check for DirectX headers when compiling SDL (apparently)

Can you try to recompile with DirectX support?

Also, how much buffering are you enforcing at the client side? It may be a problem with that.

EDIT - After the comments below:

  1. Try to increase the size of your buffer.
  2. What I mean by recompiling with DirectX support is, try to include headers from the following source so that SDL skips checking for them. http://www.libsdl.org/extras/win32/common/directx-devel.tar.gz
SuPra
  • 8,488
  • 4
  • 37
  • 30
  • I'm not really sure what that means. – Justen Jun 11 '09 at 22:12
  • @ the buffering, Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) – Justen Jun 11 '09 at 22:15
  • the buffer changing worked. I set it to 2048 which was good, but 1024 seems right on. I don't understand though, I thought I higher number would make it better but it made it worse. Do you have a link or explanation on what exactly that (buffer) parameter does in the Mix_OpenAudio()? – Justen Jun 12 '09 at 00:47
  • Not really, but I looked online and 1024 seems to be a standard. Give me some time and I'll try to think of why that is. – SuPra Jun 12 '09 at 00:52
  • Alright thanks anyways. Weird that you found 1024 to be the standard of all the tutorials I've been using, they put 4096.. oh well. – Justen Jun 12 '09 at 04:05
  • The buffer size indicates how many audio samples are played at one time. If you request that a sound be played, it could take up to that many samples before the sound starts to play. With a sample rate of 44100 (pretty standard) a buffer size of 1024 gives you a delay of up to 1024 / 44100 = 23 milliseconds. – HaberdashPI Jan 11 '17 at 17:15
  • Buffer change fixed it for me as well. Thanks!! – NHDaly Feb 04 '18 at 22:23
  • 1024 fixed it for me as well. It is weird that all the tutorials that I've looked say 4096 too, not sure why. – shell Dec 03 '19 at 23:26