mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2025-04-10 19:15:13 +00:00
Improve audio and volume handling.
Prevent a deadlock in AudioManager mixer callback when stopping sounds. Detach and free mixer handle before closing it in VolumeControl::deinit().
This commit is contained in:
parent
c8bf0cf652
commit
850147016f
|
@ -28,17 +28,13 @@ void AudioManager::mixAudio(void *unused, Uint8 *stream, int len)
|
||||||
}
|
}
|
||||||
//mix sample into stream
|
//mix sample into stream
|
||||||
SDL_MixAudio(stream, &(sound->getData()[sound->getPosition()]), restLength, SDL_MIX_MAXVOLUME);
|
SDL_MixAudio(stream, &(sound->getData()[sound->getPosition()]), restLength, SDL_MIX_MAXVOLUME);
|
||||||
if (sound->getPosition() + restLength >= sound->getLength())
|
if (sound->getPosition() + restLength < sound->getLength())
|
||||||
{
|
|
||||||
//if the position is beyond the end of the buffer, stop playing the sample
|
|
||||||
sound->stop();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
//sample hasn't ended yet
|
//sample hasn't ended yet
|
||||||
sound->setPosition(sound->getPosition() + restLength);
|
|
||||||
stillPlaying = true;
|
stillPlaying = true;
|
||||||
}
|
}
|
||||||
|
//set new sound position. if this is at or beyond the end of the sample, it will stop automatically
|
||||||
|
sound->setPosition(sound->getPosition() + restLength);
|
||||||
}
|
}
|
||||||
//advance to next sound
|
//advance to next sound
|
||||||
++soundIt;
|
++soundIt;
|
||||||
|
|
|
@ -82,18 +82,19 @@ void Sound::play()
|
||||||
if(mSampleData == NULL)
|
if(mSampleData == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
SDL_LockAudio();
|
||||||
if (playing)
|
if (playing)
|
||||||
{
|
{
|
||||||
//replay from start. rewind the sample to the beginning
|
//replay from start. rewind the sample to the beginning
|
||||||
SDL_LockAudio();
|
|
||||||
mSamplePos = 0;
|
mSamplePos = 0;
|
||||||
SDL_UnlockAudio();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//flag our sample as playing
|
//flag our sample as playing
|
||||||
playing = true;
|
playing = true;
|
||||||
}
|
}
|
||||||
|
SDL_UnlockAudio();
|
||||||
//tell the AudioManager to start playing samples
|
//tell the AudioManager to start playing samples
|
||||||
AudioManager::getInstance()->play();
|
AudioManager::getInstance()->play();
|
||||||
}
|
}
|
||||||
|
@ -126,6 +127,8 @@ void Sound::setPosition(Uint32 newPosition)
|
||||||
{
|
{
|
||||||
mSamplePos = newPosition;
|
mSamplePos = newPosition;
|
||||||
if (mSamplePos >= mSampleLength) {
|
if (mSamplePos >= mSampleLength) {
|
||||||
|
//got to or beyond the end of the sample. stop playing
|
||||||
|
playing = false;
|
||||||
mSamplePos = 0;
|
mSamplePos = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -215,6 +215,8 @@ void VolumeControl::deinit()
|
||||||
#error TODO: Not implemented for MacOS yet!!!
|
#error TODO: Not implemented for MacOS yet!!!
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
if (mixerHandle != nullptr) {
|
if (mixerHandle != nullptr) {
|
||||||
|
snd_mixer_detach(mixerHandle, mixerCard);
|
||||||
|
snd_mixer_free(mixerHandle);
|
||||||
snd_mixer_close(mixerHandle);
|
snd_mixer_close(mixerHandle);
|
||||||
mixerHandle = nullptr;
|
mixerHandle = nullptr;
|
||||||
mixerElem = nullptr;
|
mixerElem = nullptr;
|
||||||
|
|
Loading…
Reference in a new issue