From bb275f2fffd9ad877e00f2c7e9dec76e4d9d58e5 Mon Sep 17 00:00:00 2001 From: hwsmm Date: Mon, 16 Oct 2023 23:51:52 +0900 Subject: [PATCH] Abstract SampleFactory a bit more --- .../Audio/BassTestComponents.cs | 20 ++++++------ osu.Framework/Audio/BassAudioManager.cs | 12 +++++-- .../Audio/Sample/SampleBassFactory.cs | 10 ++---- osu.Framework/Audio/Sample/SampleFactory.cs | 11 +------ .../Audio/Sample/SampleSDL2Factory.cs | 31 +++++++++++++++---- osu.Framework/Audio/Sample/SampleStore.cs | 2 +- 6 files changed, 51 insertions(+), 35 deletions(-) diff --git a/osu.Framework.Tests/Audio/BassTestComponents.cs b/osu.Framework.Tests/Audio/BassTestComponents.cs index 8ec4defa1a4..cd93be5cd24 100644 --- a/osu.Framework.Tests/Audio/BassTestComponents.cs +++ b/osu.Framework.Tests/Audio/BassTestComponents.cs @@ -2,15 +2,14 @@ // See the LICENCE file in the repository root for full licence text. using System; -using System.IO; using System.Threading; using ManagedBass; using osu.Framework.Audio; -using osu.Framework.Audio.Mixing; using osu.Framework.Audio.Mixing.Bass; using osu.Framework.Audio.Sample; using osu.Framework.Audio.Track; using osu.Framework.Development; +using osu.Framework.Extensions; using osu.Framework.IO.Stores; using osu.Framework.Threading; @@ -38,16 +37,19 @@ public BassTestComponents(bool init = true) Mixer = CreateMixer(); Resources = new DllResourceStore(typeof(TrackBassTest).Assembly); - TrackStore = new TrackStore(Resources, Mixer, getNewTrack); - SampleStore = new SampleStore(Resources, Mixer, getSampleFactory); + TrackStore = new TrackStore(Resources, Mixer, (data, name) => new TrackBass(data, name)); + SampleStore = new SampleStore(Resources, Mixer, (stream, name, mixer, playbackConcurrency) => + { + byte[] data; - Add(TrackStore, SampleStore); - } + using (stream) + data = stream.ReadAllBytesToArray(); - private Track getNewTrack(Stream data, string name) => new TrackBass(data, name); + return new SampleBassFactory(data, name, (BassAudioMixer)mixer, playbackConcurrency); + }); - private SampleFactory getSampleFactory(Stream data, string name, AudioMixer mixer, int playbackConcurrency) - => new SampleBassFactory(data, name, (BassAudioMixer)mixer, playbackConcurrency); + Add(TrackStore, SampleStore); + } public void Init() { diff --git a/osu.Framework/Audio/BassAudioManager.cs b/osu.Framework/Audio/BassAudioManager.cs index ebaa502e861..6afd611b12d 100644 --- a/osu.Framework/Audio/BassAudioManager.cs +++ b/osu.Framework/Audio/BassAudioManager.cs @@ -17,6 +17,7 @@ using osu.Framework.Audio.Sample; using osu.Framework.Audio.Track; using osu.Framework.Development; +using osu.Framework.Extensions; using osu.Framework.Extensions.TypeExtensions; using osu.Framework.IO.Stores; using osu.Framework.Logging; @@ -62,8 +63,15 @@ public BassAudioManager(AudioThread audioThread, ResourceStore trackStor internal override Track.Track GetNewTrack(Stream data, string name) => new TrackBass(data, name); - internal override SampleFactory GetSampleFactory(Stream data, string name, AudioMixer mixer, int playbackConcurrency) - => new SampleBassFactory(data, name, (BassAudioMixer)mixer, playbackConcurrency); + internal override SampleFactory GetSampleFactory(Stream stream, string name, AudioMixer mixer, int playbackConcurrency) + { + byte[] data; + + using (stream) + data = stream.ReadAllBytesToArray(); + + return new SampleBassFactory(data, name, (BassAudioMixer)mixer, playbackConcurrency); + } protected override AudioMixer AudioCreateAudioMixer(AudioMixer globalMixer, string identifier) { diff --git a/osu.Framework/Audio/Sample/SampleBassFactory.cs b/osu.Framework/Audio/Sample/SampleBassFactory.cs index bc87cc387ad..dd2e90e0753 100644 --- a/osu.Framework/Audio/Sample/SampleBassFactory.cs +++ b/osu.Framework/Audio/Sample/SampleBassFactory.cs @@ -2,13 +2,11 @@ // See the LICENCE file in the repository root for full licence text. using System.Diagnostics; -using System.IO; using System.Runtime.InteropServices; using ManagedBass; using osu.Framework.Allocation; using osu.Framework.Audio.Mixing.Bass; using osu.Framework.Bindables; -using osu.Framework.Extensions; using osu.Framework.Platform; namespace osu.Framework.Audio.Sample @@ -28,13 +26,11 @@ internal class SampleBassFactory : SampleFactory private byte[]? data; - public SampleBassFactory(Stream stream, string name, BassAudioMixer mixer, int playbackConcurrency) - : base(null, name, playbackConcurrency) + public SampleBassFactory(byte[] data, string name, BassAudioMixer mixer, int playbackConcurrency) + : base(name, playbackConcurrency) { + this.data = data; this.mixer = mixer; - - using (stream) - data = stream.ReadAllBytesToArray(); } private protected override void UpdatePlaybackConcurrency(ValueChangedEvent concurrency) diff --git a/osu.Framework/Audio/Sample/SampleFactory.cs b/osu.Framework/Audio/Sample/SampleFactory.cs index dc33fd0bf1e..72b006ecf6d 100644 --- a/osu.Framework/Audio/Sample/SampleFactory.cs +++ b/osu.Framework/Audio/Sample/SampleFactory.cs @@ -1,7 +1,6 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System.IO; using osu.Framework.Bindables; namespace osu.Framework.Audio.Sample @@ -23,11 +22,8 @@ internal abstract class SampleFactory : AudioCollectionManager internal readonly Bindable PlaybackConcurrency = new Bindable(Sample.DEFAULT_CONCURRENCY); - private protected Stream? Data; - - protected SampleFactory(Stream? data, string name, int playbackConcurrency) + protected SampleFactory(string name, int playbackConcurrency) { - Data = data; Name = name; PlaybackConcurrency.Value = playbackConcurrency; @@ -67,12 +63,7 @@ protected override void Dispose(bool disposing) return; if (IsLoaded) - { FreeSample(); - } - - Data?.Dispose(); - Data = null; base.Dispose(disposing); } diff --git a/osu.Framework/Audio/Sample/SampleSDL2Factory.cs b/osu.Framework/Audio/Sample/SampleSDL2Factory.cs index 074be6caa9f..0e7e2d558d0 100644 --- a/osu.Framework/Audio/Sample/SampleSDL2Factory.cs +++ b/osu.Framework/Audio/Sample/SampleSDL2Factory.cs @@ -21,9 +21,12 @@ internal class SampleSDL2Factory : SampleFactory public float[]? DecodedAudio { get; private set; } - public SampleSDL2Factory(Stream data, string name, SDL2AudioMixer mixer, int playbackConcurrency, SDL.SDL_AudioSpec spec, AudioDecoder decoder) - : base(data, name, playbackConcurrency) + private Stream? stream; + + public SampleSDL2Factory(Stream stream, string name, SDL2AudioMixer mixer, int playbackConcurrency, SDL.SDL_AudioSpec spec, AudioDecoder decoder) + : base(name, playbackConcurrency) { + this.stream = stream; this.mixer = mixer; this.spec = spec; this.decoder = decoder; @@ -34,12 +37,12 @@ private protected override void LoadSample() Debug.Assert(CanPerformInline); Debug.Assert(!IsLoaded); - if (Data == null) + if (stream == null) return; try { - byte[] audio = decoder.DecodeAudio(Data); + byte[] audio = decoder.DecodeAudio(stream); if (audio.Length > 0) { @@ -52,8 +55,8 @@ private protected override void LoadSample() } finally { - Data.Dispose(); - Data = null; + stream.Dispose(); + stream = null; } } @@ -71,5 +74,21 @@ private protected override void FreeSample() private protected override void UpdatePlaybackConcurrency(ValueChangedEvent concurrency) { } + + ~SampleSDL2Factory() + { + Dispose(false); + } + + protected override void Dispose(bool disposing) + { + if (IsDisposed) + return; + + stream?.Dispose(); + stream = null; + + base.Dispose(disposing); + } } } diff --git a/osu.Framework/Audio/Sample/SampleStore.cs b/osu.Framework/Audio/Sample/SampleStore.cs index 824ef85e7e0..7e65123fb26 100644 --- a/osu.Framework/Audio/Sample/SampleStore.cs +++ b/osu.Framework/Audio/Sample/SampleStore.cs @@ -23,7 +23,7 @@ internal class SampleStore : AudioCollectionManager, I private readonly Dictionary factories = new Dictionary(); - public delegate SampleFactory GetSampleFactoryDelegate(Stream data, string name, AudioMixer mixer, int playbackConcurrency); + public delegate SampleFactory GetSampleFactoryDelegate(Stream stream, string name, AudioMixer mixer, int playbackConcurrency); public int PlaybackConcurrency { get; set; } = Sample.DEFAULT_CONCURRENCY;