ref: defa95dda51decece13ccda08394a9b2f73bec4d
parent: db55ae378318dae306458a6e0b8fbca7b1a14b4b
author: cbagwell <cbagwell>
date: Sun Mar 8 23:38:17 EDT 2009
Attempt to fix reports of early aborts. During underruns, write would return less then nsamps which may make flow() think it hit EOF and terminate. Now always write nsamp.
--- a/src/coreaudio.c
+++ b/src/coreaudio.c
@@ -260,7 +260,7 @@
static size_t write_samples(sox_format_t *ft, const sox_sample_t *buf, size_t nsamp)
{
priv_t *ac = (priv_t *)ft->priv;
- size_t len = nsamp;
+ size_t len, written = 0;
size_t samp_left;
OSStatus status;
float *p;
@@ -274,24 +274,29 @@
pthread_mutex_lock(&ac->mutex);
- /* Wait until there is some room to copy some samples */
- while (ac->buf_offset >= ac->buf_size - 1)
- pthread_cond_wait(&ac->cond, &ac->mutex);
+ do {
- if (len > ac->buf_size - ac->buf_offset)
- len = ac->buf_size - ac->buf_offset;
- samp_left = len;
+ /* Wait until there is some room to copy some samples */
+ while (ac->buf_offset >= ac->buf_size - 1)
+ pthread_cond_wait(&ac->cond, &ac->mutex);
- p = &ac->buffer[ac->buf_offset];
+ len = nsamp - written;
+ if (len > ac->buf_size - ac->buf_offset)
+ len = ac->buf_size - ac->buf_offset;
+ samp_left = len;
- while (samp_left--)
- *p++ = SOX_SAMPLE_TO_FLOAT_32BIT(*buf++, ft->clips);
+ p = &ac->buffer[ac->buf_offset];
- ac->buf_offset += len;
+ while (samp_left--)
+ *p++ = SOX_SAMPLE_TO_FLOAT_32BIT(*buf++, ft->clips);
+ ac->buf_offset += len;
+ written += len;
+ } while (written < nsamp);
+
pthread_mutex_unlock(&ac->mutex);
- return len;
+ return written;
}