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 void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write);
48 struct _aubio_sink_apple_audio_t {
55 AudioBufferList bufferList;
56 ExtAudioFileRef audioFile;
60 aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(const char_t * uri, uint_t samplerate) {
61 aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t);
63 s->max_frames = MAX_SIZE;
67 AUBIO_ERROR("sink_apple_audio: Aborted opening null path\n");
74 // negative samplerate given, abort
75 if ((sint_t)samplerate < 0) goto beach;
76 // zero samplerate given. do not open yet
77 if ((sint_t)samplerate == 0) return s;
79 s->samplerate = samplerate;
82 if (aubio_sink_apple_audio_open(s) != AUBIO_OK) {
93 uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate)
95 if ((sint_t)(samplerate) <= 0) return AUBIO_FAIL;
96 s->samplerate = samplerate;
97 // automatically open when both samplerate and channels have been set
98 if (s->samplerate != 0 && s->channels != 0) {
99 return aubio_sink_apple_audio_open(s);
104 uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels)
106 if ((sint_t)(channels) <= 0) return AUBIO_FAIL;
107 s->channels = channels;
108 // automatically open when both samplerate and channels have been set
109 if (s->samplerate != 0 && s->channels != 0) {
110 return aubio_sink_apple_audio_open(s);
115 uint_t aubio_sink_apple_audio_get_samplerate(const aubio_sink_apple_audio_t *s)
117 return s->samplerate;
120 uint_t aubio_sink_apple_audio_get_channels(const aubio_sink_apple_audio_t *s)
125 uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) {
127 if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL;
129 AudioStreamBasicDescription clientFormat;
130 memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
131 clientFormat.mFormatID = kAudioFormatLinearPCM;
132 clientFormat.mSampleRate = (Float64)(s->samplerate);
133 clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
134 clientFormat.mChannelsPerFrame = s->channels;
135 clientFormat.mBitsPerChannel = sizeof(short) * 8;
136 clientFormat.mFramesPerPacket = 1;
137 clientFormat.mBytesPerFrame = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
138 clientFormat.mBytesPerPacket = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
139 clientFormat.mReserved = 0;
141 AudioFileTypeID fileType = kAudioFileWAVEType;
142 CFURLRef fileURL = createURLFromPath(s->path);
143 bool overwrite = true;
144 OSStatus err = noErr;
145 err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
146 overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
150 AUBIO_ERR("sink_apple_audio: error when trying to create %s with "
151 "ExtAudioFileCreateWithURL (%s)\n", s->path,
152 getPrintableOSStatusError(errorstr, err));
155 if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
156 AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, "
157 "out of memory? \n", s->path);
166 void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
168 short *data = (short*)s->bufferList.mBuffers[0].mData;
169 if (write > s->max_frames) {
170 AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
171 write = s->max_frames;
173 smpl_t *buf = write_data->data;
176 for (c = 0; c < s->channels; c++) {
177 for (v = 0; v < write; v++) {
178 data[v * s->channels + c] =
179 FLOAT_TO_SHORT(buf[ v * s->channels + c]);
183 aubio_sink_apple_audio_write(s, write);
186 void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) {
188 short *data = (short*)s->bufferList.mBuffers[0].mData;
189 if (write > s->max_frames) {
190 AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
191 write = s->max_frames;
193 smpl_t **buf = write_data->data;
196 for (c = 0; c < s->channels; c++) {
197 for (v = 0; v < write; v++) {
198 data[v * s->channels + c] =
199 FLOAT_TO_SHORT(buf[c][v]);
203 aubio_sink_apple_audio_write(s, write);
206 void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write) {
207 OSStatus err = noErr;
209 err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
212 if (err == kExtAudioFileError_AsyncWriteBufferOverflow) {
213 sprintf(errorstr,"buffer overflow");
214 } else if (err == kExtAudioFileError_AsyncWriteTooLarge) {
215 sprintf(errorstr,"write too large");
218 getPrintableOSStatusError(errorstr, err);
220 AUBIO_ERROR("sink_apple_audio: error while writing %s "
221 "in ExtAudioFileWriteAsync (%s)\n", s->path, errorstr);
224 err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
227 AUBIO_ERROR("sink_apple_audio: error while writing %s "
228 "in ExtAudioFileWrite (%s)\n", s->path,
229 getPrintableOSStatusError(errorstr, err));
234 uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) {
235 OSStatus err = noErr;
239 err = ExtAudioFileDispose(s->audioFile);
242 AUBIO_ERROR("sink_apple_audio: error while closing %s "
243 "in ExtAudioFileDispose (%s)\n", s->path,
244 getPrintableOSStatusError(errorstr, err));
250 void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
251 if (s->audioFile) aubio_sink_apple_audio_close (s);
252 freeAudioBufferList(&s->bufferList);
257 #endif /* HAVE_SINK_APPLE_AUDIO */