Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hwsmm committed Oct 16, 2023
1 parent fc7f57f commit 7c45d47
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
7 changes: 7 additions & 0 deletions osu.Framework.Tests/Visual/Audio/TestSceneAudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -65,6 +66,12 @@ public override byte[] Get(string name)
return base.Get(name);
}

public override Stream GetStream(string name)
{
attemptedLookups.Add(name);
return base.GetStream(name);
}

public override Task<byte[]> GetAsync(string name, CancellationToken cancellationToken = default)
{
attemptedLookups.Add(name);
Expand Down
45 changes: 20 additions & 25 deletions osu.Framework/Audio/Sample/SampleBassFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ namespace osu.Framework.Audio.Sample
/// </summary>
internal class SampleBassFactory : SampleFactory
{
public int SampleId { get; private protected set; }
public int SampleId { get; private set; }

public override bool IsLoaded => SampleId != 0;

private readonly BassAudioMixer mixer;

private NativeMemoryTracker.NativeMemoryLease? memoryLease;

public SampleBassFactory(Stream data, string name, BassAudioMixer mixer, int playbackConcurrency)
: base(data, name, playbackConcurrency)
private byte[]? data;

public SampleBassFactory(Stream stream, string name, BassAudioMixer mixer, int playbackConcurrency)
: base(null, name, playbackConcurrency)
{
this.mixer = mixer;

using (stream)
data = stream.ReadAllBytesToArray();
}

private protected override void UpdatePlaybackConcurrency(ValueChangedEvent<int> concurrency)
Expand All @@ -51,33 +56,25 @@ private protected override void LoadSample()
Debug.Assert(CanPerformInline);
Debug.Assert(!IsLoaded);

if (Data == null)
if (data == null)
return;

try
{
byte[] data = Data.ReadAllBytesToArray();
int dataLength = data.Length;

int dataLength = data.Length;
const BassFlags flags = BassFlags.Default | BassFlags.SampleOverrideLongestPlaying;
using (var handle = new ObjectHandle<byte[]>(data, GCHandleType.Pinned))
SampleId = Bass.SampleLoad(handle.Address, 0, dataLength, PlaybackConcurrency.Value, flags);

const BassFlags flags = BassFlags.Default | BassFlags.SampleOverrideLongestPlaying;
using (var handle = new ObjectHandle<byte[]>(data, GCHandleType.Pinned))
SampleId = Bass.SampleLoad(handle.Address, 0, dataLength, PlaybackConcurrency.Value, flags);
if (Bass.LastError == Errors.Init)
return;

if (Bass.LastError == Errors.Init)
return;
data = null;

if (!IsLoaded)
return;
if (!IsLoaded)
return;

Length = Bass.ChannelBytes2Seconds(SampleId, dataLength) * 1000;
memoryLease = NativeMemoryTracker.AddMemory(this, dataLength);
}
finally
{
Data.Dispose();
Data = null;
}
Length = Bass.ChannelBytes2Seconds(SampleId, dataLength) * 1000;
memoryLease = NativeMemoryTracker.AddMemory(this, dataLength);
}

public override Sample CreateSample() => new SampleBass(this, mixer) { OnPlay = SampleFactoryOnPlay };
Expand All @@ -95,9 +92,7 @@ protected override void Dispose(bool disposing)
return;

if (IsLoaded)
{
memoryLease?.Dispose();
}

base.Dispose(disposing);
}
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Audio/Sample/SampleFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal abstract class SampleFactory : AudioCollectionManager<AdjustableAudioCo

private protected Stream? Data;

protected SampleFactory(Stream data, string name, int playbackConcurrency)
protected SampleFactory(Stream? data, string name, int playbackConcurrency)
{
Data = data;
Name = name;
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/IO/Stores/ResourceStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public virtual T Get(string name)
return default;
}

public Stream GetStream(string name)
public virtual Stream GetStream(string name)
{
if (name == null)
return null;
Expand Down

0 comments on commit 7c45d47

Please sign in to comment.