source: src/io/source_apple_audio.c @ f33ab63

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since f33ab63 was 493b832, checked in by Paul Brossier <piem@piem.org>, 10 years ago

src/io/source_apple_audio.c: check out of bounds _seek

  • Property mode set to 100644
File size: 12.3 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
21#include "config.h"
[9209c79]22
23#ifdef HAVE_SOURCE_APPLE_AUDIO
24
[3504dfe7]25#include "aubio_priv.h"
26#include "fvec.h"
[4865e4b]27#include "fmat.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
38#define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
39
40struct _aubio_source_apple_audio_t {
41  uint_t channels;
[7982203]42  uint_t samplerate;          //< requested samplerate
43  uint_t source_samplerate;   //< actual source samplerate
[3504dfe7]44  uint_t block_size;
45
46  char_t *path;
47
48  ExtAudioFileRef audioFile;
49  AudioBufferList bufferList;
50};
51
[c833f56]52extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
[1223979]53extern void freeAudioBufferList(AudioBufferList *bufferList);
54extern CFURLRef getURLFromPath(const char * path);
[98a3887]55char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
[3504dfe7]56
[6bbdcff]57uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path);
58
[3504dfe7]59aubio_source_apple_audio_t * new_aubio_source_apple_audio(char_t * path, uint_t samplerate, uint_t block_size)
60{
61  aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
62
[961cff13]63  if (path == NULL) {
64    AUBIO_ERROR("source_apple_audio: Aborted opening null path\n");
65    goto beach;
66  }
67
68  if ( (sint_t)block_size <= 0 ) {
69    AUBIO_ERROR("source_apple_audio: Can not open %s with null or negative block_size %d\n",
70        path, block_size);
71    goto beach;
72  }
73
74  if ( (sint_t)samplerate < 0 ) {
75    AUBIO_ERROR("source_apple_audio: Can not open %s with negative samplerate %d\n",
76        path, samplerate);
77    goto beach;
78  }
79
[3504dfe7]80  s->block_size = block_size;
[6bbdcff]81  s->samplerate = samplerate;
[961cff13]82  s->path = path;
[3504dfe7]83
[6bbdcff]84  if ( aubio_source_apple_audio_open ( s, path ) ) {
85    goto beach;
86  }
87  return s;
88
89beach:
90  AUBIO_FREE(s);
91  return NULL;
92}
93
94uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path)
95{
[3504dfe7]96  OSStatus err = noErr;
97  UInt32 propSize;
[6bbdcff]98  s->path = path;
[3504dfe7]99
100  // open the resource url
101  CFURLRef fileURL = getURLFromPath(path);
102  err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
[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;
140  propSize = sizeof(clientFormat);
141  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
142  clientFormat.mFormatID         = kAudioFormatLinearPCM;
143  clientFormat.mSampleRate       = (Float64)(s->samplerate);
144  clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
145  clientFormat.mChannelsPerFrame = s->channels;
146  clientFormat.mBitsPerChannel   = sizeof(short) * 8;
147  clientFormat.mFramesPerPacket  = 1;
148  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
149  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
150  clientFormat.mReserved         = 0;
151
[3504dfe7]152  // set the client format description
153  err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
154      propSize, &clientFormat);
[4865e4b]155  if (err) {
[98a3887]156    char_t errorstr[20];
157    AUBIO_ERROR("source_apple_audio: Failed opening %s, "
158        "error in ExtAudioFileSetProperty (%s)\n", s->path,
159        getPrintableOSStatusError(errorstr, err));
[961cff13]160#if 0
[3504dfe7]161  // print client and format descriptions
162  AUBIO_DBG("Opened %s\n", s->path);
163  AUBIO_DBG("file/client Format.mFormatID:        : %3c%c%c%c / %c%c%c%c\n",
164      (int)RT_BYTE4(fileFormat.mFormatID),   (int)RT_BYTE3(fileFormat.mFormatID),   (int)RT_BYTE2(fileFormat.mFormatID),   (int)RT_BYTE1(fileFormat.mFormatID),
165      (int)RT_BYTE4(clientFormat.mFormatID), (int)RT_BYTE3(clientFormat.mFormatID), (int)RT_BYTE2(clientFormat.mFormatID), (int)RT_BYTE1(clientFormat.mFormatID)
166      );
167
168  AUBIO_DBG("file/client Format.mSampleRate       : %6.0f / %.0f\n",     fileFormat.mSampleRate      ,      clientFormat.mSampleRate);
169  AUBIO_DBG("file/client Format.mFormatFlags      : %6d / %d\n",    (int)fileFormat.mFormatFlags     , (int)clientFormat.mFormatFlags);
170  AUBIO_DBG("file/client Format.mChannelsPerFrame : %6d / %d\n",    (int)fileFormat.mChannelsPerFrame, (int)clientFormat.mChannelsPerFrame);
171  AUBIO_DBG("file/client Format.mBitsPerChannel   : %6d / %d\n",    (int)fileFormat.mBitsPerChannel  , (int)clientFormat.mBitsPerChannel);
172  AUBIO_DBG("file/client Format.mFramesPerPacket  : %6d / %d\n",    (int)fileFormat.mFramesPerPacket , (int)clientFormat.mFramesPerPacket);
173  AUBIO_DBG("file/client Format.mBytesPerFrame    : %6d / %d\n",    (int)fileFormat.mBytesPerFrame   , (int)clientFormat.mBytesPerFrame);
174  AUBIO_DBG("file/client Format.mBytesPerPacket   : %6d / %d\n",    (int)fileFormat.mBytesPerPacket  , (int)clientFormat.mBytesPerPacket);
175  AUBIO_DBG("file/client Format.mReserved         : %6d / %d\n",    (int)fileFormat.mReserved        , (int)clientFormat.mReserved);
[1223979]176#endif
[4865e4b]177      goto beach;
178  }
[3504dfe7]179
[c833f56]180  smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
181  if (ratio < 1.) {
182    AUBIO_WRN("source_apple_audio: up-sampling %s from %0dHz to %0dHz\n",
183        s->path, s->source_samplerate, s->samplerate);
[3504dfe7]184  }
185
186  // allocate the AudioBufferList
[c833f56]187  freeAudioBufferList(&s->bufferList);
188  if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
[6bbdcff]189    AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
190    goto beach;
191  }
[3504dfe7]192
193beach:
[6bbdcff]194  return err;
[3504dfe7]195}
196
197void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
198  UInt32 c, v, loadedPackets = s->block_size;
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    goto beach;
206  }
[3504dfe7]207
208  short *data = (short*)s->bufferList.mBuffers[0].mData;
209
[8c43bf7]210  smpl_t *buf = read_to->data;
211
212  for (v = 0; v < loadedPackets; v++) {
213    buf[v] = 0.;
214    for (c = 0; c < s->channels; c++) {
215      buf[v] += SHORT_TO_FLOAT(data[ v * s->channels + c]);
216    }
217    buf[v] /= (smpl_t)s->channels;
218  }
219  // short read, fill with zeros
220  if (loadedPackets < s->block_size) {
221    for (v = loadedPackets; v < s->block_size; v++) {
222      buf[v] = 0.;
223    }
[3504dfe7]224  }
[8c43bf7]225
[3504dfe7]226  *read = (uint_t)loadedPackets;
227  return;
228beach:
229  *read = 0;
230  return;
231}
232
[4865e4b]233void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
234  UInt32 c, v, loadedPackets = s->block_size;
235  OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
[98a3887]236  if (err) {
237    char_t errorstr[20];
238    AUBIO_ERROR("source_apple_audio: error while reading %s "
239        "with ExtAudioFileRead (%s)\n", s->path,
240        getPrintableOSStatusError(errorstr, err));
241    goto beach;
242  }
[4865e4b]243
244  short *data = (short*)s->bufferList.mBuffers[0].mData;
245
246  smpl_t **buf = read_to->data;
247
248  for (v = 0; v < loadedPackets; v++) {
[af97786]249    for (c = 0; c < read_to->height; c++) {
[4865e4b]250      buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]);
251    }
252  }
[b8389de]253  // if read_data has more channels than the file
254  if (read_to->height > s->channels) {
255    // copy last channel to all additional channels
256    for (v = 0; v < loadedPackets; v++) {
257      for (c = s->channels; c < read_to->height; c++) {
258        buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + (s->channels - 1)]);
259      }
260    }
261  }
[4865e4b]262  // short read, fill with zeros
263  if (loadedPackets < s->block_size) {
264    for (v = loadedPackets; v < s->block_size; v++) {
[b8389de]265      for (c = 0; c < read_to->height; c++) {
[4865e4b]266        buf[c][v] = 0.;
267      }
268    }
269  }
270  *read = (uint_t)loadedPackets;
271  return;
272beach:
273  *read = 0;
274  return;
275}
276
[6bbdcff]277uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
278{
[3504dfe7]279  OSStatus err = noErr;
[422452b]280  if (!s->audioFile) { return AUBIO_FAIL; }
[3504dfe7]281  err = ExtAudioFileDispose(s->audioFile);
282  s->audioFile = NULL;
[98a3887]283  if (err) {
284    char_t errorstr[20];
285    AUBIO_ERROR("source_apple_audio: error while closing %s "
286        "in ExtAudioFileDispose (%s)\n", s->path,
287        getPrintableOSStatusError(errorstr, err));
288    return err;
289  }
290  return AUBIO_OK;
[6bbdcff]291}
292
293void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
294  aubio_source_apple_audio_close (s);
[3504dfe7]295  freeAudioBufferList(&s->bufferList);
296  AUBIO_FREE(s);
297  return;
298}
299
[7982203]300uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) {
[493b832]301  OSStatus err = noErr;
302  if ((sint_t)pos < 0) {
303    AUBIO_ERROR("source_apple_audio: error while seeking in %s "
304        "(can not seek at negative position %d)\n",
305        s->path, pos);
306    err = -1;
307    goto beach;
308  }
309  // check if we are not seeking out of the file
310  SInt64 fileLengthFrames = 0;
311  UInt32 propSize = sizeof(fileLengthFrames);
312  ExtAudioFileGetProperty(s->audioFile,
313      kExtAudioFileProperty_FileLengthFrames, &propSize, &fileLengthFrames);
[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
[944c7e1]354uint_t aubio_source_apple_audio_get_samplerate(aubio_source_apple_audio_t * s) {
355  return s->samplerate;
356}
357
[4865e4b]358uint_t aubio_source_apple_audio_get_channels(aubio_source_apple_audio_t * s) {
359  return s->channels;
360}
361
[9209c79]362#endif /* HAVE_SOURCE_APPLE_AUDIO */
Note: See TracBrowser for help on using the repository browser.