// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using System; using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Audio.Track; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Audio; using osu.Framework.Graphics.Containers; using osu.Framework.Logging; using osu.Framework.Threading; using osu.Game.Beatmaps; using osu.Game.Database; using osu.Game.Rulesets.Mods; namespace osu.Game.Overlays { /// /// Handles playback of the global music track. /// public partial class MusicController : CompositeDrawable { [Resolved] private BeatmapManager beatmaps { get; set; } /// /// Point in time after which the current track will be restarted on triggering a "previous track" action. /// private const double restart_cutoff_point = 5000; /// /// Whether the user has requested the track to be paused. Use to determine whether the track is still playing. /// public bool UserPauseRequested { get; private set; } /// /// Whether user control of the global track should be allowed. /// public readonly BindableBool AllowTrackControl = new BindableBool(true); /// /// Fired when the global has changed. /// Includes direction information for display purposes. /// public event Action TrackChanged; [Resolved] private IBindable beatmap { get; set; } [Resolved] private IBindable> mods { get; set; } [NotNull] public DrawableTrack CurrentTrack { get; private set; } = new DrawableTrack(new TrackVirtual(1000)); [Resolved] private RealmAccess realm { get; set; } protected override void LoadComplete() { base.LoadComplete(); beatmap.BindValueChanged(b => changeBeatmap(b.NewValue), true); mods.BindValueChanged(_ => ResetTrackAdjustments(), true); } /// /// Forcefully reload the current 's track from disk. /// public void ReloadCurrentTrack() { changeTrack(); TrackChanged?.Invoke(current, TrackChangeDirection.None); } /// /// Returns whether the beatmap track is playing. /// public bool IsPlaying => CurrentTrack.IsRunning; /// /// Returns whether the beatmap track is loaded. /// public bool TrackLoaded => CurrentTrack.TrackLoaded; private ScheduledDelegate seekDelegate; public void SeekTo(double position) { seekDelegate?.Cancel(); seekDelegate = Schedule(() => { if (beatmap.Disabled || !AllowTrackControl.Value) return; CurrentTrack.Seek(position); }); } /// /// Ensures music is playing, no matter what, unless the user has explicitly paused. /// This means that if the current beatmap has a virtual track (see ) a new beatmap will be selected. /// public void EnsurePlayingSomething() { if (UserPauseRequested) return; if (CurrentTrack.IsDummyDevice || beatmap.Value.BeatmapSetInfo.DeletePending) { if (beatmap.Disabled || !AllowTrackControl.Value) return; Logger.Log($"{nameof(MusicController)} skipping next track to {nameof(EnsurePlayingSomething)}"); NextTrack(); } else if (!IsPlaying) { Logger.Log($"{nameof(MusicController)} starting playback to {nameof(EnsurePlayingSomething)}"); Play(); } } /// /// Start playing the current track (if not already playing). /// /// Whether to restart the track from the beginning. /// /// Whether the request to play was issued by the user rather than internally. /// Specifying true will ensure that other methods like /// will resume music playback going forward. /// /// Whether the operation was successful. public bool Play(bool restart = false, bool requestedByUser = false) { if (requestedByUser && !AllowTrackControl.Value) return false; if (requestedByUser) UserPauseRequested = false; if (restart) CurrentTrack.RestartAsync(); else if (!IsPlaying) CurrentTrack.StartAsync(); return true; } /// /// Stop playing the current track and pause at the current position. /// /// /// Whether the request to stop was issued by the user rather than internally. /// Specifying true will ensure that other methods like /// will not resume music playback until the next explicit call to . /// public void Stop(bool requestedByUser = false) { if (requestedByUser && !AllowTrackControl.Value) return; UserPauseRequested |= requestedByUser; if (CurrentTrack.IsRunning) CurrentTrack.StopAsync(); } /// /// Toggle pause / play. /// /// Whether the operation was successful. public bool TogglePause() { if (!AllowTrackControl.Value) return false; if (CurrentTrack.IsRunning) Stop(true); else Play(requestedByUser: true); return true; } /// /// Play the previous track or restart the current track if it's current time below . /// /// Invoked when the operation has been performed successfully. public void PreviousTrack(Action onSuccess = null) => Schedule(() => { PreviousTrackResult res = prev(); if (res != PreviousTrackResult.None) onSuccess?.Invoke(res); }); /// /// Play the previous track or restart the current track if it's current time below . /// /// The that indicate the decided action. private PreviousTrackResult prev() { if (beatmap.Disabled || !AllowTrackControl.Value) return PreviousTrackResult.None; double currentTrackPosition = CurrentTrack.CurrentTime; if (currentTrackPosition >= restart_cutoff_point) { SeekTo(0); return PreviousTrackResult.Restart; } queuedDirection = TrackChangeDirection.Prev; var playableSet = getBeatmapSets().AsEnumerable().TakeWhile(i => !i.Equals(current.BeatmapSetInfo)).LastOrDefault() ?? getBeatmapSets().LastOrDefault(); if (playableSet != null) { changeBeatmap(beatmaps.GetWorkingBeatmap(playableSet.Beatmaps.First())); restartTrack(); return PreviousTrackResult.Previous; } return PreviousTrackResult.None; } /// /// Play the next random or playlist track. /// /// Invoked when the operation has been performed successfully. /// A of the operation. public void NextTrack(Action onSuccess = null) => Schedule(() => { bool res = next(); if (res) onSuccess?.Invoke(); }); private bool next() { if (beatmap.Disabled || !AllowTrackControl.Value) return false; queuedDirection = TrackChangeDirection.Next; var playableSet = getBeatmapSets().AsEnumerable().SkipWhile(i => !i.Equals(current.BeatmapSetInfo)).ElementAtOrDefault(1) ?? getBeatmapSets().FirstOrDefault(); var playableBeatmap = playableSet?.Beatmaps.FirstOrDefault(); if (playableBeatmap != null) { changeBeatmap(beatmaps.GetWorkingBeatmap(playableBeatmap)); restartTrack(); return true; } return false; } private void restartTrack() { // if not scheduled, the previously track will be stopped one frame later (see ScheduleAfterChildren logic in GameBase). // we probably want to move this to a central method for switching to a new working beatmap in the future. Schedule(() => CurrentTrack.RestartAsync()); } private WorkingBeatmap current; private TrackChangeDirection? queuedDirection; private IQueryable getBeatmapSets() => realm.Realm.All().Where(s => !s.DeletePending); private void changeBeatmap(WorkingBeatmap newWorking) { // This method can potentially be triggered multiple times as it is eagerly fired in next() / prev() to ensure correct execution order // (changeBeatmap must be called before consumers receive the bindable changed event, which is not the case when the local beatmap bindable is updated directly). if (newWorking == current) return; var lastWorking = current; TrackChangeDirection direction = TrackChangeDirection.None; bool audioEquals = newWorking?.BeatmapInfo?.AudioEquals(current?.BeatmapInfo) == true; if (current != null) { if (audioEquals) direction = TrackChangeDirection.None; else if (queuedDirection.HasValue) { direction = queuedDirection.Value; queuedDirection = null; } else { // figure out the best direction based on order in playlist. int last = getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(current.BeatmapSetInfo)).Count(); int next = newWorking == null ? -1 : getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(newWorking.BeatmapSetInfo)).Count(); direction = last > next ? TrackChangeDirection.Prev : TrackChangeDirection.Next; } } current = newWorking; if (lastWorking == null || !lastWorking.TryTransferTrack(current)) changeTrack(); TrackChanged?.Invoke(current, direction); ResetTrackAdjustments(); queuedDirection = null; // this will be a noop if coming from the beatmapChanged event. // the exception is local operations like next/prev, where we want to complete loading the track before sending out a change. if (beatmap.Value != current && beatmap is Bindable working) working.Value = current; } private void changeTrack() { var queuedTrack = getQueuedTrack(); var lastTrack = CurrentTrack; lastTrack.Completed -= onTrackCompleted; CurrentTrack = queuedTrack; // At this point we may potentially be in an async context from tests. This is extremely dangerous but we have to make do for now. // CurrentTrack is immediately updated above for situations where a immediate knowledge about the new track is required, // but the mutation of the hierarchy is scheduled to avoid exceptions. Schedule(() => { lastTrack.VolumeTo(0, 500, Easing.Out).Expire(); if (queuedTrack == CurrentTrack) { AddInternal(queuedTrack); queuedTrack.VolumeTo(0).Then().VolumeTo(1, 300, Easing.Out); } else { // If the track has changed since the call to changeTrack, it is safe to dispose the // queued track rather than consume it. queuedTrack.Dispose(); } }); } private DrawableTrack getQueuedTrack() { // Important to keep this in its own method to avoid inadvertently capturing unnecessary variables in the callback. // Can lead to leaks. var queuedTrack = new DrawableTrack(current.LoadTrack()); queuedTrack.Completed += onTrackCompleted; return queuedTrack; } private void onTrackCompleted() { if (!CurrentTrack.Looping && !beatmap.Disabled && AllowTrackControl.Value) NextTrack(); } private bool applyModTrackAdjustments; /// /// Whether mod track adjustments are allowed to be applied. /// public bool ApplyModTrackAdjustments { get => applyModTrackAdjustments; set { if (applyModTrackAdjustments == value) return; applyModTrackAdjustments = value; ResetTrackAdjustments(); } } private AudioAdjustments modTrackAdjustments; /// /// Resets the adjustments currently applied on and applies the mod adjustments if is true. /// /// /// Does not reset any adjustments applied directly to the beatmap track. /// public void ResetTrackAdjustments() { // todo: we probably want a helper method rather than this. CurrentTrack.RemoveAllAdjustments(AdjustableProperty.Balance); CurrentTrack.RemoveAllAdjustments(AdjustableProperty.Frequency); CurrentTrack.RemoveAllAdjustments(AdjustableProperty.Tempo); CurrentTrack.RemoveAllAdjustments(AdjustableProperty.Volume); if (applyModTrackAdjustments) { CurrentTrack.BindAdjustments(modTrackAdjustments = new AudioAdjustments()); foreach (var mod in mods.Value.OfType()) mod.ApplyToTrack(modTrackAdjustments); } } } public enum TrackChangeDirection { None, Next, Prev } public enum PreviousTrackResult { None, Restart, Previous } }