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/>.
21 #include "aubio_priv.h"
23 #ifdef HAVE_SINK_APPLE_AUDIO
26 #include "io/sink_apple_audio.h"
27 #include "io/ioutils.h"
29 // CFURLRef, CFURLCreateWithFileSystemPath, ...
30 #include <CoreFoundation/CoreFoundation.h>
31 // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
32 #include <AudioToolbox/AudioToolbox.h>
34 #define FLOAT_TO_SHORT(x) (short)(x * 32768)
36 extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
37 extern void freeAudioBufferList(AudioBufferList *bufferList);
38 extern CFURLRef createURLFromPath(const char * path);
39 char_t *getPrintableOSStatusError(char_t *str, OSStatus error);
41 uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s);
43 #define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
45 void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write);
47 struct _aubio_sink_apple_audio_t {
54 AudioBufferList bufferList;
55 ExtAudioFileRef audioFile;
59 aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(const char_t * uri, uint_t samplerate) {
60 aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t);
61 s->max_frames = MAX_SIZE;
64 if ( (uri == NULL) || (strnlen(uri, PATH_MAX) < 1) ) {
65 AUBIO_ERROR("sink_apple_audio: Aborted opening null path\n");
69 s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1);
70 strncpy(s->path, uri, strnlen(uri, PATH_MAX) + 1);
75 // zero samplerate given. do not open yet
76 if ((sint_t)samplerate == 0) {
79 // invalid samplerate given, abort
80 if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) {
84 s->samplerate = samplerate;
87 if (aubio_sink_apple_audio_open(s) != AUBIO_OK) {
94 del_aubio_sink_apple_audio(s);
98 uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate)
100 if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) {
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);
111 uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels)
113 if (aubio_io_validate_channels("sink_apple_audio", s->path, channels)) {
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);
124 uint_t aubio_sink_apple_audio_get_samplerate(const aubio_sink_apple_audio_t *s)
126 return s->samplerate;
129 uint_t aubio_sink_apple_audio_get_channels(const aubio_sink_apple_audio_t *s)
134 uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) {
136 if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL;
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;
150 AudioFileTypeID fileType = kAudioFileWAVEType;
151 CFURLRef fileURL = createURLFromPath(s->path);
152 bool overwrite = true;
153 OSStatus err = noErr;
154 err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
155 overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
159 AUBIO_ERR("sink_apple_audio: error when trying to create %s with "
160 "ExtAudioFileCreateWithURL (%s)\n", s->path,
161 getPrintableOSStatusError(errorstr, err));
164 if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
165 AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
166 "out of memory? \n", s->path);
175 void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
177 short *data = (short*)s->bufferList.mBuffers[0].mData;
178 uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path,
179 s->max_frames, write_data->length, write);
181 for (c = 0; c < s->channels; c++) {
182 for (v = 0; v < length; v++) {
183 data[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[v]);
187 aubio_sink_apple_audio_write(s, length);
190 void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) {
192 short *data = (short*)s->bufferList.mBuffers[0].mData;
193 uint_t channels = aubio_sink_validate_input_channels("sink_apple_audio",
194 s->path, s->channels, write_data->height);
195 uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path,
196 s->max_frames, write_data->length, write);
198 for (c = 0; c < channels; c++) {
199 for (v = 0; v < length; v++) {
200 data[v * s->channels + c] = FLOAT_TO_SHORT(write_data->data[c][v]);
204 aubio_sink_apple_audio_write(s, length);
207 void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write) {
208 OSStatus err = noErr;
210 err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
213 if (err == kExtAudioFileError_AsyncWriteBufferOverflow) {
214 sprintf(errorstr,"buffer overflow");
215 } else if (err == kExtAudioFileError_AsyncWriteTooLarge) {
216 sprintf(errorstr,"write too large");
219 getPrintableOSStatusError(errorstr, err);
221 AUBIO_ERROR("sink_apple_audio: error while writing %s "
222 "in ExtAudioFileWriteAsync (%s)\n", s->path, errorstr);
225 err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
228 AUBIO_ERROR("sink_apple_audio: error while writing %s "
229 "in ExtAudioFileWrite (%s)\n", s->path,
230 getPrintableOSStatusError(errorstr, err));
235 uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) {
236 OSStatus err = noErr;
240 err = ExtAudioFileDispose(s->audioFile);
243 AUBIO_ERROR("sink_apple_audio: error while closing %s "
244 "in ExtAudioFileDispose (%s)\n", s->path,
245 getPrintableOSStatusError(errorstr, err));
251 void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
254 aubio_sink_apple_audio_close (s);
257 freeAudioBufferList(&s->bufferList);
261 #endif /* HAVE_SINK_APPLE_AUDIO */