Merge branch 'master' into feature/pytest
[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 struct _aubio_source_apple_audio_t {
38   uint_t channels;
39   uint_t samplerate;          //< requested samplerate
40   uint_t source_samplerate;   //< actual source samplerate
41   uint_t block_size;
42
43   char_t *path;
44
45   ExtAudioFileRef audioFile;
46   AudioBufferList bufferList;
47 };
48
49 extern int createAudioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
50 extern void freeAudioBufferList(AudioBufferList *bufferList);
51 extern CFURLRef createURLFromPath(const char * path);
52 char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
53
54 uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path);
55
56 aubio_source_apple_audio_t * new_aubio_source_apple_audio(const char_t * path, uint_t samplerate, uint_t block_size)
57 {
58   aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
59
60   if (path == NULL || strnlen(path, PATH_MAX) < 1) {
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
77   s->block_size = block_size;
78   s->samplerate = samplerate;
79
80   if ( aubio_source_apple_audio_open ( s, path ) ) {
81     goto beach;
82   }
83   return s;
84
85 beach:
86   del_aubio_source_apple_audio(s);
87   return NULL;
88 }
89
90 uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path)
91 {
92   OSStatus err = noErr;
93   UInt32 propSize;
94
95   s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
96   strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
97
98   // open the resource url
99   CFURLRef fileURL = createURLFromPath(s->path);
100   err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
101   CFRelease(fileURL);
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   }
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);
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   }
129
130   if (s->samplerate == 0) {
131     s->samplerate = fileFormat.mSampleRate;
132     //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
133   }
134
135   s->source_samplerate = fileFormat.mSampleRate;
136   s->channels = fileFormat.mChannelsPerFrame;
137
138   AudioStreamBasicDescription clientFormat;
139   propSize = sizeof(AudioStreamBasicDescription);
140   memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
141   clientFormat.mFormatID         = kAudioFormatLinearPCM;
142   clientFormat.mSampleRate       = (Float64)(s->samplerate);
143   clientFormat.mFormatFlags      = kAudioFormatFlagIsFloat;
144   clientFormat.mChannelsPerFrame = s->channels;
145   clientFormat.mBitsPerChannel   = sizeof(smpl_t) * 8;
146   clientFormat.mFramesPerPacket  = 1;
147   clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
148   clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
149
150   // set the client format description
151   err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
152       propSize, &clientFormat);
153   if (err) {
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));
158 #if 0
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);
174 #endif
175       goto beach;
176   }
177
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);
182   }
183
184   // allocate the AudioBufferList
185   freeAudioBufferList(&s->bufferList);
186   if (createAudioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
187     AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
188     goto beach;
189   }
190
191 beach:
192   return err;
193 }
194
195 static UInt32 aubio_source_apple_audio_read_frame(aubio_source_apple_audio_t *s)
196 {
197   UInt32 loadedPackets = s->block_size;
198   OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
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   }
205   return loadedPackets;
206 }
207
208 void 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;
213
214   for (v = 0; v < loadedPackets; v++) {
215     read_to->data[v] = 0.;
216     for (c = 0; c < s->channels; c++) {
217       read_to->data[v] += data[ v * s->channels + c];
218     }
219     read_to->data[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       read_to->data[v] = 0.;
225     }
226   }
227
228   *read = (uint_t)loadedPackets;
229   return;
230 }
231
232 void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
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;
236
237   for (v = 0; v < loadedPackets; v++) {
238     for (c = 0; c < read_to->height; c++) {
239       read_to->data[c][v] = data[ v * s->channels + c];
240     }
241   }
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++) {
247         read_to->data[c][v] = data[ v * s->channels + (s->channels - 1)];
248       }
249     }
250   }
251   // short read, fill with zeros
252   if (loadedPackets < s->block_size) {
253     for (v = loadedPackets; v < s->block_size; v++) {
254       for (c = 0; c < read_to->height; c++) {
255         read_to->data[c][v] = 0.;
256       }
257     }
258   }
259
260   *read = (uint_t)loadedPackets;
261   return;
262 }
263
264 uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
265 {
266   OSStatus err = noErr;
267   if (!s->audioFile) { return AUBIO_OK; }
268   err = ExtAudioFileDispose(s->audioFile);
269   s->audioFile = NULL;
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;
278 }
279
280 void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
281   AUBIO_ASSERT(s);
282   aubio_source_apple_audio_close (s);
283   if (s->path) AUBIO_FREE(s->path);
284   freeAudioBufferList(&s->bufferList);
285   AUBIO_FREE(s);
286 }
287
288 uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) {
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
298   uint_t fileLengthFrames = aubio_source_apple_audio_get_duration(s);
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 );
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;
311   bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (smpl_t);
312   // do the actual seek
313   err = ExtAudioFileSeek(s->audioFile, resampled_pos);
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   }
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
335 beach:
336   return err;
337 }
338
339 uint_t aubio_source_apple_audio_get_samplerate(const aubio_source_apple_audio_t * s) {
340   return s->samplerate;
341 }
342
343 uint_t aubio_source_apple_audio_get_channels(const aubio_source_apple_audio_t * s) {
344   return s->channels;
345 }
346
347 uint_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
362 #endif /* HAVE_SOURCE_APPLE_AUDIO */