all repos — mgba @ 2f00e3d146be22ebc257f98a1396378f1a77faaa

mGBA Game Boy Advance Emulator

Don't trample audio buffer
Jeffrey Pfau jeffrey@endrift.com
Thu, 03 Oct 2013 12:08:52 -0700
commit

2f00e3d146be22ebc257f98a1396378f1a77faaa

parent

4a09d41aabd45f5790567284bfe80a9ab0d5b336

3 files changed, 12 insertions(+), 1 deletions(-)

jump to
M src/gba/gba-audio.csrc/gba/gba-audio.c

@@ -19,6 +19,8 @@ CircleBufferInit(&audio->left, GBA_AUDIO_SAMPLES * sizeof(int32_t));

CircleBufferInit(&audio->right, GBA_AUDIO_SAMPLES * sizeof(int32_t)); CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE); CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE); + + pthread_mutex_init(&audio->bufferMutex, 0); } void GBAAudioDeinit(struct GBAAudio* audio) {

@@ -26,6 +28,8 @@ CircleBufferDeinit(&audio->left);

CircleBufferDeinit(&audio->right); CircleBufferDeinit(&audio->chA.fifo); CircleBufferDeinit(&audio->chB.fifo); + + pthread_mutex_destroy(&audio->bufferMutex); } int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {

@@ -171,6 +175,8 @@ if (audio->chBRight) {

sampleRight += audio->chB.sample; } + pthread_mutex_lock(&audio->bufferMutex); CircleBufferWrite32(&audio->left, sampleLeft); CircleBufferWrite32(&audio->right, sampleRight); + pthread_mutex_unlock(&audio->bufferMutex); }
M src/gba/gba-audio.hsrc/gba/gba-audio.h

@@ -3,6 +3,7 @@ #define GBA_AUDIO_H

#include "circle-buffer.h" +#include <pthread.h> #include <stdint.h> struct GBADMA;

@@ -175,6 +176,8 @@ int32_t eventDiff;

int32_t nextSample; int32_t sampleInterval; + + pthread_mutex_t bufferMutex; }; void GBAAudioInit(struct GBAAudio* audio);
M src/sdl/sdl-audio.csrc/sdl/sdl-audio.c

@@ -13,7 +13,7 @@

context->desiredSpec.freq = 44100; context->desiredSpec.format = AUDIO_S16SYS; context->desiredSpec.channels = 2; - context->desiredSpec.samples = GBA_AUDIO_SAMPLES >> 1; + context->desiredSpec.samples = GBA_AUDIO_SAMPLES >> 2; context->desiredSpec.callback = _GBASDLAudioCallback; context->desiredSpec.userdata = context; context->audio = 0;

@@ -58,6 +58,7 @@ }

struct StereoSample* ssamples = (struct StereoSample*) data; len /= 2 * audioContext->obtainedSpec.channels; if (audioContext->obtainedSpec.channels == 2) { + pthread_mutex_lock(&audioContext->audio->bufferMutex); for (i = 0; i < len; ++i) { audioContext->drift += audioContext->audio->sampleRate / (float) audioContext->obtainedSpec.freq; while (audioContext->drift >= 0) {

@@ -66,5 +67,6 @@ audioContext->drift -= 1.f;

} ssamples[i] = audioContext->currentSample; } + pthread_mutex_unlock(&audioContext->audio->bufferMutex); } }