[3504dfe7] | 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 "config.h" |
---|
[331249b] | 22 | |
---|
| 23 | #ifdef HAVE_SOURCE_APPLE_AUDIO |
---|
| 24 | |
---|
[3504dfe7] | 25 | #include "aubio_priv.h" |
---|
| 26 | #include "fvec.h" |
---|
[4865e4b] | 27 | #include "fmat.h" |
---|
[3504dfe7] | 28 | #include "io/source_apple_audio.h" |
---|
| 29 | |
---|
| 30 | // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ... |
---|
| 31 | #include <AudioToolbox/AudioToolbox.h> |
---|
| 32 | |
---|
| 33 | #define RT_BYTE1( a ) ( (a) & 0xff ) |
---|
| 34 | #define RT_BYTE2( a ) ( ((a) >> 8) & 0xff ) |
---|
| 35 | #define RT_BYTE3( a ) ( ((a) >> 16) & 0xff ) |
---|
| 36 | #define RT_BYTE4( a ) ( ((a) >> 24) & 0xff ) |
---|
| 37 | |
---|
| 38 | #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05) |
---|
| 39 | |
---|
| 40 | struct _aubio_source_apple_audio_t { |
---|
| 41 | uint_t channels; |
---|
[7982203] | 42 | uint_t samplerate; //< requested samplerate |
---|
| 43 | uint_t source_samplerate; //< actual source samplerate |
---|
[3504dfe7] | 44 | uint_t block_size; |
---|
| 45 | |
---|
| 46 | char_t *path; |
---|
| 47 | |
---|
| 48 | ExtAudioFileRef audioFile; |
---|
| 49 | AudioBufferList bufferList; |
---|
| 50 | }; |
---|
| 51 | |
---|
[c833f56] | 52 | extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int max_source_samples); |
---|
[1223979] | 53 | extern void freeAudioBufferList(AudioBufferList *bufferList); |
---|
| 54 | extern CFURLRef getURLFromPath(const char * path); |
---|
[98a3887] | 55 | char_t *getPrintableOSStatusError(char_t *str, OSStatus error); |
---|
[3504dfe7] | 56 | |
---|
[6bbdcff] | 57 | uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path); |
---|
| 58 | |
---|
[3504dfe7] | 59 | aubio_source_apple_audio_t * new_aubio_source_apple_audio(char_t * path, uint_t samplerate, uint_t block_size) |
---|
| 60 | { |
---|
| 61 | aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t); |
---|
| 62 | |
---|
| 63 | s->block_size = block_size; |
---|
[6bbdcff] | 64 | s->samplerate = samplerate; |
---|
[3504dfe7] | 65 | |
---|
[6bbdcff] | 66 | if ( aubio_source_apple_audio_open ( s, path ) ) { |
---|
| 67 | goto beach; |
---|
| 68 | } |
---|
| 69 | return s; |
---|
| 70 | |
---|
| 71 | beach: |
---|
| 72 | AUBIO_FREE(s); |
---|
| 73 | return NULL; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path) |
---|
| 77 | { |
---|
[3504dfe7] | 78 | OSStatus err = noErr; |
---|
| 79 | UInt32 propSize; |
---|
[6bbdcff] | 80 | s->path = path; |
---|
[3504dfe7] | 81 | |
---|
| 82 | // open the resource url |
---|
| 83 | CFURLRef fileURL = getURLFromPath(path); |
---|
| 84 | err = ExtAudioFileOpenURL(fileURL, &s->audioFile); |
---|
[98a3887] | 85 | if (err == -43) { |
---|
| 86 | AUBIO_ERR("source_apple_audio: Failed opening %s, " |
---|
| 87 | "file not found, or no read access\n", s->path); |
---|
| 88 | goto beach; |
---|
| 89 | } else if (err) { |
---|
| 90 | char_t errorstr[20]; |
---|
| 91 | AUBIO_ERR("source_apple_audio: Failed opening %s, " |
---|
| 92 | "error in ExtAudioFileOpenURL (%s)\n", s->path, |
---|
| 93 | getPrintableOSStatusError(errorstr, err)); |
---|
| 94 | goto beach; |
---|
| 95 | } |
---|
[3504dfe7] | 96 | |
---|
| 97 | // create an empty AudioStreamBasicDescription |
---|
| 98 | AudioStreamBasicDescription fileFormat; |
---|
| 99 | propSize = sizeof(fileFormat); |
---|
| 100 | memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription)); |
---|
| 101 | |
---|
| 102 | // fill it with the file description |
---|
| 103 | err = ExtAudioFileGetProperty(s->audioFile, |
---|
| 104 | kExtAudioFileProperty_FileDataFormat, &propSize, &fileFormat); |
---|
[98a3887] | 105 | if (err) { |
---|
| 106 | char_t errorstr[20]; |
---|
| 107 | AUBIO_ERROR("source_apple_audio: Failed opening %s, " |
---|
| 108 | "error in ExtAudioFileGetProperty (%s)\n", s->path, |
---|
| 109 | getPrintableOSStatusError(errorstr, err)); |
---|
| 110 | goto beach; |
---|
| 111 | } |
---|
[3504dfe7] | 112 | |
---|
[6bbdcff] | 113 | if (s->samplerate == 0) { |
---|
| 114 | s->samplerate = fileFormat.mSampleRate; |
---|
[4db8eab] | 115 | //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate); |
---|
[944c7e1] | 116 | } |
---|
[6bbdcff] | 117 | |
---|
[7982203] | 118 | s->source_samplerate = fileFormat.mSampleRate; |
---|
[4865e4b] | 119 | s->channels = fileFormat.mChannelsPerFrame; |
---|
[944c7e1] | 120 | |
---|
| 121 | AudioStreamBasicDescription clientFormat; |
---|
| 122 | propSize = sizeof(clientFormat); |
---|
| 123 | memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription)); |
---|
| 124 | clientFormat.mFormatID = kAudioFormatLinearPCM; |
---|
| 125 | clientFormat.mSampleRate = (Float64)(s->samplerate); |
---|
| 126 | clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; |
---|
| 127 | clientFormat.mChannelsPerFrame = s->channels; |
---|
| 128 | clientFormat.mBitsPerChannel = sizeof(short) * 8; |
---|
| 129 | clientFormat.mFramesPerPacket = 1; |
---|
| 130 | clientFormat.mBytesPerFrame = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8; |
---|
| 131 | clientFormat.mBytesPerPacket = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame; |
---|
| 132 | clientFormat.mReserved = 0; |
---|
| 133 | |
---|
[3504dfe7] | 134 | // set the client format description |
---|
| 135 | err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat, |
---|
| 136 | propSize, &clientFormat); |
---|
[4865e4b] | 137 | if (err) { |
---|
[98a3887] | 138 | char_t errorstr[20]; |
---|
| 139 | AUBIO_ERROR("source_apple_audio: Failed opening %s, " |
---|
| 140 | "error in ExtAudioFileSetProperty (%s)\n", s->path, |
---|
| 141 | getPrintableOSStatusError(errorstr, err)); |
---|
[4865e4b] | 142 | #if 1 |
---|
[3504dfe7] | 143 | // print client and format descriptions |
---|
| 144 | AUBIO_DBG("Opened %s\n", s->path); |
---|
| 145 | AUBIO_DBG("file/client Format.mFormatID: : %3c%c%c%c / %c%c%c%c\n", |
---|
| 146 | (int)RT_BYTE4(fileFormat.mFormatID), (int)RT_BYTE3(fileFormat.mFormatID), (int)RT_BYTE2(fileFormat.mFormatID), (int)RT_BYTE1(fileFormat.mFormatID), |
---|
| 147 | (int)RT_BYTE4(clientFormat.mFormatID), (int)RT_BYTE3(clientFormat.mFormatID), (int)RT_BYTE2(clientFormat.mFormatID), (int)RT_BYTE1(clientFormat.mFormatID) |
---|
| 148 | ); |
---|
| 149 | |
---|
| 150 | AUBIO_DBG("file/client Format.mSampleRate : %6.0f / %.0f\n", fileFormat.mSampleRate , clientFormat.mSampleRate); |
---|
| 151 | AUBIO_DBG("file/client Format.mFormatFlags : %6d / %d\n", (int)fileFormat.mFormatFlags , (int)clientFormat.mFormatFlags); |
---|
| 152 | AUBIO_DBG("file/client Format.mChannelsPerFrame : %6d / %d\n", (int)fileFormat.mChannelsPerFrame, (int)clientFormat.mChannelsPerFrame); |
---|
| 153 | AUBIO_DBG("file/client Format.mBitsPerChannel : %6d / %d\n", (int)fileFormat.mBitsPerChannel , (int)clientFormat.mBitsPerChannel); |
---|
| 154 | AUBIO_DBG("file/client Format.mFramesPerPacket : %6d / %d\n", (int)fileFormat.mFramesPerPacket , (int)clientFormat.mFramesPerPacket); |
---|
| 155 | AUBIO_DBG("file/client Format.mBytesPerFrame : %6d / %d\n", (int)fileFormat.mBytesPerFrame , (int)clientFormat.mBytesPerFrame); |
---|
| 156 | AUBIO_DBG("file/client Format.mBytesPerPacket : %6d / %d\n", (int)fileFormat.mBytesPerPacket , (int)clientFormat.mBytesPerPacket); |
---|
| 157 | AUBIO_DBG("file/client Format.mReserved : %6d / %d\n", (int)fileFormat.mReserved , (int)clientFormat.mReserved); |
---|
[1223979] | 158 | #endif |
---|
[4865e4b] | 159 | goto beach; |
---|
| 160 | } |
---|
[3504dfe7] | 161 | |
---|
[c833f56] | 162 | smpl_t ratio = s->source_samplerate * 1. / s->samplerate; |
---|
| 163 | if (ratio < 1.) { |
---|
| 164 | AUBIO_WRN("source_apple_audio: up-sampling %s from %0dHz to %0dHz\n", |
---|
| 165 | s->path, s->source_samplerate, s->samplerate); |
---|
[3504dfe7] | 166 | } |
---|
| 167 | |
---|
| 168 | // allocate the AudioBufferList |
---|
[c833f56] | 169 | freeAudioBufferList(&s->bufferList); |
---|
| 170 | if (createAubioBufferList(&s->bufferList, s->channels, s->block_size * s->channels)) { |
---|
[6bbdcff] | 171 | AUBIO_ERR("source_apple_audio: failed creating bufferList\n"); |
---|
| 172 | goto beach; |
---|
| 173 | } |
---|
[3504dfe7] | 174 | |
---|
| 175 | beach: |
---|
[6bbdcff] | 176 | return err; |
---|
[3504dfe7] | 177 | } |
---|
| 178 | |
---|
| 179 | void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) { |
---|
| 180 | UInt32 c, v, loadedPackets = s->block_size; |
---|
| 181 | OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList); |
---|
[98a3887] | 182 | if (err) { |
---|
| 183 | char_t errorstr[20]; |
---|
| 184 | AUBIO_ERROR("source_apple_audio: error while reading %s " |
---|
| 185 | "with ExtAudioFileRead (%s)\n", s->path, |
---|
| 186 | getPrintableOSStatusError(errorstr, err)); |
---|
| 187 | goto beach; |
---|
| 188 | } |
---|
[3504dfe7] | 189 | |
---|
| 190 | short *data = (short*)s->bufferList.mBuffers[0].mData; |
---|
| 191 | |
---|
[8c43bf7] | 192 | smpl_t *buf = read_to->data; |
---|
| 193 | |
---|
| 194 | for (v = 0; v < loadedPackets; v++) { |
---|
| 195 | buf[v] = 0.; |
---|
| 196 | for (c = 0; c < s->channels; c++) { |
---|
| 197 | buf[v] += SHORT_TO_FLOAT(data[ v * s->channels + c]); |
---|
| 198 | } |
---|
| 199 | buf[v] /= (smpl_t)s->channels; |
---|
| 200 | } |
---|
| 201 | // short read, fill with zeros |
---|
| 202 | if (loadedPackets < s->block_size) { |
---|
| 203 | for (v = loadedPackets; v < s->block_size; v++) { |
---|
| 204 | buf[v] = 0.; |
---|
| 205 | } |
---|
[3504dfe7] | 206 | } |
---|
[8c43bf7] | 207 | |
---|
[3504dfe7] | 208 | *read = (uint_t)loadedPackets; |
---|
| 209 | return; |
---|
| 210 | beach: |
---|
| 211 | *read = 0; |
---|
| 212 | return; |
---|
| 213 | } |
---|
| 214 | |
---|
[4865e4b] | 215 | void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) { |
---|
| 216 | UInt32 c, v, loadedPackets = s->block_size; |
---|
| 217 | OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList); |
---|
[98a3887] | 218 | if (err) { |
---|
| 219 | char_t errorstr[20]; |
---|
| 220 | AUBIO_ERROR("source_apple_audio: error while reading %s " |
---|
| 221 | "with ExtAudioFileRead (%s)\n", s->path, |
---|
| 222 | getPrintableOSStatusError(errorstr, err)); |
---|
| 223 | goto beach; |
---|
| 224 | } |
---|
[4865e4b] | 225 | |
---|
| 226 | short *data = (short*)s->bufferList.mBuffers[0].mData; |
---|
| 227 | |
---|
| 228 | smpl_t **buf = read_to->data; |
---|
| 229 | |
---|
| 230 | for (v = 0; v < loadedPackets; v++) { |
---|
[af97786] | 231 | for (c = 0; c < read_to->height; c++) { |
---|
[4865e4b] | 232 | buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]); |
---|
| 233 | } |
---|
| 234 | } |
---|
[b8389de] | 235 | // if read_data has more channels than the file |
---|
| 236 | if (read_to->height > s->channels) { |
---|
| 237 | // copy last channel to all additional channels |
---|
| 238 | for (v = 0; v < loadedPackets; v++) { |
---|
| 239 | for (c = s->channels; c < read_to->height; c++) { |
---|
| 240 | buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + (s->channels - 1)]); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | } |
---|
[4865e4b] | 244 | // short read, fill with zeros |
---|
| 245 | if (loadedPackets < s->block_size) { |
---|
| 246 | for (v = loadedPackets; v < s->block_size; v++) { |
---|
[b8389de] | 247 | for (c = 0; c < read_to->height; c++) { |
---|
[4865e4b] | 248 | buf[c][v] = 0.; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | *read = (uint_t)loadedPackets; |
---|
| 253 | return; |
---|
| 254 | beach: |
---|
| 255 | *read = 0; |
---|
| 256 | return; |
---|
| 257 | } |
---|
| 258 | |
---|
[6bbdcff] | 259 | uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s) |
---|
| 260 | { |
---|
[3504dfe7] | 261 | OSStatus err = noErr; |
---|
[422452b] | 262 | if (!s->audioFile) { return AUBIO_FAIL; } |
---|
[3504dfe7] | 263 | err = ExtAudioFileDispose(s->audioFile); |
---|
| 264 | s->audioFile = NULL; |
---|
[98a3887] | 265 | if (err) { |
---|
| 266 | char_t errorstr[20]; |
---|
| 267 | AUBIO_ERROR("source_apple_audio: error while closing %s " |
---|
| 268 | "in ExtAudioFileDispose (%s)\n", s->path, |
---|
| 269 | getPrintableOSStatusError(errorstr, err)); |
---|
| 270 | return err; |
---|
| 271 | } |
---|
| 272 | return AUBIO_OK; |
---|
[6bbdcff] | 273 | } |
---|
| 274 | |
---|
| 275 | void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){ |
---|
| 276 | aubio_source_apple_audio_close (s); |
---|
[3504dfe7] | 277 | freeAudioBufferList(&s->bufferList); |
---|
| 278 | AUBIO_FREE(s); |
---|
| 279 | return; |
---|
| 280 | } |
---|
| 281 | |
---|
[7982203] | 282 | uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) { |
---|
[c833f56] | 283 | // after a short read, the bufferList size needs to resetted to prepare for a full read |
---|
| 284 | AudioBufferList *bufferList = &s->bufferList; |
---|
| 285 | bufferList->mBuffers[0].mDataByteSize = s->block_size * s->channels * sizeof (short); |
---|
| 286 | // compute position in the source file, before resampling |
---|
| 287 | smpl_t ratio = s->source_samplerate * 1. / s->samplerate; |
---|
| 288 | SInt64 resampled_pos = (SInt64)ROUND( pos * ratio ); |
---|
[4865e4b] | 289 | OSStatus err = ExtAudioFileSeek(s->audioFile, resampled_pos); |
---|
[98a3887] | 290 | if (err) { |
---|
| 291 | char_t errorstr[20]; |
---|
| 292 | AUBIO_ERROR("source_apple_audio: error while seeking %s at %d " |
---|
| 293 | "in ExtAudioFileSeek (%s)\n", s->path, pos, |
---|
| 294 | getPrintableOSStatusError(errorstr, err)); |
---|
| 295 | } |
---|
[7982203] | 296 | return err; |
---|
| 297 | } |
---|
| 298 | |
---|
[944c7e1] | 299 | uint_t aubio_source_apple_audio_get_samplerate(aubio_source_apple_audio_t * s) { |
---|
| 300 | return s->samplerate; |
---|
| 301 | } |
---|
| 302 | |
---|
[4865e4b] | 303 | uint_t aubio_source_apple_audio_get_channels(aubio_source_apple_audio_t * s) { |
---|
| 304 | return s->channels; |
---|
| 305 | } |
---|
| 306 | |
---|
[331249b] | 307 | #endif /* HAVE_SOURCE_APPLE_AUDIO */ |
---|