[1223979] | 1 | /* |
---|
[222b176] | 2 | Copyright (C) 2012-2014 Paul Brossier <piem@aubio.org> |
---|
[1223979] | 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 | |
---|
[33d0242] | 21 | #include "aubio_priv.h" |
---|
[dd5a052] | 22 | |
---|
[9209c79] | 23 | #ifdef HAVE_SINK_APPLE_AUDIO |
---|
[1223979] | 24 | #include "fvec.h" |
---|
[222b176] | 25 | #include "fmat.h" |
---|
[1223979] | 26 | #include "io/sink_apple_audio.h" |
---|
[cf19b8a] | 27 | #include "io/ioutils.h" |
---|
[1223979] | 28 | |
---|
| 29 | // CFURLRef, CFURLCreateWithFileSystemPath, ... |
---|
| 30 | #include <CoreFoundation/CoreFoundation.h> |
---|
| 31 | // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ... |
---|
| 32 | #include <AudioToolbox/AudioToolbox.h> |
---|
| 33 | |
---|
| 34 | #define FLOAT_TO_SHORT(x) (short)(x * 32768) |
---|
| 35 | |
---|
| 36 | extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize); |
---|
| 37 | extern void freeAudioBufferList(AudioBufferList *bufferList); |
---|
[2da7526] | 38 | extern CFURLRef createURLFromPath(const char * path); |
---|
[98a3887] | 39 | char_t *getPrintableOSStatusError(char_t *str, OSStatus error); |
---|
[1223979] | 40 | |
---|
[222b176] | 41 | uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s); |
---|
| 42 | |
---|
[1223979] | 43 | #define MAX_SIZE 4096 // the maximum number of frames that can be written at a time |
---|
| 44 | |
---|
[9fa9b86] | 45 | void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write); |
---|
| 46 | |
---|
[8a7b344] | 47 | struct _aubio_sink_apple_audio_t { |
---|
[1223979] | 48 | uint_t samplerate; |
---|
| 49 | uint_t channels; |
---|
| 50 | char_t *path; |
---|
| 51 | |
---|
| 52 | uint_t max_frames; |
---|
| 53 | |
---|
| 54 | AudioBufferList bufferList; |
---|
| 55 | ExtAudioFileRef audioFile; |
---|
[4bc92c0] | 56 | bool async; |
---|
[1223979] | 57 | }; |
---|
| 58 | |
---|
[ae5d58a] | 59 | aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(const char_t * uri, uint_t samplerate) { |
---|
[1223979] | 60 | aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t); |
---|
| 61 | s->max_frames = MAX_SIZE; |
---|
[9fa9b86] | 62 | s->async = false; |
---|
[1223979] | 63 | |
---|
[31c306a] | 64 | if ( (uri == NULL) || (strlen(uri) < 1) ) { |
---|
[9657163] | 65 | AUBIO_ERROR("sink_apple_audio: Aborted opening null path\n"); |
---|
| 66 | goto beach; |
---|
| 67 | } |
---|
[b643a33] | 68 | if (s->path != NULL) AUBIO_FREE(s->path); |
---|
[d2be104] | 69 | s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1); |
---|
| 70 | strncpy(s->path, uri, strnlen(uri, PATH_MAX) + 1); |
---|
[9657163] | 71 | |
---|
[222b176] | 72 | s->samplerate = 0; |
---|
| 73 | s->channels = 0; |
---|
| 74 | |
---|
| 75 | // zero samplerate given. do not open yet |
---|
[cf19b8a] | 76 | if ((sint_t)samplerate == 0) { |
---|
| 77 | return s; |
---|
| 78 | } |
---|
| 79 | // invalid samplerate given, abort |
---|
| 80 | if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) { |
---|
| 81 | goto beach; |
---|
| 82 | } |
---|
[222b176] | 83 | |
---|
| 84 | s->samplerate = samplerate; |
---|
| 85 | s->channels = 1; |
---|
| 86 | |
---|
| 87 | if (aubio_sink_apple_audio_open(s) != AUBIO_OK) { |
---|
| 88 | // open failed, abort |
---|
| 89 | goto beach; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | return s; |
---|
| 93 | beach: |
---|
| 94 | AUBIO_FREE(s); |
---|
| 95 | return NULL; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate) |
---|
| 99 | { |
---|
[cf19b8a] | 100 | if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) { |
---|
| 101 | return AUBIO_FAIL; |
---|
| 102 | } |
---|
[222b176] | 103 | s->samplerate = samplerate; |
---|
| 104 | // automatically open when both samplerate and channels have been set |
---|
| 105 | if (s->samplerate != 0 && s->channels != 0) { |
---|
| 106 | return aubio_sink_apple_audio_open(s); |
---|
| 107 | } |
---|
| 108 | return AUBIO_OK; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels) |
---|
| 112 | { |
---|
[cf19b8a] | 113 | if (aubio_io_validate_channels("sink_apple_audio", s->path, channels)) { |
---|
| 114 | return AUBIO_FAIL; |
---|
| 115 | } |
---|
[222b176] | 116 | s->channels = channels; |
---|
| 117 | // automatically open when both samplerate and channels have been set |
---|
| 118 | if (s->samplerate != 0 && s->channels != 0) { |
---|
| 119 | return aubio_sink_apple_audio_open(s); |
---|
| 120 | } |
---|
| 121 | return AUBIO_OK; |
---|
| 122 | } |
---|
| 123 | |
---|
[ae5d58a] | 124 | uint_t aubio_sink_apple_audio_get_samplerate(const aubio_sink_apple_audio_t *s) |
---|
[222b176] | 125 | { |
---|
| 126 | return s->samplerate; |
---|
| 127 | } |
---|
| 128 | |
---|
[ae5d58a] | 129 | uint_t aubio_sink_apple_audio_get_channels(const aubio_sink_apple_audio_t *s) |
---|
[222b176] | 130 | { |
---|
| 131 | return s->channels; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) { |
---|
| 135 | |
---|
| 136 | if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL; |
---|
| 137 | |
---|
[1223979] | 138 | AudioStreamBasicDescription clientFormat; |
---|
| 139 | memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription)); |
---|
| 140 | clientFormat.mFormatID = kAudioFormatLinearPCM; |
---|
| 141 | clientFormat.mSampleRate = (Float64)(s->samplerate); |
---|
| 142 | clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; |
---|
| 143 | clientFormat.mChannelsPerFrame = s->channels; |
---|
| 144 | clientFormat.mBitsPerChannel = sizeof(short) * 8; |
---|
| 145 | clientFormat.mFramesPerPacket = 1; |
---|
| 146 | clientFormat.mBytesPerFrame = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8; |
---|
| 147 | clientFormat.mBytesPerPacket = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame; |
---|
| 148 | clientFormat.mReserved = 0; |
---|
| 149 | |
---|
| 150 | AudioFileTypeID fileType = kAudioFileWAVEType; |
---|
[2da7526] | 151 | CFURLRef fileURL = createURLFromPath(s->path); |
---|
[1223979] | 152 | bool overwrite = true; |
---|
| 153 | OSStatus err = noErr; |
---|
| 154 | err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL, |
---|
| 155 | overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile); |
---|
[2da7526] | 156 | CFRelease(fileURL); |
---|
[1223979] | 157 | if (err) { |
---|
[98a3887] | 158 | char_t errorstr[20]; |
---|
| 159 | AUBIO_ERR("sink_apple_audio: error when trying to create %s with " |
---|
| 160 | "ExtAudioFileCreateWithURL (%s)\n", s->path, |
---|
| 161 | getPrintableOSStatusError(errorstr, err)); |
---|
[1223979] | 162 | goto beach; |
---|
| 163 | } |
---|
| 164 | if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) { |
---|
[98a3887] | 165 | AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, " |
---|
| 166 | "out of memory? \n", s->path); |
---|
[1223979] | 167 | goto beach; |
---|
| 168 | } |
---|
[222b176] | 169 | return AUBIO_OK; |
---|
[1223979] | 170 | |
---|
| 171 | beach: |
---|
[222b176] | 172 | return AUBIO_FAIL; |
---|
[1223979] | 173 | } |
---|
| 174 | |
---|
| 175 | void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) { |
---|
| 176 | UInt32 c, v; |
---|
| 177 | short *data = (short*)s->bufferList.mBuffers[0].mData; |
---|
[8a7b344] | 178 | if (write > s->max_frames) { |
---|
| 179 | AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames); |
---|
[1223979] | 180 | write = s->max_frames; |
---|
| 181 | } |
---|
| 182 | smpl_t *buf = write_data->data; |
---|
| 183 | |
---|
| 184 | if (buf) { |
---|
| 185 | for (c = 0; c < s->channels; c++) { |
---|
| 186 | for (v = 0; v < write; v++) { |
---|
| 187 | data[v * s->channels + c] = |
---|
| 188 | FLOAT_TO_SHORT(buf[ v * s->channels + c]); |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | } |
---|
[9fa9b86] | 192 | aubio_sink_apple_audio_write(s, write); |
---|
[222b176] | 193 | } |
---|
| 194 | |
---|
| 195 | void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) { |
---|
| 196 | UInt32 c, v; |
---|
| 197 | short *data = (short*)s->bufferList.mBuffers[0].mData; |
---|
| 198 | if (write > s->max_frames) { |
---|
| 199 | AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames); |
---|
| 200 | write = s->max_frames; |
---|
| 201 | } |
---|
| 202 | smpl_t **buf = write_data->data; |
---|
| 203 | |
---|
| 204 | if (buf) { |
---|
| 205 | for (c = 0; c < s->channels; c++) { |
---|
| 206 | for (v = 0; v < write; v++) { |
---|
| 207 | data[v * s->channels + c] = |
---|
| 208 | FLOAT_TO_SHORT(buf[c][v]); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | } |
---|
[9fa9b86] | 212 | aubio_sink_apple_audio_write(s, write); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write) { |
---|
| 216 | OSStatus err = noErr; |
---|
[222b176] | 217 | if (s->async) { |
---|
| 218 | err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList); |
---|
| 219 | if (err) { |
---|
| 220 | char_t errorstr[20]; |
---|
[9fa9b86] | 221 | if (err == kExtAudioFileError_AsyncWriteBufferOverflow) { |
---|
| 222 | sprintf(errorstr,"buffer overflow"); |
---|
| 223 | } else if (err == kExtAudioFileError_AsyncWriteTooLarge) { |
---|
| 224 | sprintf(errorstr,"write too large"); |
---|
| 225 | } else { |
---|
| 226 | // unknown error |
---|
| 227 | getPrintableOSStatusError(errorstr, err); |
---|
| 228 | } |
---|
[222b176] | 229 | AUBIO_ERROR("sink_apple_audio: error while writing %s " |
---|
[9fa9b86] | 230 | "in ExtAudioFileWriteAsync (%s)\n", s->path, errorstr); |
---|
[4bc92c0] | 231 | } |
---|
[1223979] | 232 | } else { |
---|
| 233 | err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList); |
---|
[4bc92c0] | 234 | if (err) { |
---|
[98a3887] | 235 | char_t errorstr[20]; |
---|
| 236 | AUBIO_ERROR("sink_apple_audio: error while writing %s " |
---|
| 237 | "in ExtAudioFileWrite (%s)\n", s->path, |
---|
| 238 | getPrintableOSStatusError(errorstr, err)); |
---|
[4bc92c0] | 239 | } |
---|
[1223979] | 240 | } |
---|
| 241 | } |
---|
| 242 | |
---|
[a9fd272] | 243 | uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) { |
---|
[1223979] | 244 | OSStatus err = noErr; |
---|
[a9fd272] | 245 | if (!s->audioFile) { |
---|
| 246 | return AUBIO_FAIL; |
---|
[93e3463] | 247 | } |
---|
[1223979] | 248 | err = ExtAudioFileDispose(s->audioFile); |
---|
[98a3887] | 249 | if (err) { |
---|
| 250 | char_t errorstr[20]; |
---|
| 251 | AUBIO_ERROR("sink_apple_audio: error while closing %s " |
---|
| 252 | "in ExtAudioFileDispose (%s)\n", s->path, |
---|
| 253 | getPrintableOSStatusError(errorstr, err)); |
---|
| 254 | } |
---|
[1223979] | 255 | s->audioFile = NULL; |
---|
[a9fd272] | 256 | return err; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) { |
---|
| 260 | if (s->audioFile) aubio_sink_apple_audio_close (s); |
---|
[b643a33] | 261 | if (s->path) AUBIO_FREE(s->path); |
---|
[1223979] | 262 | freeAudioBufferList(&s->bufferList); |
---|
| 263 | AUBIO_FREE(s); |
---|
| 264 | return; |
---|
| 265 | } |
---|
[dd5a052] | 266 | |
---|
[9209c79] | 267 | #endif /* HAVE_SINK_APPLE_AUDIO */ |
---|