streaming sound buffer consumption tweak

pull/47/head
cosmonaut 2023-03-07 00:48:36 -08:00
parent f9628f12c7
commit 93fe59fb15
3 changed files with 22 additions and 13 deletions

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using EasingFunction = System.Func<float, float>;
namespace MoonWorks.Audio
{

View File

@ -10,11 +10,17 @@ namespace MoonWorks.Audio
/// </summary>
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();
}
}

View File

@ -14,12 +14,9 @@ namespace MoonWorks.Audio
{
if (Loop)
{
ConsumingBuffers = true;
Seek(0);
}
else
{
Stop();
}
}
}
}