-
Notifications
You must be signed in to change notification settings - Fork 452
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 6 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,47 @@ | ||
| // 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 class BindableVolume | ||
| { | ||
| public const double MIN = -60; | ||
| public const double STEP = 0.5; | ||
|
|
||
| private const double ln_ten = 2.302585092994045684017991454684364208; | ||
| private const double k = ln_ten / 20; | ||
|
|
||
| public readonly BindableNumber<double> Real; | ||
| public readonly BindableNumber<double> Scaled = new BindableNumber<double>(1) | ||
| { | ||
| MinValue = MIN, | ||
| MaxValue = 0, | ||
| Precision = STEP, | ||
| }; | ||
|
|
||
| private double scaledToReal(double x) => x <= MIN ? 0 : Math.Exp(k * x); | ||
|
|
||
| private double realToScaled(double x) => x <= 0 ? MIN : Math.Log(x) / k; | ||
|
|
||
| public BindableVolume(BindableNumber<double>? real = null) | ||
| { | ||
| Real = real ?? new BindableNumber<double>(1) { MinValue = 0, MaxValue = 1 }; | ||
| Scaled.BindValueChanged(x => Real.Value = scaledToReal(x.NewValue)); | ||
| } | ||
|
|
||
| public double Value | ||
| { | ||
| get => Real.Value; | ||
| set => Scaled.Value = realToScaled(value); | ||
| } | ||
|
|
||
| public void Scale() | ||
| { | ||
| Scaled.Value = realToScaled(Real.Value); | ||
| Scaled.Default = realToScaled(Real.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,9 +173,13 @@ private void load(FrameworkConfigManager config) | |
|
|
||
| // attach our bindables to the audio subsystem. | ||
| config.BindWith(FrameworkSetting.AudioDevice, Audio.AudioDevice); | ||
| config.BindWith(FrameworkSetting.VolumeUniversal, Audio.Volume); | ||
| config.BindWith(FrameworkSetting.VolumeEffect, Audio.VolumeSample); | ||
| config.BindWith(FrameworkSetting.VolumeMusic, Audio.VolumeTrack); | ||
| config.BindWith(FrameworkSetting.VolumeUniversal, Audio.Volume.Real); | ||
| config.BindWith(FrameworkSetting.VolumeEffect, Audio.VolumeSample.Real); | ||
| config.BindWith(FrameworkSetting.VolumeMusic, Audio.VolumeTrack.Real); | ||
|
|
||
| Audio.Volume.Scale(); | ||
| Audio.VolumeSample.Scale(); | ||
| Audio.VolumeTrack.Scale(); | ||
|
Comment on lines
+180
to
+182
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be required. It will happen on |
||
|
|
||
| Shaders = new ShaderManager(Host.Renderer, new NamespacedResourceStore<byte[]>(Resources, @"Shaders")); | ||
| dependencies.Cache(Shaders); | ||
|
|
||
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.
in what world is it sane to have a property return values on a different scale than the one used to set the value
it should not be the case that after you set a property's value, you read it back and get a completely different number. that is nonsensical
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.
I should probably be able to get rid of that. I think at first, I wasn't as explicit about whether I was using the real value or the scaled value.
Uh oh!
There was an error while loading. Please reload this page.
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.
Okay, so it does sorta make sense. We don't want to assign directly to
Real. Instead, we want to assign toScaledonly, and whenever we do, that triggers aBindValueChangedfunction, and that's whereRealgets assigned. So assigning toScaledeffectively assigns to both of them.Realdoesn't have an equivalentBindValueChangedfunction as that would result in an infinite loop.Also, instead of assigning directly to
Scaled, sometimes we want to assign a linear value, but it has to go throughScaledfirst, so the setter's job is to first translate the linear value into a logarithmic value.I'll probably still get rid of the getter, as that is a bit confusing, and I think it's just there for convenience sake, but it's better to be explicit.