2019-01-24 16:43:03 +08: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-09 03:55:48 +08:00
2019-06-07 10:20:39 +08:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Threading.Tasks ;
2018-05-09 03:55:48 +08:00
using osu.Framework.Allocation ;
using osu.Framework.Audio ;
2021-07-27 02:55:49 +08:00
using osu.Framework.Audio.Mixing ;
2018-05-09 03:55:48 +08:00
using osu.Framework.Audio.Track ;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables ;
2018-05-09 19:51:04 +08:00
using osu.Framework.Graphics ;
2018-05-09 03:55:48 +08:00
using osu.Framework.IO.Stores ;
2020-11-15 12:21:09 +08:00
using osu.Framework.Logging ;
2018-05-09 19:51:04 +08:00
using osu.Game.Beatmaps ;
2018-05-09 03:55:48 +08:00
namespace osu.Game.Audio
{
2018-05-09 19:51:04 +08:00
public class PreviewTrackManager : Component
2018-05-09 03:55:48 +08:00
{
2018-06-21 15:19:07 +08:00
private readonly BindableDouble muteBindable = new BindableDouble ( ) ;
2020-02-14 21:14:00 +08:00
[Resolved]
private AudioManager audio { get ; set ; }
2019-06-07 10:20:39 +08:00
private PreviewTrackStore trackStore ;
2018-05-09 19:51:04 +08:00
2019-11-14 19:20:29 +08:00
protected TrackManagerPreviewTrack CurrentTrack ;
2018-05-09 03:55:48 +08:00
2021-02-17 19:40:15 +08:00
private readonly BindableNumber < double > globalTrackVolumeAdjust = new BindableNumber < double > ( OsuGameBase . GLOBAL_TRACK_VOLUME_ADJUST ) ;
2018-05-09 03:55:48 +08:00
[BackgroundDependencyLoader]
2021-07-27 02:55:49 +08:00
private void load ( AudioManager audioManager )
2018-05-09 03:55:48 +08:00
{
2019-06-07 10:20:39 +08:00
// this is a temporary solution to get around muting ourselves.
// todo: update this once we have a BackgroundTrackManager or similar.
2021-08-27 18:51:51 +08:00
trackStore = new PreviewTrackStore ( audioManager . TrackMixer , new OnlineStore ( ) ) ;
2019-06-07 10:20:39 +08:00
audio . AddItem ( trackStore ) ;
2021-02-17 19:40:15 +08:00
trackStore . AddAdjustment ( AdjustableProperty . Volume , globalTrackVolumeAdjust ) ;
2019-06-07 10:20:39 +08:00
trackStore . AddAdjustment ( AdjustableProperty . Volume , audio . VolumeTrack ) ;
2018-06-02 02:36:30 +08:00
}
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>
2018-06-21 15:19:07 +08:00
public PreviewTrack Get ( BeatmapSetInfo beatmapSetInfo )
2018-06-02 02:36:30 +08:00
{
2019-05-28 16:06:01 +08:00
var track = CreatePreviewTrack ( beatmapSetInfo , trackStore ) ;
2018-05-09 19:51:04 +08:00
2019-11-11 04:07:51 +08:00
track . Started + = ( ) = > Schedule ( ( ) = >
2018-05-25 05:37:53 +08:00
{
2019-11-14 19:20:29 +08:00
CurrentTrack ? . Stop ( ) ;
CurrentTrack = track ;
2019-11-11 04:07:51 +08:00
audio . Tracks . AddAdjustment ( AdjustableProperty . Volume , muteBindable ) ;
} ) ;
2018-06-02 02:36:30 +08:00
2019-11-06 14:58:47 +08:00
track . Stopped + = ( ) = > Schedule ( ( ) = >
2018-05-26 03:35:15 +08:00
{
2019-11-14 19:20:29 +08:00
if ( CurrentTrack ! = track )
2019-11-11 04:07:51 +08:00
return ;
2019-11-14 19:20:29 +08:00
CurrentTrack = null ;
2019-05-28 16:06:01 +08:00
audio . Tracks . RemoveAdjustment ( AdjustableProperty . Volume , muteBindable ) ;
2019-11-06 14:58:47 +08:00
} ) ;
2018-05-09 03:55:48 +08:00
2018-06-21 15:19:07 +08:00
return track ;
}
2018-06-21 17:54:42 +08:00
/// <summary>
2018-06-22 11:35:43 +08:00
/// 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>
2018-06-22 11:35:43 +08:00
public void StopAnyPlaying ( IPreviewTrackOwner source )
2018-06-21 15:19:07 +08:00
{
2020-11-15 12:21:09 +08:00
if ( CurrentTrack = = null | | ( CurrentTrack . Owner ! = null & & CurrentTrack . Owner ! = source ) )
2018-06-21 15:19:07 +08:00
return ;
2019-11-14 19:20:29 +08:00
CurrentTrack . Stop ( ) ;
2019-11-21 09:37:50 +08:00
// CurrentTrack should not be set to null here as it will result in incorrect handling in the track.Stopped callback above.
2018-06-21 15:19:07 +08:00
}
2018-06-21 18:31:07 +08:00
/// <summary>
/// Creates the <see cref="TrackManagerPreviewTrack"/>.
/// </summary>
2020-11-15 12:21:09 +08:00
protected virtual TrackManagerPreviewTrack CreatePreviewTrack ( BeatmapSetInfo beatmapSetInfo , ITrackStore trackStore ) = >
new TrackManagerPreviewTrack ( beatmapSetInfo , trackStore ) ;
2018-06-21 18:31:07 +08:00
2019-11-11 04:09:04 +08:00
public class TrackManagerPreviewTrack : PreviewTrack
2018-06-21 15:19:07 +08:00
{
2020-11-15 12:21:09 +08:00
[Resolved(canBeNull: true)]
2018-06-21 15:19:07 +08:00
public IPreviewTrackOwner Owner { get ; private set ; }
private readonly BeatmapSetInfo beatmapSetInfo ;
2019-05-29 15:43:15 +08:00
private readonly ITrackStore trackManager ;
2018-06-21 15:19:07 +08:00
2019-05-29 15:43:15 +08:00
public TrackManagerPreviewTrack ( BeatmapSetInfo beatmapSetInfo , ITrackStore trackManager )
2018-06-21 15:19:07 +08:00
{
this . beatmapSetInfo = beatmapSetInfo ;
this . trackManager = trackManager ;
}
2020-11-15 12:21:09 +08:00
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
Logger . Log ( $"A {nameof(PreviewTrack)} was created without a containing {nameof(IPreviewTrackOwner)}. An owner should be added for correct behaviour." ) ;
}
2018-06-21 15:19:07 +08:00
protected override Track GetTrack ( ) = > trackManager . Get ( $"https://b.ppy.sh/preview/{beatmapSetInfo?.OnlineBeatmapSetID}.mp3" ) ;
2018-05-09 03:55:48 +08:00
}
2019-06-07 10:20:39 +08:00
private class PreviewTrackStore : AudioCollectionManager < AdjustableAudioComponent > , ITrackStore
{
2021-07-27 02:55:49 +08:00
private readonly AudioMixer defaultMixer ;
2019-06-07 10:20:39 +08:00
private readonly IResourceStore < byte [ ] > store ;
2021-07-27 02:55:49 +08:00
internal PreviewTrackStore ( AudioMixer defaultMixer , IResourceStore < byte [ ] > store )
2019-06-07 10:20:39 +08:00
{
2021-07-27 02:55:49 +08:00
this . defaultMixer = defaultMixer ;
2019-06-07 10:20:39 +08:00
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 ;
2021-07-28 20:54:11 +08:00
// Todo: This is quite unsafe. TrackBass shouldn't be exposed as public.
Track track = new TrackBass ( dataStream ) ;
defaultMixer . Add ( track ) ;
2019-06-07 10:20:39 +08:00
AddItem ( track ) ;
2021-07-28 20:54:11 +08:00
2019-06-07 10:20:39 +08:00
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-09 03:55:48 +08:00
}
}