From 6cd44c6f24ae740d2fbc7ba814567b73e74f7267 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 4 May 2023 11:06:38 -0700 Subject: [PATCH] fix streaming audio buffer size initialization --- src/Audio/StreamingSound.cs | 14 ++++++++------ src/Audio/StreamingSoundOgg.cs | 8 ++++---- src/Audio/StreamingSoundQoa.cs | 6 ++---- src/Audio/StreamingSoundSeekable.cs | 20 ++++++++++++++++++-- src/Video/StreamingSoundTheora.cs | 8 +++++--- 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/Audio/StreamingSound.cs b/src/Audio/StreamingSound.cs index 2f3630d..62afec9 100644 --- a/src/Audio/StreamingSound.cs +++ b/src/Audio/StreamingSound.cs @@ -10,9 +10,6 @@ namespace MoonWorks.Audio /// 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? public abstract bool AutoUpdate { get; } @@ -20,6 +17,7 @@ namespace MoonWorks.Audio protected bool ConsumingBuffers = false; private const int BUFFER_COUNT = 3; + private nuint BufferSize; private readonly IntPtr[] buffers; private int nextBufferIndex = 0; private uint queuedBufferCount = 0; @@ -32,13 +30,17 @@ namespace MoonWorks.Audio ushort bitsPerSample, ushort blockAlign, ushort channels, - uint samplesPerSecond + uint samplesPerSecond, + uint bufferSize ) : base(device, formatTag, bitsPerSample, blockAlign, channels, samplesPerSecond) { + BufferSize = bufferSize; + System.Console.WriteLine(BufferSize); + buffers = new IntPtr[BUFFER_COUNT]; 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( (void*) buffer, - BUFFER_SIZE, + (int) BufferSize, out int filledLengthInBytes, out bool reachedEnd ); diff --git a/src/Audio/StreamingSoundOgg.cs b/src/Audio/StreamingSoundOgg.cs index 027faea..ba04697 100644 --- a/src/Audio/StreamingSoundOgg.cs +++ b/src/Audio/StreamingSoundOgg.cs @@ -9,8 +9,6 @@ namespace MoonWorks.Audio private IntPtr VorbisHandle; private IntPtr FileDataPtr; private FAudio.stb_vorbis_info Info; - - protected override int BUFFER_SIZE => 32768; public override bool AutoUpdate => true; public unsafe static StreamingSoundOgg Load(AudioDevice device, string filePath) @@ -43,14 +41,16 @@ namespace MoonWorks.Audio AudioDevice device, IntPtr fileDataPtr, // MUST BE A NATIVE MEMORY HANDLE!! IntPtr vorbisHandle, - FAudio.stb_vorbis_info info + FAudio.stb_vorbis_info info, + uint bufferSize = 32768 ) : base( device, 3, /* float type */ 32, /* size of float */ (ushort) (4 * info.channels), (ushort) info.channels, - info.sample_rate + info.sample_rate, + bufferSize ) { FileDataPtr = fileDataPtr; VorbisHandle = vorbisHandle; diff --git a/src/Audio/StreamingSoundQoa.cs b/src/Audio/StreamingSoundQoa.cs index 266c51f..012ca16 100644 --- a/src/Audio/StreamingSoundQoa.cs +++ b/src/Audio/StreamingSoundQoa.cs @@ -9,7 +9,6 @@ namespace MoonWorks.Audio private IntPtr QoaHandle; private IntPtr FileDataPtr; - protected override int BUFFER_SIZE { get; } public override bool AutoUpdate => true; uint Channels; @@ -60,15 +59,14 @@ namespace MoonWorks.Audio 16, (ushort) (2 * channels), (ushort) channels, - samplesPerSecond + samplesPerSecond, + samplesPerChannelPerFrame * channels * sizeof(short) ) { FileDataPtr = fileDataPtr; QoaHandle = qoaHandle; Channels = channels; SamplesPerChannelPerFrame = samplesPerChannelPerFrame; TotalSamplesPerChannel = totalSamplesPerChannel; - - BUFFER_SIZE = (int) (samplesPerChannelPerFrame * Channels * sizeof(short)); } public override void Seek(uint sampleFrame) diff --git a/src/Audio/StreamingSoundSeekable.cs b/src/Audio/StreamingSoundSeekable.cs index cf8beb4..6ee7915 100644 --- a/src/Audio/StreamingSoundSeekable.cs +++ b/src/Audio/StreamingSoundSeekable.cs @@ -4,8 +4,24 @@ namespace MoonWorks.Audio { 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); diff --git a/src/Video/StreamingSoundTheora.cs b/src/Video/StreamingSoundTheora.cs index 2b111b5..a830c33 100644 --- a/src/Video/StreamingSoundTheora.cs +++ b/src/Video/StreamingSoundTheora.cs @@ -6,7 +6,7 @@ namespace MoonWorks.Video public unsafe class StreamingSoundTheora : StreamingSound { private IntPtr VideoHandle; - protected override int BUFFER_SIZE => 8192; + // Theorafile is not thread safe, so let's update on the main thread. public override bool AutoUpdate => false; @@ -14,14 +14,16 @@ namespace MoonWorks.Video AudioDevice device, IntPtr videoHandle, int channels, - uint sampleRate + uint sampleRate, + uint bufferSize = 8192 ) : base( device, 3, /* float type */ 32, /* size of float */ (ushort) (4 * channels), (ushort) channels, - sampleRate + sampleRate, + bufferSize ) { VideoHandle = videoHandle; }