source: src/io/source_apple_audio.c @ ff6d1b6

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since ff6d1b6 was ff6d1b6, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[io] source_apple_audio to use native format conversion

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