Skip to content
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

logarithmic volume control for global soundtray / volume #3248

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions flixel/system/frontEnds/SoundFrontEnd.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion flixel/system/ui/FlxSoundTray.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Loading