From b577cc75565c61f58a926d186ca7ee30e754afd4 Mon Sep 17 00:00:00 2001 From: Cameron Taylor Date: Mon, 9 Sep 2024 13:00:28 -0400 Subject: [PATCH] logarithmic volume control for global soundtray / volume --- flixel/system/frontEnds/SoundFrontEnd.hx | 20 ++++++++++++++++++++ flixel/system/ui/FlxSoundTray.hx | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/flixel/system/frontEnds/SoundFrontEnd.hx b/flixel/system/frontEnds/SoundFrontEnd.hx index 9784d759bb..b645445a45 100644 --- a/flixel/system/frontEnds/SoundFrontEnd.hx +++ b/flixel/system/frontEnds/SoundFrontEnd.hx @@ -357,10 +357,30 @@ class SoundFrontEnd public function changeVolume(Amount:Float):Void { muted = false; + volume = logToLinear(volume); volume += Amount; + volume = linearToLog(volume); showSoundTray(Amount > 0); } + public function linearToLog(x:Float, minValue:Float = 0.001):Float + { + // Ensure x is between 0 and 1 + x = Math.max(0, Math.min(1, x)); + + // Convert linear scale to logarithmic + return Math.exp(Math.log(minValue) * (1 - x)); + } + + public function logToLinear(x:Float, minValue:Float = 0.001):Float + { + // Ensure x is between minValue and 1 + x = Math.max(minValue, Math.min(1, x)); + + // Convert logarithmic scale to linear + return 1 - (Math.log(x) / Math.log(minValue)); + } + /** * Shows the sound tray if it is enabled. * @param up Whether or not the volume is increasing. diff --git a/flixel/system/ui/FlxSoundTray.hx b/flixel/system/ui/FlxSoundTray.hx index a5492bca4d..62869efc85 100644 --- a/flixel/system/ui/FlxSoundTray.hx +++ b/flixel/system/ui/FlxSoundTray.hx @@ -157,7 +157,7 @@ class FlxSoundTray extends Sprite y = 0; visible = true; active = true; - var globalVolume:Int = Math.round(FlxG.sound.volume * 10); + var globalVolume:Int = Math.round(FlxG.sound.logToLinear(FlxG.sound.volume) * 10); if (FlxG.sound.muted) {