shithub: sox

Download patch

ref: b90cd902c5f6252122b0c526b624c5f3896239a1
parent: 2c1b8ba27e95fcd918b5fc2bbbc0720c7b0ec6e6
author: cbagwell <cbagwell>
date: Tue Feb 22 23:27:33 EST 2011

Move device start until after locking mutex so less chance of playing
silence while buffering data.

--- a/src/coreaudio.c
+++ b/src/coreaudio.c
@@ -338,14 +338,22 @@
   float *p;
   SOX_SAMPLE_LOCALS;
 
+  pthread_mutex_lock(&ac->mutex);
+
+  /* Wait to start until mutex is locked to help prevent callback
+   * getting zero samples.
+   */
   if (!ac->device_started)
   {
-    status = AudioDeviceStart(ac->adid, PlaybackIOProc);
-    ac->device_started = 1;
+      status = AudioDeviceStart(ac->adid, PlaybackIOProc);
+      if (status)
+      {
+	  pthread_mutex_unlock(&ac->mutex);
+	  return SOX_EOF;
+      }
+      ac->device_started = 1;
   }
 
-  pthread_mutex_lock(&ac->mutex);
-
   /* globals.bufsize is in samples
    * buf_offset is in bytes
    * buf_size is in bytes
@@ -352,7 +360,7 @@
    */
   do {
     while (ac->buf_offset >= ac->buf_size)
-      pthread_cond_wait(&ac->cond, &ac->mutex);
+	pthread_cond_wait(&ac->cond, &ac->mutex);
 
     len = nsamp - written;
     if (len > (ac->buf_size - ac->buf_offset) / sizeof(float))