On 1/3/12, Winhelp <winh
...@tlen.pl> wrote:
>> I tried unlocking audio just before SDL_PauseAudio(false) and there is
>> no sound at all.
> Now it works - it looks like bad things happen when you try to lock
> already locked audio - and i did something like that.
> Something like this works:
> void sound_stop()
> {
> SDL_PauseAudio( true );
> SDL_LockAudio();
> }
> void sound_start()
> {
> SDL_UnlockAudio();
> SDL_PauseAudio(false);
> }
> So is this thing from example an error? I agree that this looks more
> logical than locking audio just to unlock it in next line.
I'm thinking I went the simpler route in the example because the above
seems to require special treatment of the first time you start sound,
and the last time you end. When first starting, presumably the audio
isn't locked, so the unlock call seems in error (though SDL probably
ignores that). And when last using audio and you've stopped it and
then locked it, it seems plausible that this might prevent closing of
audio, since there's still a lock being held. So for the example I
must have figured out that just locking and then unlocking is enough
to ensure that the callback doesn't fire anymore after pausing (that
is, the only way it could be still running is if you happened to pause
when the callback was active in another thread on a
multiprocessor/core machine).
If we really wanted to do things the most correct way, we might write a wrapper:
bool sound_started;
void sound_init()
{
SDL_OpenAudio( ... );
sound_started = false;
}
void sound_start()
{
if ( !sound_started )
{
SDL_UnlockAudio();
SDL_PauseAudio( false );
sound_started = true;
}
}
void sound_stop()
{
if ( sound_started )
{
SDL_PauseAudio( true );
SDL_LockAudio();
sound_started = false;
}
}
void sound_close()
{
sound_stop();
SDL_UnlockAudio();
SDL_CloseAudio();
}
This simpler wrapper ensures that all the right calls occur without
burdening user code with the details; it can call start/stop multiple
times in a row without deadlock etc. Whether it's necessary depends on
SDL's implementation; their documentation is unfortunately vague about
semantics. I'm a big fan of small wrappers like this to separate the
API details and simplify what I have to think about in the main code.