fix streaming audio buffer size initialization

pull/48/head
cosmonaut 2023-05-04 11:06:38 -07:00
parent a869a0e958
commit 6cd44c6f24
5 changed files with 37 additions and 19 deletions

View File

@ -10,9 +10,6 @@ namespace MoonWorks.Audio
/// </summary> /// </summary>
public abstract class StreamingSound : SoundInstance public abstract class StreamingSound : SoundInstance
{ {
// How big should each buffer we consume be?
protected abstract int BUFFER_SIZE { get; }
// Should the AudioDevice thread automatically update this class? // Should the AudioDevice thread automatically update this class?
public abstract bool AutoUpdate { get; } public abstract bool AutoUpdate { get; }
@ -20,6 +17,7 @@ namespace MoonWorks.Audio
protected bool ConsumingBuffers = false; protected bool ConsumingBuffers = false;
private const int BUFFER_COUNT = 3; private const int BUFFER_COUNT = 3;
private nuint BufferSize;
private readonly IntPtr[] buffers; private readonly IntPtr[] buffers;
private int nextBufferIndex = 0; private int nextBufferIndex = 0;
private uint queuedBufferCount = 0; private uint queuedBufferCount = 0;
@ -32,13 +30,17 @@ namespace MoonWorks.Audio
ushort bitsPerSample, ushort bitsPerSample,
ushort blockAlign, ushort blockAlign,
ushort channels, ushort channels,
uint samplesPerSecond uint samplesPerSecond,
uint bufferSize
) : base(device, formatTag, bitsPerSample, blockAlign, channels, samplesPerSecond) ) : base(device, formatTag, bitsPerSample, blockAlign, channels, samplesPerSecond)
{ {
BufferSize = bufferSize;
System.Console.WriteLine(BufferSize);
buffers = new IntPtr[BUFFER_COUNT]; buffers = new IntPtr[BUFFER_COUNT];
for (int i = 0; i < BUFFER_COUNT; i += 1) for (int i = 0; i < BUFFER_COUNT; i += 1)
{ {
buffers[i] = (IntPtr) NativeMemory.Alloc((nuint) BUFFER_SIZE); buffers[i] = (IntPtr) NativeMemory.Alloc(bufferSize);
} }
} }
@ -156,7 +158,7 @@ namespace MoonWorks.Audio
FillBuffer( FillBuffer(
(void*) buffer, (void*) buffer,
BUFFER_SIZE, (int) BufferSize,
out int filledLengthInBytes, out int filledLengthInBytes,
out bool reachedEnd out bool reachedEnd
); );

View File

@ -9,8 +9,6 @@ namespace MoonWorks.Audio
private IntPtr VorbisHandle; private IntPtr VorbisHandle;
private IntPtr FileDataPtr; private IntPtr FileDataPtr;
private FAudio.stb_vorbis_info Info; private FAudio.stb_vorbis_info Info;
protected override int BUFFER_SIZE => 32768;
public override bool AutoUpdate => true; public override bool AutoUpdate => true;
public unsafe static StreamingSoundOgg Load(AudioDevice device, string filePath) public unsafe static StreamingSoundOgg Load(AudioDevice device, string filePath)
@ -43,14 +41,16 @@ namespace MoonWorks.Audio
AudioDevice device, AudioDevice device,
IntPtr fileDataPtr, // MUST BE A NATIVE MEMORY HANDLE!! IntPtr fileDataPtr, // MUST BE A NATIVE MEMORY HANDLE!!
IntPtr vorbisHandle, IntPtr vorbisHandle,
FAudio.stb_vorbis_info info FAudio.stb_vorbis_info info,
uint bufferSize = 32768
) : base( ) : base(
device, device,
3, /* float type */ 3, /* float type */
32, /* size of float */ 32, /* size of float */
(ushort) (4 * info.channels), (ushort) (4 * info.channels),
(ushort) info.channels, (ushort) info.channels,
info.sample_rate info.sample_rate,
bufferSize
) { ) {
FileDataPtr = fileDataPtr; FileDataPtr = fileDataPtr;
VorbisHandle = vorbisHandle; VorbisHandle = vorbisHandle;

View File

@ -9,7 +9,6 @@ namespace MoonWorks.Audio
private IntPtr QoaHandle; private IntPtr QoaHandle;
private IntPtr FileDataPtr; private IntPtr FileDataPtr;
protected override int BUFFER_SIZE { get; }
public override bool AutoUpdate => true; public override bool AutoUpdate => true;
uint Channels; uint Channels;
@ -60,15 +59,14 @@ namespace MoonWorks.Audio
16, 16,
(ushort) (2 * channels), (ushort) (2 * channels),
(ushort) channels, (ushort) channels,
samplesPerSecond samplesPerSecond,
samplesPerChannelPerFrame * channels * sizeof(short)
) { ) {
FileDataPtr = fileDataPtr; FileDataPtr = fileDataPtr;
QoaHandle = qoaHandle; QoaHandle = qoaHandle;
Channels = channels; Channels = channels;
SamplesPerChannelPerFrame = samplesPerChannelPerFrame; SamplesPerChannelPerFrame = samplesPerChannelPerFrame;
TotalSamplesPerChannel = totalSamplesPerChannel; TotalSamplesPerChannel = totalSamplesPerChannel;
BUFFER_SIZE = (int) (samplesPerChannelPerFrame * Channels * sizeof(short));
} }
public override void Seek(uint sampleFrame) public override void Seek(uint sampleFrame)

View File

@ -4,8 +4,24 @@ namespace MoonWorks.Audio
{ {
public bool Loop { get; set; } public bool Loop { get; set; }
protected StreamingSoundSeekable(AudioDevice device, ushort formatTag, ushort bitsPerSample, ushort blockAlign, ushort channels, uint samplesPerSecond) : base(device, formatTag, bitsPerSample, blockAlign, channels, samplesPerSecond) protected StreamingSoundSeekable(
{ AudioDevice device,
ushort formatTag,
ushort bitsPerSample,
ushort blockAlign,
ushort channels,
uint samplesPerSecond,
uint bufferSize
) : base(
device,
formatTag,
bitsPerSample,
blockAlign,
channels,
samplesPerSecond,
bufferSize
) {
} }
public abstract void Seek(uint sampleFrame); public abstract void Seek(uint sampleFrame);

View File

@ -6,7 +6,7 @@ namespace MoonWorks.Video
public unsafe class StreamingSoundTheora : StreamingSound public unsafe class StreamingSoundTheora : StreamingSound
{ {
private IntPtr VideoHandle; private IntPtr VideoHandle;
protected override int BUFFER_SIZE => 8192;
// Theorafile is not thread safe, so let's update on the main thread. // Theorafile is not thread safe, so let's update on the main thread.
public override bool AutoUpdate => false; public override bool AutoUpdate => false;
@ -14,14 +14,16 @@ namespace MoonWorks.Video
AudioDevice device, AudioDevice device,
IntPtr videoHandle, IntPtr videoHandle,
int channels, int channels,
uint sampleRate uint sampleRate,
uint bufferSize = 8192
) : base( ) : base(
device, device,
3, /* float type */ 3, /* float type */
32, /* size of float */ 32, /* size of float */
(ushort) (4 * channels), (ushort) (4 * channels),
(ushort) channels, (ushort) channels,
sampleRate sampleRate,
bufferSize
) { ) {
VideoHandle = videoHandle; VideoHandle = videoHandle;
} }