From: Paul Brossier Date: Sat, 23 Mar 2013 06:06:02 +0000 (-0500) Subject: src/io/source_apple_audio.c: simplify buffer creation, reset size on seek X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=c833f56b197c83dbe95c0796141e2b5d6dbed2cd;p=aubio.git src/io/source_apple_audio.c: simplify buffer creation, reset size on seek --- diff --git a/src/io/source_apple_audio.c b/src/io/source_apple_audio.c index 10dc8343..f9206ed6 100644 --- a/src/io/source_apple_audio.c +++ b/src/io/source_apple_audio.c @@ -47,7 +47,7 @@ struct _aubio_source_apple_audio_t { 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 @@ uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * pa 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 @@ void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){ } 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; diff --git a/src/io/utils_apple_audio.c b/src/io/utils_apple_audio.c index 31538edd..f0804159 100644 --- a/src/io/utils_apple_audio.c +++ b/src/io/utils_apple_audio.c @@ -10,11 +10,11 @@ int createAubioBufferList(AudioBufferList *bufferList, int channels, int segment 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; }