2 Copyright (C) 2012 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 #include "aubio_priv.h"
23 #ifdef HAVE_SOURCE_APPLE_AUDIO
27 #include "io/source_apple_audio.h"
29 // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
30 #include <AudioToolbox/AudioToolbox.h>
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 )
37 struct _aubio_source_apple_audio_t {
39 uint_t samplerate; //< requested samplerate
40 uint_t source_samplerate; //< actual source samplerate
45 ExtAudioFileRef audioFile;
46 AudioBufferList bufferList;
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);
54 uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path);
56 aubio_source_apple_audio_t * new_aubio_source_apple_audio(const char_t * path, uint_t samplerate, uint_t block_size)
58 aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
60 if (path == NULL || strnlen(path, PATH_MAX) < 1) {
61 AUBIO_ERROR("source_apple_audio: Aborted opening null path\n");
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",
71 if ( (sint_t)samplerate < 0 ) {
72 AUBIO_ERROR("source_apple_audio: Can not open %s with negative samplerate %d\n",
77 s->block_size = block_size;
78 s->samplerate = samplerate;
80 if ( aubio_source_apple_audio_open ( s, path ) ) {
86 del_aubio_source_apple_audio(s);
90 uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path)
95 s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
96 strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
98 // open the resource url
99 CFURLRef fileURL = createURLFromPath(s->path);
100 err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
103 AUBIO_ERR("source_apple_audio: Failed opening %s, "
104 "file not found, or no read access\n", s->path);
108 AUBIO_ERR("source_apple_audio: Failed opening %s, "
109 "error in ExtAudioFileOpenURL (%s)\n", s->path,
110 getPrintableOSStatusError(errorstr, err));
114 // create an empty AudioStreamBasicDescription
115 AudioStreamBasicDescription fileFormat;
116 propSize = sizeof(fileFormat);
117 memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription));
119 // fill it with the file description
120 err = ExtAudioFileGetProperty(s->audioFile,
121 kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
124 AUBIO_ERROR("source_apple_audio: Failed opening %s, "
125 "error in ExtAudioFileGetProperty (%s)\n", s->path,
126 getPrintableOSStatusError(errorstr, err));
130 if (s->samplerate == 0) {
131 s->samplerate = fileFormat.mSampleRate;
132 //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
135 s->source_samplerate = fileFormat.mSampleRate;
136 s->channels = fileFormat.mChannelsPerFrame;
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;
150 // set the client format description
151 err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
152 propSize, &clientFormat);
155 AUBIO_ERROR("source_apple_audio: Failed opening %s, "
156 "error in ExtAudioFileSetProperty (%s)\n", s->path,
157 getPrintableOSStatusError(errorstr, err));
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)
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);
178 smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
180 AUBIO_WRN("source_apple_audio: up-sampling %s from %0dHz to %0dHz\n",
181 s->path, s->source_samplerate, s->samplerate);
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");
195 static UInt32 aubio_source_apple_audio_read_frame(aubio_source_apple_audio_t *s)
197 UInt32 loadedPackets = s->block_size;
198 OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
201 AUBIO_ERROR("source_apple_audio: error while reading %s "
202 "with ExtAudioFileRead (%s)\n", s->path,
203 getPrintableOSStatusError(errorstr, err));
205 return loadedPackets;
208 void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to,
211 UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
212 smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
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];
219 read_to->data[v] /= (smpl_t)s->channels;
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.;
228 *read = (uint_t)loadedPackets;
232 void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
234 UInt32 loadedPackets = aubio_source_apple_audio_read_frame(s);
235 smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData;
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];
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)];
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.;
260 *read = (uint_t)loadedPackets;
264 uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
266 OSStatus err = noErr;
267 if (!s->audioFile) { return AUBIO_OK; }
268 err = ExtAudioFileDispose(s->audioFile);
272 AUBIO_ERROR("source_apple_audio: error while closing %s "
273 "in ExtAudioFileDispose (%s)\n", s->path,
274 getPrintableOSStatusError(errorstr, err));
280 void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
282 aubio_source_apple_audio_close (s);
283 if (s->path) AUBIO_FREE(s->path);
284 freeAudioBufferList(&s->bufferList);
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",
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));
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);
316 AUBIO_ERROR("source_apple_audio: error while seeking %s at %d "
317 "in ExtAudioFileSeek (%s)\n", s->path, pos,
318 getPrintableOSStatusError(errorstr, err));
321 // check position after seek
323 SInt64 outFrameOffset = 0;
324 err = ExtAudioFileTell(s->audioFile, &outFrameOffset);
327 AUBIO_ERROR("source_apple_audio: error while seeking %s at %d "
328 "in ExtAudioFileTell (%s)\n", s->path, pos,
329 getPrintableOSStatusError(errorstr, err));
331 AUBIO_DBG("source_apple_audio: asked seek at %d, tell got %d\n",
332 pos, (uint_t)(outFrameOffset / ratio + .5));
339 uint_t aubio_source_apple_audio_get_samplerate(const aubio_source_apple_audio_t * s) {
340 return s->samplerate;
343 uint_t aubio_source_apple_audio_get_channels(const aubio_source_apple_audio_t * s) {
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);
354 AUBIO_ERROR("source_apple_audio: Failed getting %s duration, "
355 "error in ExtAudioFileGetProperty (%s)\n", s->path,
356 getPrintableOSStatusError(errorstr, err));
359 return (uint_t)fileLengthFrames;
362 #endif /* HAVE_SOURCE_APPLE_AUDIO */