Synchronized sound with SDL_mixer

Hello

I would like to synchronise sounds playing on different
channels.

SDL seems (to me…) to affect a sample to a channel and
then play the channel via Mix_PlayChannel() what i would
like to do is to affect two sounds (at least) to two
channels and THEN play both channels so that they are
correctly synchronized. And not affect first sound to
first channel and play and then affect second channel to
second channel and play.
I hope i’m clear… :wink:

Is it possible ?

regards.--------------
Profitez des 2 offres exceptionnelles Tiscali !
“Internet Gratuit le Jour” et "Modem ADSL rembours?"
Cliquez ici, http://register.tiscali.fr/forfaits_ls/
Offres soumises ? conditions.

SDL seems (to me…) to affect a sample to a channel and
then play the channel via Mix_PlayChannel() what i would
like to do is to affect two sounds (at least) to two
channels and THEN play both channels so that they are
correctly synchronized. And not affect first sound to
first channel and play and then affect second channel to
second channel and play.
I hope i’m clear… :wink:

What you want to do is play two channels so that they both start at the
same time and stay in sync the whole time they play, correct?

There isn’t an official mechanism for this, but if you can start both
channels with Mix_PlayChannel() without the audio callback running between
calls, the two channels should stay in sync. Guaranteeing this is
difficult.

–ryan.

That’s exactly what I need thanks.
But what must I do to prevent the sound callback from
running between the two calls to Mix_PlayChannel ? I was
thinking of closing audio via Mix_CloseAudio() but it may
alter performances :

Mix_CloseAudio();
Mix_PlayChannel(channel1,sample1,…);
Mix_PlayChannel(channel2,sample2,…);
Mix_OpenAudio(…);

should work, no ?

SDL seems (to me…) to affect a sample to a channel
and

then play the channel via Mix_PlayChannel() what i
would

like to do is to affect two sounds (at least) to two
channels and THEN play both channels so that they are
correctly synchronized. And not affect first sound to
first channel and play and then affect second channel
to

second channel and play.
I hope i’m clear… :wink:

What you want to do is play two channels so that they
both start at the
same time and stay in sync the whole time they play,
correct?

There isn’t an official mechanism for this, but if you
can start both
channels with Mix_PlayChannel() without the audio
callback running between
calls, the two channels should stay in sync.
Guaranteeing this is> Subject : Re: [SDL] synchronized sound with SDL_mixer
difficult.

–ryan.


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


Profitez des 2 offres exceptionnelles Tiscali !
“Internet Gratuit le Jour” et "Modem ADSL rembours?"
Cliquez ici, http://register.tiscali.fr/forfaits_ls/
Offres soumises ? conditions.

Oups sorry… I shouldn’t have post so fast, i didn’t try
the code. what I said in my last post can’t work as I was
deallocating the channel array, before affecting values
to it !

I think that :

SDL_LockAudio();
Mix_PlayChannel(channel1,sample1,…);
Mix_PlayChannel(channel2,sample2,…);
Mix_UnlockAudio(…)

is better to lock audio callback while affecting the
channels.

SDL_mixer documentation says that using SDL_LockAudio()
while using SDL_mixer could create some conflict but
looking at SDL_mixer source code I don’t think that what
I plan to do doesn’t create conflict. I hope i’m
wright… :wink:

thanks for the help.

That’s exactly what I need thanks.
But what must I do to prevent the sound callback from
running between the two calls to Mix_PlayChannel ? I
was
thinking of closing audio via Mix_CloseAudio() but it
may
alter performances :

Mix_CloseAudio();
Mix_PlayChannel(channel1,sample1,…);
Mix_PlayChannel(channel2,sample2,…);
Mix_OpenAudio(…);

should work, no ?

SDL seems (to me…) to affect a sample to a channel
and

then play the channel via Mix_PlayChannel() what i
would

like to do is to affect two sounds (at least) to two
channels and THEN play both channels so that they
are

correctly synchronized. And not affect first sound
to

first channel and play and then affect second
channel
to

second channel and play.
I hope i’m clear… :wink:

What you want to do is play two channels so that they
both start at the
same time and stay in sync the whole time they play,
correct?

There isn’t an official mechanism for this, but if
you> > Subject : Re: [SDL] synchronized sound with SDL_mixer
can start both

channels with Mix_PlayChannel() without the audio
callback running between
calls, the two channels should stay in sync.
Guaranteeing this is
difficult.

–ryan.


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


Profitez des 2 offres exceptionnelles Tiscali !
“Internet Gratuit le Jour” et "Modem ADSL rembours?"
Cliquez ici, http://register.tiscali.fr/forfaits_ls/
Offres soumises ? conditions.


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


Profitez des 2 offres exceptionnelles Tiscali !
“Internet Gratuit le Jour” et "Modem ADSL rembours?"
Cliquez ici, http://register.tiscali.fr/forfaits_ls/
Offres soumises ? conditions.

Mix_CloseAudio();
Mix_PlayChannel(channel1,sample1,…);
Mix_PlayChannel(channel2,sample2,…);
Mix_OpenAudio(…);

should work, no ?

SDL_LockAudio() is probably what you want.

–ryan.

SDL_mixer documentation says that using SDL_LockAudio()
while using SDL_mixer could create some conflict but
looking at SDL_mixer source code I don’t think that what
I plan to do doesn’t create conflict. I hope i’m
wright… :wink:

LockAudio prevents the audio callback (where the actual mixing and sending
of data to the audio device is done) from running, but shouldn’t cause
trouble if you lock and unlock as quickly as possible (i.e.-lock, call
Mix_PlayChannel() twice, unlock).

–ryan.

Alwright but if I want to keep sending data to hardware
(background music for exmple) SDL_LockAudio() will also
stop this audio sending, even if it’s for a short time.

I may have to make a function :my_SDL_LockAudio() which
will stop only some of the channels (those I want to
synchronise). The mutex used in Mix_PlayChannel() could
be used to affect several channel isn’t it ?

SDL_mixer documentation says that using SDL_LockAudio
()

while using SDL_mixer could create some conflict but
looking at SDL_mixer source code I don’t think that
what

I plan to do doesn’t create conflict. I hope i’m
wright… :wink:

LockAudio prevents the audio callback (where the actual
mixing and sending
of data to the audio device is done) from running, but
shouldn’t cause
trouble if you lock and unlock as quickly as possible
(i.e.-lock, call> Subject : Re: [SDL] synchronized sound with SDL_mixer
Mix_PlayChannel() twice, unlock).

–ryan.


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


Profitez des 2 offres exceptionnelles Tiscali !
“Internet Gratuit le Jour” et "Modem ADSL rembours?"
Cliquez ici, http://register.tiscali.fr/forfaits_ls/
Offres soumises ? conditions.

Alwright but if I want to keep sending data to hardware
(background music for exmple) SDL_LockAudio() will also
stop this audio sending, even if it’s for a short time.

SDL_LockAudio() guarantees that the audio callback doesn’t run; generally
this only happens every couple hundred milliseconds, so it rarely
conflicts, and if it does, it runs right after you call SDL_UnlockAudio().
Unless you lock the audio for a REALLY long time, you should not have
skips in the sound playback.

I may have to make a function :my_SDL_LockAudio() which
will stop only some of the channels (those I want to
synchronise). The mutex used in Mix_PlayChannel() could
be used to affect several channel isn’t it ?

“Channels” are just a concept in SDL_mixer. Ultimately the channels are
really only “running” during the audio callback (in which case a block of
data is mixed and sent the card), which means you can tinker with them all
you want while the callback isn’t running.

The mutex in Mix_PlayChannel is gone in the latest CVS revision. Do not
use it. Use SDL_LockAudio(). The mixer itself uses this internally for
synchronization, now.

–ryan.