Changeset dbad82c


Ignore:
Timestamp:
Dec 19, 2018, 2:58:09 PM (6 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/crepe, fix/ffmpeg5, master
Children:
2b208a8
Parents:
0da5208 (diff), 7b5e1a5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into feature/autosink

Location:
src/io
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/io/sink_apple_audio.c

    r0da5208 rdbad82c  
    3232#include <AudioToolbox/AudioToolbox.h>
    3333
    34 #define FLOAT_TO_SHORT(x) (short)(x * 32768)
    35 
    36 extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
     34extern int createAudioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
    3735extern void freeAudioBufferList(AudioBufferList *bufferList);
    3836extern CFURLRef createURLFromPath(const char * path);
     
    7775    return s;
    7876  }
     77
    7978  // invalid samplerate given, abort
    8079  if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) {
     
    151150  CFURLRef fileURL = createURLFromPath(s->path);
    152151  bool overwrite = true;
     152
     153  // set the in-memory format
     154  AudioStreamBasicDescription inputFormat;
     155  memset(&inputFormat, 0, sizeof(AudioStreamBasicDescription));
     156  inputFormat.mFormatID         = kAudioFormatLinearPCM;
     157  inputFormat.mSampleRate       = (Float64)(s->samplerate);
     158  inputFormat.mFormatFlags      = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
     159  inputFormat.mChannelsPerFrame = s->channels;
     160  inputFormat.mBitsPerChannel   = sizeof(smpl_t) * 8;
     161  inputFormat.mFramesPerPacket  = 1;
     162  inputFormat.mBytesPerFrame    = inputFormat.mBitsPerChannel * inputFormat.mChannelsPerFrame / 8;
     163  inputFormat.mBytesPerPacket   = inputFormat.mFramesPerPacket * inputFormat.mBytesPerFrame;
    153164  OSStatus err = noErr;
    154165  err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
     
    162173    goto beach;
    163174  }
    164   if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
     175
     176  err = ExtAudioFileSetProperty(s->audioFile,
     177      kExtAudioFileProperty_ClientDataFormat,
     178      sizeof(AudioStreamBasicDescription), &inputFormat);
     179  if (err) {
     180    char_t errorstr[20];
     181    AUBIO_ERR("sink_apple_audio: error when trying to set output format on %s "
     182        "(%s)\n", s->path, getPrintableOSStatusError(errorstr, err));
     183    goto beach;
     184  }
     185
     186  if (createAudioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
    165187    AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
    166188        "out of memory? \n", s->path);
     
    175197void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
    176198  UInt32 c, v;
    177   short *data = (short*)s->bufferList.mBuffers[0].mData;
     199  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
    178200  uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path,
    179201      s->max_frames, write_data->length, write);
     
    181203  for (c = 0; c < s->channels; c++) {
    182204    for (v = 0; v < length; v++) {
    183       data[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[v]);
     205      data[v * s->channels + c] = write_data->data[v];
    184206    }
    185207  }
     
    190212void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) {
    191213  UInt32 c, v;
    192   short *data = (short*)s->bufferList.mBuffers[0].mData;
     214  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
    193215  uint_t channels = aubio_sink_validate_input_channels("sink_apple_audio",
    194216      s->path, s->channels, write_data->height);
     
    198220  for (c = 0; c < channels; c++) {
    199221    for (v = 0; v < length; v++) {
    200       data[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[c][v]);
     222      data[v * s->channels + c] = write_data->data[c][v];
    201223    }
    202224  }
     
    207229void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write) {
    208230  OSStatus err = noErr;
     231  // set mDataByteSize to match the number of frames to be written
     232  // see https://www.mail-archive.com/coreaudio-api@lists.apple.com/msg01109.html
     233  s->bufferList.mBuffers[0].mDataByteSize = write * s->channels
     234    * sizeof(smpl_t);
    209235  if (s->async) {
    210236    err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
  • src/io/source_apple_audio.c

    r0da5208 rdbad82c  
    3434#define RT_BYTE3( a )      ( ((a) >> 16) & 0xff )
    3535#define RT_BYTE4( a )      ( ((a) >> 24) & 0xff )
    36 
    37 #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
    3836
    3937struct _aubio_source_apple_audio_t {
     
    4947};
    5048
    51 extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
     49extern int createAudioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
    5250extern void freeAudioBufferList(AudioBufferList *bufferList);
    5351extern CFURLRef createURLFromPath(const char * path);
     
    139137
    140138  AudioStreamBasicDescription clientFormat;
    141   propSize = sizeof(clientFormat);
     139  propSize = sizeof(AudioStreamBasicDescription);
    142140  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
    143141  clientFormat.mFormatID         = kAudioFormatLinearPCM;
    144142  clientFormat.mSampleRate       = (Float64)(s->samplerate);
    145   clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
     143  clientFormat.mFormatFlags      = kAudioFormatFlagIsFloat;
    146144  clientFormat.mChannelsPerFrame = s->channels;
    147   clientFormat.mBitsPerChannel   = sizeof(short) * 8;
     145  clientFormat.mBitsPerChannel   = sizeof(smpl_t) * 8;
    148146  clientFormat.mFramesPerPacket  = 1;
    149147  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
    150148  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
    151   clientFormat.mReserved         = 0;
    152149
    153150  // set the client format description
     
    187184  // allocate the AudioBufferList
    188185  freeAudioBufferList(&s->bufferList);
    189   if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
     186  if (createAudioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
    190187    AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
    191188    goto beach;
     
    196193}
    197194
    198 void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
    199   UInt32 c, v, loadedPackets = s->block_size;
     195static UInt32 aubio_source_apple_audio_read_frame(aubio_source_apple_audio_t *s)
     196{
     197  UInt32 loadedPackets = s->block_size;
    200198  OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
    201199  if (err) {
     
    204202        "with ExtAudioFileRead (%s)\n", s->path,
    205203        getPrintableOSStatusError(errorstr, err));
    206     goto beach;
    207   }
    208 
    209   short *data = (short*)s->bufferList.mBuffers[0].mData;
    210 
    211   smpl_t *buf = read_to->data;
     204  }
     205  return loadedPackets;
     206}
     207
     208void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to,
     209    uint_t * read) {
     210  uint_t c, v;
     211  UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
     212  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
    212213
    213214  for (v = 0; v < loadedPackets; v++) {
    214     buf[v] = 0.;
     215    read_to->data[v] = 0.;
    215216    for (c = 0; c < s->channels; c++) {
    216       buf[v] += SHORT_TO_FLOAT(data[ v * s->channels + c]);
    217     }
    218     buf[v] /= (smpl_t)s->channels;
     217      read_to->data[v] += data[ v * s->channels + c];
     218    }
     219    read_to->data[v] /= (smpl_t)s->channels;
    219220  }
    220221  // short read, fill with zeros
    221222  if (loadedPackets < s->block_size) {
    222223    for (v = loadedPackets; v < s->block_size; v++) {
    223       buf[v] = 0.;
     224      read_to->data[v] = 0.;
    224225    }
    225226  }
     
    227228  *read = (uint_t)loadedPackets;
    228229  return;
    229 beach:
    230   *read = 0;
    231   return;
    232230}
    233231
    234232void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
    235   UInt32 c, v, loadedPackets = s->block_size;
    236   OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
    237   if (err) {
    238     char_t errorstr[20];
    239     AUBIO_ERROR("source_apple_audio: error while reading %s "
    240         "with ExtAudioFileRead (%s)\n", s->path,
    241         getPrintableOSStatusError(errorstr, err));
    242     goto beach;
    243   }
    244 
    245   short *data = (short*)s->bufferList.mBuffers[0].mData;
    246 
    247   smpl_t **buf = read_to->data;
     233  uint_t c, v;
     234  UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
     235  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
    248236
    249237  for (v = 0; v < loadedPackets; v++) {
    250238    for (c = 0; c < read_to->height; c++) {
    251       buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]);
     239      read_to->data[c][v] = data[ v * s->channels + c];
    252240    }
    253241  }
     
    257245    for (v = 0; v < loadedPackets; v++) {
    258246      for (c = s->channels; c < read_to->height; c++) {
    259         buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + (s->channels - 1)]);
     247        read_to->data[c][v] = data[ v * s->channels + (s->channels - 1)];
    260248      }
    261249    }
     
    265253    for (v = loadedPackets; v < s->block_size; v++) {
    266254      for (c = 0; c < read_to->height; c++) {
    267         buf[c][v] = 0.;
     255        read_to->data[c][v] = 0.;
    268256      }
    269257    }
    270258  }
     259
    271260  *read = (uint_t)loadedPackets;
    272   return;
    273 beach:
    274   *read = 0;
    275261  return;
    276262}
     
    323309  // after a short read, the bufferList size needs to resetted to prepare for a full read
    324310  AudioBufferList *bufferList = &s->bufferList;
    325   bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (short);
     311  bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (smpl_t);
    326312  // do the actual seek
    327313  err = ExtAudioFileSeek(s->audioFile, resampled_pos);
  • src/io/utils_apple_audio.c

    r0da5208 rdbad82c  
    1313char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
    1414
    15 int createAubioBufferList(AudioBufferList * bufferList, int channels, int max_source_samples) {
     15int createAudioBufferList(AudioBufferList * bufferList, int channels,
     16    int max_source_samples) {
    1617  bufferList->mNumberBuffers = 1;
    1718  bufferList->mBuffers[0].mNumberChannels = channels;
    18   bufferList->mBuffers[0].mData = AUBIO_ARRAY(short, max_source_samples);
    19   bufferList->mBuffers[0].mDataByteSize = max_source_samples * sizeof(short);
     19  bufferList->mBuffers[0].mData = AUBIO_ARRAY(smpl_t, max_source_samples);
     20  bufferList->mBuffers[0].mDataByteSize = max_source_samples * sizeof(smpl_t);
    2021  return 0;
    2122}
Note: See TracChangeset for help on using the changeset viewer.