shithub: sox

Download patch

ref: 2bc58856c06f6aa0c77a74ef5b9557db951088af
parent: b7dbdc17a9c3c56103f2336dbc4e92af8e3a56bb
author: cbagwell <cbagwell>
date: Mon Feb 21 23:26:32 EST 2011

Another attempt and fixing coreaudio segfaults.  Simplify by
only looking at first output buffer and only copy lesser of
buffered data or output buffer length now.  If output buffer
is larger, add silence.

--- a/src/coreaudio.c
+++ b/src/coreaudio.c
@@ -29,34 +29,28 @@
   sox_format_t *ft = (sox_format_t *)inClientData;
   priv_t *ac = (priv_t *)ft->priv;
   float *buf = outOutputData->mBuffers[0].mData;
-  unsigned int len;
-  unsigned int buf_num;
+  unsigned int len, output_len;
 
-  pthread_mutex_lock(&ac->mutex);
-
-  for (buf_num = 0; buf_num <  outOutputData->mNumberBuffers; buf_num++)
+  if (outOutputData->mNumberBuffers != 1)
   {
-      /* TODO: Does more than 1 output buffer need to be handled? */
-      if (buf_num > 0)
-      {
-    	  outOutputData->mBuffers[buf_num].mDataByteSize = 0;
-	  continue;
-      }
+	  lsx_warn("coreaudio: unhandled extra buffer.  Data discarded.");
+	  return kAudioHardwareNoError;
+  }
 
-      buf = outOutputData->mBuffers[0].mData;
+  buf = outOutputData->mBuffers[0].mData;
+  output_len = outOutputData->mBuffers[0].mDataByteSize;
 
-      /* mDataByteSize may be non-zero even when mData is NULL, but that is
-       * not an error.
-       */
-      if (buf == NULL)
-	  continue;
+  pthread_mutex_lock(&ac->mutex);
 
-      len = ac->buf_offset;
+  len = (ac->buf_offset < output_len) ? ac->buf_offset : output_len;
 
-      memcpy(buf, ac->buffer, len);
-      outOutputData->mBuffers[buf_num].mDataByteSize = len;
-      ac->buf_offset = 0;
-  }
+  memcpy(buf, ac->buffer, len);
+
+  /* Fill partial output buffers with silence */
+  if (len < output_len)
+      memset(buf+len, 0, output_len-len);
+
+  ac->buf_offset = 0;
 
   pthread_mutex_unlock(&ac->mutex);
   pthread_cond_signal(&ac->cond);