diff --git a/src/common/SoundSDL2.cxx b/src/common/SoundSDL2.cxx index f7b3e256e..d23f1ad52 100644 --- a/src/common/SoundSDL2.cxx +++ b/src/common/SoundSDL2.cxx @@ -28,16 +28,16 @@ #include "System.hxx" #include "OSystem.hxx" #include "Console.hxx" -#include "SoundSDL2.hxx" #include "AudioQueue.hxx" #include "EmulationTiming.hxx" #include "AudioSettings.hxx" #include "audio/SimpleResampler.hxx" #include "audio/LanczosResampler.hxx" #include "StaggeredLogger.hxx" - #include "ThreadDebugging.hxx" +#include "SoundSDL2.hxx" + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - SoundSDL2::SoundSDL2(OSystem& osystem, AudioSettings& audioSettings) : Sound{osystem}, @@ -47,7 +47,8 @@ SoundSDL2::SoundSDL2(OSystem& osystem, AudioSettings& audioSettings) Logger::debug("SoundSDL2::SoundSDL2 started ..."); - if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { + if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) + { ostringstream buf; buf << "WARNING: Failed to initialize SDL audio system! " << endl @@ -72,12 +73,14 @@ SoundSDL2::~SoundSDL2() { ASSERT_MAIN_THREAD; - stopWav(); // NOLINT + if(!myIsInitializedFlag) + return; + if(myWavDevice) + { SDL_CloseAudioDevice(myWavDevice); - - if (!myIsInitializedFlag) return; - + SDL_FreeWAV(myWavBuffer); + } SDL_CloseAudioDevice(myDevice); SDL_QuitSubSystem(SDL_INIT_AUDIO); } @@ -95,7 +98,8 @@ void SoundSDL2::queryHardware(VariantList& devices) Logger::debug(s.str()); VarList::push_back(devices, "Default", 0); - for(int i = 0; i < numDevices; ++i) { + for(int i = 0; i < numDevices; ++i) + { ostringstream ss; ss << " " << i + 1 << ": " << SDL_GetAudioDeviceName(i, 0); @@ -144,7 +148,8 @@ bool SoundSDL2::openDevice() void SoundSDL2::setEnabled(bool enable) { myAudioSettings.setEnabled(enable); - if (myAudioQueue) myAudioQueue->ignoreOverflows(!enable); + if(myAudioQueue) + myAudioQueue->ignoreOverflows(!enable); Logger::debug(enable ? "SoundSDL2::setEnabled(true)" : "SoundSDL2::setEnabled(false)"); @@ -199,11 +204,13 @@ void SoundSDL2::open(shared_ptr audioQueue, void SoundSDL2::close() { stopWav(); - if(!myIsInitializedFlag) return; + if(!myIsInitializedFlag) + return; mute(true); - if (myAudioQueue) myAudioQueue->closeSink(myCurrentFragment); + if(myAudioQueue) + myAudioQueue->closeSink(myCurrentFragment); myAudioQueue.reset(); myCurrentFragment = nullptr; } @@ -233,21 +240,6 @@ bool SoundSDL2::toggleMute() myOSystem.frameBuffer().showTextMessage(message); - //ostringstream strval; - //uInt32 volume; - //// Now show an onscreen message - //if(enabled) - //{ - // volume = myVolume; - // strval << volume << "%"; - //} - //else - //{ - // volume = 0; - // strval << "Muted"; - //} - //myOSystem.frameBuffer().showMessage("Volume", strval.str(), volume); - return enabled; } @@ -268,9 +260,7 @@ void SoundSDL2::setVolume(uInt32 percent) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void SoundSDL2::adjustVolume(int direction) { - ostringstream strval; Int32 percent = myVolume; - percent = BSPF::clamp(percent + direction * 2, 0, 100); setVolume(percent); @@ -285,10 +275,8 @@ void SoundSDL2::adjustVolume(int direction) } // Now show an onscreen message - if(percent) - strval << percent << "%"; - else - strval << "Off"; + ostringstream strval; + (percent) ? strval << percent << "%" : strval << "Off"; myOSystem.frameBuffer().showGaugeMessage("Volume", strval.str(), percent); } @@ -302,7 +290,8 @@ string SoundSDL2::about() const << " Channels: " << static_cast(myHardwareSpec.channels) << (myAudioQueue->isStereo() ? " (Stereo)" : " (Mono)") << endl << " Preset: "; - switch (myAudioSettings.preset()) { + switch(myAudioSettings.preset()) + { case AudioSettings::Preset::custom: buf << "Custom" << endl; break; @@ -319,8 +308,10 @@ string SoundSDL2::about() const buf << "Ultra quality, minimal lag" << endl; break; } - buf << " Fragment size: " << static_cast(myHardwareSpec.samples) << " bytes" << endl - << " Sample rate: " << static_cast(myHardwareSpec.freq) << " Hz" << endl; + buf << " Fragment size: " << static_cast(myHardwareSpec.samples) + << " bytes" << endl + << " Sample rate: " << static_cast(myHardwareSpec.freq) + << " Hz" << endl; buf << " Resampling: "; switch(myAudioSettings.resamplingQuality()) { @@ -346,7 +337,7 @@ void SoundSDL2::processFragment(float* stream, uInt32 length) { myResampler->fillFragment(stream, length); - for (uInt32 i = 0; i < length; ++i) + for(uInt32 i = 0; i < length; ++i) stream[i] *= myVolumeFactor; } @@ -356,34 +347,41 @@ void SoundSDL2::initResampler() Resampler::NextFragmentCallback nextFragmentCallback = [this] () -> Int16* { Int16* nextFragment = nullptr; - if (myUnderrun) + if(myUnderrun) nextFragment = myAudioQueue->size() >= myEmulationTiming->prebufferFragmentCount() ? myAudioQueue->dequeue(myCurrentFragment) : nullptr; else nextFragment = myAudioQueue->dequeue(myCurrentFragment); myUnderrun = nextFragment == nullptr; - if (nextFragment) myCurrentFragment = nextFragment; + if(nextFragment) + myCurrentFragment = nextFragment; return nextFragment; }; Resampler::Format formatFrom = - Resampler::Format(myEmulationTiming->audioSampleRate(), myAudioQueue->fragmentSize(), myAudioQueue->isStereo()); + Resampler::Format(myEmulationTiming->audioSampleRate(), + myAudioQueue->fragmentSize(), myAudioQueue->isStereo()); Resampler::Format formatTo = - Resampler::Format(myHardwareSpec.freq, myHardwareSpec.samples, myHardwareSpec.channels > 1); + Resampler::Format(myHardwareSpec.freq, myHardwareSpec.samples, + myHardwareSpec.channels > 1); - switch (myAudioSettings.resamplingQuality()) { + switch(myAudioSettings.resamplingQuality()) + { case AudioSettings::ResamplingQuality::nearestNeightbour: - myResampler = make_unique(formatFrom, formatTo, nextFragmentCallback); + myResampler = make_unique(formatFrom, formatTo, + nextFragmentCallback); break; case AudioSettings::ResamplingQuality::lanczos_2: - myResampler = make_unique(formatFrom, formatTo, nextFragmentCallback, 2); + myResampler = make_unique(formatFrom, formatTo, + nextFragmentCallback, 2); break; case AudioSettings::ResamplingQuality::lanczos_3: - myResampler = make_unique(formatFrom, formatTo, nextFragmentCallback, 3); + myResampler = make_unique(formatFrom, formatTo, + nextFragmentCallback, 3); break; default: @@ -396,14 +394,14 @@ void SoundSDL2::callback(void* udata, uInt8* stream, int len) { auto* self = static_cast(udata); - if (self->myAudioQueue) + if(self->myAudioQueue) self->processFragment(reinterpret_cast(stream), len >> 2); else SDL_memset(stream, 0, len); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -bool SoundSDL2::playWav(const char* fileName, uInt32 position, uInt32 length) +bool SoundSDL2::playWav(const string& fileName, uInt32 position, uInt32 length) { uInt32 wavLength{0}; @@ -411,7 +409,7 @@ bool SoundSDL2::playWav(const char* fileName, uInt32 position, uInt32 length) stopWav(); // Load WAV file - SDL_AudioSpec* result = SDL_LoadWAV(fileName, &myWavSpec, &myWavBuffer, &wavLength); + auto* result = SDL_LoadWAV(fileName.c_str(), &myWavSpec, &myWavBuffer, &wavLength); if(result == nullptr || position > wavLength) return false; @@ -452,6 +450,7 @@ void SoundSDL2::stopWav() } } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - uInt32 SoundSDL2::wavSize() const { return myWavBuffer ? myWavLen /*SDL_GetQueuedAudioSize(myWavDevice)*/ : 0; @@ -465,6 +464,7 @@ void SoundSDL2::wavCallback(void* udata, uInt8* stream, int len) { if(static_cast(len) > myWavLen) len = myWavLen; + // Mix volume adjusted WAV into silent buffer SDL_MixAudioFormat(stream, myWavPos, myWavSpec.format, len, SDL_MIX_MAXVOLUME * myVolumeFactor); @@ -473,6 +473,7 @@ void SoundSDL2::wavCallback(void* udata, uInt8* stream, int len) } } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - float SoundSDL2::myVolumeFactor = 0xffff; SDL_AudioSpec SoundSDL2::myWavSpec; // audio output format uInt8* SoundSDL2::myWavPos = nullptr; // pointer to the audio buffer to be played diff --git a/src/common/SoundSDL2.hxx b/src/common/SoundSDL2.hxx index 567dd08ad..b2b9b4ee6 100644 --- a/src/common/SoundSDL2.hxx +++ b/src/common/SoundSDL2.hxx @@ -116,7 +116,8 @@ class SoundSDL2 : public Sound @return True, if the WAV file can be played */ - bool playWav(const char* fileName, uInt32 position = 0, uInt32 length = 0) override; + bool playWav(const string& fileName, uInt32 position = 0, + uInt32 length = 0) override; /** Stop any currently playing WAV file. diff --git a/src/emucore/KidVid.cxx b/src/emucore/KidVid.cxx index 351cee40c..adc87a140 100644 --- a/src/emucore/KidVid.cxx +++ b/src/emucore/KidVid.cxx @@ -191,13 +191,12 @@ void KidVid::setNextSong() mySongLength = ourSongStart[temp + 1] - ourSongStart[temp]; // Play the WAV file - const string fileName = (temp < 10) ? myBaseDir + "KVSHARED.WAV" : mySampleFile; - mySound.playWav(fileName.c_str(), ourSongStart[temp], mySongLength); + const string& fileName = (temp < 10) ? myBaseDir + "KVSHARED.WAV" : mySampleFile; + mySound.playWav(fileName, ourSongStart[temp], mySongLength); #ifdef DEBUG_BUILD cerr << fileName << ": " << (ourSongPositions[mySongPointer] & 0x7f) << endl; #endif - mySongPlaying = myTapeBusy = true; ++mySongPointer; } diff --git a/src/emucore/KidVid.hxx b/src/emucore/KidVid.hxx index 86d2880e1..57743ce64 100644 --- a/src/emucore/KidVid.hxx +++ b/src/emucore/KidVid.hxx @@ -33,7 +33,7 @@ class Sound; This code was heavily borrowed from z26. - @author Stephen Anthony & z26 team + @author Thomas Jentzsch & z26 team */ class KidVid : public Controller { diff --git a/src/emucore/Sound.hxx b/src/emucore/Sound.hxx index 016f7c102..902fb80a5 100644 --- a/src/emucore/Sound.hxx +++ b/src/emucore/Sound.hxx @@ -114,7 +114,7 @@ class Sound @return True, if the WAV file can be played */ - virtual bool playWav(const char* fileName, uInt32 position = 0, + virtual bool playWav(const string& fileName, uInt32 position = 0, uInt32 length = 0) { return false; } /**