osu/osu.Game/Rulesets/Mods/ModTimeRamp.cs

101 lines
3.6 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
2019-03-06 05:14:41 +00:00
using System.Linq;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Audio;
using osu.Game.Beatmaps;
2019-12-09 10:58:51 +00:00
using osu.Game.Configuration;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.UI;
namespace osu.Game.Rulesets.Mods
{
public abstract class ModTimeRamp : Mod, IUpdatableByPlayfield, IApplicableToBeatmap, IApplicableToAudio
{
/// <summary>
/// The point in the beatmap at which the final ramping rate should be reached.
/// </summary>
private const double final_rate_progress = 0.75f;
[SettingSource("Initial rate", "The starting speed of the track")]
public abstract BindableNumber<double> InitialRate { get; }
2019-12-09 10:58:51 +00:00
[SettingSource("Final rate", "The final speed to ramp to")]
public abstract BindableNumber<double> FinalRate { get; }
[SettingSource("Adjust pitch", "Should pitch be adjusted with speed")]
public abstract BindableBool AdjustPitch { get; }
2020-03-23 16:54:08 +00:00
public override string SettingDescription => $"{InitialRate.Value:N2}x to {FinalRate.Value:N2}x";
2020-03-19 03:43:26 +00:00
2019-03-06 05:14:41 +00:00
private double finalRateTime;
private double beginRampTime;
public BindableNumber<double> SpeedChange { get; } = new BindableDouble
{
Default = 1,
Value = 1,
Precision = 0.01,
};
2019-03-06 05:14:41 +00:00
2020-08-05 12:10:38 +00:00
private ITrack track;
2019-12-12 14:33:18 +00:00
protected ModTimeRamp()
{
// for preview purpose at song select. eventually we'll want to be able to update every frame.
2020-06-14 16:48:49 +00:00
FinalRate.BindValueChanged(val => applyRateAdjustment(1), true);
AdjustPitch.BindValueChanged(applyPitchAdjustment);
}
public void ApplyToTrack(ITrack track)
{
2020-08-05 12:10:38 +00:00
this.track = track;
2019-12-12 08:45:11 +00:00
FinalRate.TriggerChange();
AdjustPitch.TriggerChange();
}
public void ApplyToSample(DrawableSample sample)
{
sample.AddAdjustment(AdjustableProperty.Frequency, SpeedChange);
}
public virtual void ApplyToBeatmap(IBeatmap beatmap)
{
2019-03-06 05:14:41 +00:00
HitObject lastObject = beatmap.HitObjects.LastOrDefault();
2019-12-09 10:58:51 +00:00
SpeedChange.SetDefault();
2019-03-06 05:14:41 +00:00
beginRampTime = beatmap.HitObjects.FirstOrDefault()?.StartTime ?? 0;
finalRateTime = final_rate_progress * (lastObject?.GetEndTime() ?? 0);
}
public virtual void Update(Playfield playfield)
{
2020-06-14 16:48:49 +00:00
applyRateAdjustment((track.CurrentTime - beginRampTime) / finalRateTime);
}
/// <summary>
/// Adjust the rate along the specified ramp
/// </summary>
/// <param name="amount">The amount of adjustment to apply (from 0..1).</param>
2020-06-14 16:48:49 +00:00
private void applyRateAdjustment(double amount) =>
SpeedChange.Value = InitialRate.Value + (FinalRate.Value - InitialRate.Value) * Math.Clamp(amount, 0, 1);
2020-06-14 16:50:07 +00:00
private void applyPitchAdjustment(ValueChangedEvent<bool> adjustPitchSetting)
{
// remove existing old adjustment
2020-08-11 16:41:21 +00:00
track?.RemoveAdjustment(adjustmentForPitchSetting(adjustPitchSetting.OldValue), SpeedChange);
2020-08-11 16:41:21 +00:00
track?.AddAdjustment(adjustmentForPitchSetting(adjustPitchSetting.NewValue), SpeedChange);
}
2020-06-14 16:50:07 +00:00
private AdjustableProperty adjustmentForPitchSetting(bool adjustPitchSettingValue)
=> adjustPitchSettingValue ? AdjustableProperty.Frequency : AdjustableProperty.Tempo;
}
2019-03-06 05:14:41 +00:00
}