fixed Sound muting for WAV files

This commit is contained in:
Thomas Jentzsch 2022-09-14 13:39:24 +02:00
parent d488b9c860
commit c53e0eabcb
2 changed files with 27 additions and 10 deletions

View File

@ -64,6 +64,8 @@ SoundSDL2::SoundSDL2(OSystem& osystem, AudioSettings& audioSettings)
return; return;
SoundSDL2::mute(true); SoundSDL2::mute(true);
myMuteState = !audioSettings.enabled();
myWavVolumeFactor = myMuteState ? 0 : myVolumeFactor;
Logger::debug("SoundSDL2::SoundSDL2 initialized"); Logger::debug("SoundSDL2::SoundSDL2 initialized");
} }
@ -151,6 +153,10 @@ void SoundSDL2::setEnabled(bool enable)
if(myAudioQueue) if(myAudioQueue)
myAudioQueue->ignoreOverflows(!enable); myAudioQueue->ignoreOverflows(!enable);
// Set new mute state and resulting WAV data volume
myMuteState = !enable;
myWavVolumeFactor = myMuteState ? 0 : myVolumeFactor;
Logger::debug(enable ? "SoundSDL2::setEnabled(true)" : Logger::debug(enable ? "SoundSDL2::setEnabled(true)" :
"SoundSDL2::setEnabled(false)"); "SoundSDL2::setEnabled(false)");
} }
@ -195,7 +201,7 @@ void SoundSDL2::open(shared_ptr<AudioQueue> audioQueue,
Logger::info(myAboutString); Logger::info(myAboutString);
// And start the SDL sound subsystem ... // And start the SDL sound subsystem ...
mute(false); mute(false && myMuteState);
Logger::debug("SoundSDL2::open finished"); Logger::debug("SoundSDL2::open finished");
} }
@ -203,11 +209,11 @@ void SoundSDL2::open(shared_ptr<AudioQueue> audioQueue,
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SoundSDL2::close() void SoundSDL2::close()
{ {
stopWav();
if(!myIsInitializedFlag) if(!myIsInitializedFlag)
return; return;
mute(true); // Mute and remember current mute state for 'open()'
myMuteState = mute(true);
if(myAudioQueue) if(myAudioQueue)
myAudioQueue->closeSink(myCurrentFragment); myAudioQueue->closeSink(myCurrentFragment);
@ -235,6 +241,13 @@ bool SoundSDL2::toggleMute()
setEnabled(enabled); setEnabled(enabled);
myOSystem.console().initializeAudio(); myOSystem.console().initializeAudio();
// Adjust TIA sound to new mute state
myMuteState = !enabled;
mute(myMuteState);
// Make sure the current WAV file continues playing if it got stopped by 'mute()'
if(myWavDevice)
SDL_PauseAudioDevice(myWavDevice, 0);
string message = "Sound "; string message = "Sound ";
message += enabled ? "unmuted" : "muted"; message += enabled ? "unmuted" : "muted";
@ -253,6 +266,7 @@ void SoundSDL2::setVolume(uInt32 percent)
SDL_LockAudioDevice(myDevice); SDL_LockAudioDevice(myDevice);
myVolumeFactor = static_cast<float>(percent) / 100.F; myVolumeFactor = static_cast<float>(percent) / 100.F;
myWavVolumeFactor = myAudioSettings.enabled() ? myVolumeFactor : 0;
SDL_UnlockAudioDevice(myDevice); SDL_UnlockAudioDevice(myDevice);
} }
} }
@ -263,16 +277,15 @@ void SoundSDL2::adjustVolume(int direction)
Int32 percent = myVolume; Int32 percent = myVolume;
percent = BSPF::clamp(percent + direction * 2, 0, 100); percent = BSPF::clamp(percent + direction * 2, 0, 100);
setVolume(percent);
// Enable audio if it is currently disabled // Enable audio if it is currently disabled
const bool enabled = myAudioSettings.enabled(); const bool enabled = myAudioSettings.enabled();
if(percent > 0 && !enabled) if(percent > 0 && direction && !enabled)
{ {
setEnabled(true); setEnabled(true);
myOSystem.console().initializeAudio(); myOSystem.console().initializeAudio();
} }
setVolume(percent);
// Now show an onscreen message // Now show an onscreen message
ostringstream strval; ostringstream strval;
@ -418,6 +431,7 @@ bool SoundSDL2::playWav(const string& fileName, const uInt32 position,
// Set the callback function // Set the callback function
myWavSpec.callback = wavCallback; myWavSpec.callback = wavCallback;
myWavSpec.userdata = nullptr; myWavSpec.userdata = nullptr;
//myWavSpec.samples = 4096; // decrease for smaller samples;
} }
if(position > myWavLength) if(position > myWavLength)
return false; return false;
@ -472,16 +486,16 @@ void SoundSDL2::wavCallback(void* udata, uInt8* stream, int len)
if(static_cast<uInt32>(len) > myWavLen) if(static_cast<uInt32>(len) > myWavLen)
len = myWavLen; len = myWavLen;
// Mix volume adjusted WAV into silent buffer // Mix volume adjusted WAV data into silent buffer
SDL_MixAudioFormat(stream, myWavPos, myWavSpec.format, len, SDL_MixAudioFormat(stream, myWavPos, myWavSpec.format, len,
SDL_MIX_MAXVOLUME * myVolumeFactor); SDL_MIX_MAXVOLUME * myWavVolumeFactor);
myWavPos += len; myWavPos += len;
myWavLen -= len; myWavLen -= len;
} }
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
float SoundSDL2::myVolumeFactor = 0xffff; float SoundSDL2::myWavVolumeFactor = 0xffff;
SDL_AudioSpec SoundSDL2::myWavSpec; // audio output format SDL_AudioSpec SoundSDL2::myWavSpec; // audio output format
uInt8* SoundSDL2::myWavPos = nullptr; // pointer to the audio buffer to be played uInt8* SoundSDL2::myWavPos = nullptr; // pointer to the audio buffer to be played
uInt32 SoundSDL2::myWavLen = 0; // remaining length of the sample we have to play uInt32 SoundSDL2::myWavLen = 0; // remaining length of the sample we have to play

View File

@ -165,7 +165,9 @@ class SoundSDL2 : public Sound
// Current volume as a percentage (0 - 100) // Current volume as a percentage (0 - 100)
uInt32 myVolume{100}; uInt32 myVolume{100};
static float myVolumeFactor; float myVolumeFactor{0xffff};
// Current mute state, used to control WAV file sound
bool myMuteState{false};
// Audio specification structure // Audio specification structure
SDL_AudioSpec myHardwareSpec; SDL_AudioSpec myHardwareSpec;
@ -191,6 +193,7 @@ class SoundSDL2 : public Sound
SDL_AudioDeviceID myWavDevice{0}; SDL_AudioDeviceID myWavDevice{0};
uInt8* myWavBuffer{nullptr}; uInt8* myWavBuffer{nullptr};
static float myWavVolumeFactor;
static SDL_AudioSpec myWavSpec; // audio output format static SDL_AudioSpec myWavSpec; // audio output format
static uInt8* myWavPos; // pointer to the audio buffer to be played static uInt8* myWavPos; // pointer to the audio buffer to be played
static uInt32 myWavLen; // remaining length of the sample we have to play static uInt32 myWavLen; // remaining length of the sample we have to play