source: src/io/source_apple_audio.c @ 918b764

Last change on this file since 918b764 was 51a35a7, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[source_apple_audio] get_duration returns 0 on failure

  • Property mode set to 100644
File size: 12.5 KB
RevLine 
[3504dfe7]1/*
2  Copyright (C) 2012 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
[33d0242]21#include "aubio_priv.h"
[9209c79]22
23#ifdef HAVE_SOURCE_APPLE_AUDIO
24
[3504dfe7]25#include "fvec.h"
[4865e4b]26#include "fmat.h"
[4c72a9c]27#include "ioutils.h"
[3504dfe7]28#include "io/source_apple_audio.h"
29
30// ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
31#include <AudioToolbox/AudioToolbox.h>
32
33#define RT_BYTE1( a )      ( (a) & 0xff )
34#define RT_BYTE2( a )      ( ((a) >> 8) & 0xff )
35#define RT_BYTE3( a )      ( ((a) >> 16) & 0xff )
36#define RT_BYTE4( a )      ( ((a) >> 24) & 0xff )
37
38struct _aubio_source_apple_audio_t {
39  uint_t channels;
[7982203]40  uint_t samplerate;          //< requested samplerate
41  uint_t source_samplerate;   //< actual source samplerate
[3504dfe7]42  uint_t block_size;
43
44  char_t *path;
45
46  ExtAudioFileRef audioFile;
47  AudioBufferList bufferList;
48};
49
[ff6d1b6]50extern int createAudioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
[1223979]51extern void freeAudioBufferList(AudioBufferList *bufferList);
[2da7526]52extern CFURLRef createURLFromPath(const char * path);
[98a3887]53char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
[3504dfe7]54
[ae5d58a]55uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path);
[6bbdcff]56
[ae5d58a]57aubio_source_apple_audio_t * new_aubio_source_apple_audio(const char_t * path, uint_t samplerate, uint_t block_size)
[3504dfe7]58{
59  aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
60
[e957246]61  if (path == NULL || strnlen(path, PATH_MAX) < 1) {
[961cff13]62    AUBIO_ERROR("source_apple_audio: Aborted opening null path\n");
63    goto beach;
64  }
65
66  if ( (sint_t)block_size <= 0 ) {
67    AUBIO_ERROR("source_apple_audio: Can not open %s with null or negative block_size %d\n",
68        path, block_size);
69    goto beach;
70  }
71
72  if ( (sint_t)samplerate < 0 ) {
73    AUBIO_ERROR("source_apple_audio: Can not open %s with negative samplerate %d\n",
74        path, samplerate);
75    goto beach;
76  }
77
[3504dfe7]78  s->block_size = block_size;
[6bbdcff]79  s->samplerate = samplerate;
[3504dfe7]80
[6bbdcff]81  if ( aubio_source_apple_audio_open ( s, path ) ) {
82    goto beach;
83  }
84  return s;
85
86beach:
[dea8506]87  del_aubio_source_apple_audio(s);
[6bbdcff]88  return NULL;
89}
90
[ae5d58a]91uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path)
[6bbdcff]92{
[3504dfe7]93  OSStatus err = noErr;
94  UInt32 propSize;
[b643a33]95
[d2be104]96  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
97  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
[3504dfe7]98
99  // open the resource url
[b643a33]100  CFURLRef fileURL = createURLFromPath(s->path);
[3504dfe7]101  err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
[2da7526]102  CFRelease(fileURL);
[98a3887]103  if (err == -43) {
104    AUBIO_ERR("source_apple_audio: Failed opening %s, "
105        "file not found, or no read access\n", s->path);
106    goto beach;
107  } else if (err) {
108    char_t errorstr[20];
109    AUBIO_ERR("source_apple_audio: Failed opening %s, "
110        "error in ExtAudioFileOpenURL (%s)\n", s->path,
111        getPrintableOSStatusError(errorstr, err));
112    goto beach;
113  }
[3504dfe7]114
115  // create an empty AudioStreamBasicDescription
116  AudioStreamBasicDescription fileFormat;
117  propSize = sizeof(fileFormat);
118  memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription));
119
120  // fill it with the file description
121  err = ExtAudioFileGetProperty(s->audioFile,
122      kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
[98a3887]123  if (err) {
124    char_t errorstr[20];
125    AUBIO_ERROR("source_apple_audio: Failed opening %s, "
126        "error in ExtAudioFileGetProperty (%s)\n", s->path,
127        getPrintableOSStatusError(errorstr, err));
128    goto beach;
129  }
[3504dfe7]130
[6bbdcff]131  if (s->samplerate == 0) {
132    s->samplerate = fileFormat.mSampleRate;
[4db8eab]133    //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
[944c7e1]134  }
[6bbdcff]135
[7982203]136  s->source_samplerate = fileFormat.mSampleRate;
[4865e4b]137  s->channels = fileFormat.mChannelsPerFrame;
[944c7e1]138
139  AudioStreamBasicDescription clientFormat;
[ff6d1b6]140  propSize = sizeof(AudioStreamBasicDescription);
[944c7e1]141  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
142  clientFormat.mFormatID         = kAudioFormatLinearPCM;
143  clientFormat.mSampleRate       = (Float64)(s->samplerate);
[ff6d1b6]144  clientFormat.mFormatFlags      = kAudioFormatFlagIsFloat;
[944c7e1]145  clientFormat.mChannelsPerFrame = s->channels;
[ff6d1b6]146  clientFormat.mBitsPerChannel   = sizeof(smpl_t) * 8;
[944c7e1]147  clientFormat.mFramesPerPacket  = 1;
148  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
149  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
150
[3504dfe7]151  // set the client format description
152  err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
153      propSize, &clientFormat);
[4865e4b]154  if (err) {
[98a3887]155    char_t errorstr[20];
156    AUBIO_ERROR("source_apple_audio: Failed opening %s, "
157        "error in ExtAudioFileSetProperty (%s)\n", s->path,
158        getPrintableOSStatusError(errorstr, err));
[961cff13]159#if 0
[3504dfe7]160  // print client and format descriptions
161  AUBIO_DBG("Opened %s\n", s->path);
162  AUBIO_DBG("file/client Format.mFormatID:        : %3c%c%c%c / %c%c%c%c\n",
163      (int)RT_BYTE4(fileFormat.mFormatID),   (int)RT_BYTE3(fileFormat.mFormatID),   (int)RT_BYTE2(fileFormat.mFormatID),   (int)RT_BYTE1(fileFormat.mFormatID),
164      (int)RT_BYTE4(clientFormat.mFormatID), (int)RT_BYTE3(clientFormat.mFormatID), (int)RT_BYTE2(clientFormat.mFormatID), (int)RT_BYTE1(clientFormat.mFormatID)
165      );
166
167  AUBIO_DBG("file/client Format.mSampleRate       : %6.0f / %.0f\n",     fileFormat.mSampleRate      ,      clientFormat.mSampleRate);
168  AUBIO_DBG("file/client Format.mFormatFlags      : %6d / %d\n",    (int)fileFormat.mFormatFlags     , (int)clientFormat.mFormatFlags);
169  AUBIO_DBG("file/client Format.mChannelsPerFrame : %6d / %d\n",    (int)fileFormat.mChannelsPerFrame, (int)clientFormat.mChannelsPerFrame);
170  AUBIO_DBG("file/client Format.mBitsPerChannel   : %6d / %d\n",    (int)fileFormat.mBitsPerChannel  , (int)clientFormat.mBitsPerChannel);
171  AUBIO_DBG("file/client Format.mFramesPerPacket  : %6d / %d\n",    (int)fileFormat.mFramesPerPacket , (int)clientFormat.mFramesPerPacket);
172  AUBIO_DBG("file/client Format.mBytesPerFrame    : %6d / %d\n",    (int)fileFormat.mBytesPerFrame   , (int)clientFormat.mBytesPerFrame);
173  AUBIO_DBG("file/client Format.mBytesPerPacket   : %6d / %d\n",    (int)fileFormat.mBytesPerPacket  , (int)clientFormat.mBytesPerPacket);
174  AUBIO_DBG("file/client Format.mReserved         : %6d / %d\n",    (int)fileFormat.mReserved        , (int)clientFormat.mReserved);
[1223979]175#endif
[4865e4b]176      goto beach;
177  }
[3504dfe7]178
[c833f56]179  smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
180  if (ratio < 1.) {
181    AUBIO_WRN("source_apple_audio: up-sampling %s from %0dHz to %0dHz\n",
182        s->path, s->source_samplerate, s->samplerate);
[3504dfe7]183  }
184
185  // allocate the AudioBufferList
[c833f56]186  freeAudioBufferList(&s->bufferList);
[ff6d1b6]187  if (createAudioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
[6bbdcff]188    AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
189    goto beach;
190  }
[3504dfe7]191
192beach:
[6bbdcff]193  return err;
[3504dfe7]194}
195
[ff6d1b6]196static UInt32 aubio_source_apple_audio_read_frame(aubio_source_apple_audio_t *s)
197{
198  UInt32 loadedPackets = s->block_size;
[3504dfe7]199  OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
[98a3887]200  if (err) {
201    char_t errorstr[20];
202    AUBIO_ERROR("source_apple_audio: error while reading %s "
203        "with ExtAudioFileRead (%s)\n", s->path,
204        getPrintableOSStatusError(errorstr, err));
205  }
[ff6d1b6]206  return loadedPackets;
207}
[3504dfe7]208
[ff6d1b6]209void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to,
210    uint_t * read) {
211  uint_t c, v;
212  UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
[4c72a9c]213  uint_t length = aubio_source_validate_input_length("source_apple_audio",
214      s->path, s->block_size, read_to->length);
[ff6d1b6]215  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
[8c43bf7]216
[4c72a9c]217  length = MIN(loadedPackets, length);
218
219  for (v = 0; v < length; v++) {
[ff6d1b6]220    read_to->data[v] = 0.;
[8c43bf7]221    for (c = 0; c < s->channels; c++) {
[ff6d1b6]222      read_to->data[v] += data[ v * s->channels + c];
[8c43bf7]223    }
[ff6d1b6]224    read_to->data[v] /= (smpl_t)s->channels;
[8c43bf7]225  }
226  // short read, fill with zeros
[4c72a9c]227  aubio_source_pad_output(read_to, length);
[8c43bf7]228
[4c72a9c]229  *read = (uint_t)length;
[3504dfe7]230}
231
[4865e4b]232void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
[ff6d1b6]233  uint_t c, v;
[4c72a9c]234  uint_t length = aubio_source_validate_input_length("source_apple_audio",
235      s->path, s->block_size, read_to->length);
236  uint_t channels = aubio_source_validate_input_channels("source_apple_audio",
237      s->path, s->channels, read_to->height);
[ff6d1b6]238  UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
239  smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
[4865e4b]240
[4c72a9c]241  length = MIN(loadedPackets, length);
242
243  for (v = 0; v < length; v++) {
244    for (c = 0; c < channels; c++) {
[ff6d1b6]245      read_to->data[c][v] = data[ v * s->channels + c];
[4865e4b]246    }
247  }
[ff6d1b6]248
[4c72a9c]249  aubio_source_pad_multi_output(read_to, s->channels, (uint_t)length);
250
251  *read = (uint_t)length;
[4865e4b]252}
253
[6bbdcff]254uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
255{
[3504dfe7]256  OSStatus err = noErr;
[25d58dc]257  if (!s->audioFile) { return AUBIO_OK; }
[3504dfe7]258  err = ExtAudioFileDispose(s->audioFile);
259  s->audioFile = NULL;
[98a3887]260  if (err) {
261    char_t errorstr[20];
262    AUBIO_ERROR("source_apple_audio: error while closing %s "
263        "in ExtAudioFileDispose (%s)\n", s->path,
264        getPrintableOSStatusError(errorstr, err));
265    return err;
266  }
267  return AUBIO_OK;
[6bbdcff]268}
269
270void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
[c0a1906]271  AUBIO_ASSERT(s);
[6bbdcff]272  aubio_source_apple_audio_close (s);
[b643a33]273  if (s->path) AUBIO_FREE(s->path);
[3504dfe7]274  freeAudioBufferList(&s->bufferList);
275  AUBIO_FREE(s);
276}
277
[7982203]278uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) {
[493b832]279  OSStatus err = noErr;
280  if ((sint_t)pos < 0) {
281    AUBIO_ERROR("source_apple_audio: error while seeking in %s "
282        "(can not seek at negative position %d)\n",
283        s->path, pos);
284    err = -1;
285    goto beach;
286  }
287  // check if we are not seeking out of the file
[a41b1ef]288  uint_t fileLengthFrames = aubio_source_apple_audio_get_duration(s);
[c833f56]289  // compute position in the source file, before resampling
290  smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
291  SInt64 resampled_pos = (SInt64)ROUND( pos * ratio );
[493b832]292  if (resampled_pos > fileLengthFrames) {
293    AUBIO_ERR("source_apple_audio: trying to seek in %s at pos %d, "
294        "but file has only %d frames\n",
295        s->path, pos, (uint_t)(fileLengthFrames / ratio));
296    err = -1;
297    goto beach;
298  }
299  // after a short read, the bufferList size needs to resetted to prepare for a full read
300  AudioBufferList *bufferList = &s->bufferList;
[ff6d1b6]301  bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (smpl_t);
[493b832]302  // do the actual seek
303  err = ExtAudioFileSeek(s->audioFile, resampled_pos);
[98a3887]304  if (err) {
305    char_t errorstr[20];
306    AUBIO_ERROR("source_apple_audio: error while seeking %s at %d "
307        "in ExtAudioFileSeek (%s)\n", s->path, pos,
308        getPrintableOSStatusError(errorstr, err));
309  }
[493b832]310#if 0
311  // check position after seek
312  {
313    SInt64 outFrameOffset = 0;
314    err = ExtAudioFileTell(s->audioFile, &outFrameOffset);
315    if (err) {
316      char_t errorstr[20];
317      AUBIO_ERROR("source_apple_audio: error while seeking %s at %d "
318          "in ExtAudioFileTell (%s)\n", s->path, pos,
319          getPrintableOSStatusError(errorstr, err));
320    }
321    AUBIO_DBG("source_apple_audio: asked seek at %d, tell got %d\n",
322        pos, (uint_t)(outFrameOffset / ratio + .5));
323  }
324#endif
325beach:
[7982203]326  return err;
327}
328
[ae5d58a]329uint_t aubio_source_apple_audio_get_samplerate(const aubio_source_apple_audio_t * s) {
[944c7e1]330  return s->samplerate;
331}
332
[ae5d58a]333uint_t aubio_source_apple_audio_get_channels(const aubio_source_apple_audio_t * s) {
[4865e4b]334  return s->channels;
335}
336
[a41b1ef]337uint_t aubio_source_apple_audio_get_duration(const aubio_source_apple_audio_t * s) {
338  SInt64 fileLengthFrames = 0;
339  UInt32 propSize = sizeof(fileLengthFrames);
340  OSStatus err = ExtAudioFileGetProperty(s->audioFile,
341      kExtAudioFileProperty_FileLengthFrames, &propSize, &fileLengthFrames);
342  if (err) {
343    char_t errorstr[20];
344    AUBIO_ERROR("source_apple_audio: Failed getting %s duration, "
345        "error in ExtAudioFileGetProperty (%s)\n", s->path,
346        getPrintableOSStatusError(errorstr, err));
[51a35a7]347    return 0;
[a41b1ef]348  }
349  return (uint_t)fileLengthFrames;
350}
351
[9209c79]352#endif /* HAVE_SOURCE_APPLE_AUDIO */
Note: See TracBrowser for help on using the repository browser.