source: src/io/source_apple_audio.c @ 9209c79

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

wscript, src/io/*.c: use custom defines instead of APPLE

  • Property mode set to 100644
File size: 10.6 KB
Line 
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"
22
23#ifdef HAVE_SOURCE_APPLE_AUDIO
24
25#include "aubio_priv.h"
26#include "fvec.h"
27#include "fmat.h"
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;
42  uint_t samplerate;          //< requested samplerate
43  uint_t source_samplerate;   //< actual source samplerate
44  uint_t block_size;
45
46  char_t *path;
47
48  ExtAudioFileRef audioFile;
49  AudioBufferList bufferList;
50};
51
52extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
53extern void freeAudioBufferList(AudioBufferList *bufferList);
54extern CFURLRef getURLFromPath(const char * path);
55char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
56
57uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path);
58
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
63  s->block_size = block_size;
64  s->samplerate = samplerate;
65
66  if ( aubio_source_apple_audio_open ( s, path ) ) {
67    goto beach;
68  }
69  return s;
70
71beach:
72  AUBIO_FREE(s);
73  return NULL;
74}
75
76uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path)
77{
78  OSStatus err = noErr;
79  UInt32 propSize;
80  s->path = path;
81
82  // open the resource url
83  CFURLRef fileURL = getURLFromPath(path);
84  err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
85  if (err == -43) {
86    AUBIO_ERR("source_apple_audio: Failed opening %s, "
87        "file not found, or no read access\n", s->path);
88    goto beach;
89  } else if (err) {
90    char_t errorstr[20];
91    AUBIO_ERR("source_apple_audio: Failed opening %s, "
92        "error in ExtAudioFileOpenURL (%s)\n", s->path,
93        getPrintableOSStatusError(errorstr, err));
94    goto beach;
95  }
96
97  // create an empty AudioStreamBasicDescription
98  AudioStreamBasicDescription fileFormat;
99  propSize = sizeof(fileFormat);
100  memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription));
101
102  // fill it with the file description
103  err = ExtAudioFileGetProperty(s->audioFile,
104      kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
105  if (err) {
106    char_t errorstr[20];
107    AUBIO_ERROR("source_apple_audio: Failed opening %s, "
108        "error in ExtAudioFileGetProperty (%s)\n", s->path,
109        getPrintableOSStatusError(errorstr, err));
110    goto beach;
111  }
112
113  if (s->samplerate == 0) {
114    s->samplerate = fileFormat.mSampleRate;
115    //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
116  }
117
118  s->source_samplerate = fileFormat.mSampleRate;
119  s->channels = fileFormat.mChannelsPerFrame;
120
121  AudioStreamBasicDescription clientFormat;
122  propSize = sizeof(clientFormat);
123  memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
124  clientFormat.mFormatID         = kAudioFormatLinearPCM;
125  clientFormat.mSampleRate       = (Float64)(s->samplerate);
126  clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
127  clientFormat.mChannelsPerFrame = s->channels;
128  clientFormat.mBitsPerChannel   = sizeof(short) * 8;
129  clientFormat.mFramesPerPacket  = 1;
130  clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
131  clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
132  clientFormat.mReserved         = 0;
133
134  // set the client format description
135  err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
136      propSize, &clientFormat);
137  if (err) {
138    char_t errorstr[20];
139    AUBIO_ERROR("source_apple_audio: Failed opening %s, "
140        "error in ExtAudioFileSetProperty (%s)\n", s->path,
141        getPrintableOSStatusError(errorstr, err));
142#if 1
143  // print client and format descriptions
144  AUBIO_DBG("Opened %s\n", s->path);
145  AUBIO_DBG("file/client Format.mFormatID:        : %3c%c%c%c / %c%c%c%c\n",
146      (int)RT_BYTE4(fileFormat.mFormatID),   (int)RT_BYTE3(fileFormat.mFormatID),   (int)RT_BYTE2(fileFormat.mFormatID),   (int)RT_BYTE1(fileFormat.mFormatID),
147      (int)RT_BYTE4(clientFormat.mFormatID), (int)RT_BYTE3(clientFormat.mFormatID), (int)RT_BYTE2(clientFormat.mFormatID), (int)RT_BYTE1(clientFormat.mFormatID)
148      );
149
150  AUBIO_DBG("file/client Format.mSampleRate       : %6.0f / %.0f\n",     fileFormat.mSampleRate      ,      clientFormat.mSampleRate);
151  AUBIO_DBG("file/client Format.mFormatFlags      : %6d / %d\n",    (int)fileFormat.mFormatFlags     , (int)clientFormat.mFormatFlags);
152  AUBIO_DBG("file/client Format.mChannelsPerFrame : %6d / %d\n",    (int)fileFormat.mChannelsPerFrame, (int)clientFormat.mChannelsPerFrame);
153  AUBIO_DBG("file/client Format.mBitsPerChannel   : %6d / %d\n",    (int)fileFormat.mBitsPerChannel  , (int)clientFormat.mBitsPerChannel);
154  AUBIO_DBG("file/client Format.mFramesPerPacket  : %6d / %d\n",    (int)fileFormat.mFramesPerPacket , (int)clientFormat.mFramesPerPacket);
155  AUBIO_DBG("file/client Format.mBytesPerFrame    : %6d / %d\n",    (int)fileFormat.mBytesPerFrame   , (int)clientFormat.mBytesPerFrame);
156  AUBIO_DBG("file/client Format.mBytesPerPacket   : %6d / %d\n",    (int)fileFormat.mBytesPerPacket  , (int)clientFormat.mBytesPerPacket);
157  AUBIO_DBG("file/client Format.mReserved         : %6d / %d\n",    (int)fileFormat.mReserved        , (int)clientFormat.mReserved);
158#endif
159      goto beach;
160  }
161
162  smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
163  if (ratio < 1.) {
164    AUBIO_WRN("source_apple_audio: up-sampling %s from %0dHz to %0dHz\n",
165        s->path, s->source_samplerate, s->samplerate);
166  }
167
168  // allocate the AudioBufferList
169  freeAudioBufferList(&s->bufferList);
170  if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
171    AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
172    goto beach;
173  }
174
175beach:
176  return err;
177}
178
179void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
180  UInt32 c, v, loadedPackets = s->block_size;
181  OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
182  if (err) {
183    char_t errorstr[20];
184    AUBIO_ERROR("source_apple_audio: error while reading %s "
185        "with ExtAudioFileRead (%s)\n", s->path,
186        getPrintableOSStatusError(errorstr, err));
187    goto beach;
188  }
189
190  short *data = (short*)s->bufferList.mBuffers[0].mData;
191
192  smpl_t *buf = read_to->data;
193
194  for (v = 0; v < loadedPackets; v++) {
195    buf[v] = 0.;
196    for (c = 0; c < s->channels; c++) {
197      buf[v] += SHORT_TO_FLOAT(data[ v * s->channels + c]);
198    }
199    buf[v] /= (smpl_t)s->channels;
200  }
201  // short read, fill with zeros
202  if (loadedPackets < s->block_size) {
203    for (v = loadedPackets; v < s->block_size; v++) {
204      buf[v] = 0.;
205    }
206  }
207
208  *read = (uint_t)loadedPackets;
209  return;
210beach:
211  *read = 0;
212  return;
213}
214
215void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
216  UInt32 c, v, loadedPackets = s->block_size;
217  OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
218  if (err) {
219    char_t errorstr[20];
220    AUBIO_ERROR("source_apple_audio: error while reading %s "
221        "with ExtAudioFileRead (%s)\n", s->path,
222        getPrintableOSStatusError(errorstr, err));
223    goto beach;
224  }
225
226  short *data = (short*)s->bufferList.mBuffers[0].mData;
227
228  smpl_t **buf = read_to->data;
229
230  for (v = 0; v < loadedPackets; v++) {
231    for (c = 0; c < read_to->height; c++) {
232      buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]);
233    }
234  }
235  // if read_data has more channels than the file
236  if (read_to->height > s->channels) {
237    // copy last channel to all additional channels
238    for (v = 0; v < loadedPackets; v++) {
239      for (c = s->channels; c < read_to->height; c++) {
240        buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + (s->channels - 1)]);
241      }
242    }
243  }
244  // short read, fill with zeros
245  if (loadedPackets < s->block_size) {
246    for (v = loadedPackets; v < s->block_size; v++) {
247      for (c = 0; c < read_to->height; c++) {
248        buf[c][v] = 0.;
249      }
250    }
251  }
252  *read = (uint_t)loadedPackets;
253  return;
254beach:
255  *read = 0;
256  return;
257}
258
259uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
260{
261  OSStatus err = noErr;
262  if (!s->audioFile) { return AUBIO_FAIL; }
263  err = ExtAudioFileDispose(s->audioFile);
264  s->audioFile = NULL;
265  if (err) {
266    char_t errorstr[20];
267    AUBIO_ERROR("source_apple_audio: error while closing %s "
268        "in ExtAudioFileDispose (%s)\n", s->path,
269        getPrintableOSStatusError(errorstr, err));
270    return err;
271  }
272  return AUBIO_OK;
273}
274
275void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
276  aubio_source_apple_audio_close (s);
277  freeAudioBufferList(&s->bufferList);
278  AUBIO_FREE(s);
279  return;
280}
281
282uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) {
283  // after a short read, the bufferList size needs to resetted to prepare for a full read
284  AudioBufferList *bufferList = &s->bufferList;
285  bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (short);
286  // compute position in the source file, before resampling
287  smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
288  SInt64 resampled_pos = (SInt64)ROUND( pos * ratio );
289  OSStatus err = ExtAudioFileSeek(s->audioFile, resampled_pos);
290  if (err) {
291    char_t errorstr[20];
292    AUBIO_ERROR("source_apple_audio: error while seeking %s at %d "
293        "in ExtAudioFileSeek (%s)\n", s->path, pos,
294        getPrintableOSStatusError(errorstr, err));
295  }
296  return err;
297}
298
299uint_t aubio_source_apple_audio_get_samplerate(aubio_source_apple_audio_t * s) {
300  return s->samplerate;
301}
302
303uint_t aubio_source_apple_audio_get_channels(aubio_source_apple_audio_t * s) {
304  return s->channels;
305}
306
307#endif /* HAVE_SOURCE_APPLE_AUDIO */
Note: See TracBrowser for help on using the repository browser.