From 93fe59fb15fddb92bea85d1d00f66159948c3458 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 7 Mar 2023 00:48:36 -0800 Subject: [PATCH] streaming sound buffer consumption tweak --- src/Audio/AudioDevice.cs | 1 - src/Audio/StreamingSound.cs | 29 +++++++++++++++++++++-------- src/Audio/StreamingSoundSeekable.cs | 5 +---- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Audio/AudioDevice.cs b/src/Audio/AudioDevice.cs index 7b6c634..7413df1 100644 --- a/src/Audio/AudioDevice.cs +++ b/src/Audio/AudioDevice.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Threading; -using EasingFunction = System.Func; namespace MoonWorks.Audio { diff --git a/src/Audio/StreamingSound.cs b/src/Audio/StreamingSound.cs index 023dd97..8412753 100644 --- a/src/Audio/StreamingSound.cs +++ b/src/Audio/StreamingSound.cs @@ -10,11 +10,17 @@ namespace MoonWorks.Audio /// public abstract class StreamingSound : SoundInstance { + // How big should each buffer we consume be? + protected abstract int BUFFER_SIZE { get; } + + // Are we actively consuming buffers? + protected bool ConsumingBuffers = false; + private const int BUFFER_COUNT = 3; private readonly IntPtr[] buffers; private int nextBufferIndex = 0; private uint queuedBufferCount = 0; - protected abstract int BUFFER_SIZE { get; } + private readonly object StateLock = new object(); public unsafe StreamingSound( @@ -54,6 +60,7 @@ namespace MoonWorks.Audio State = SoundState.Playing; + ConsumingBuffers = true; QueueBuffers(); FAudio.FAudioSourceVoice_Start(Voice, 0, operationSet); } @@ -65,6 +72,7 @@ namespace MoonWorks.Audio { if (State == SoundState.Playing) { + ConsumingBuffers = false; FAudio.FAudioSourceVoice_Stop(Voice, 0, 0); State = SoundState.Paused; } @@ -73,6 +81,7 @@ namespace MoonWorks.Audio public override void Stop() { + ConsumingBuffers = false; State = SoundState.Stopped; } @@ -80,6 +89,7 @@ namespace MoonWorks.Audio { lock (StateLock) { + ConsumingBuffers = false; FAudio.FAudioSourceVoice_Stop(Voice, 0, 0); FAudio.FAudioSourceVoice_FlushSourceBuffers(Voice); ClearBuffers(); @@ -114,9 +124,16 @@ namespace MoonWorks.Audio queuedBufferCount = state.BuffersQueued; - for (int i = 0; i < BUFFER_COUNT - queuedBufferCount; i += 1) + if (ConsumingBuffers) { - AddBuffer(); + for (int i = 0; i < BUFFER_COUNT - queuedBufferCount; i += 1) + { + AddBuffer(); + } + } + else if (queuedBufferCount == 0) + { + Stop(); } } @@ -159,15 +176,11 @@ namespace MoonWorks.Audio queuedBufferCount += 1; } - else if (queuedBufferCount == 0) - { - // no more data, nothing queued, we're done - Stop(); - } if (reachedEnd) { /* We have reached the end of the data, what do we do? */ + ConsumingBuffers = false; OnReachedEnd(); } } diff --git a/src/Audio/StreamingSoundSeekable.cs b/src/Audio/StreamingSoundSeekable.cs index 7ca563b..cf8beb4 100644 --- a/src/Audio/StreamingSoundSeekable.cs +++ b/src/Audio/StreamingSoundSeekable.cs @@ -14,12 +14,9 @@ namespace MoonWorks.Audio { if (Loop) { + ConsumingBuffers = true; Seek(0); } - else - { - Stop(); - } } } }