2 Copyright (C) 2012-2014 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 #ifdef HAVE_SINK_APPLE_AUDIO
25 #include "aubio_priv.h"
28 #include "io/sink_apple_audio.h"
30 // CFURLRef, CFURLCreateWithFileSystemPath, ...
31 #include <CoreFoundation/CoreFoundation.h>
32 // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
33 #include <AudioToolbox/AudioToolbox.h>
35 #define FLOAT_TO_SHORT(x) (short)(x * 32768)
37 extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
38 extern void freeAudioBufferList(AudioBufferList *bufferList);
39 extern CFURLRef createURLFromPath(const char * path);
40 char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
42 uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s);
44 #define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
46 struct _aubio_sink_apple_audio_t {
53 AudioBufferList bufferList;
54 ExtAudioFileRef audioFile;
58 aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate) {
59 aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t);
61 s->max_frames = MAX_SIZE;
65 AUBIO_ERROR("sink_apple_audio: Aborted opening null path\n");
72 // negative samplerate given, abort
73 if ((sint_t)samplerate < 0) goto beach;
74 // zero samplerate given. do not open yet
75 if ((sint_t)samplerate == 0) return s;
77 s->samplerate = samplerate;
80 if (aubio_sink_apple_audio_open(s) != AUBIO_OK) {
91 uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate)
93 if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
94 s->samplerate = samplerate;
95 // automatically open when both samplerate and channels have been set
96 if (s->samplerate != 0 && s->channels != 0) {
97 return aubio_sink_apple_audio_open(s);
102 uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels)
104 if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
105 s->channels = channels;
106 // automatically open when both samplerate and channels have been set
107 if (s->samplerate != 0 && s->channels != 0) {
108 return aubio_sink_apple_audio_open(s);
113 uint_t aubio_sink_apple_audio_get_samplerate(aubio_sink_apple_audio_t *s)
115 return s->samplerate;
118 uint_t aubio_sink_apple_audio_get_channels(aubio_sink_apple_audio_t *s)
123 uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) {
125 if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL;
127 AudioStreamBasicDescription clientFormat;
128 memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
129 clientFormat.mFormatID = kAudioFormatLinearPCM;
130 clientFormat.mSampleRate = (Float64)(s->samplerate);
131 clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
132 clientFormat.mChannelsPerFrame = s->channels;
133 clientFormat.mBitsPerChannel = sizeof(short) * 8;
134 clientFormat.mFramesPerPacket = 1;
135 clientFormat.mBytesPerFrame = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
136 clientFormat.mBytesPerPacket = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
137 clientFormat.mReserved = 0;
139 AudioFileTypeID fileType = kAudioFileWAVEType;
140 CFURLRef fileURL = createURLFromPath(s->path);
141 bool overwrite = true;
142 OSStatus err = noErr;
143 err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
144 overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
148 AUBIO_ERR("sink_apple_audio: error when trying to create %s with "
149 "ExtAudioFileCreateWithURL (%s)\n", s->path,
150 getPrintableOSStatusError(errorstr, err));
153 if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
154 AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
155 "out of memory? \n", s->path);
164 void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
165 OSStatus err = noErr;
167 short *data = (short*)s->bufferList.mBuffers[0].mData;
168 if (write > s->max_frames) {
169 AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
170 write = s->max_frames;
172 smpl_t *buf = write_data->data;
175 for (c = 0; c < s->channels; c++) {
176 for (v = 0; v < write; v++) {
177 data[v * s->channels + c] =
178 FLOAT_TO_SHORT(buf[ v * s->channels + c]);
183 err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
187 AUBIO_ERROR("sink_apple_audio: error while writing %s "
188 "in ExtAudioFileWriteAsync (%s), switching to sync\n", s->path,
189 getPrintableOSStatusError(errorstr, err));
196 err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
200 AUBIO_ERROR("sink_apple_audio: error while writing %s "
201 "in ExtAudioFileWrite (%s)\n", s->path,
202 getPrintableOSStatusError(errorstr, err));
208 void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) {
209 OSStatus err = noErr;
211 short *data = (short*)s->bufferList.mBuffers[0].mData;
212 if (write > s->max_frames) {
213 AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
214 write = s->max_frames;
216 smpl_t **buf = write_data->data;
219 for (c = 0; c < s->channels; c++) {
220 for (v = 0; v < write; v++) {
221 data[v * s->channels + c] =
222 FLOAT_TO_SHORT(buf[c][v]);
227 err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
231 AUBIO_ERROR("sink_apple_audio: error while writing %s "
232 "in ExtAudioFileWriteAsync (%s), switching to sync\n", s->path,
233 getPrintableOSStatusError(errorstr, err));
240 err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
244 AUBIO_ERROR("sink_apple_audio: error while writing %s "
245 "in ExtAudioFileWrite (%s)\n", s->path,
246 getPrintableOSStatusError(errorstr, err));
252 uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) {
253 OSStatus err = noErr;
257 err = ExtAudioFileDispose(s->audioFile);
260 AUBIO_ERROR("sink_apple_audio: error while closing %s "
261 "in ExtAudioFileDispose (%s)\n", s->path,
262 getPrintableOSStatusError(errorstr, err));
268 void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
269 if (s->audioFile) aubio_sink_apple_audio_close (s);
270 freeAudioBufferList(&s->bufferList);
275 #endif /* HAVE_SINK_APPLE_AUDIO */