[io] always call del_ when new_ fails
[aubio.git] / src / io / source_apple_audio.c
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 "aubio_priv.h"
22
23 #ifdef HAVE_SOURCE_APPLE_AUDIO
24
25 #include "fvec.h"
26 #include "fmat.h"
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
39 struct _aubio_source_apple_audio_t {
40   uint_t channels;
41   uint_t samplerate;          //< requested samplerate
42   uint_t source_samplerate;   //< actual source samplerate
43   uint_t block_size;
44
45   char_t *path;
46
47   ExtAudioFileRef audioFile;
48   AudioBufferList bufferList;
49 };
50
51 extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
52 extern void freeAudioBufferList(AudioBufferList *bufferList);
53 extern CFURLRef createURLFromPath(const char * path);
54 char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
55
56 uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path);
57
58 aubio_source_apple_audio_t * new_aubio_source_apple_audio(const char_t * path, uint_t samplerate, uint_t block_size)
59 {
60   aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
61
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
79   s->block_size = block_size;
80   s->samplerate = samplerate;
81
82   if ( aubio_source_apple_audio_open ( s, path ) ) {
83     goto beach;
84   }
85   return s;
86
87 beach:
88   del_aubio_source_apple_audio(s);
89   return NULL;
90 }
91
92 uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path)
93 {
94   OSStatus err = noErr;
95   UInt32 propSize;
96
97   if (s->path) AUBIO_FREE(s->path);
98   s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
99   strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
100
101   // open the resource url
102   CFURLRef fileURL = createURLFromPath(s->path);
103   err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
104   CFRelease(fileURL);
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   }
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);
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   }
132
133   if (s->samplerate == 0) {
134     s->samplerate = fileFormat.mSampleRate;
135     //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
136   }
137
138   s->source_samplerate = fileFormat.mSampleRate;
139   s->channels = fileFormat.mChannelsPerFrame;
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
154   // set the client format description
155   err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
156       propSize, &clientFormat);
157   if (err) {
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));
162 #if 0
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);
178 #endif
179       goto beach;
180   }
181
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);
186   }
187
188   // allocate the AudioBufferList
189   freeAudioBufferList(&s->bufferList);
190   if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
191     AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
192     goto beach;
193   }
194
195 beach:
196   return err;
197 }
198
199 void 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);
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   }
209
210   short *data = (short*)s->bufferList.mBuffers[0].mData;
211
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     }
226   }
227
228   *read = (uint_t)loadedPackets;
229   return;
230 beach:
231   *read = 0;
232   return;
233 }
234
235 void 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);
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   }
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++) {
251     for (c = 0; c < read_to->height; c++) {
252       buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]);
253     }
254   }
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   }
264   // short read, fill with zeros
265   if (loadedPackets < s->block_size) {
266     for (v = loadedPackets; v < s->block_size; v++) {
267       for (c = 0; c < read_to->height; c++) {
268         buf[c][v] = 0.;
269       }
270     }
271   }
272   *read = (uint_t)loadedPackets;
273   return;
274 beach:
275   *read = 0;
276   return;
277 }
278
279 uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
280 {
281   OSStatus err = noErr;
282   if (!s->audioFile) { return AUBIO_OK; }
283   err = ExtAudioFileDispose(s->audioFile);
284   s->audioFile = NULL;
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;
293 }
294
295 void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
296   aubio_source_apple_audio_close (s);
297   if (s->path) AUBIO_FREE(s->path);
298   freeAudioBufferList(&s->bufferList);
299   AUBIO_FREE(s);
300   return;
301 }
302
303 uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) {
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
313   uint_t fileLengthFrames = aubio_source_apple_audio_get_duration(s);
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 );
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);
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   }
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
350 beach:
351   return err;
352 }
353
354 uint_t aubio_source_apple_audio_get_samplerate(const aubio_source_apple_audio_t * s) {
355   return s->samplerate;
356 }
357
358 uint_t aubio_source_apple_audio_get_channels(const aubio_source_apple_audio_t * s) {
359   return s->channels;
360 }
361
362 uint_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
377 #endif /* HAVE_SOURCE_APPLE_AUDIO */