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.
This commit is contained in:
Dean Herbert 2021-07-02 15:44:35 +09:00
parent 69b1347730
commit 13254d51fc

View File

@ -38,7 +38,6 @@ namespace osu.Game.Overlays.Volume
private OsuSpriteText text; private OsuSpriteText text;
private BufferedContainer maxGlow; private BufferedContainer maxGlow;
private bool firstUpdate = true;
private Sample sample; private Sample sample;
private double sampleLastPlaybackTime; 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; bgProgress.Current.Value = 0.75f;
} }
protected override void LoadComplete() private int displayVolumeInt;
{
base.LoadComplete();
Bindable.TriggerChange();
}
private double displayVolume; private double displayVolume;
@ -205,9 +200,16 @@ namespace osu.Game.Overlays.Volume
get => displayVolume; get => displayVolume;
set set
{ {
bool displayVolumeChanged = Math.Round(displayVolume * 100) != Math.Round(value * 100); if (value == displayVolume)
return;
displayVolume = value; displayVolume = value;
int intValue = (int)Math.Round(displayVolume * 100);
bool intVolumeChanged = intValue != displayVolumeInt;
displayVolumeInt = intValue;
if (displayVolume >= 0.995f) if (displayVolume >= 0.995f)
{ {
text.Text = "MAX"; text.Text = "MAX";
@ -216,37 +218,36 @@ namespace osu.Game.Overlays.Volume
else else
{ {
maxGlow.EffectColour = Color4.Transparent; 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; volumeCircle.Current.Value = displayVolume * 0.75f;
volumeCircleGlow.Current.Value = displayVolume * 0.75f; volumeCircleGlow.Current.Value = displayVolume * 0.75f;
Schedule(() => if (intVolumeChanged && IsLoaded)
{ Scheduler.AddOnce(playTickSound);
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;
});
} }
} }
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 public double Volume
{ {
get => Bindable.Value; get => Bindable.Value;