Rewrite PreviewTrackManager to avoid constructing TrackBass locally

This paves the way for the framework code quality change
(https://github.com/ppy/osu-framework/pull/4873) which stops exposing
the constructor.

Most of the restructuring here is required to give `PreviewTrackManager`
an adjustable target to apply the global mute.
This commit is contained in:
Dean Herbert
2021-11-09 17:27:07 +09:00
parent 0cb5113601
commit 3b485b5f37
6 changed files with 46 additions and 78 deletions

View File

@ -1,13 +1,8 @@
// 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.
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Mixing;
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
@ -19,27 +14,26 @@ namespace osu.Game.Audio
{
public class PreviewTrackManager : Component
{
private readonly IAdjustableAudioComponent mainTrackAdjustments;
private readonly BindableDouble muteBindable = new BindableDouble();
[Resolved]
private AudioManager audio { get; set; }
private PreviewTrackStore trackStore;
private ITrackStore trackStore;
protected TrackManagerPreviewTrack CurrentTrack;
private readonly BindableNumber<double> globalTrackVolumeAdjust = new BindableNumber<double>(OsuGameBase.GLOBAL_TRACK_VOLUME_ADJUST);
public PreviewTrackManager(IAdjustableAudioComponent mainTrackAdjustments)
{
this.mainTrackAdjustments = mainTrackAdjustments;
}
[BackgroundDependencyLoader]
private void load(AudioManager audioManager)
{
// this is a temporary solution to get around muting ourselves.
// todo: update this once we have a BackgroundTrackManager or similar.
trackStore = new PreviewTrackStore(audioManager.TrackMixer, new OnlineStore());
audio.AddItem(trackStore);
trackStore.AddAdjustment(AdjustableProperty.Volume, globalTrackVolumeAdjust);
trackStore.AddAdjustment(AdjustableProperty.Volume, audio.VolumeTrack);
trackStore = audioManager.GetTrackStore(new OnlineStore());
}
/// <summary>
@ -55,7 +49,7 @@ namespace osu.Game.Audio
{
CurrentTrack?.Stop();
CurrentTrack = track;
audio.Tracks.AddAdjustment(AdjustableProperty.Volume, muteBindable);
mainTrackAdjustments.AddAdjustment(AdjustableProperty.Volume, muteBindable);
});
track.Stopped += () => Schedule(() =>
@ -64,7 +58,7 @@ namespace osu.Game.Audio
return;
CurrentTrack = null;
audio.Tracks.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
mainTrackAdjustments.RemoveAdjustment(AdjustableProperty.Volume, muteBindable);
});
return track;
@ -116,52 +110,5 @@ namespace osu.Game.Audio
protected override Track GetTrack() => trackManager.Get($"https://b.ppy.sh/preview/{beatmapSetInfo.OnlineID}.mp3");
}
private class PreviewTrackStore : AudioCollectionManager<AdjustableAudioComponent>, ITrackStore
{
private readonly AudioMixer defaultMixer;
private readonly IResourceStore<byte[]> store;
internal PreviewTrackStore(AudioMixer defaultMixer, IResourceStore<byte[]> store)
{
this.defaultMixer = defaultMixer;
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;
// Todo: This is quite unsafe. TrackBass shouldn't be exposed as public.
Track track = new TrackBass(dataStream);
defaultMixer.Add(track);
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();
}
}
}