Different behaviour on playing wav with SDL_mixer on Windows / Linux

Hello

i am developping a game with SDL on Linux / Windows
i use SDL_mixer to play wav.
But with the same code, the wav sounds aren’t playend similar under windows or linux.
Indeed, i play the wav when my character make a step.
Under linux, the sound is played correctly when the character make the step, but under windows (with exactly the same code),
the sound is played a second after the step.

Here is the fonctions is use in my main code :

typedef Mix_Chunk *Wavefile;

// to init SDL_mixer

void init_sound(){
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096);
Mix_AllocateChannels(4);
}

// to load a wav

Wavefile charge_wav(char *son){
Wavefile sample;
sample=Mix_LoadWAV(son);
if(!sample) {
printf(“Mix_LoadWAV: %s\n”, Mix_GetError());
// handle error
}

return (Wavefile) sample;

}

// to unload a wav

void dechargeSon(Wavefile son){

Mix_FreeChunk(son);
son=NULL; // to be safe…

}

// to play a wav

int lit_wav(Wavefile son,int mix,int nbfois){
if(Mix_PlayChannel(mix, son, nbfois)==-1) {
printf(“Mix_PlayChannel: %s\n”,Mix_GetError());
}
}

Does anyone can help me please ?
Thanks for help.

(sorry if my english is bad)------------------------------------------

Faites un voeu et puis Voila ! www.voila.fr

I had similar problems. The problem is here :

Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096);

4096 means a 4096 byte buffer. It’s a FIFO buffer, so the bigger
it is, the longer it takes from when you put data in it to when
it comes out the speaker (I’m simplifying, Sam explained it to me
in a much more precise way)

Try reducing that value to 2048. Latency should halve. Don’t reduce
it too much or you will have problems such as static and cracking
with some soundcards (SB Live for example)

Good luck,
–Gabriel

Ing. Gabriel Gambetta
ARTech - GeneXus Development Team
ggambett at artech.com.uy

thanks for your answer !

well, i try your correction, but without success.
i try also Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2); and the sound is still not synchro with the character’s step.

Do you have another idea, or do you know a native SDL way to play instantly wav or another lib ?> Message du 05/02/04 ? 14h36

De : Gabriel Gambetta
A : sdl at libsdl.org
Copie ? :
Objet : RE: [SDL] Different behaviour on playing wav with SDL_mixer on Windows / Linux

I had similar problems. The problem is here :

Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096);

4096 means a 4096 byte buffer. It’s a FIFO buffer, so the bigger
it is, the longer it takes from when you put data in it to when
it comes out the speaker (I’m simplifying, Sam explained it to me
in a much more precise way)

Try reducing that value to 2048. Latency should halve. Don’t reduce
it too much or you will have problems such as static and cracking
with some soundcards (SB Live for example)

Good luck,
–Gabriel

Ing. Gabriel Gambetta
ARTech - GeneXus Development Team
ggambett at artech.com.uy


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Faites un voeu et puis Voila ! www.voila.fr

thanks for your answer !
well, i try your correction, but without success.
i try also Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2);
and the sound is still not synchro with the character’s step.
Do you have another idea, or do you know a native SDL way to
play instantly wav or another lib ?

It’s 2048, not 2.

If that was just a mail typo,… I don’t know. 2048 is good enough
for us (hear the results - http://www.mrio-software.com/info.php?id=bbb)

If you need absolute precision, you could calculate the delay
introduced by the buffer size (44100 samples per second per channel at
16
bits per sample = 176400 bytes per second => 2048 bytes = 0,01 seconds)
and
start playing the sample earlier? (I know, it’s error prone and it’s too
complicated…)

Ing. Gabriel Gambetta
ARTech - GeneXus Development Team
ggambett at artech.com.uy

stephane_bou wrote:

well, i try your correction, but without success.
i try also Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2); and the sound is still not synchro with the character’s step.

Do you have another idea, or do you know a native SDL way to play instantly wav or another lib ?

I’m sorry I ahven’t read this thread from the start.

If sound lags on Linux, try killing your sound server, and playing the
sound.

If it lags on Windows, can you please write what SDL .dlls are you
using. I found that when I build my own DLL files, the sound lags.
Search archive of messages from this list for more info. Also, what
compiler are you using?

If all else fails, you can take a look at FMod library.–
Milan Babuskov
http://njam.sourceforge.net

Lags is on WinXp

Well, for the dlls i tried both the dll from SDL-1.2.6-win32.zip and a dll compiled from the sources (SDL-1.2.6.zip)
For the SDL_mixer dll, i use the SDL_mixer-1.2.5a-win32.zip. I tried to compile the CVS version but didn’t achieve.

I’m using a cross compiler on linux to compile for windows. mingw32 gcc 3.2.3 (mingw special 20030504-1)> Message du 05/02/04 ? 17h55

De : Milan Babuskov
A : sdl at libsdl.org
Copie ? :
Objet : Re: [SDL] Different behaviour on playing wav with SDL_mixer on Windows / Linux

stephane_bou wrote:

well, i try your correction, but without success.
i try also Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2); and the sound is still not synchro with the character’s step.

Do you have another idea, or do you know a native SDL way to play instantly wav or another lib ?

I’m sorry I ahven’t read this thread from the start.

If sound lags on Linux, try killing your sound server, and playing the
sound.

If it lags on Windows, can you please write what SDL .dlls are you
using. I found that when I build my own DLL files, the sound lags.
Search archive of messages from this list for more info. Also, what
compiler are you using?

If all else fails, you can take a look at FMod library.


Milan Babuskov
http://njam.sourceforge.net


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Faites un voeu et puis Voila ! www.voila.fr

Well i tried different values : 2048 and others between 2048 and 2 but no improvement for the lag under winxp> Message du 05/02/04 ? 17h03

De : Gabriel Gambetta
A : sdl at libsdl.org
Copie ? :
Objet : RE: RE: [SDL] Different behaviour on playing wav with SDL_mixer on Windows / Linux

It’s 2048, not 2.

If that was just a mail typo,… I don’t know. 2048 is good enough
for us (hear the results - http://www.mrio-software.com/info.php?id=bbb)

If you need absolute precision, you could calculate the delay
introduced by the buffer size (44100 samples per second per channel at
16
bits per sample = 176400 bytes per second => 2048 bytes = 0,01 seconds)
and
start playing the sample earlier? (I know, it’s error prone and it’s too
complicated…)

Ing. Gabriel Gambetta
ARTech - GeneXus Development Team
ggambett at artech.com.uy


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Faites un voeu et puis Voila ! www.voila.fr