From c5aad2ae7aa5f0b860b481da3ebb0cc304682ffb Mon Sep 17 00:00:00 2001 From: Christian Speckner Date: Sun, 28 Jan 2018 23:50:02 +0100 Subject: [PATCH] Volume adjustment. --- src/common/SoundSDL2.cxx | 26 +++++++++++++++++++------- src/common/SoundSDL2.hxx | 1 + 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/common/SoundSDL2.cxx b/src/common/SoundSDL2.cxx index 32ef5ec90..243a498a1 100644 --- a/src/common/SoundSDL2.cxx +++ b/src/common/SoundSDL2.cxx @@ -30,11 +30,19 @@ #include "SoundSDL2.hxx" #include "AudioQueue.hxx" +namespace { + inline Int16 applyVolume(Int16 sample, Int32 volumeFactor) + { + return static_cast(static_cast(sample) * volumeFactor / 0xffff); + } +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - SoundSDL2::SoundSDL2(OSystem& osystem) : Sound(osystem), myIsInitializedFlag(false), myVolume(100), + myVolumeFactor(0xffff), myAudioQueue(0), myCurrentFragment(0) { @@ -170,8 +178,10 @@ void SoundSDL2::setVolume(Int32 percent) if(myIsInitializedFlag && (percent >= 0) && (percent <= 100)) { myOSystem.settings().setValue("volume", percent); - SDL_LockAudio(); myVolume = percent; + + SDL_LockAudio(); + myVolumeFactor = static_cast(floor(static_cast(0xffff) * static_cast(myVolume) / 100.)); SDL_UnlockAudio(); } } @@ -243,17 +253,19 @@ void SoundSDL2::processFragment(Int16* stream, uInt32 length) if (isStereo) { if (isStereoTIA) { - stream[2*i] = myCurrentFragment[2*myFragmentIndex]; - stream[2*i + 1] = myCurrentFragment[2*myFragmentIndex + 1]; + stream[2*i] = applyVolume(myCurrentFragment[2*myFragmentIndex], myVolumeFactor); + stream[2*i + 1] = applyVolume(myCurrentFragment[2*myFragmentIndex + 1], myVolumeFactor); } else { - stream[2*i] = stream[2*i + 1] = myCurrentFragment[myFragmentIndex]; + stream[2*i] = stream[2*i + 1] = applyVolume(myCurrentFragment[myFragmentIndex], myVolumeFactor); } } else { if (isStereoTIA) { - stream[i] = - ((myCurrentFragment[2*myFragmentIndex] / 2) + (myCurrentFragment[2*myFragmentIndex + 1] / 2)); + stream[i] = applyVolume( + (myCurrentFragment[2*myFragmentIndex] / 2) + (myCurrentFragment[2*myFragmentIndex + 1] / 2), + myVolumeFactor + ); } else { - stream[i] = myCurrentFragment[myFragmentIndex]; + stream[i] = applyVolume(myCurrentFragment[myFragmentIndex], myVolumeFactor); } } } diff --git a/src/common/SoundSDL2.hxx b/src/common/SoundSDL2.hxx index a058eaca8..da141959d 100644 --- a/src/common/SoundSDL2.hxx +++ b/src/common/SoundSDL2.hxx @@ -113,6 +113,7 @@ class SoundSDL2 : public Sound // Current volume as a percentage (0 - 100) uInt32 myVolume; + Int32 myVolumeFactor; // Audio specification structure SDL_AudioSpec myHardwareSpec;