1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 19:27:26 +08:00
osu-lazer/osu.Game/Audio/PreviewTrackManager.cs

108 lines
3.9 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.
2018-05-09 03:55:48 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-05-09 03:55:48 +08:00
using osu.Framework.Configuration;
using osu.Framework.Graphics;
2018-05-09 03:55:48 +08:00
using osu.Framework.IO.Stores;
using osu.Game.Beatmaps;
2018-05-09 03:55:48 +08:00
namespace osu.Game.Audio
{
2018-06-21 17:54:42 +08:00
/// <summary>
/// A central store for the retrieval of <see cref="PreviewTrack"/>s.
/// </summary>
public class PreviewTrackManager : Component
2018-05-09 03:55:48 +08:00
{
private readonly BindableDouble muteBindable = new BindableDouble();
private AudioManager audio;
2019-05-29 15:43:15 +08:00
private ITrackStore trackStore;
private TrackManagerPreviewTrack current;
2018-05-09 03:55:48 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio, FrameworkConfigManager config)
{
trackStore = audio.GetTrackStore(new OnlineStore());
2018-05-09 03:55:48 +08:00
this.audio = audio;
config.BindWith(FrameworkSetting.VolumeMusic, trackStore.Volume);
}
2018-06-21 17:54:42 +08:00
/// <summary>
/// Retrieves a <see cref="PreviewTrack"/> for a <see cref="BeatmapSetInfo"/>.
/// </summary>
/// <param name="beatmapSetInfo">The <see cref="BeatmapSetInfo"/> to retrieve the preview track for.</param>
/// <returns>The playable <see cref="PreviewTrack"/>.</returns>
public PreviewTrack Get(BeatmapSetInfo beatmapSetInfo)
{
var track = CreatePreviewTrack(beatmapSetInfo, trackStore);
track.Started += () =>
2018-05-25 05:37:53 +08:00
{
current?.Stop();
current = track;
audio.Tracks.AddAdjustment(AdjustableProperty.Volume, muteBindable);
2018-05-25 05:37:53 +08:00
};
track.Stopped += () =>
{
current = null;
audio.Tracks.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
};
2018-05-09 03:55:48 +08:00
return track;
}
2018-06-21 17:54:42 +08:00
/// <summary>
/// Stops any currently playing <see cref="PreviewTrack"/>.
2018-06-21 17:54:42 +08:00
/// </summary>
/// <remarks>
/// Only the immediate owner (an object that implements <see cref="IPreviewTrackOwner"/>) of the playing <see cref="PreviewTrack"/>
/// can globally stop the currently playing <see cref="PreviewTrack"/>. The object holding a reference to the <see cref="PreviewTrack"/>
/// can always stop the <see cref="PreviewTrack"/> themselves through <see cref="PreviewTrack.Stop()"/>.
/// </remarks>
/// <param name="source">The <see cref="IPreviewTrackOwner"/> which may be the owner of the <see cref="PreviewTrack"/>.</param>
public void StopAnyPlaying(IPreviewTrackOwner source)
{
if (current == null || current.Owner != source)
return;
current.Stop();
current = null;
}
2018-06-21 18:31:07 +08:00
/// <summary>
/// Creates the <see cref="TrackManagerPreviewTrack"/>.
/// </summary>
2019-05-29 15:43:15 +08:00
protected virtual TrackManagerPreviewTrack CreatePreviewTrack(BeatmapSetInfo beatmapSetInfo, ITrackStore trackStore) => new TrackManagerPreviewTrack(beatmapSetInfo, trackStore);
2018-06-21 18:31:07 +08:00
protected class TrackManagerPreviewTrack : PreviewTrack
{
public IPreviewTrackOwner Owner { get; private set; }
private readonly BeatmapSetInfo beatmapSetInfo;
2019-05-29 15:43:15 +08:00
private readonly ITrackStore trackManager;
2019-05-29 15:43:15 +08:00
public TrackManagerPreviewTrack(BeatmapSetInfo beatmapSetInfo, ITrackStore trackManager)
{
this.beatmapSetInfo = beatmapSetInfo;
this.trackManager = trackManager;
}
[BackgroundDependencyLoader]
private void load(IPreviewTrackOwner owner)
{
Owner = owner;
}
protected override Track GetTrack() => trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo?.OnlineBeatmapSetID}.mp3");
2018-05-09 03:55:48 +08:00
}
}
}