fix various AddBuffer problems

pull/1/head
cosmonaut 2021-10-27 22:07:19 -07:00
parent 23eab952c1
commit f91b1090e0
1 changed files with 25 additions and 19 deletions

View File

@ -164,6 +164,8 @@ typedef struct FAudioGMS_StreamingSound
{ {
stb_vorbis* fileHandle; stb_vorbis* fileHandle;
stb_vorbis_info info; stb_vorbis_info info;
float* streamBuffer;
uint32_t streamBufferSize;
} FAudioGMS_StreamingSound; } FAudioGMS_StreamingSound;
typedef struct FAudioGMS_SoundInstance typedef struct FAudioGMS_SoundInstance
@ -226,8 +228,6 @@ typedef struct FAudioGMS_EffectChain
static const float SPEED_OF_SOUND = 343.5f; static const float SPEED_OF_SOUND = 343.5f;
static const float DOPPLER_SCALE = 1.0f; static const float DOPPLER_SCALE = 1.0f;
#define STREAMING_BUFFER_SIZE 1024 * 16 * sizeof(float) /* FIXME: what should this value be? */
typedef struct FAudioGMS_Device typedef struct FAudioGMS_Device
{ {
FAudio* handle; FAudio* handle;
@ -253,7 +253,6 @@ typedef struct FAudioGMS_Device
uint32_t effectChainCount; uint32_t effectChainCount;
IdStack effectChainIndexStack; IdStack effectChainIndexStack;
float streamStagingBuffer[STREAMING_BUFFER_SIZE];
double timestep; double timestep;
} FAudioGMS_Device; } FAudioGMS_Device;
@ -788,30 +787,33 @@ static FAudioGMS_SoundInstance* FAudioGMS_INTERNAL_SoundInstance_CreateFromStati
static void FAudioGMS_INTERNAL_SoundInstance_AddBuffer(FAudioGMS_SoundInstance* instance) static void FAudioGMS_INTERNAL_SoundInstance_AddBuffer(FAudioGMS_SoundInstance* instance)
{ {
uint32_t requestedSampleCount = instance->format.nSamplesPerSec / 4;
uint32_t requiredStagingBufferSize = requestedSampleCount * instance->format.nChannels * sizeof(float);
if (instance->soundData.streamingSound.streamBufferSize < requiredStagingBufferSize)
{
instance->soundData.streamingSound.streamBuffer = SDL_realloc(instance->soundData.streamingSound.streamBuffer, requiredStagingBufferSize);
}
/* NOTE: this function returns samples per channel, not total samples */ /* NOTE: this function returns samples per channel, not total samples */
uint32_t samples = stb_vorbis_get_samples_float_interleaved( uint32_t sampleCount = stb_vorbis_get_samples_float_interleaved(
instance->soundData.streamingSound.fileHandle, instance->soundData.streamingSound.fileHandle,
instance->format.nChannels, instance->format.nChannels,
device->streamStagingBuffer, instance->soundData.streamingSound.streamBuffer,
STREAMING_BUFFER_SIZE requestedSampleCount * instance->format.nChannels
); );
uint32_t sampleCount = samples * instance->format.nChannels;
uint32_t bufferLength = sampleCount * sizeof(float);
uint8_t* nextBuffer = SDL_malloc(bufferLength);
SDL_memcpy(nextBuffer, device->streamStagingBuffer, bufferLength);
FAudioBuffer buffer; FAudioBuffer buffer;
buffer.AudioBytes = bufferLength; buffer.AudioBytes = sampleCount * instance->format.nChannels * sizeof(float);
buffer.pAudioData = nextBuffer; buffer.pAudioData = (uint8_t*) instance->soundData.streamingSound.streamBuffer;
buffer.PlayLength =
bufferLength /
instance->format.nChannels /
(instance->format.wBitsPerSample / 8);
buffer.PlayBegin = 0; buffer.PlayBegin = 0;
buffer.PlayLength = sampleCount;
buffer.Flags = 0; buffer.Flags = 0;
/*(sampleCount < instance->soundData.streamingSound.info.sample_rate) ?
FAUDIO_END_OF_STREAM :
0;
*/
buffer.LoopBegin = 0; buffer.LoopBegin = 0;
buffer.LoopCount = 0; buffer.LoopCount = 0;
buffer.LoopLength = 0; buffer.LoopLength = 0;
@ -820,7 +822,8 @@ static void FAudioGMS_INTERNAL_SoundInstance_AddBuffer(FAudioGMS_SoundInstance*
FAudioSourceVoice_SubmitSourceBuffer(instance->handle, &buffer, NULL); FAudioSourceVoice_SubmitSourceBuffer(instance->handle, &buffer, NULL);
/* We have reached the end of the file! */ /* We have reached the end of the file! */
if (sampleCount < STREAMING_BUFFER_SIZE) /* FIXME: maybe move this to a OnStreamEnd callback? */
if (sampleCount < requestedSampleCount)
{ {
if (instance->loop) if (instance->loop)
{ {
@ -856,6 +859,8 @@ double FAudioGMS_StreamingSound_LoadOGG(char* filePath)
instance->soundData.streamingSound.fileHandle = fileHandle; instance->soundData.streamingSound.fileHandle = fileHandle;
instance->soundData.streamingSound.info = info; instance->soundData.streamingSound.info = info;
instance->soundData.streamingSound.streamBuffer = NULL;
instance->soundData.streamingSound.streamBufferSize = 0;
FAudioGMS_INTERNAL_SoundInstance_AddBuffer(instance); FAudioGMS_INTERNAL_SoundInstance_AddBuffer(instance);
@ -1247,6 +1252,7 @@ static void FAudioGMS_INTERNAL_SoundInstance_Destroy(FAudioGMS_SoundInstance* in
FAudioVoice_DestroyVoice(instance->handle); FAudioVoice_DestroyVoice(instance->handle);
if (!instance->isStatic) if (!instance->isStatic)
{ {
SDL_free(instance->soundData.streamingSound.streamBuffer);
stb_vorbis_close(instance->soundData.streamingSound.fileHandle); stb_vorbis_close(instance->soundData.streamingSound.fileHandle);
} }
if (instance->is3D) if (instance->is3D)