[Commits] SDL: coreaudio: dynamically allocate AudioQueueBuffers.

libsdl.org revision control commits-owner at libsdl.org
Tue May 23 21:12:52 PDT 2017


details:   https://hg.libsdl.org/SDL/rev/ad26a063ffff
changeset: 11022:ad26a063ffff
user:      Ryan C. Gordon <icculus at icculus.org>
date:      Wed May 24 00:12:22 2017 -0400
description:
coreaudio: dynamically allocate AudioQueueBuffers.

We need more than two buffers to flip between if they are small, or CoreAudio
won't make any sound; apparently it needs X milliseconds of audio queued when
it needs to play more or it drops any queued buffers. We are currently
guessing 50 milliseconds as a minimum, but there's probably a more proper
way to get the minimum time period from the system.

Fixes Bugzilla #3656.

diffstat:

 src/audio/coreaudio/SDL_coreaudio.h |   3 ++-
 src/audio/coreaudio/SDL_coreaudio.m |  23 +++++++++++++++++++++--
 2 files changed, 23 insertions(+), 3 deletions(-)

diffs (57 lines):

diff -r 7860594a8ad7 -r ad26a063ffff src/audio/coreaudio/SDL_coreaudio.h
--- a/src/audio/coreaudio/SDL_coreaudio.h	Sat May 20 23:30:47 2017 +0200
+++ b/src/audio/coreaudio/SDL_coreaudio.h	Wed May 24 00:12:22 2017 -0400
@@ -47,7 +47,8 @@
 {
     SDL_Thread *thread;
     AudioQueueRef audioQueue;
-    AudioQueueBufferRef audioBuffer[2];
+    int numAudioBuffers;
+    AudioQueueBufferRef *audioBuffer;
     void *buffer;
     UInt32 bufferOffset;
     UInt32 bufferSize;
diff -r 7860594a8ad7 -r ad26a063ffff src/audio/coreaudio/SDL_coreaudio.m
--- a/src/audio/coreaudio/SDL_coreaudio.m	Sat May 20 23:30:47 2017 +0200
+++ b/src/audio/coreaudio/SDL_coreaudio.m	Wed May 24 00:12:22 2017 -0400
@@ -533,11 +533,12 @@
     }
 
     if (this->hidden->audioQueue) {
-        for (i = 0; i < SDL_arraysize(this->hidden->audioBuffer); i++) {
+        for (i = 0; i < this->hidden->numAudioBuffers; i++) {
             if (this->hidden->audioBuffer[i]) {
                 AudioQueueFreeBuffer(this->hidden->audioQueue, this->hidden->audioBuffer[i]);
             }
         }
+        SDL_free(this->hidden->audioBuffer);
         AudioQueueDispose(this->hidden->audioQueue, 1);
     }
 
@@ -663,7 +664,25 @@
         return 0;
     }
 
-    for (i = 0; i < SDL_arraysize(this->hidden->audioBuffer); i++) {
+    /* Make sure we can feed the device at least 50 milliseconds at a time. */
+    const double msecs = (this->spec.size / ((double) this->spec.freq)) * 1000.0;
+    if (msecs >= 50.0) {
+        this->hidden->numAudioBuffers = 2;
+    } else {
+        this->hidden->numAudioBuffers = (int) (SDL_ceil(50.0 / msecs) * 2);
+    }
+
+    this->hidden->audioBuffer = SDL_calloc(1, sizeof (AudioQueueBufferRef) * this->hidden->numAudioBuffers);
+    if (this->hidden->audioBuffer == NULL) {
+        SDL_OutOfMemory();
+        return 0;
+    }
+
+#if DEBUG_COREAUDIO
+    printf("COREAUDIO: numAudioBuffers == %d\n", this->hidden->numAudioBuffers);
+#endif
+
+    for (i = 0; i < this->hidden->numAudioBuffers; i++) {
         result = AudioQueueAllocateBuffer(this->hidden->audioQueue, this->spec.size, &this->hidden->audioBuffer[i]);
         CHECK_RESULT("AudioQueueAllocateBuffer");
         SDL_memset(this->hidden->audioBuffer[i]->mAudioData, this->spec.silence, this->hidden->audioBuffer[i]->mAudioDataBytesCapacity);


More information about the commits mailing list