2019-01-24 08:43:03 +00:00
|
|
|
|
// 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-08 19:55:48 +00:00
|
|
|
|
|
2019-06-07 02:20:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-05-08 19:55:48 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
|
using osu.Framework.Audio.Track;
|
2019-02-21 10:04:31 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-05-09 11:51:04 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2018-05-08 19:55:48 +00:00
|
|
|
|
using osu.Framework.IO.Stores;
|
2018-05-09 11:51:04 +00:00
|
|
|
|
using osu.Game.Beatmaps;
|
2018-05-08 19:55:48 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Audio
|
|
|
|
|
{
|
2018-05-09 11:51:04 +00:00
|
|
|
|
public class PreviewTrackManager : Component
|
2018-05-08 19:55:48 +00:00
|
|
|
|
{
|
2018-06-21 07:19:07 +00:00
|
|
|
|
private readonly BindableDouble muteBindable = new BindableDouble();
|
|
|
|
|
|
2018-06-01 18:36:30 +00:00
|
|
|
|
private AudioManager audio;
|
2019-06-07 02:20:39 +00:00
|
|
|
|
private PreviewTrackStore trackStore;
|
2018-05-09 11:51:04 +00:00
|
|
|
|
|
2018-06-21 07:19:07 +00:00
|
|
|
|
private TrackManagerPreviewTrack current;
|
2018-05-08 19:55:48 +00:00
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2019-06-21 15:11:10 +00:00
|
|
|
|
private void load(AudioManager audio)
|
2018-05-08 19:55:48 +00:00
|
|
|
|
{
|
2019-06-07 02:20:39 +00:00
|
|
|
|
// this is a temporary solution to get around muting ourselves.
|
|
|
|
|
// todo: update this once we have a BackgroundTrackManager or similar.
|
|
|
|
|
trackStore = new PreviewTrackStore(new OnlineStore());
|
|
|
|
|
|
|
|
|
|
audio.AddItem(trackStore);
|
|
|
|
|
trackStore.AddAdjustment(AdjustableProperty.Volume, audio.VolumeTrack);
|
2018-05-08 19:55:48 +00:00
|
|
|
|
|
2018-06-01 18:36:30 +00:00
|
|
|
|
this.audio = audio;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 09:54:42 +00: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>
|
2018-06-21 07:19:07 +00:00
|
|
|
|
public PreviewTrack Get(BeatmapSetInfo beatmapSetInfo)
|
2018-06-01 18:36:30 +00:00
|
|
|
|
{
|
2019-05-28 08:06:01 +00:00
|
|
|
|
var track = CreatePreviewTrack(beatmapSetInfo, trackStore);
|
2018-05-09 11:51:04 +00:00
|
|
|
|
|
2019-11-08 10:19:06 +00:00
|
|
|
|
track.Started += () =>
|
2018-05-24 21:37:53 +00:00
|
|
|
|
{
|
2019-11-08 10:19:06 +00:00
|
|
|
|
// Stopping track should not be within the below schedule since its stop event schedules a null assign to current.
|
|
|
|
|
// Due to that, assigning the new track to current must be scheduled after the null assign to avoid current track loss.
|
2018-06-21 07:19:07 +00:00
|
|
|
|
current?.Stop();
|
2019-11-08 10:19:06 +00:00
|
|
|
|
|
|
|
|
|
Schedule(() =>
|
|
|
|
|
{
|
|
|
|
|
current = track;
|
|
|
|
|
audio.Tracks.AddAdjustment(AdjustableProperty.Volume, muteBindable);
|
|
|
|
|
});
|
|
|
|
|
};
|
2018-06-01 18:36:30 +00:00
|
|
|
|
|
2019-11-06 06:58:47 +00:00
|
|
|
|
track.Stopped += () => Schedule(() =>
|
2018-05-25 19:35:15 +00:00
|
|
|
|
{
|
2018-06-21 07:19:07 +00:00
|
|
|
|
current = null;
|
2019-05-28 08:06:01 +00:00
|
|
|
|
audio.Tracks.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
|
2019-11-06 06:58:47 +00:00
|
|
|
|
});
|
2018-05-08 19:55:48 +00:00
|
|
|
|
|
2018-06-21 07:19:07 +00:00
|
|
|
|
return track;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 09:54:42 +00:00
|
|
|
|
/// <summary>
|
2018-06-22 03:35:43 +00:00
|
|
|
|
/// Stops any currently playing <see cref="PreviewTrack"/>.
|
2018-06-21 09:54:42 +00: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>
|
2018-06-22 03:35:43 +00:00
|
|
|
|
public void StopAnyPlaying(IPreviewTrackOwner source)
|
2018-06-21 07:19:07 +00:00
|
|
|
|
{
|
2018-06-22 03:35:43 +00:00
|
|
|
|
if (current == null || current.Owner != source)
|
2018-06-21 07:19:07 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2018-06-22 03:35:43 +00:00
|
|
|
|
current.Stop();
|
2018-06-21 07:19:07 +00:00
|
|
|
|
current = null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 10:31:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the <see cref="TrackManagerPreviewTrack"/>.
|
|
|
|
|
/// </summary>
|
2019-05-29 07:43:15 +00:00
|
|
|
|
protected virtual TrackManagerPreviewTrack CreatePreviewTrack(BeatmapSetInfo beatmapSetInfo, ITrackStore trackStore) => new TrackManagerPreviewTrack(beatmapSetInfo, trackStore);
|
2018-06-21 10:31:07 +00:00
|
|
|
|
|
|
|
|
|
protected class TrackManagerPreviewTrack : PreviewTrack
|
2018-06-21 07:19:07 +00:00
|
|
|
|
{
|
|
|
|
|
public IPreviewTrackOwner Owner { get; private set; }
|
|
|
|
|
|
|
|
|
|
private readonly BeatmapSetInfo beatmapSetInfo;
|
2019-05-29 07:43:15 +00:00
|
|
|
|
private readonly ITrackStore trackManager;
|
2018-06-21 07:19:07 +00:00
|
|
|
|
|
2019-05-29 07:43:15 +00:00
|
|
|
|
public TrackManagerPreviewTrack(BeatmapSetInfo beatmapSetInfo, ITrackStore trackManager)
|
2018-06-21 07:19:07 +00:00
|
|
|
|
{
|
|
|
|
|
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-08 19:55:48 +00:00
|
|
|
|
}
|
2019-06-07 02:20:39 +00:00
|
|
|
|
|
|
|
|
|
private class PreviewTrackStore : AudioCollectionManager<AdjustableAudioComponent>, ITrackStore
|
|
|
|
|
{
|
|
|
|
|
private readonly IResourceStore<byte[]> store;
|
|
|
|
|
|
|
|
|
|
internal PreviewTrackStore(IResourceStore<byte[]> store)
|
|
|
|
|
{
|
|
|
|
|
this.store = store;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Track GetVirtual(double length = double.PositiveInfinity)
|
|
|
|
|
{
|
|
|
|
|
if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(PreviewTrackStore)}");
|
|
|
|
|
|
|
|
|
|
var track = new TrackVirtual(length);
|
|
|
|
|
AddItem(track);
|
|
|
|
|
return track;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Track Get(string name)
|
|
|
|
|
{
|
|
|
|
|
if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(PreviewTrackStore)}");
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(name)) return null;
|
|
|
|
|
|
|
|
|
|
var dataStream = store.GetStream(name);
|
|
|
|
|
|
|
|
|
|
if (dataStream == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
Track track = new TrackBass(dataStream);
|
|
|
|
|
AddItem(track);
|
|
|
|
|
return track;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<Track> GetAsync(string name) => Task.Run(() => Get(name));
|
|
|
|
|
|
|
|
|
|
public Stream GetStream(string name) => store.GetStream(name);
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetAvailableResources() => store.GetAvailableResources();
|
|
|
|
|
}
|
2018-05-08 19:55:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|