Skip to content

Volume control UI changes, part 2 #19971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement new volume conversion functions, add test
  • Loading branch information
hrydgard committed Feb 12, 2025
commit 33c4516e7258a0b08075a8fad4ad5c6e33d823f7
28 changes: 28 additions & 0 deletions Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
15 changes: 5 additions & 10 deletions Core/ConfigValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions unittest/UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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[]) {
Expand Down