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 getURLFromPath(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;
67 // negative samplerate given, abort
68 if ((sint_t)samplerate < 0) goto beach;
69 // zero samplerate given. do not open yet
70 if ((sint_t)samplerate == 0) return s;
72 s->samplerate = samplerate;
75 if (aubio_sink_apple_audio_open(s) != AUBIO_OK) {
86 uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate)
88 if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
89 s->samplerate = samplerate;
90 // automatically open when both samplerate and channels have been set
91 if (s->samplerate != 0 && s->channels != 0) {
92 return aubio_sink_apple_audio_open(s);
97 uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels)
99 if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
100 s->channels = channels;
101 // automatically open when both samplerate and channels have been set
102 if (s->samplerate != 0 && s->channels != 0) {
103 return aubio_sink_apple_audio_open(s);
108 uint_t aubio_sink_apple_audio_get_samplerate(aubio_sink_apple_audio_t *s)
110 return s->samplerate;
113 uint_t aubio_sink_apple_audio_get_channels(aubio_sink_apple_audio_t *s)
118 uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) {
120 if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL;
122 AudioStreamBasicDescription 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;
134 AudioFileTypeID fileType = kAudioFileWAVEType;
135 CFURLRef fileURL = getURLFromPath(s->path);
136 bool overwrite = true;
137 OSStatus err = noErr;
138 err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
139 overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
142 AUBIO_ERR("sink_apple_audio: error when trying to create %s with "
143 "ExtAudioFileCreateWithURL (%s)\n", s->path,
144 getPrintableOSStatusError(errorstr, err));
147 if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
148 AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
149 "out of memory? \n", s->path);
158 void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
159 OSStatus err = noErr;
161 short *data = (short*)s->bufferList.mBuffers[0].mData;
162 if (write > s->max_frames) {
163 AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
164 write = s->max_frames;
166 smpl_t *buf = write_data->data;
169 for (c = 0; c < s->channels; c++) {
170 for (v = 0; v < write; v++) {
171 data[v * s->channels + c] =
172 FLOAT_TO_SHORT(buf[ v * s->channels + c]);
177 err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
181 AUBIO_ERROR("sink_apple_audio: error while writing %s "
182 "in ExtAudioFileWriteAsync (%s), switching to sync\n", s->path,
183 getPrintableOSStatusError(errorstr, err));
190 err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
194 AUBIO_ERROR("sink_apple_audio: error while writing %s "
195 "in ExtAudioFileWrite (%s)\n", s->path,
196 getPrintableOSStatusError(errorstr, err));
202 void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) {
203 OSStatus err = noErr;
205 short *data = (short*)s->bufferList.mBuffers[0].mData;
206 if (write > s->max_frames) {
207 AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
208 write = s->max_frames;
210 smpl_t **buf = write_data->data;
213 for (c = 0; c < s->channels; c++) {
214 for (v = 0; v < write; v++) {
215 data[v * s->channels + c] =
216 FLOAT_TO_SHORT(buf[c][v]);
221 err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
225 AUBIO_ERROR("sink_apple_audio: error while writing %s "
226 "in ExtAudioFileWriteAsync (%s), switching to sync\n", s->path,
227 getPrintableOSStatusError(errorstr, err));
234 err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
238 AUBIO_ERROR("sink_apple_audio: error while writing %s "
239 "in ExtAudioFileWrite (%s)\n", s->path,
240 getPrintableOSStatusError(errorstr, err));
246 uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) {
247 OSStatus err = noErr;
251 err = ExtAudioFileDispose(s->audioFile);
254 AUBIO_ERROR("sink_apple_audio: error while closing %s "
255 "in ExtAudioFileDispose (%s)\n", s->path,
256 getPrintableOSStatusError(errorstr, err));
262 void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
263 if (s->audioFile) aubio_sink_apple_audio_close (s);
264 freeAudioBufferList(&s->bufferList);
269 #endif /* HAVE_SINK_APPLE_AUDIO */