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
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 ;
2020-11-15 04:21:09 +00:00
using osu.Framework.Logging ;
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 partial class PreviewTrackManager : Component
2018-05-08 19:55:48 +00:00
{
2021-11-09 08:27:07 +00:00
private readonly IAdjustableAudioComponent mainTrackAdjustments ;
2018-06-21 07:19:07 +00:00
private readonly BindableDouble muteBindable = new BindableDouble ( ) ;
2022-07-31 14:00:14 +00:00
private ITrackStore trackStore = null ! ;
2018-05-09 11:51:04 +00:00
2022-07-31 14:00:14 +00:00
protected TrackManagerPreviewTrack ? CurrentTrack ;
2018-05-08 19:55:48 +00:00
2021-11-09 08:27:07 +00:00
public PreviewTrackManager ( IAdjustableAudioComponent mainTrackAdjustments )
{
this . mainTrackAdjustments = mainTrackAdjustments ;
}
2021-02-17 11:40:15 +00:00
2018-05-08 19:55:48 +00:00
[BackgroundDependencyLoader]
2021-07-26 18:55:49 +00:00
private void load ( AudioManager audioManager )
2018-05-08 19:55:48 +00:00
{
2021-11-09 08:27:07 +00:00
trackStore = audioManager . GetTrackStore ( new OnlineStore ( ) ) ;
2018-06-01 18:36:30 +00:00
}
2018-06-21 09:54:42 +00:00
/// <summary>
2021-10-25 05:25:01 +00:00
/// Retrieves a <see cref="PreviewTrack"/> for a <see cref="IBeatmapSetInfo"/>.
2018-06-21 09:54:42 +00:00
/// </summary>
2021-10-25 05:25:01 +00:00
/// <param name="beatmapSetInfo">The <see cref="IBeatmapSetInfo"/> to retrieve the preview track for.</param>
2018-06-21 09:54:42 +00:00
/// <returns>The playable <see cref="PreviewTrack"/>.</returns>
2021-10-25 05:25:01 +00:00
public PreviewTrack Get ( IBeatmapSetInfo 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-10 20:07:51 +00:00
track . Started + = ( ) = > Schedule ( ( ) = >
2018-05-24 21:37:53 +00:00
{
2019-11-14 11:20:29 +00:00
CurrentTrack ? . Stop ( ) ;
CurrentTrack = track ;
2021-11-09 08:27:07 +00:00
mainTrackAdjustments . AddAdjustment ( AdjustableProperty . Volume , muteBindable ) ;
2019-11-10 20:07:51 +00:00
} ) ;
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
{
2019-11-14 11:20:29 +00:00
if ( CurrentTrack ! = track )
2019-11-10 20:07:51 +00:00
return ;
2019-11-14 11:20:29 +00:00
CurrentTrack = null ;
2021-11-09 08:27:07 +00:00
mainTrackAdjustments . 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
{
2020-11-15 04:21:09 +00:00
if ( CurrentTrack = = null | | ( CurrentTrack . Owner ! = null & & CurrentTrack . Owner ! = source ) )
2018-06-21 07:19:07 +00:00
return ;
2019-11-14 11:20:29 +00:00
CurrentTrack . Stop ( ) ;
2019-11-21 01:37:50 +00: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 07:19:07 +00:00
}
2018-06-21 10:31:07 +00:00
/// <summary>
/// Creates the <see cref="TrackManagerPreviewTrack"/>.
/// </summary>
2021-10-25 05:25:01 +00:00
protected virtual TrackManagerPreviewTrack CreatePreviewTrack ( IBeatmapSetInfo beatmapSetInfo , ITrackStore trackStore ) = >
2020-11-15 04:21:09 +00:00
new TrackManagerPreviewTrack ( beatmapSetInfo , trackStore ) ;
2018-06-21 10:31:07 +00:00
2019-11-10 20:09:04 +00:00
public partial class TrackManagerPreviewTrack : PreviewTrack
2018-06-21 07:19:07 +00:00
{
2022-08-01 11:45:15 +00:00
[Resolved]
2022-07-31 14:00:14 +00:00
public IPreviewTrackOwner ? Owner { get ; private set ; }
2018-06-21 07:19:07 +00:00
2021-10-25 05:25:01 +00:00
private readonly IBeatmapSetInfo beatmapSetInfo ;
2019-05-29 07:43:15 +00:00
private readonly ITrackStore trackManager ;
2018-06-21 07:19:07 +00:00
2021-10-25 05:25:01 +00:00
public TrackManagerPreviewTrack ( IBeatmapSetInfo beatmapSetInfo , ITrackStore trackManager )
2018-06-21 07:19:07 +00:00
{
this . beatmapSetInfo = beatmapSetInfo ;
this . trackManager = trackManager ;
}
2020-11-15 04:21:09 +00:00
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
2021-11-26 12:37:43 +00:00
if ( Owner = = null )
Logger . Log ( $"A {nameof(PreviewTrack)} was created without a containing {nameof(IPreviewTrackOwner)}. An owner should be added for correct behaviour." ) ;
2020-11-15 04:21:09 +00:00
}
2021-10-26 02:57:51 +00:00
protected override Track GetTrack ( ) = > trackManager . Get ( $"https://b.ppy.sh/preview/{beatmapSetInfo.OnlineID}.mp3" ) ;
2018-05-08 19:55:48 +00:00
}
}
}