shithub: aubio

Download patch

ref: c833f56b197c83dbe95c0796141e2b5d6dbed2cd
parent: b8389deb101c95e4b7f796b9f0052d4d6445abbf
author: Paul Brossier <piem@piem.org>
date: Fri Mar 22 21:06:02 EDT 2013

src/io/source_apple_audio.c: simplify buffer creation, reset size on seek

--- a/src/io/source_apple_audio.c
+++ b/src/io/source_apple_audio.c
@@ -47,7 +47,7 @@
   AudioBufferList bufferList;
 };
 
-extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
+extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
 extern void freeAudioBufferList(AudioBufferList *bufferList);
 extern CFURLRef getURLFromPath(const char * path);
 
@@ -137,21 +137,15 @@
       goto beach;
   }
 
-  // compute the size of the segments needed to read the input file
-  UInt32 samples = s->block_size * s->channels;
-  Float64 rateRatio = s->samplerate / s->source_samplerate;
-  uint_t segmentSize= (uint_t)(samples * rateRatio + .5);
-  if (rateRatio < 1.) {
-    segmentSize = (uint_t)(samples / rateRatio + .5);
-  } else if (rateRatio > 1.) {
-    AUBIO_WRN("up-sampling %s from %0dHz to %0dHz\n", s->path, s->source_samplerate, s->samplerate);
-  } else {
-    assert ( segmentSize == samples );
-    //AUBIO_DBG("not resampling, segmentSize %d, block_size %d\n", segmentSize, s->block_size);
+  smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
+  if (ratio < 1.) {
+    AUBIO_WRN("source_apple_audio: up-sampling %s from %0dHz to %0dHz\n",
+        s->path, s->source_samplerate, s->samplerate);
   }
 
   // allocate the AudioBufferList
-  if (createAubioBufferList(&s->bufferList, s->channels, segmentSize)) {
+  freeAudioBufferList(&s->bufferList);
+  if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
     AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
     goto beach;
   }
@@ -246,14 +240,12 @@
 }
 
 uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) {
-  if (1) {
-    AudioBufferList *bufferList = &s->bufferList;
-    UInt32 samples = s->block_size * s->channels;
-    Float64 rateRatio = s->samplerate / s->source_samplerate;
-    uint_t segmentSize= (uint_t)(samples * rateRatio + .5);
-    bufferList->mBuffers[0].mDataByteSize = segmentSize * sizeof(short);
-  }
-  SInt64 resampled_pos = (SInt64)ROUND( pos * s->source_samplerate * 1. / s->samplerate );
+  // 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);
+  // compute position in the source file, before resampling
+  smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
+  SInt64 resampled_pos = (SInt64)ROUND( pos * ratio );
   OSStatus err = ExtAudioFileSeek(s->audioFile, resampled_pos);
   if (err) AUBIO_ERROR("source_apple_audio: error in ExtAudioFileSeek (%d)\n", (int)err);
   return err;
--- a/src/io/utils_apple_audio.c
+++ b/src/io/utils_apple_audio.c
@@ -10,11 +10,11 @@
 void freeAudioBufferList(AudioBufferList *bufferList);
 CFURLRef getURLFromPath(const char * path);
 
-int createAubioBufferList(AudioBufferList * bufferList, int channels, int segmentSize) {
+int createAubioBufferList(AudioBufferList * bufferList, int channels, int max_source_samples) {
   bufferList->mNumberBuffers = 1;
   bufferList->mBuffers[0].mNumberChannels = channels;
-  bufferList->mBuffers[0].mData = AUBIO_ARRAY(short, segmentSize);
-  bufferList->mBuffers[0].mDataByteSize = segmentSize * sizeof(short);
+  bufferList->mBuffers[0].mData = AUBIO_ARRAY(short, max_source_samples);
+  bufferList->mBuffers[0].mDataByteSize = max_source_samples * sizeof(short);
   return 0;
 }