diff --git a/Core/Config.cpp b/Core/Config.cpp index 118c9de5ad..f56e92175f 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -2092,3 +2092,31 @@ bool PlayTimeTracker::GetPlayedTimeString(const std::string &gameId, std::string *str = ApplySafeSubstitutions(ga->T("Time Played: %1h %2m %3s"), hours, minutes, seconds); return true; } + +// This matches exactly the old shift-based curve. +float Volume10ToMultiplier(int volume) { + // Allow muting entirely. + if (volume <= 0) { + return 0.0f; + } + return powf(2.0f, (float)(volume - 10)); +} + +// NOTE: This is used for new volume parameters. +// It uses a more intuitive-feeling curve. +float Volume100ToMultiplier(int volume) { + // Switch to linear above the 1.0f point. + if (volume > 100) { + return volume / 100.0f; + } + return powf(volume * 0.01f, 1.75f); +} + +// Used for migration from the old settings. +int MultiplierToVolume100(float multiplier) { + // Switch to linear above the 1.0f point. + if (multiplier > 1.0f) { + return multiplier * 100; + } + return (int)(powf(multiplier, 1.0f / 1.75f) * 100.f + 0.5f); +} diff --git a/Core/ConfigValues.h b/Core/ConfigValues.h index 1fefc2d03f..1dd1fc88b4 100644 --- a/Core/ConfigValues.h +++ b/Core/ConfigValues.h @@ -34,19 +34,14 @@ constexpr int VOLUME_FULL = 10; constexpr int VOLUMEHI_FULL = 100; // for newer volume params. will convert them all later // This matches exactly the old shift-based curve. -inline float Volume10ToMultiplier(int volume) { - // Allow muting entirely. - if (volume <= 0) { - return 0.0f; - } - return powf(2.0f, (float)(volume - 10)); -} +float Volume10ToMultiplier(int volume); // NOTE: This is used for new volume parameters. // It uses a more intuitive-feeling curve. -inline float Volume100ToMultiplier(int volume) { - return powf(volume * 0.01f, 1.75f); -} +float Volume100ToMultiplier(int volume); + +// Used for migration from the old settings. +int MultiplierToVolume100(float multiplier); struct ConfigTouchPos { float x; diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index 52a72f36fc..733bb10884 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -1218,6 +1218,20 @@ bool TestCrossSIMD() { return true; } +bool TestVolumeFunc() { + for (int i = 0; i <= 20; i++) { + float mul = Volume10ToMultiplier(i); + + int vol100 = MultiplierToVolume100(mul); + float mul2 = Volume100ToMultiplier(vol100); + + bool smaller = (fabsf(mul2 - mul) < 0.02f); + EXPECT_TRUE(smaller); + // printf("%d -> %f -> %d -> %f\n", i, mul, vol100, mul2); + } + return true; +} + typedef bool (*TestFunc)(); struct TestItem { const char *name; @@ -1282,6 +1296,7 @@ TestItem availableTests[] = { TEST_ITEM(Buffer), TEST_ITEM(SIMD), TEST_ITEM(CrossSIMD), + TEST_ITEM(VolumeFunc), }; int main(int argc, const char *argv[]) {