Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_apple_audio.c

    r25d58dc rff6d1b6  
    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);
     
    6058  aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
    6159
    62   if (path == NULL) {
     60  if (path == NULL || strnlen(path, PATH_MAX) < 1) {
    6361    AUBIO_ERROR("source_apple_audio: Aborted opening null path\n");
    6462    goto beach;
     
    8684
    8785beach:
    88   AUBIO_FREE(s);
     86  del_aubio_source_apple_audio(s);
    8987  return NULL;
    9088}
     
    9593  UInt32 propSize;
    9694
    97   if (s->path) AUBIO_FREE(s->path);
    9895  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
    9996  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
     
    140137
    141138  AudioStreamBasicDescription clientFormat;
    142   propSize = sizeof(clientFormat);
     139  propSize = sizeof(AudioStreamBasicDescription);
    143140  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
    144141  clientFormat.mFormatID         = kAudioFormatLinearPCM;
    145142  clientFormat.mSampleRate       = (Float64)(s->samplerate);
    146   clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
     143  clientFormat.mFormatFlags      = kAudioFormatFlagIsFloat;
    147144  clientFormat.mChannelsPerFrame = s->channels;
    148   clientFormat.mBitsPerChannel   = sizeof(short) * 8;
     145  clientFormat.mBitsPerChannel   = sizeof(smpl_t) * 8;
    149146  clientFormat.mFramesPerPacket  = 1;
    150147  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
    151148  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
    152   clientFormat.mReserved         = 0;
    153149
    154150  // set the client format description
     
    188184  // allocate the AudioBufferList
    189185  freeAudioBufferList(&s->bufferList);
    190   if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
     186  if (createAudioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
    191187    AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
    192188    goto beach;
     
    197193}
    198194
    199 void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
    200   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;
    201198  OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
    202199  if (err) {
     
    205202        "with ExtAudioFileRead (%s)\n", s->path,
    206203        getPrintableOSStatusError(errorstr, err));
    207     goto beach;
    208   }
    209 
    210   short *data = (short*)s->bufferList.mBuffers[0].mData;
    211 
    212   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;
    213213
    214214  for (v = 0; v < loadedPackets; v++) {
    215     buf[v] = 0.;
     215    read_to->data[v] = 0.;
    216216    for (c = 0; c < s->channels; c++) {
    217       buf[v] += SHORT_TO_FLOAT(data[ v * s->channels + c]);
    218     }
    219     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;
    220220  }
    221221  // short read, fill with zeros
    222222  if (loadedPackets < s->block_size) {
    223223    for (v = loadedPackets; v < s->block_size; v++) {
    224       buf[v] = 0.;
     224      read_to->data[v] = 0.;
    225225    }
    226226  }
     
    228228  *read = (uint_t)loadedPackets;
    229229  return;
    230 beach:
    231   *read = 0;
    232   return;
    233230}
    234231
    235232void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
    236   UInt32 c, v, loadedPackets = s->block_size;
    237   OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
    238   if (err) {
    239     char_t errorstr[20];
    240     AUBIO_ERROR("source_apple_audio: error while reading %s "
    241         "with ExtAudioFileRead (%s)\n", s->path,
    242         getPrintableOSStatusError(errorstr, err));
    243     goto beach;
    244   }
    245 
    246   short *data = (short*)s->bufferList.mBuffers[0].mData;
    247 
    248   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;
    249236
    250237  for (v = 0; v < loadedPackets; v++) {
    251238    for (c = 0; c < read_to->height; c++) {
    252       buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]);
     239      read_to->data[c][v] = data[ v * s->channels + c];
    253240    }
    254241  }
     
    258245    for (v = 0; v < loadedPackets; v++) {
    259246      for (c = s->channels; c < read_to->height; c++) {
    260         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)];
    261248      }
    262249    }
     
    266253    for (v = loadedPackets; v < s->block_size; v++) {
    267254      for (c = 0; c < read_to->height; c++) {
    268         buf[c][v] = 0.;
     255        read_to->data[c][v] = 0.;
    269256      }
    270257    }
    271258  }
     259
    272260  *read = (uint_t)loadedPackets;
    273   return;
    274 beach:
    275   *read = 0;
    276261  return;
    277262}
     
    294279
    295280void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
     281  AUBIO_ASSERT(s);
    296282  aubio_source_apple_audio_close (s);
    297283  if (s->path) AUBIO_FREE(s->path);
    298284  freeAudioBufferList(&s->bufferList);
    299285  AUBIO_FREE(s);
    300   return;
    301286}
    302287
     
    324309  // after a short read, the bufferList size needs to resetted to prepare for a full read
    325310  AudioBufferList *bufferList = &s->bufferList;
    326   bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (short);
     311  bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (smpl_t);
    327312  // do the actual seek
    328313  err = ExtAudioFileSeek(s->audioFile, resampled_pos);
Note: See TracChangeset for help on using the changeset viewer.