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"
26 #include "io/source_apple_audio.h"
28 // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
29 #include <AudioToolbox/AudioToolbox.h>
31 #define RT_BYTE1( a ) ( (a) & 0xff )
32 #define RT_BYTE2( a ) ( ((a) >> 8) & 0xff )
33 #define RT_BYTE3( a ) ( ((a) >> 16) & 0xff )
34 #define RT_BYTE4( a ) ( ((a) >> 24) & 0xff )
36 #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05)
38 struct _aubio_source_apple_audio_t {
40 uint_t samplerate; //< requested samplerate
41 uint_t source_samplerate; //< actual source samplerate
46 ExtAudioFileRef audioFile;
47 AudioBufferList bufferList;
50 extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples);
51 extern void freeAudioBufferList(AudioBufferList *bufferList);
52 extern CFURLRef getURLFromPath(const char * path);
54 uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path);
56 aubio_source_apple_audio_t * new_aubio_source_apple_audio(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 s->block_size = block_size;
61 s->samplerate = samplerate;
63 if ( aubio_source_apple_audio_open ( s, path ) ) {
73 uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path)
79 // open the resource url
80 CFURLRef fileURL = getURLFromPath(path);
81 err = ExtAudioFileOpenURL(fileURL, &s->audioFile);
82 if (err) { AUBIO_ERR("error when trying to access %s, in ExtAudioFileOpenURL, %d\n", s->path, (int)err); goto beach;}
84 // create an empty AudioStreamBasicDescription
85 AudioStreamBasicDescription fileFormat;
86 propSize = sizeof(fileFormat);
87 memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription));
89 // fill it with the file description
90 err = ExtAudioFileGetProperty(s->audioFile,
91 kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat);
92 if (err) { AUBIO_ERROR("error in ExtAudioFileGetProperty, %d\n", (int)err); goto beach;}
94 if (s->samplerate == 0) {
95 s->samplerate = fileFormat.mSampleRate;
96 //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate);
99 s->source_samplerate = fileFormat.mSampleRate;
100 s->channels = fileFormat.mChannelsPerFrame;
102 AudioStreamBasicDescription clientFormat;
103 propSize = sizeof(clientFormat);
104 memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
105 clientFormat.mFormatID = kAudioFormatLinearPCM;
106 clientFormat.mSampleRate = (Float64)(s->samplerate);
107 clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
108 clientFormat.mChannelsPerFrame = s->channels;
109 clientFormat.mBitsPerChannel = sizeof(short) * 8;
110 clientFormat.mFramesPerPacket = 1;
111 clientFormat.mBytesPerFrame = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
112 clientFormat.mBytesPerPacket = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
113 clientFormat.mReserved = 0;
115 // set the client format description
116 err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat,
117 propSize, &clientFormat);
119 AUBIO_ERROR("error in ExtAudioFileSetProperty, %d\n", (int)err);
121 // print client and format descriptions
122 AUBIO_DBG("Opened %s\n", s->path);
123 AUBIO_DBG("file/client Format.mFormatID: : %3c%c%c%c / %c%c%c%c\n",
124 (int)RT_BYTE4(fileFormat.mFormatID), (int)RT_BYTE3(fileFormat.mFormatID), (int)RT_BYTE2(fileFormat.mFormatID), (int)RT_BYTE1(fileFormat.mFormatID),
125 (int)RT_BYTE4(clientFormat.mFormatID), (int)RT_BYTE3(clientFormat.mFormatID), (int)RT_BYTE2(clientFormat.mFormatID), (int)RT_BYTE1(clientFormat.mFormatID)
128 AUBIO_DBG("file/client Format.mSampleRate : %6.0f / %.0f\n", fileFormat.mSampleRate , clientFormat.mSampleRate);
129 AUBIO_DBG("file/client Format.mFormatFlags : %6d / %d\n", (int)fileFormat.mFormatFlags , (int)clientFormat.mFormatFlags);
130 AUBIO_DBG("file/client Format.mChannelsPerFrame : %6d / %d\n", (int)fileFormat.mChannelsPerFrame, (int)clientFormat.mChannelsPerFrame);
131 AUBIO_DBG("file/client Format.mBitsPerChannel : %6d / %d\n", (int)fileFormat.mBitsPerChannel , (int)clientFormat.mBitsPerChannel);
132 AUBIO_DBG("file/client Format.mFramesPerPacket : %6d / %d\n", (int)fileFormat.mFramesPerPacket , (int)clientFormat.mFramesPerPacket);
133 AUBIO_DBG("file/client Format.mBytesPerFrame : %6d / %d\n", (int)fileFormat.mBytesPerFrame , (int)clientFormat.mBytesPerFrame);
134 AUBIO_DBG("file/client Format.mBytesPerPacket : %6d / %d\n", (int)fileFormat.mBytesPerPacket , (int)clientFormat.mBytesPerPacket);
135 AUBIO_DBG("file/client Format.mReserved : %6d / %d\n", (int)fileFormat.mReserved , (int)clientFormat.mReserved);
140 smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
142 AUBIO_WRN("source_apple_audio: up-sampling %s from %0dHz to %0dHz\n",
143 s->path, s->source_samplerate, s->samplerate);
146 // allocate the AudioBufferList
147 freeAudioBufferList(&s->bufferList);
148 if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) {
149 AUBIO_ERR("source_apple_audio: failed creating bufferList\n");
157 void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) {
158 UInt32 c, v, loadedPackets = s->block_size;
159 OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
160 if (err) { AUBIO_ERROR("error in ExtAudioFileRead %s %d\n", s->path, (int)err); goto beach;}
162 short *data = (short*)s->bufferList.mBuffers[0].mData;
164 smpl_t *buf = read_to->data;
166 for (v = 0; v < loadedPackets; v++) {
168 for (c = 0; c < s->channels; c++) {
169 buf[v] += SHORT_TO_FLOAT(data[ v * s->channels + c]);
171 buf[v] /= (smpl_t)s->channels;
173 // short read, fill with zeros
174 if (loadedPackets < s->block_size) {
175 for (v = loadedPackets; v < s->block_size; v++) {
180 *read = (uint_t)loadedPackets;
187 void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) {
188 UInt32 c, v, loadedPackets = s->block_size;
189 OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList);
190 if (err) { AUBIO_ERROR("source_apple_audio: error in ExtAudioFileRead, %d\n", (int)err); goto beach;}
192 short *data = (short*)s->bufferList.mBuffers[0].mData;
194 smpl_t **buf = read_to->data;
196 for (v = 0; v < loadedPackets; v++) {
197 for (c = 0; c < s->channels; c++) {
198 buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]);
201 // if read_data has more channels than the file
202 if (read_to->height > s->channels) {
203 // copy last channel to all additional channels
204 for (v = 0; v < loadedPackets; v++) {
205 for (c = s->channels; c < read_to->height; c++) {
206 buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + (s->channels - 1)]);
210 // short read, fill with zeros
211 if (loadedPackets < s->block_size) {
212 for (v = loadedPackets; v < s->block_size; v++) {
213 for (c = 0; c < read_to->height; c++) {
218 *read = (uint_t)loadedPackets;
225 uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s)
227 OSStatus err = noErr;
228 if (!s || !s->audioFile) { return 1; }
229 err = ExtAudioFileDispose(s->audioFile);
230 if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err);
235 void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){
236 aubio_source_apple_audio_close (s);
237 freeAudioBufferList(&s->bufferList);
242 uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) {
243 // after a short read, the bufferList size needs to resetted to prepare for a full read
244 AudioBufferList *bufferList = &s->bufferList;
245 bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (short);
246 // compute position in the source file, before resampling
247 smpl_t ratio = s->source_samplerate * 1. / s->samplerate;
248 SInt64 resampled_pos = (SInt64)ROUND( pos * ratio );
249 OSStatus err = ExtAudioFileSeek(s->audioFile, resampled_pos);
250 if (err) AUBIO_ERROR("source_apple_audio: error in ExtAudioFileSeek (%d)\n", (int)err);
254 uint_t aubio_source_apple_audio_get_samplerate(aubio_source_apple_audio_t * s) {
255 return s->samplerate;
258 uint_t aubio_source_apple_audio_get_channels(aubio_source_apple_audio_t * s) {
262 #endif /* __APPLE__ */