Skip to content

Commit

Permalink
Wait until sample gets loaded in SampleSDL2
Browse files Browse the repository at this point in the history
  • Loading branch information
hwsmm committed Dec 29, 2023
1 parent dbef466 commit 7dc9e98
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 8 additions & 1 deletion osu.Framework/Audio/Sample/SampleFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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.Threading.Tasks;
using osu.Framework.Bindables;

namespace osu.Framework.Audio.Sample
Expand All @@ -22,12 +23,18 @@ internal abstract class SampleFactory : AudioCollectionManager<AdjustableAudioCo
/// </summary>
internal readonly Bindable<int> PlaybackConcurrency = new Bindable<int>(Sample.DEFAULT_CONCURRENCY);

protected Task? LoadSampleTask;

protected SampleFactory(string name, int playbackConcurrency)
{
Name = name;
PlaybackConcurrency.Value = playbackConcurrency;

EnqueueAction(LoadSample);
LoadSampleTask = EnqueueAction(() =>
{
LoadSample();
LoadSampleTask = null;
});

PlaybackConcurrency.BindValueChanged(UpdatePlaybackConcurrency);
}
Expand Down
8 changes: 7 additions & 1 deletion osu.Framework/Audio/Sample/SampleSDL2Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using osu.Framework.Audio.Mixing.SDL2;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using SDL2;

namespace osu.Framework.Audio.Sample
Expand Down Expand Up @@ -58,7 +59,12 @@ private protected override void LoadSample()
}
}

public SampleSDL2AudioPlayer CreatePlayer() => new SampleSDL2AudioPlayer(decodedAudio, spec.freq, spec.channels);
public SampleSDL2AudioPlayer CreatePlayer()
{
LoadSampleTask?.WaitSafely();

return new SampleSDL2AudioPlayer(decodedAudio, spec.freq, spec.channels);
}

public override Sample CreateSample() => new SampleSDL2(this, mixer) { OnPlay = SampleFactoryOnPlay };

Expand Down

0 comments on commit 7dc9e98

Please sign in to comment.