shithub: aubio

Download patch

ref: dbad82cf21d8e6e18a1ce4d18364f96fb390e148
parent: 0da5208d25cc71a5e79fcdbbf51ebbf5efb01074
parent: 7b5e1a5cb8dbcf879fca6c4ad6ab3f7dfaf088d8
author: Paul Brossier <piem@piem.org>
date: Wed Dec 19 09:58:09 EST 2018

Merge branch 'master' into feature/autosink

--- a/src/io/sink_apple_audio.c
+++ b/src/io/sink_apple_audio.c
@@ -31,9 +31,7 @@
 // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
 #include <AudioToolbox/AudioToolbox.h>
 
-#define FLOAT_TO_SHORT(x) (short)(x * 32768)
-
-extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
+extern int createAudioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
 extern void freeAudioBufferList(AudioBufferList *bufferList);
 extern CFURLRef createURLFromPath(const char * path);
 char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
@@ -76,6 +74,7 @@
   if ((sint_t)samplerate == 0) {
     return s;
   }
+
   // invalid samplerate given, abort
   if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) {
     goto beach;
@@ -150,6 +149,18 @@
   AudioFileTypeID fileType = kAudioFileWAVEType;
   CFURLRef fileURL = createURLFromPath(s->path);
   bool overwrite = true;
+
+  // set the in-memory format
+  AudioStreamBasicDescription inputFormat;
+  memset(&inputFormat, 0, sizeof(AudioStreamBasicDescription));
+  inputFormat.mFormatID         = kAudioFormatLinearPCM;
+  inputFormat.mSampleRate       = (Float64)(s->samplerate);
+  inputFormat.mFormatFlags      = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
+  inputFormat.mChannelsPerFrame = s->channels;
+  inputFormat.mBitsPerChannel   = sizeof(smpl_t) * 8;
+  inputFormat.mFramesPerPacket  = 1;
+  inputFormat.mBytesPerFrame    = inputFormat.mBitsPerChannel * inputFormat.mChannelsPerFrame / 8;
+  inputFormat.mBytesPerPacket   = inputFormat.mFramesPerPacket * inputFormat.mBytesPerFrame;
   OSStatus err = noErr;
   err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
      overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
@@ -161,7 +172,18 @@
         getPrintableOSStatusError(errorstr, err));
     goto beach;
   }
-  if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
+
+  err = ExtAudioFileSetProperty(s->audioFile,
+      kExtAudioFileProperty_ClientDataFormat,
+      sizeof(AudioStreamBasicDescription), &inputFormat);
+  if (err) {
+    char_t errorstr[20];
+    AUBIO_ERR("sink_apple_audio: error when trying to set output format on %s "
+        "(%s)\n", s->path, getPrintableOSStatusError(errorstr, err));
+    goto beach;
+  }
+
+  if (createAudioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
     AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
         "out of memory? \n", s->path);
     goto beach;
@@ -174,13 +196,13 @@
 
 void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
   UInt32 c, v;
-  short *data = (short*)s->bufferList.mBuffers[0].mData;
+  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
   uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path,
       s->max_frames, write_data->length, write);
 
   for (c = 0; c < s->channels; c++) {
     for (v = 0; v < length; v++) {
-      data[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[v]);
+      data[v * s->channels + c] = write_data->data[v];
     }
   }
 
@@ -189,7 +211,7 @@
 
 void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) {
   UInt32 c, v;
-  short *data = (short*)s->bufferList.mBuffers[0].mData;
+  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
   uint_t channels = aubio_sink_validate_input_channels("sink_apple_audio",
       s->path, s->channels, write_data->height);
   uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path,
@@ -197,7 +219,7 @@
 
   for (c = 0; c < channels; c++) {
     for (v = 0; v < length; v++) {
-      data[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[c][v]);
+      data[v * s->channels + c] = write_data->data[c][v];
     }
   }
 
@@ -206,6 +228,10 @@
 
 void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write) {
   OSStatus err = noErr;
+  // set mDataByteSize to match the number of frames to be written
+  // see https://www.mail-archive.com/coreaudio-api@lists.apple.com/msg01109.html
+  s->bufferList.mBuffers[0].mDataByteSize = write * s->channels
+    * sizeof(smpl_t);
   if (s->async) {
     err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
     if (err) {
--- a/src/io/source_apple_audio.c
+++ b/src/io/source_apple_audio.c
@@ -34,8 +34,6 @@
 #define RT_BYTE3( a )      ( ((a) >> 16) & 0xff )
 #define RT_BYTE4( a )      ( ((a) >> 24) & 0xff )
 
-#define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
-
 struct _aubio_source_apple_audio_t {
   uint_t channels;
   uint_t samplerate;          //< requested samplerate
@@ -48,7 +46,7 @@
   AudioBufferList bufferList;
 };
 
-extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
+extern int createAudioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
 extern void freeAudioBufferList(AudioBufferList *bufferList);
 extern CFURLRef createURLFromPath(const char * path);
 char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
@@ -138,17 +136,16 @@
   s->channels = fileFormat.mChannelsPerFrame;
 
   AudioStreamBasicDescription clientFormat;
-  propSize = sizeof(clientFormat);
+  propSize = sizeof(AudioStreamBasicDescription);
   memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
   clientFormat.mFormatID         = kAudioFormatLinearPCM;
   clientFormat.mSampleRate       = (Float64)(s->samplerate);
-  clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
+  clientFormat.mFormatFlags      = kAudioFormatFlagIsFloat;
   clientFormat.mChannelsPerFrame = s->channels;
-  clientFormat.mBitsPerChannel   = sizeof(short) * 8;
+  clientFormat.mBitsPerChannel   = sizeof(smpl_t) * 8;
   clientFormat.mFramesPerPacket  = 1;
   clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
   clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
-  clientFormat.mReserved         = 0;
 
   // set the client format description
   err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
@@ -186,7 +183,7 @@
 
   // allocate the AudioBufferList
   freeAudioBufferList(&s->bufferList);
-  if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
+  if (createAudioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
     AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
     goto beach;
   }
@@ -195,8 +192,9 @@
   return err;
 }
 
-void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
-  UInt32 c, v, loadedPackets = s->block_size;
+static UInt32 aubio_source_apple_audio_read_frame(aubio_source_apple_audio_t *s)
+{
+  UInt32 loadedPackets = s->block_size;
   OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
   if (err) {
     char_t errorstr[20];
@@ -203,52 +201,42 @@
     AUBIO_ERROR("source_apple_audio: error while reading %s "
         "with ExtAudioFileRead (%s)\n", s->path,
         getPrintableOSStatusError(errorstr, err));
-    goto beach;
   }
+  return loadedPackets;
+}
 
-  short *data = (short*)s->bufferList.mBuffers[0].mData;
+void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to,
+    uint_t * read) {
+  uint_t c, v;
+  UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
+  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
 
-  smpl_t *buf = read_to->data;
-
   for (v = 0; v < loadedPackets; v++) {
-    buf[v] = 0.;
+    read_to->data[v] = 0.;
     for (c = 0; c < s->channels; c++) {
-      buf[v] += SHORT_TO_FLOAT(data[ v * s->channels + c]);
+      read_to->data[v] += data[ v * s->channels + c];
     }
-    buf[v] /= (smpl_t)s->channels;
+    read_to->data[v] /= (smpl_t)s->channels;
   }
   // short read, fill with zeros
   if (loadedPackets < s->block_size) {
     for (v = loadedPackets; v < s->block_size; v++) {
-      buf[v] = 0.;
+      read_to->data[v] = 0.;
     }
   }
 
   *read = (uint_t)loadedPackets;
   return;
-beach:
-  *read = 0;
-  return;
 }
 
 void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
-  UInt32 c, v, loadedPackets = s->block_size;
-  OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
-  if (err) {
-    char_t errorstr[20];
-    AUBIO_ERROR("source_apple_audio: error while reading %s "
-        "with ExtAudioFileRead (%s)\n", s->path,
-        getPrintableOSStatusError(errorstr, err));
-    goto beach;
-  }
+  uint_t c, v;
+  UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
+  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
 
-  short *data = (short*)s->bufferList.mBuffers[0].mData;
-
-  smpl_t **buf = read_to->data;
-
   for (v = 0; v < loadedPackets; v++) {
     for (c = 0; c < read_to->height; c++) {
-      buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]);
+      read_to->data[c][v] = data[ v * s->channels + c];
     }
   }
   // if read_data has more channels than the file
@@ -256,7 +244,7 @@
     // copy last channel to all additional channels
     for (v = 0; v < loadedPackets; v++) {
       for (c = s->channels; c < read_to->height; c++) {
-        buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + (s->channels - 1)]);
+        read_to->data[c][v] = data[ v * s->channels + (s->channels - 1)];
       }
     }
   }
@@ -264,15 +252,13 @@
   if (loadedPackets < s->block_size) {
     for (v = loadedPackets; v < s->block_size; v++) {
       for (c = 0; c < read_to->height; c++) {
-        buf[c][v] = 0.;
+        read_to->data[c][v] = 0.;
       }
     }
   }
+
   *read = (uint_t)loadedPackets;
   return;
-beach:
-  *read = 0;
-  return;
 }
 
 uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
@@ -322,7 +308,7 @@
   }
   // after a short read, the bufferList size needs to resetted to prepare for a full read
   AudioBufferList *bufferList = &s->bufferList;
-  bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (short);
+  bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (smpl_t);
   // do the actual seek
   err = ExtAudioFileSeek(s->audioFile, resampled_pos);
   if (err) {
--- a/src/io/utils_apple_audio.c
+++ b/src/io/utils_apple_audio.c
@@ -12,11 +12,12 @@
 CFURLRef getURLFromPath(const char * path);
 char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
 
-int createAubioBufferList(AudioBufferList * bufferList, int channels, int max_source_samples) {
+int createAudioBufferList(AudioBufferList * bufferList, int channels,
+    int max_source_samples) {
   bufferList->mNumberBuffers = 1;
   bufferList->mBuffers[0].mNumberChannels = channels;
-  bufferList->mBuffers[0].mData = AUBIO_ARRAY(short, max_source_samples);
-  bufferList->mBuffers[0].mDataByteSize = max_source_samples * sizeof(short);
+  bufferList->mBuffers[0].mData = AUBIO_ARRAY(smpl_t, max_source_samples);
+  bufferList->mBuffers[0].mDataByteSize = max_source_samples * sizeof(smpl_t);
   return 0;
 }