Guest User

Untitled

a guest
Jul 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. --- sox-14.3.1/src/coreaudio.c 2009/03/09 03:38:17 1.9
  2. +++ sox-14.3.1/src/coreaudio.c 2010/08/07 00:01:11 1.10
  3. @@ -18,8 +18,8 @@
  4. float *buffer;
  5. } priv_t;
  6.  
  7. -static OSStatus PlaybackIOProc(AudioDeviceID inDevice UNUSED,
  8. - const AudioTimeStamp *inNow UNUSED,
  9. +static OSStatus PlaybackIOProc(AudioDeviceID inDevice UNUSED,
  10. + const AudioTimeStamp *inNow UNUSED,
  11. const AudioBufferList *inInputData UNUSED,
  12. const AudioTimeStamp *inInputTime UNUSED,
  13. AudioBufferList *outOutputData,
  14. @@ -32,7 +32,7 @@
  15.  
  16. pthread_mutex_lock(&ac->mutex);
  17.  
  18. - memcpy(buf, ac->buffer, (ac->buf_offset) * sizeof(float));
  19. + memcpy(buf, ac->buffer, ac->buf_offset);
  20. ac->buf_offset = 0;
  21.  
  22. pthread_mutex_unlock(&ac->mutex);
  23. @@ -41,8 +41,8 @@
  24. return kAudioHardwareNoError;
  25. }
  26.  
  27. -static OSStatus RecIOProc(AudioDeviceID inDevice UNUSED,
  28. - const AudioTimeStamp *inNow UNUSED,
  29. +static OSStatus RecIOProc(AudioDeviceID inDevice UNUSED,
  30. + const AudioTimeStamp *inNow UNUSED,
  31. const AudioBufferList *inInputData,
  32. const AudioTimeStamp *inInputTime UNUSED,
  33. AudioBufferList *outOutputData UNUSED,
  34. @@ -52,11 +52,24 @@
  35. sox_format_t *ft = (sox_format_t *)inClientData;
  36. priv_t *ac = (priv_t *)ft->priv;
  37. float *buf = inInputData->mBuffers[0].mData;
  38. + size_t buflen = inInputData->mBuffers[0].mDataByteSize;
  39. + float *destbuf = (float *)((unsigned char *)ac->buffer + ac->buf_offset);
  40. + int i;
  41. +
  42. + /* mDataByteSize may be non-zero even when mData is NULL, but that is not an error */
  43. + if (buf == NULL)
  44. + return kAudioHardwareNoError;
  45. +
  46. + if (buflen > (ac->buf_size + ac->buf_offset))
  47. + buflen = ac->buf_size - ac->buf_offset;
  48.  
  49. pthread_mutex_lock(&ac->mutex);
  50.  
  51. - memcpy(ac->buffer, buf, ac->buf_size * sizeof(float));
  52. - ac->buf_offset = ac->buf_size-1;
  53. + for (i = 0; i < (int)(buflen / sizeof(float)); i += 2) {
  54. + destbuf[i] = buf[i];
  55. + destbuf[i + 1] = buf[i + 1];
  56. + ac->buf_offset += sizeof(float) * 2;
  57. + }
  58.  
  59. pthread_mutex_unlock(&ac->mutex);
  60. pthread_cond_signal(&ac->cond);
  61. @@ -104,20 +117,10 @@
  62. return SOX_EOF;
  63. }
  64.  
  65. - /* If user doesn't specify, default to some reasonable values.
  66. - * Since this is mainly for recording case, default to typical
  67. - * 16-bit values to prevent saving larger files then average user
  68. - * wants. Power users can override to 32-bit if they wish.
  69. - */
  70. - if (ft->signal.channels == 0)
  71. - ft->signal.channels = 2;
  72. - if (ft->signal.rate == 0)
  73. - ft->signal.rate = 44100;
  74. - if (ft->encoding.bits_per_sample == 0)
  75. - {
  76. - ft->encoding.bits_per_sample = 16;
  77. - ft->encoding.encoding = SOX_ENCODING_SIGN2;
  78. - }
  79. + /* OS X effectively only supports these values. */
  80. + ft->signal.channels = 2;
  81. + ft->signal.rate = 44100;
  82. + ft->encoding.bits_per_sample = 32;
  83.  
  84. /* TODO: My limited experience with hardware can only get floats working which a fixed sample
  85. * rate and stereo. I know that is a limitiation of audio device I have so this may not be
  86. @@ -166,25 +169,25 @@
  87. ft->signal.rate = stream_desc.mSampleRate;
  88. }
  89.  
  90. - ac->buf_size = sox_globals.bufsiz;
  91. + ac->buf_size = sox_globals.bufsiz * sizeof(float);
  92. ac->buf_offset = 0;
  93. - ac->buffer = lsx_malloc(ac->buf_size * sizeof(sox_sample_t));
  94. + ac->buffer = lsx_malloc(ac->buf_size);
  95.  
  96. - buf_size = sox_globals.bufsiz * sizeof(float);
  97. + buf_size = ac->buf_size;
  98. property_size = sizeof(buf_size);
  99. - status = AudioDeviceSetProperty(ac->adid, NULL, 0, is_input,
  100. + status = AudioDeviceSetProperty(ac->adid, NULL, 0, is_input,
  101. kAudioDevicePropertyBufferSize,
  102. property_size, &buf_size);
  103.  
  104. rc = pthread_mutex_init(&ac->mutex, NULL);
  105. - if (rc)
  106. + if (rc)
  107. {
  108. lsx_fail_errno(ft, SOX_EPERM, "failed initializing mutex");
  109. return SOX_EOF;
  110. }
  111.  
  112. rc = pthread_cond_init(&ac->cond, NULL);
  113. - if (rc)
  114. + if (rc)
  115. {
  116. lsx_fail_errno(ft, SOX_EPERM, "failed initializing condition");
  117. return SOX_EOF;
  118. @@ -224,19 +227,13 @@
  119. pthread_mutex_lock(&ac->mutex);
  120.  
  121. /* Wait until input buffer has been filled by device driver */
  122. - while (ac->buf_offset == 0 || ac->buf_offset > ac->buf_size)
  123. + while (ac->buf_offset < ac->buf_size)
  124. pthread_cond_wait(&ac->cond, &ac->mutex);
  125.  
  126. - if (len > ac->buf_size - ac->buf_offset)
  127. - len = ac->buf_size - ac->buf_offset;
  128. - samp_left = len;
  129. -
  130. - p = &ac->buffer[ac->buf_offset];
  131. -
  132. - while (samp_left--)
  133. - *buf++ = SOX_FLOAT_32BIT_TO_SAMPLE(*p++, ft->clips);
  134. -
  135. - ac->buf_offset -= len;
  136. + len = ac->buf_offset / sizeof(float);
  137. + for (p = ac->buffer, samp_left = len; samp_left > 0; samp_left--, buf++, p++)
  138. + *buf = SOX_FLOAT_32BIT_TO_SAMPLE(*p, ft->clips);
  139. + ac->buf_offset = 0;
  140.  
  141. pthread_mutex_unlock(&ac->mutex);
  142.  
  143. @@ -274,23 +271,28 @@
  144.  
  145. pthread_mutex_lock(&ac->mutex);
  146.  
  147. + /* globals.bufsize is in samples
  148. + * buf_offset is in bytes
  149. + * buf_size is in bytes
  150. + */
  151. do {
  152. -
  153. - /* Wait until there is some room to copy some samples */
  154. - while (ac->buf_offset >= ac->buf_size - 1)
  155. + /* Wait until callback has cleared the buffer. We move in
  156. + * lock-step with the callback; we never deal with a partially
  157. + * written buffer. */
  158. + while (ac->buf_offset != 0)
  159. pthread_cond_wait(&ac->cond, &ac->mutex);
  160.  
  161. len = nsamp - written;
  162. - if (len > ac->buf_size - ac->buf_offset)
  163. - len = ac->buf_size - ac->buf_offset;
  164. + if (len > (ac->buf_size - ac->buf_offset) / sizeof(float))
  165. + len = (ac->buf_size - ac->buf_offset) / sizeof(float);
  166. samp_left = len;
  167.  
  168. - p = &ac->buffer[ac->buf_offset];
  169. + p = ((unsigned char *)ac->buffer) + ac->buf_offset;
  170.  
  171. while (samp_left--)
  172. *p++ = SOX_SAMPLE_TO_FLOAT_32BIT(*buf++, ft->clips);
  173.  
  174. - ac->buf_offset += len;
  175. + ac->buf_offset += len * sizeof(float);
  176. written += len;
  177. } while (written < nsamp);
Add Comment
Please, Sign In to add comment