Skip to content

Commit

Permalink
Abstract SampleFactory a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
hwsmm committed Oct 16, 2023
1 parent 7c45d47 commit bb275f2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 35 deletions.
20 changes: 11 additions & 9 deletions osu.Framework.Tests/Audio/BassTestComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
{
Expand Down
12 changes: 10 additions & 2 deletions osu.Framework/Audio/BassAudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,8 +63,15 @@ public BassAudioManager(AudioThread audioThread, ResourceStore<byte[]> 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)
{
Expand Down
10 changes: 3 additions & 7 deletions osu.Framework/Audio/Sample/SampleBassFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<int> concurrency)
Expand Down
11 changes: 1 addition & 10 deletions osu.Framework/Audio/Sample/SampleFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. 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
Expand All @@ -23,11 +22,8 @@ internal abstract class SampleFactory : AudioCollectionManager<AdjustableAudioCo
/// </summary>
internal readonly Bindable<int> PlaybackConcurrency = new Bindable<int>(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;

Expand Down Expand Up @@ -67,12 +63,7 @@ protected override void Dispose(bool disposing)
return;

if (IsLoaded)
{
FreeSample();
}

Data?.Dispose();
Data = null;

base.Dispose(disposing);
}
Expand Down
31 changes: 25 additions & 6 deletions osu.Framework/Audio/Sample/SampleSDL2Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -52,8 +55,8 @@ private protected override void LoadSample()
}
finally
{
Data.Dispose();
Data = null;
stream.Dispose();
stream = null;
}
}

Expand All @@ -71,5 +74,21 @@ private protected override void FreeSample()
private protected override void UpdatePlaybackConcurrency(ValueChangedEvent<int> concurrency)
{
}

~SampleSDL2Factory()
{
Dispose(false);
}

protected override void Dispose(bool disposing)
{
if (IsDisposed)
return;

stream?.Dispose();
stream = null;

base.Dispose(disposing);
}
}
}
2 changes: 1 addition & 1 deletion osu.Framework/Audio/Sample/SampleStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class SampleStore : AudioCollectionManager<AdjustableAudioComponent>, I

private readonly Dictionary<string, SampleFactory> factories = new Dictionary<string, SampleFactory>();

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;

Expand Down

0 comments on commit bb275f2

Please sign in to comment.