Skip to content

Commit 79fde36

Browse files
committed
fix audio bug
1 parent a092bba commit 79fde36

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/SimulationFramework.Desktop/Audio/DesktopSound.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ internal sealed class DesktopSound : ISound
1010
private readonly DesktopAudioProvider provider;
1111
// internal readonly List<DesktopSoundPlayback> activePlaybacks;
1212
internal readonly uint buffer;
13+
internal HashSet<DesktopSoundPlayback> activePlaybacks = [];
1314

1415
public int SampleRate { get; }
1516
public float Duration { get; }
@@ -41,7 +42,7 @@ public unsafe DesktopSound(DesktopAudioProvider provider, ReadOnlySpan<byte> enc
4142
reader = new Wave24To16Stream(reader);
4243
}
4344

44-
var data = new byte[stream.Length / reader.BlockAlign * reader.BlockAlign];
45+
var data = new byte[reader.Length / reader.BlockAlign * reader.BlockAlign];
4546
reader.Read(data.AsSpan());
4647

4748
buffer = provider.al.GenBuffer();
@@ -108,6 +109,8 @@ private static BufferFormat GetBufferFormat(WaveFormat format)
108109

109110
public SoundPlayback Play(float volume = 1)
110111
{
112+
activePlaybacks.RemoveWhere(pb => pb.IsStopped);
113+
111114
return new DesktopSoundPlayback(provider, this, volume, false);
112115
}
113116

src/SimulationFramework.Desktop/Audio/DesktopSoundPlayback.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ internal sealed unsafe class DesktopSoundPlayback : SoundPlayback, IDisposable
55
{
66
private readonly uint source;
77
private readonly DesktopAudioProvider provider;
8+
private readonly DesktopSound sound;
89

910
public override float Volume
1011
{
@@ -41,6 +42,7 @@ public override bool IsPaused
4142

4243
public DesktopSoundPlayback(DesktopAudioProvider provider, DesktopSound sound, float volume, bool loop)
4344
{
45+
sound.activePlaybacks.Add(this);
4446
this.provider = provider;
4547

4648
source = provider.al.GenSource();
@@ -49,7 +51,6 @@ public DesktopSoundPlayback(DesktopAudioProvider provider, DesktopSound sound, f
4951
throw new Exception($"alGenSource() returned 0! (error: {provider.al.GetError()})");
5052
}
5153

52-
5354
if (loop)
5455
{
5556
provider.al.SetSourceProperty(source, SourceBoolean.Looping, loop);
@@ -67,6 +68,8 @@ public DesktopSoundPlayback(DesktopAudioProvider provider, DesktopSound sound, f
6768
public override void Dispose()
6869
{
6970
GC.SuppressFinalize(this);
71+
72+
_ = sound.activePlaybacks.Remove(this);
7073
provider.al.DeleteSource(source);
7174
}
7275

@@ -83,6 +86,8 @@ public override void Resume()
8386
public override void Stop()
8487
{
8588
provider.al.SourceStop(source);
89+
sound.activePlaybacks.Remove(this);
90+
Dispose();
8691
}
8792

8893
public override void Restart()

src/SimulationFramework.OpenGL/GLCanvas.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void Antialias(bool antialias)
9696

9797
public void Clear(ColorF color)
9898
{
99+
SubmitCommands();
99100
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
100101
glClearColor(color.R, color.G, color.B, color.A);
101102
glClear(GL_COLOR_BUFFER_BIT);

0 commit comments

Comments
 (0)