1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-17 22:17:25 +08:00

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 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;