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/>.
23 #include "aubio_priv.h"
25 #include "io/source_apple_audio.h"
27 // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
28 #include <AudioToolbox/AudioToolbox.h>
30 #define RT_BYTE1( a ) ( (a) & 0xff )
31 #define RT_BYTE2( a ) ( ((a) >> 8) & 0xff )
32 #define RT_BYTE3( a ) ( ((a) >> 16) & 0xff )
33 #define RT_BYTE4( a ) ( ((a) >> 24) & 0xff )
35 #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
37 struct _aubio_source_apple_audio_t {
44 ExtAudioFileRef audioFile;
45 AudioBufferList bufferList;
48 extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
49 extern void freeAudioBufferList(AudioBufferList *bufferList);
50 extern CFURLRef getURLFromPath(const char * path);
52 aubio_source_apple_audio_t * new_aubio_source_apple_audio(char_t * path, uint_t samplerate, uint_t block_size)
54 aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t);
57 s->block_size = block_size;
63 // open the resource url
64 CFURLRef fileURL = getURLFromPath(path);
65 err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
66 if (err) { AUBIO_ERR("error when trying to access %s, in ExtAudioFileOpenURL, %d\n", s->path, (int)err); goto beach;}
68 // create an empty AudioStreamBasicDescription
69 AudioStreamBasicDescription fileFormat;
70 propSize = sizeof(fileFormat);
71 memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription));
73 // fill it with the file description
74 err = ExtAudioFileGetProperty(s->audioFile,
75 kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
76 if (err) { AUBIO_ERROR("error in ExtAudioFileGetProperty, %d\n", (int)err); goto beach;}
78 if (samplerate == 0) {
79 samplerate = fileFormat.mSampleRate;
80 //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
82 s->samplerate = samplerate;
84 AudioStreamBasicDescription clientFormat;
85 propSize = sizeof(clientFormat);
86 memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
87 clientFormat.mFormatID = kAudioFormatLinearPCM;
88 clientFormat.mSampleRate = (Float64)(s->samplerate);
89 clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
90 clientFormat.mChannelsPerFrame = s->channels;
91 clientFormat.mBitsPerChannel = sizeof(short) * 8;
92 clientFormat.mFramesPerPacket = 1;
93 clientFormat.mBytesPerFrame = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
94 clientFormat.mBytesPerPacket = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
95 clientFormat.mReserved = 0;
97 // set the client format description
98 err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
99 propSize, &clientFormat);
100 if (err) { AUBIO_ERROR("error in ExtAudioFileSetProperty, %d\n", (int)err); goto beach;}
103 // print client and format descriptions
104 AUBIO_DBG("Opened %s\n", s->path);
105 AUBIO_DBG("file/client Format.mFormatID: : %3c%c%c%c / %c%c%c%c\n",
106 (int)RT_BYTE4(fileFormat.mFormatID), (int)RT_BYTE3(fileFormat.mFormatID), (int)RT_BYTE2(fileFormat.mFormatID), (int)RT_BYTE1(fileFormat.mFormatID),
107 (int)RT_BYTE4(clientFormat.mFormatID), (int)RT_BYTE3(clientFormat.mFormatID), (int)RT_BYTE2(clientFormat.mFormatID), (int)RT_BYTE1(clientFormat.mFormatID)
110 AUBIO_DBG("file/client Format.mSampleRate : %6.0f / %.0f\n", fileFormat.mSampleRate , clientFormat.mSampleRate);
111 AUBIO_DBG("file/client Format.mFormatFlags : %6d / %d\n", (int)fileFormat.mFormatFlags , (int)clientFormat.mFormatFlags);
112 AUBIO_DBG("file/client Format.mChannelsPerFrame : %6d / %d\n", (int)fileFormat.mChannelsPerFrame, (int)clientFormat.mChannelsPerFrame);
113 AUBIO_DBG("file/client Format.mBitsPerChannel : %6d / %d\n", (int)fileFormat.mBitsPerChannel , (int)clientFormat.mBitsPerChannel);
114 AUBIO_DBG("file/client Format.mFramesPerPacket : %6d / %d\n", (int)fileFormat.mFramesPerPacket , (int)clientFormat.mFramesPerPacket);
115 AUBIO_DBG("file/client Format.mBytesPerFrame : %6d / %d\n", (int)fileFormat.mBytesPerFrame , (int)clientFormat.mBytesPerFrame);
116 AUBIO_DBG("file/client Format.mBytesPerPacket : %6d / %d\n", (int)fileFormat.mBytesPerPacket , (int)clientFormat.mBytesPerPacket);
117 AUBIO_DBG("file/client Format.mReserved : %6d / %d\n", (int)fileFormat.mReserved , (int)clientFormat.mReserved);
120 // compute the size of the segments needed to read the input file
121 UInt32 samples = s->block_size * clientFormat.mChannelsPerFrame;
122 Float64 rateRatio = clientFormat.mSampleRate / fileFormat.mSampleRate;
123 uint_t segmentSize= (uint_t)(samples * rateRatio + .5);
124 if (rateRatio < 1.) {
125 segmentSize = (uint_t)(samples / rateRatio + .5);
126 } else if (rateRatio > 1.) {
127 AUBIO_WRN("up-sampling %s from %0.2fHz to %0.2fHz\n", s->path, fileFormat.mSampleRate, clientFormat.mSampleRate);
129 assert (segmentSize == samples );
130 //AUBIO_DBG("not resampling, segmentSize %d, block_size %d\n", segmentSize, s->block_size);
133 // allocate the AudioBufferList
134 if (createAubioBufferList(&s->bufferList, s->channels, segmentSize)) err = -1;
143 void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
144 UInt32 c, v, loadedPackets = s->block_size;
145 OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
146 if (err) { AUBIO_ERROR("error in ExtAudioFileRead, %d\n", (int)err); goto beach;}
148 smpl_t *buf = read_to->data;
150 short *data = (short*)s->bufferList.mBuffers[0].mData;
153 for (c = 0; c < s->channels; c++) {
154 for (v = 0; v < s->block_size; v++) {
155 if (v < loadedPackets) {
156 buf[v * s->channels + c] =
157 SHORT_TO_FLOAT(data[ v * s->channels + c]);
159 buf[v * s->channels + c] = 0.f;
164 //if (loadedPackets < s->block_size) return EOF;
165 *read = (uint_t)loadedPackets;
172 void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
173 OSStatus err = noErr;
174 if (!s || !s->audioFile) { return; }
175 err = ExtAudioFileDispose(s->audioFile);
176 if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err);
178 freeAudioBufferList(&s->bufferList);
183 uint_t aubio_source_apple_audio_get_samplerate(aubio_source_apple_audio_t * s) {
184 return s->samplerate;
187 #endif /* __APPLE__ */