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>
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
);

View File

@ -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;

View File

@ -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)

View File

@ -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);

View File

@ -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;
}