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 #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
39 struct _aubio_source_apple_audio_t {
41 uint_t samplerate; //< requested samplerate
42 uint_t source_samplerate; //< actual source samplerate
47 ExtAudioFileRef audioFile;
48 AudioBufferList bufferList;
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);
56 uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path);
58 aubio_source_apple_audio_t * new_aubio_source_apple_audio(const char_t * path, uint_t samplerate, uint_t block_size)
60 aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
63 AUBIO_ERROR("source_apple_audio: Aborted opening null path\n");
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",
73 if ( (sint_t)samplerate < 0 ) {
74 AUBIO_ERROR("source_apple_audio: Can not open %s with negative samplerate %d\n",
79 s->block_size = block_size;
80 s->samplerate = samplerate;
82 if ( aubio_source_apple_audio_open ( s, path ) ) {
92 uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, const char_t * path)
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);
101 // open the resource url
102 CFURLRef fileURL = createURLFromPath(s->path);
103 err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
106 AUBIO_ERR("source_apple_audio: Failed opening %s, "
107 "file not found, or no read access\n", s->path);
111 AUBIO_ERR("source_apple_audio: Failed opening %s, "
112 "error in ExtAudioFileOpenURL (%s)\n", s->path,
113 getPrintableOSStatusError(errorstr, err));
117 // create an empty AudioStreamBasicDescription
118 AudioStreamBasicDescription fileFormat;
119 propSize = sizeof(fileFormat);
120 memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription));
122 // fill it with the file description
123 err = ExtAudioFileGetProperty(s->audioFile,
124 kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
127 AUBIO_ERROR("source_apple_audio: Failed opening %s, "
128 "error in ExtAudioFileGetProperty (%s)\n", s->path,
129 getPrintableOSStatusError(errorstr, err));
133 if (s->samplerate == 0) {
134 s->samplerate = fileFormat.mSampleRate;
135 //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
138 s->source_samplerate = fileFormat.mSampleRate;
139 s->channels = fileFormat.mChannelsPerFrame;
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;
154 // set the client format description
155 err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
156 propSize, &clientFormat);
159 AUBIO_ERROR("source_apple_audio: Failed opening %s, "
160 "error in ExtAudioFileSetProperty (%s)\n", s->path,
161 getPrintableOSStatusError(errorstr, err));
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)
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);
182 smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
184 AUBIO_WRN("source_apple_audio: up-sampling %s from %0dHz to %0dHz\n",
185 s->path, s->source_samplerate, s->samplerate);
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");
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);
204 AUBIO_ERROR("source_apple_audio: error while reading %s "
205 "with ExtAudioFileRead (%s)\n", s->path,
206 getPrintableOSStatusError(errorstr, err));
210 short *data = (short*)s->bufferList.mBuffers[0].mData;
212 smpl_t *buf = read_to->data;
214 for (v = 0; v < loadedPackets; v++) {
216 for (c = 0; c < s->channels; c++) {
217 buf[v] += SHORT_TO_FLOAT(data[ v * s->channels + c]);
219 buf[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++) {
228 *read = (uint_t)loadedPackets;
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);
240 AUBIO_ERROR("source_apple_audio: error while reading %s "
241 "with ExtAudioFileRead (%s)\n", s->path,
242 getPrintableOSStatusError(errorstr, err));
246 short *data = (short*)s->bufferList.mBuffers[0].mData;
248 smpl_t **buf = read_to->data;
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]);
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)]);
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++) {
272 *read = (uint_t)loadedPackets;
279 uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
281 OSStatus err = noErr;
282 if (!s->audioFile) { return AUBIO_OK; }
283 err = ExtAudioFileDispose(s->audioFile);
287 AUBIO_ERROR("source_apple_audio: error while closing %s "
288 "in ExtAudioFileDispose (%s)\n", s->path,
289 getPrintableOSStatusError(errorstr, err));
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);
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",
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));
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);
331 AUBIO_ERROR("source_apple_audio: error while seeking %s at %d "
332 "in ExtAudioFileSeek (%s)\n", s->path, pos,
333 getPrintableOSStatusError(errorstr, err));
336 // check position after seek
338 SInt64 outFrameOffset = 0;
339 err = ExtAudioFileTell(s->audioFile, &outFrameOffset);
342 AUBIO_ERROR("source_apple_audio: error while seeking %s at %d "
343 "in ExtAudioFileTell (%s)\n", s->path, pos,
344 getPrintableOSStatusError(errorstr, err));
346 AUBIO_DBG("source_apple_audio: asked seek at %d, tell got %d\n",
347 pos, (uint_t)(outFrameOffset / ratio + .5));
354 uint_t aubio_source_apple_audio_get_samplerate(const aubio_source_apple_audio_t * s) {
355 return s->samplerate;
358 uint_t aubio_source_apple_audio_get_channels(const aubio_source_apple_audio_t * s) {
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);
369 AUBIO_ERROR("source_apple_audio: Failed getting %s duration, "
370 "error in ExtAudioFileGetProperty (%s)\n", s->path,
371 getPrintableOSStatusError(errorstr, err));
374 return (uint_t)fileLengthFrames;
377 #endif /* HAVE_SOURCE_APPLE_AUDIO */