SDL sound delay

Started by
20 comments, last by Roots 18 years, 7 months ago
Hi! My problem is that when I play some chunk using Mix_PlayChannel(), it sounds about a second later than the call of the function. I am using SDL 1.2.8 with SDL_mixer 1.2.6 and mingw32(Dev-C++). Here are some snippets:

 mixer::mixer(int frequency, int audio_channels, int chunksize)
{
 music=NULL;
 Uint16 format=AUDIO_S16SYS;
 if((Mix_OpenAudio(frequency,format,audio_channels,chunksize))<0)
  printf("Audio startup error: %s\r\n",Mix_GetError());
}  
sound::sound()
{
 chunk=NULL;
 channel=-1; 
}
int sound::loadfile(char* soundfile)
{
 if(chunk) Mix_FreeChunk(chunk);
 chunk=Mix_LoadWAV(soundfile);
 return 0;
}
int sound::play(int channelreq,int loop)
{
 return channel=Mix_PlayChannel(channelreq,chunk,loop); 
}


No more than a wrapper class. Here is how I load the sound:

    mx=new mixer(44100,2,512);
    mx->loadfile("mus.ogg");
    mx->play(-1);

    down=mx->newsound();			
    down->loadfile("down.wav");	


After this, down->play(-1,0) delays a sec before I hear the sound. I've tried several buffer sizes, frequencies, etc. to no avail. Have you got any ideas? Thanks in advance, Thunder
Advertisement
You could try doing the same thing with FMOD and see if you get the same result. You also might want to attempt to disable hardware audio as that caused so many headaches for me when working with third party audio libraries.
Rob Loach [Website] [Projects] [Contact]
Thanks for the advice.

FMOD does indeed play fine.
However, it is not a free library, so i would be happier to go with SDL_Mixer.
Can you think of any reasons for that delay?

Thanks,
Thunder
I'm getting the same problem with a click.wav that I use for the menu screen when I click on a button. It's very irritating. Now it looks like my program is slow as hell while it's just the audio part :)

The sound part of SDL just sucks, might need to use something else for it.
is it a second each time you play it or just the first time? I take it you are allowing time for it to load the file into the buffer etc.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
It's just everytime, and I loaded the music files before the loop started so I didn't load them everytime before I play.
I have encountered similar audio output delays in the past but I don't think it's to do directly with SDL.

from what I can tell SDL tries to use your system's DirectSound capabilities and if it can't do DirectX for whatever reason then it will revert to the winmm (Windows Multimedia library) functions (just like it does with DirectDraw vs. GDI rendering)

btw I have found Audiere to have the same delay problems with the winmm driver (up to 500ms second or so)

In other words, make sure your system supports DirectSound (dxdiag) and SDL_mixer acually makes use of it.

a wild guess but it may help.
Thanks, but as I've said, there is no noticeable delay with FMOD, but that one needs licensing.
And yes, it happens every time the song is played, and the waves are loaded at startup.
Any other ideas?

Thunder
I've had that problem before. If you try different chunk sizes that may help.
If you mean the audio buffer size, then i've tried settings from 64 to 65535 bytes with no success. Obviously at 65535 the delay was much worse than with 64 byte chunk size, however there was still a second delay with 64 bytes.

Thunder

This topic is closed to new replies.

Advertisement