source: src/io/source_apple_audio.c @ b63c7d3

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampler
Last change on this file since b63c7d3 was 25d58dc, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/io/source_apple_audio.c: allow closing twice

  • Property mode set to 100644
File size: 12.9 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"
[3504dfe7]27#include "io/source_apple_audio.h"
28
29// ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
30#include <AudioToolbox/AudioToolbox.h>
31
32#define RT_BYTE1( a )      ( (a) & 0xff )
33#define RT_BYTE2( a )      ( ((a) >> 8) & 0xff )
34#define RT_BYTE3( a )      ( ((a) >> 16) & 0xff )
35#define RT_BYTE4( a )      ( ((a) >> 24) & 0xff )
36
37#define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
38
39struct _aubio_source_apple_audio_t {
40  uint_t channels;
[7982203]41  uint_t samplerate;          //< requested samplerate
42  uint_t source_samplerate;   //< actual source samplerate
[3504dfe7]43  uint_t block_size;
44
45  char_t *path;
46
47  ExtAudioFileRef audioFile;
48  AudioBufferList bufferList;
49};
50
[c833f56]51extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
[1223979]52extern void freeAudioBufferList(AudioBufferList *bufferList);
[2da7526]53extern CFURLRef createURLFromPath(const char * path);
[98a3887]54char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
[3504dfe7]55
[ae5d58a]56uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path);
[6bbdcff]57
[ae5d58a]58aubio_source_apple_audio_t * new_aubio_source_apple_audio(const char_t * path, uint_t samplerate, uint_t block_size)
[3504dfe7]59{
60  aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
61
[961cff13]62  if (path == NULL) {
63    AUBIO_ERROR("source_apple_audio: Aborted opening null path\n");
64    goto beach;
65  }
66
67  if ( (sint_t)block_size <= 0 ) {
68    AUBIO_ERROR("source_apple_audio: Can not open %s with null or negative block_size %d\n",
69        path, block_size);
70    goto beach;
71  }
72
73  if ( (sint_t)samplerate < 0 ) {
74    AUBIO_ERROR("source_apple_audio: Can not open %s with negative samplerate %d\n",
75        path, samplerate);
76    goto beach;
77  }
78
[3504dfe7]79  s->block_size = block_size;
[6bbdcff]80  s->samplerate = samplerate;
[3504dfe7]81
[6bbdcff]82  if ( aubio_source_apple_audio_open ( s, path ) ) {
83    goto beach;
84  }
85  return s;
86
87beach:
88  AUBIO_FREE(s);
89  return NULL;
90}
91
[ae5d58a]92uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path)
[6bbdcff]93{
[3504dfe7]94  OSStatus err = noErr;
95  UInt32 propSize;
[b643a33]96
97  if (s->path) AUBIO_FREE(s->path);
[d2be104]98  s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
99  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
[3504dfe7]100
101  // open the resource url
[b643a33]102  CFURLRef fileURL = createURLFromPath(s->path);
[3504dfe7]103  err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
[2da7526]104  CFRelease(fileURL);
[98a3887]105  if (err == -43) {
106    AUBIO_ERR("source_apple_audio: Failed opening %s, "
107        "file not found, or no read access\n", s->path);
108    goto beach;
109  } else if (err) {
110    char_t errorstr[20];
111    AUBIO_ERR("source_apple_audio: Failed opening %s, "
112        "error in ExtAudioFileOpenURL (%s)\n", s->path,
113        getPrintableOSStatusError(errorstr, err));
114    goto beach;
115  }
[3504dfe7]116
117  // create an empty AudioStreamBasicDescription
118  AudioStreamBasicDescription fileFormat;
119  propSize = sizeof(fileFormat);
120  memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription));
121
122  // fill it with the file description
123  err = ExtAudioFileGetProperty(s->audioFile,
124      kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
[98a3887]125  if (err) {
126    char_t errorstr[20];
127    AUBIO_ERROR("source_apple_audio: Failed opening %s, "
128        "error in ExtAudioFileGetProperty (%s)\n", s->path,
129        getPrintableOSStatusError(errorstr, err));
130    goto beach;
131  }
[3504dfe7]132
[6bbdcff]133  if (s->samplerate == 0) {
134    s->samplerate = fileFormat.mSampleRate;
[4db8eab]135    //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
[944c7e1]136  }
[6bbdcff]137
[7982203]138  s->source_samplerate = fileFormat.mSampleRate;
[4865e4b]139  s->channels = fileFormat.mChannelsPerFrame;
[944c7e1]140
141  AudioStreamBasicDescription clientFormat;
142  propSize = sizeof(clientFormat);
143  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
144  clientFormat.mFormatID         = kAudioFormatLinearPCM;
145  clientFormat.mSampleRate       = (Float64)(s->samplerate);
146  clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
147  clientFormat.mChannelsPerFrame = s->channels;
148  clientFormat.mBitsPerChannel   = sizeof(short) * 8;
149  clientFormat.mFramesPerPacket  = 1;
150  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
151  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
152  clientFormat.mReserved         = 0;
153
[3504dfe7]154  // set the client format description
155  err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
156      propSize, &clientFormat);
[4865e4b]157  if (err) {
[98a3887]158    char_t errorstr[20];
159    AUBIO_ERROR("source_apple_audio: Failed opening %s, "
160        "error in ExtAudioFileSetProperty (%s)\n", s->path,
161        getPrintableOSStatusError(errorstr, err));
[961cff13]162#if 0
[3504dfe7]163  // print client and format descriptions
164  AUBIO_DBG("Opened %s\n", s->path);
165  AUBIO_DBG("file/client Format.mFormatID:        : %3c%c%c%c / %c%c%c%c\n",
166      (int)RT_BYTE4(fileFormat.mFormatID),   (int)RT_BYTE3(fileFormat.mFormatID),   (int)RT_BYTE2(fileFormat.mFormatID),   (int)RT_BYTE1(fileFormat.mFormatID),
167      (int)RT_BYTE4(clientFormat.mFormatID), (int)RT_BYTE3(clientFormat.mFormatID), (int)RT_BYTE2(clientFormat.mFormatID), (int)RT_BYTE1(clientFormat.mFormatID)
168      );
169
170  AUBIO_DBG("file/client Format.mSampleRate       : %6.0f / %.0f\n",     fileFormat.mSampleRate      ,      clientFormat.mSampleRate);
171  AUBIO_DBG("file/client Format.mFormatFlags      : %6d / %d\n",    (int)fileFormat.mFormatFlags     , (int)clientFormat.mFormatFlags);
172  AUBIO_DBG("file/client Format.mChannelsPerFrame : %6d / %d\n",    (int)fileFormat.mChannelsPerFrame, (int)clientFormat.mChannelsPerFrame);
173  AUBIO_DBG("file/client Format.mBitsPerChannel   : %6d / %d\n",    (int)fileFormat.mBitsPerChannel  , (int)clientFormat.mBitsPerChannel);
174  AUBIO_DBG("file/client Format.mFramesPerPacket  : %6d / %d\n",    (int)fileFormat.mFramesPerPacket , (int)clientFormat.mFramesPerPacket);
175  AUBIO_DBG("file/client Format.mBytesPerFrame    : %6d / %d\n",    (int)fileFormat.mBytesPerFrame   , (int)clientFormat.mBytesPerFrame);
176  AUBIO_DBG("file/client Format.mBytesPerPacket   : %6d / %d\n",    (int)fileFormat.mBytesPerPacket  , (int)clientFormat.mBytesPerPacket);
177  AUBIO_DBG("file/client Format.mReserved         : %6d / %d\n",    (int)fileFormat.mReserved        , (int)clientFormat.mReserved);
[1223979]178#endif
[4865e4b]179      goto beach;
180  }
[3504dfe7]181
[c833f56]182  smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
183  if (ratio < 1.) {
184    AUBIO_WRN("source_apple_audio: up-sampling %s from %0dHz to %0dHz\n",
185        s->path, s->source_samplerate, s->samplerate);
[3504dfe7]186  }
187
188  // allocate the AudioBufferList
[c833f56]189  freeAudioBufferList(&s->bufferList);
190  if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
[6bbdcff]191    AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
192    goto beach;
193  }
[3504dfe7]194
195beach:
[6bbdcff]196  return err;
[3504dfe7]197}
198
199void 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;
201  OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
[98a3887]202  if (err) {
203    char_t errorstr[20];
204    AUBIO_ERROR("source_apple_audio: error while reading %s "
205        "with ExtAudioFileRead (%s)\n", s->path,
206        getPrintableOSStatusError(errorstr, err));
207    goto beach;
208  }
[3504dfe7]209
210  short *data = (short*)s->bufferList.mBuffers[0].mData;
211
[8c43bf7]212  smpl_t *buf = read_to->data;
213
214  for (v = 0; v < loadedPackets; v++) {
215    buf[v] = 0.;
216    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;
220  }
221  // short read, fill with zeros
222  if (loadedPackets < s->block_size) {
223    for (v = loadedPackets; v < s->block_size; v++) {
224      buf[v] = 0.;
225    }
[3504dfe7]226  }
[8c43bf7]227
[3504dfe7]228  *read = (uint_t)loadedPackets;
229  return;
230beach:
231  *read = 0;
232  return;
233}
234
[4865e4b]235void 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);
[98a3887]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  }
[4865e4b]245
246  short *data = (short*)s->bufferList.mBuffers[0].mData;
247
248  smpl_t **buf = read_to->data;
249
250  for (v = 0; v < loadedPackets; v++) {
[af97786]251    for (c = 0; c < read_to->height; c++) {
[4865e4b]252      buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]);
253    }
254  }
[b8389de]255  // if read_data has more channels than the file
256  if (read_to->height > s->channels) {
257    // copy last channel to all additional channels
258    for (v = 0; v < loadedPackets; v++) {
259      for (c = s->channels; c < read_to->height; c++) {
260        buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + (s->channels - 1)]);
261      }
262    }
263  }
[4865e4b]264  // short read, fill with zeros
265  if (loadedPackets < s->block_size) {
266    for (v = loadedPackets; v < s->block_size; v++) {
[b8389de]267      for (c = 0; c < read_to->height; c++) {
[4865e4b]268        buf[c][v] = 0.;
269      }
270    }
271  }
272  *read = (uint_t)loadedPackets;
273  return;
274beach:
275  *read = 0;
276  return;
277}
278
[6bbdcff]279uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
280{
[3504dfe7]281  OSStatus err = noErr;
[25d58dc]282  if (!s->audioFile) { return AUBIO_OK; }
[3504dfe7]283  err = ExtAudioFileDispose(s->audioFile);
284  s->audioFile = NULL;
[98a3887]285  if (err) {
286    char_t errorstr[20];
287    AUBIO_ERROR("source_apple_audio: error while closing %s "
288        "in ExtAudioFileDispose (%s)\n", s->path,
289        getPrintableOSStatusError(errorstr, err));
290    return err;
291  }
292  return AUBIO_OK;
[6bbdcff]293}
294
295void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
296  aubio_source_apple_audio_close (s);
[b643a33]297  if (s->path) AUBIO_FREE(s->path);
[3504dfe7]298  freeAudioBufferList(&s->bufferList);
299  AUBIO_FREE(s);
300  return;
301}
302
[7982203]303uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) {
[493b832]304  OSStatus err = noErr;
305  if ((sint_t)pos < 0) {
306    AUBIO_ERROR("source_apple_audio: error while seeking in %s "
307        "(can not seek at negative position %d)\n",
308        s->path, pos);
309    err = -1;
310    goto beach;
311  }
312  // check if we are not seeking out of the file
[a41b1ef]313  uint_t fileLengthFrames = aubio_source_apple_audio_get_duration(s);
[c833f56]314  // compute position in the source file, before resampling
315  smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
316  SInt64 resampled_pos = (SInt64)ROUND( pos * ratio );
[493b832]317  if (resampled_pos > fileLengthFrames) {
318    AUBIO_ERR("source_apple_audio: trying to seek in %s at pos %d, "
319        "but file has only %d frames\n",
320        s->path, pos, (uint_t)(fileLengthFrames / ratio));
321    err = -1;
322    goto beach;
323  }
324  // after a short read, the bufferList size needs to resetted to prepare for a full read
325  AudioBufferList *bufferList = &s->bufferList;
326  bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (short);
327  // do the actual seek
328  err = ExtAudioFileSeek(s->audioFile, resampled_pos);
[98a3887]329  if (err) {
330    char_t errorstr[20];
331    AUBIO_ERROR("source_apple_audio: error while seeking %s at %d "
332        "in ExtAudioFileSeek (%s)\n", s->path, pos,
333        getPrintableOSStatusError(errorstr, err));
334  }
[493b832]335#if 0
336  // check position after seek
337  {
338    SInt64 outFrameOffset = 0;
339    err = ExtAudioFileTell(s->audioFile, &outFrameOffset);
340    if (err) {
341      char_t errorstr[20];
342      AUBIO_ERROR("source_apple_audio: error while seeking %s at %d "
343          "in ExtAudioFileTell (%s)\n", s->path, pos,
344          getPrintableOSStatusError(errorstr, err));
345    }
346    AUBIO_DBG("source_apple_audio: asked seek at %d, tell got %d\n",
347        pos, (uint_t)(outFrameOffset / ratio + .5));
348  }
349#endif
350beach:
[7982203]351  return err;
352}
353
[ae5d58a]354uint_t aubio_source_apple_audio_get_samplerate(const aubio_source_apple_audio_t * s) {
[944c7e1]355  return s->samplerate;
356}
357
[ae5d58a]358uint_t aubio_source_apple_audio_get_channels(const aubio_source_apple_audio_t * s) {
[4865e4b]359  return s->channels;
360}
361
[a41b1ef]362uint_t aubio_source_apple_audio_get_duration(const aubio_source_apple_audio_t * s) {
363  SInt64 fileLengthFrames = 0;
364  UInt32 propSize = sizeof(fileLengthFrames);
365  OSStatus err = ExtAudioFileGetProperty(s->audioFile,
366      kExtAudioFileProperty_FileLengthFrames, &propSize, &fileLengthFrames);
367  if (err) {
368    char_t errorstr[20];
369    AUBIO_ERROR("source_apple_audio: Failed getting %s duration, "
370        "error in ExtAudioFileGetProperty (%s)\n", s->path,
371        getPrintableOSStatusError(errorstr, err));
372    return err;
373  }
374  return (uint_t)fileLengthFrames;
375}
376
[9209c79]377#endif /* HAVE_SOURCE_APPLE_AUDIO */
Note: See TracBrowser for help on using the repository browser.