From 13254d51fc93a33dbe67921d4a62b112bd4ae2ae Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 2 Jul 2021 15:44:35 +0900 Subject: [PATCH] Remove usage of `bool` for initial playback Also refactors the whole method to generally clean things up. One more important fix is setting the frequency on the channel before starting playback, so avoid the frequency potentially being adjusted after the playback is already started. --- osu.Game/Overlays/Volume/VolumeMeter.cs | 63 +++++++++++++------------ 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/osu.Game/Overlays/Volume/VolumeMeter.cs b/osu.Game/Overlays/Volume/VolumeMeter.cs index 6a26de204c..3321dcf329 100644 --- a/osu.Game/Overlays/Volume/VolumeMeter.cs +++ b/osu.Game/Overlays/Volume/VolumeMeter.cs @@ -38,7 +38,6 @@ namespace osu.Game.Overlays.Volume private OsuSpriteText text; private BufferedContainer maxGlow; - private bool firstUpdate = true; private Sample sample; private double sampleLastPlaybackTime; @@ -187,16 +186,12 @@ namespace osu.Game.Overlays.Volume } }; - Bindable.ValueChanged += volume => { this.TransformTo(nameof(DisplayVolume), volume.NewValue, 400, Easing.OutQuint); }; + Bindable.BindValueChanged(volume => { this.TransformTo(nameof(DisplayVolume), volume.NewValue, 400, Easing.OutQuint); }, true); bgProgress.Current.Value = 0.75f; } - protected override void LoadComplete() - { - base.LoadComplete(); - Bindable.TriggerChange(); - } + private int displayVolumeInt; private double displayVolume; @@ -205,9 +200,16 @@ namespace osu.Game.Overlays.Volume get => displayVolume; set { - bool displayVolumeChanged = Math.Round(displayVolume * 100) != Math.Round(value * 100); + if (value == displayVolume) + return; + displayVolume = value; + int intValue = (int)Math.Round(displayVolume * 100); + bool intVolumeChanged = intValue != displayVolumeInt; + + displayVolumeInt = intValue; + if (displayVolume >= 0.995f) { text.Text = "MAX"; @@ -216,37 +218,36 @@ namespace osu.Game.Overlays.Volume else { maxGlow.EffectColour = Color4.Transparent; - text.Text = Math.Round(displayVolume * 100).ToString(CultureInfo.CurrentCulture); + text.Text = displayVolumeInt.ToString(CultureInfo.CurrentCulture); } volumeCircle.Current.Value = displayVolume * 0.75f; volumeCircleGlow.Current.Value = displayVolume * 0.75f; - Schedule(() => - { - const int sfx_debounce_time = 30; - - if (firstUpdate || - !displayVolumeChanged || - Time.Current - sampleLastPlaybackTime <= sfx_debounce_time) - { - firstUpdate = false; - return; - } - - var channel = sample.Play(); - - channel.Frequency.Value = 1 + displayVolume * 0.1f + RNG.NextDouble(0.02f); - if (displayVolume < 0.005f) - channel.Frequency.Value -= 0.5f; - else if (displayVolume > 0.995f) - channel.Frequency.Value -= 0.5f; - - sampleLastPlaybackTime = Time.Current; - }); + if (intVolumeChanged && IsLoaded) + Scheduler.AddOnce(playTickSound); } } + private void playTickSound() + { + const int tick_debounce_time = 30; + + if (Time.Current - sampleLastPlaybackTime <= tick_debounce_time) + return; + + var channel = sample.GetChannel(); + + channel.Frequency.Value = 1 + displayVolume * 0.1f + RNG.NextDouble(0.02f); + + if (displayVolumeInt == 0) + channel.Frequency.Value -= 0.5f; + else if (displayVolumeInt == 100) channel.Frequency.Value -= 0.5f; + + channel.Play(); + sampleLastPlaybackTime = Time.Current; + } + public double Volume { get => Bindable.Value;