[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 | #ifdef __APPLE__ |
---|
| 22 | #include "config.h" |
---|
| 23 | #include "aubio_priv.h" |
---|
| 24 | #include "fvec.h" |
---|
[4865e4b] | 25 | #include "fmat.h" |
---|
[3504dfe7] | 26 | #include "io/source_apple_audio.h" |
---|
| 27 | |
---|
| 28 | // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ... |
---|
| 29 | #include <AudioToolbox/AudioToolbox.h> |
---|
| 30 | |
---|
| 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 ) |
---|
| 35 | |
---|
| 36 | #define SHORT_TO_FLOAT(x) (smpl_t)(x * 3.0517578125e-05) |
---|
| 37 | |
---|
| 38 | struct _aubio_source_apple_audio_t { |
---|
| 39 | uint_t channels; |
---|
[7982203] | 40 | uint_t samplerate; //< requested samplerate |
---|
| 41 | uint_t source_samplerate; //< actual source samplerate |
---|
[3504dfe7] | 42 | uint_t block_size; |
---|
| 43 | |
---|
| 44 | char_t *path; |
---|
| 45 | |
---|
| 46 | ExtAudioFileRef audioFile; |
---|
| 47 | AudioBufferList bufferList; |
---|
| 48 | }; |
---|
| 49 | |
---|
[1223979] | 50 | extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize); |
---|
| 51 | extern void freeAudioBufferList(AudioBufferList *bufferList); |
---|
| 52 | extern CFURLRef getURLFromPath(const char * path); |
---|
[3504dfe7] | 53 | |
---|
[6bbdcff] | 54 | uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path); |
---|
| 55 | |
---|
[3504dfe7] | 56 | aubio_source_apple_audio_t * new_aubio_source_apple_audio(char_t * path, uint_t samplerate, uint_t block_size) |
---|
| 57 | { |
---|
| 58 | aubio_source_apple_audio_t * s = AUBIO_NEW(aubio_source_apple_audio_t); |
---|
| 59 | |
---|
| 60 | s->block_size = block_size; |
---|
[6bbdcff] | 61 | s->samplerate = samplerate; |
---|
[3504dfe7] | 62 | |
---|
[6bbdcff] | 63 | if ( aubio_source_apple_audio_open ( s, path ) ) { |
---|
| 64 | goto beach; |
---|
| 65 | } |
---|
| 66 | return s; |
---|
| 67 | |
---|
| 68 | beach: |
---|
| 69 | AUBIO_FREE(s); |
---|
| 70 | return NULL; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | uint_t aubio_source_apple_audio_open (aubio_source_apple_audio_t *s, char_t * path) |
---|
| 74 | { |
---|
[3504dfe7] | 75 | OSStatus err = noErr; |
---|
| 76 | UInt32 propSize; |
---|
[6bbdcff] | 77 | s->path = path; |
---|
[3504dfe7] | 78 | |
---|
| 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;} |
---|
| 83 | |
---|
| 84 | // create an empty AudioStreamBasicDescription |
---|
| 85 | AudioStreamBasicDescription fileFormat; |
---|
| 86 | propSize = sizeof(fileFormat); |
---|
| 87 | memset(&fileFormat, 0, sizeof(AudioStreamBasicDescription)); |
---|
| 88 | |
---|
| 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;} |
---|
| 93 | |
---|
[6bbdcff] | 94 | if (s->samplerate == 0) { |
---|
| 95 | s->samplerate = fileFormat.mSampleRate; |
---|
[4db8eab] | 96 | //AUBIO_DBG("sampling rate set to 0, automagically adjusting to %d\n", samplerate); |
---|
[944c7e1] | 97 | } |
---|
[6bbdcff] | 98 | |
---|
[7982203] | 99 | s->source_samplerate = fileFormat.mSampleRate; |
---|
[4865e4b] | 100 | s->channels = fileFormat.mChannelsPerFrame; |
---|
[944c7e1] | 101 | |
---|
| 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; |
---|
| 114 | |
---|
[3504dfe7] | 115 | // set the client format description |
---|
| 116 | err = ExtAudioFileSetProperty(s->audioFile, kExtAudioFileProperty_ClientDataFormat, |
---|
| 117 | propSize, &clientFormat); |
---|
[4865e4b] | 118 | if (err) { |
---|
| 119 | AUBIO_ERROR("error in ExtAudioFileSetProperty, %d\n", (int)err); |
---|
| 120 | #if 1 |
---|
[3504dfe7] | 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) |
---|
| 126 | ); |
---|
| 127 | |
---|
| 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); |
---|
[1223979] | 136 | #endif |
---|
[4865e4b] | 137 | goto beach; |
---|
| 138 | } |
---|
[3504dfe7] | 139 | |
---|
| 140 | // compute the size of the segments needed to read the input file |
---|
[6bbdcff] | 141 | UInt32 samples = s->block_size * s->channels; |
---|
| 142 | Float64 rateRatio = s->samplerate / s->source_samplerate; |
---|
[3504dfe7] | 143 | uint_t segmentSize= (uint_t)(samples * rateRatio + .5); |
---|
| 144 | if (rateRatio < 1.) { |
---|
| 145 | segmentSize = (uint_t)(samples / rateRatio + .5); |
---|
| 146 | } else if (rateRatio > 1.) { |
---|
[6bbdcff] | 147 | AUBIO_WRN("up-sampling %s from %0dHz to %0dHz\n", s->path, s->source_samplerate, s->samplerate); |
---|
[3504dfe7] | 148 | } else { |
---|
[4865e4b] | 149 | assert ( segmentSize == samples ); |
---|
[3504dfe7] | 150 | //AUBIO_DBG("not resampling, segmentSize %d, block_size %d\n", segmentSize, s->block_size); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | // allocate the AudioBufferList |
---|
[6bbdcff] | 154 | if (createAubioBufferList(&s->bufferList, s->channels, segmentSize)) { |
---|
| 155 | AUBIO_ERR("source_apple_audio: failed creating bufferList\n"); |
---|
| 156 | goto beach; |
---|
| 157 | } |
---|
[3504dfe7] | 158 | |
---|
| 159 | beach: |
---|
[6bbdcff] | 160 | return err; |
---|
[3504dfe7] | 161 | } |
---|
| 162 | |
---|
| 163 | void aubio_source_apple_audio_do(aubio_source_apple_audio_t *s, fvec_t * read_to, uint_t * read) { |
---|
| 164 | UInt32 c, v, loadedPackets = s->block_size; |
---|
| 165 | OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList); |
---|
[7982203] | 166 | if (err) { AUBIO_ERROR("error in ExtAudioFileRead %s %d\n", s->path, (int)err); goto beach;} |
---|
[3504dfe7] | 167 | |
---|
| 168 | short *data = (short*)s->bufferList.mBuffers[0].mData; |
---|
| 169 | |
---|
[8c43bf7] | 170 | smpl_t *buf = read_to->data; |
---|
| 171 | |
---|
| 172 | for (v = 0; v < loadedPackets; v++) { |
---|
| 173 | buf[v] = 0.; |
---|
| 174 | for (c = 0; c < s->channels; c++) { |
---|
| 175 | buf[v] += SHORT_TO_FLOAT(data[ v * s->channels + c]); |
---|
| 176 | } |
---|
| 177 | buf[v] /= (smpl_t)s->channels; |
---|
| 178 | } |
---|
| 179 | // short read, fill with zeros |
---|
| 180 | if (loadedPackets < s->block_size) { |
---|
| 181 | for (v = loadedPackets; v < s->block_size; v++) { |
---|
| 182 | buf[v] = 0.; |
---|
| 183 | } |
---|
[3504dfe7] | 184 | } |
---|
[8c43bf7] | 185 | |
---|
[3504dfe7] | 186 | *read = (uint_t)loadedPackets; |
---|
| 187 | return; |
---|
| 188 | beach: |
---|
| 189 | *read = 0; |
---|
| 190 | return; |
---|
| 191 | } |
---|
| 192 | |
---|
[4865e4b] | 193 | void aubio_source_apple_audio_do_multi(aubio_source_apple_audio_t *s, fmat_t * read_to, uint_t * read) { |
---|
| 194 | UInt32 c, v, loadedPackets = s->block_size; |
---|
| 195 | OSStatus err = ExtAudioFileRead(s->audioFile, &loadedPackets, &s->bufferList); |
---|
| 196 | if (err) { AUBIO_ERROR("source_apple_audio: error in ExtAudioFileRead, %d\n", (int)err); goto beach;} |
---|
| 197 | |
---|
| 198 | short *data = (short*)s->bufferList.mBuffers[0].mData; |
---|
| 199 | |
---|
| 200 | smpl_t **buf = read_to->data; |
---|
| 201 | |
---|
| 202 | for (v = 0; v < loadedPackets; v++) { |
---|
| 203 | for (c = 0; c < s->channels; c++) { |
---|
| 204 | buf[c][v] = SHORT_TO_FLOAT(data[ v * s->channels + c]); |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | // short read, fill with zeros |
---|
| 208 | if (loadedPackets < s->block_size) { |
---|
| 209 | for (v = loadedPackets; v < s->block_size; v++) { |
---|
| 210 | for (c = 0; c < s->channels; c++) { |
---|
| 211 | buf[c][v] = 0.; |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | *read = (uint_t)loadedPackets; |
---|
| 216 | return; |
---|
| 217 | beach: |
---|
| 218 | *read = 0; |
---|
| 219 | return; |
---|
| 220 | } |
---|
| 221 | |
---|
[6bbdcff] | 222 | uint_t aubio_source_apple_audio_close (aubio_source_apple_audio_t *s) |
---|
| 223 | { |
---|
[3504dfe7] | 224 | OSStatus err = noErr; |
---|
[6bbdcff] | 225 | if (!s || !s->audioFile) { return 1; } |
---|
[3504dfe7] | 226 | err = ExtAudioFileDispose(s->audioFile); |
---|
| 227 | if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err); |
---|
| 228 | s->audioFile = NULL; |
---|
[6bbdcff] | 229 | return err; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | void del_aubio_source_apple_audio(aubio_source_apple_audio_t * s){ |
---|
| 233 | aubio_source_apple_audio_close (s); |
---|
[3504dfe7] | 234 | freeAudioBufferList(&s->bufferList); |
---|
| 235 | AUBIO_FREE(s); |
---|
| 236 | return; |
---|
| 237 | } |
---|
| 238 | |
---|
[7982203] | 239 | uint_t aubio_source_apple_audio_seek (aubio_source_apple_audio_t * s, uint_t pos) { |
---|
[987fb86] | 240 | if (1) { |
---|
| 241 | AudioBufferList *bufferList = &s->bufferList; |
---|
| 242 | UInt32 samples = s->block_size * s->channels; |
---|
| 243 | Float64 rateRatio = s->samplerate / s->source_samplerate; |
---|
| 244 | uint_t segmentSize= (uint_t)(samples * rateRatio + .5); |
---|
| 245 | bufferList->mBuffers[0].mDataByteSize = segmentSize * sizeof(short); |
---|
| 246 | } |
---|
[4865e4b] | 247 | SInt64 resampled_pos = (SInt64)ROUND( pos * s->source_samplerate * 1. / s->samplerate ); |
---|
| 248 | OSStatus err = ExtAudioFileSeek(s->audioFile, resampled_pos); |
---|
[7982203] | 249 | if (err) AUBIO_ERROR("source_apple_audio: error in ExtAudioFileSeek (%d)\n", (int)err); |
---|
| 250 | return err; |
---|
| 251 | } |
---|
| 252 | |
---|
[944c7e1] | 253 | uint_t aubio_source_apple_audio_get_samplerate(aubio_source_apple_audio_t * s) { |
---|
| 254 | return s->samplerate; |
---|
| 255 | } |
---|
| 256 | |
---|
[4865e4b] | 257 | uint_t aubio_source_apple_audio_get_channels(aubio_source_apple_audio_t * s) { |
---|
| 258 | return s->channels; |
---|
| 259 | } |
---|
| 260 | |
---|
[3504dfe7] | 261 | #endif /* __APPLE__ */ |
---|