-
Notifications
You must be signed in to change notification settings - Fork 437
Use decibel scaling for volume control #6490
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f0a489f
Use volume scaling
myQwil ef5a892
Display volume in decibel format
myQwil 41ec0a8
Use Math.Log(10) instead of the numeric literal
myQwil 38e105d
Further simplify the assigning of `k`
myQwil 2b61198
Rename `VolumeScaler` to `BindableVolume`
myQwil 998f687
Bring back `ln_ten` as a numeric literal
myQwil 370a49c
Make BindableVolume a sealed class
myQwil 590d1d0
Remove BindableVolume.Value
myQwil 8ea57e8
Make `k` a static readonly so we can use Math.Log(10)
myQwil 844989f
Rename `Real/Scaled` to `Linear/Decibel`
myQwil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System; | ||
using osu.Framework.Bindables; | ||
|
||
namespace osu.Framework.Audio | ||
{ | ||
public sealed class BindableVolume | ||
{ | ||
public const double MIN = -60; | ||
public const double STEP = 0.5; | ||
|
||
private static readonly double k = Math.Log(10) / 20; | ||
|
||
public readonly BindableNumber<double> Linear; | ||
public readonly BindableNumber<double> Decibel = new BindableNumber<double>(1) | ||
{ | ||
MinValue = MIN, | ||
MaxValue = 0, | ||
Precision = STEP, | ||
}; | ||
|
||
private double decibelToLinear(double x) => x <= MIN ? 0 : Math.Exp(k * x); | ||
|
||
private double linearToDecibel(double x) => x <= 0 ? MIN : Math.Log(x) / k; | ||
|
||
public BindableVolume(BindableNumber<double>? linear = null) | ||
{ | ||
Linear = linear ?? new BindableNumber<double>(1) { MinValue = 0, MaxValue = 1 }; | ||
Decibel.BindValueChanged(x => Linear.Value = decibelToLinear(x.NewValue)); | ||
} | ||
|
||
public void SetFromLinear(double linear) | ||
{ | ||
Decibel.Value = linearToDecibel(linear); | ||
} | ||
|
||
public void Scale() | ||
{ | ||
Decibel.Value = linearToDecibel(Linear.Value); | ||
Decibel.Default = linearToDecibel(Linear.Default); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be required. It will happen on
BindWith
.