1 | /* |
---|
2 | Copyright (C) 2012-2014 Paul Brossier <piem@aubio.org> |
---|
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 | |
---|
21 | #include "aubio_priv.h" |
---|
22 | |
---|
23 | #ifdef HAVE_SINK_APPLE_AUDIO |
---|
24 | #include "fvec.h" |
---|
25 | #include "fmat.h" |
---|
26 | #include "io/sink_apple_audio.h" |
---|
27 | #include "io/ioutils.h" |
---|
28 | |
---|
29 | // CFURLRef, CFURLCreateWithFileSystemPath, ... |
---|
30 | #include <CoreFoundation/CoreFoundation.h> |
---|
31 | // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ... |
---|
32 | #include <AudioToolbox/AudioToolbox.h> |
---|
33 | |
---|
34 | extern int createAudioBufferList(AudioBufferList *bufferList, int channels, int segmentSize); |
---|
35 | extern void freeAudioBufferList(AudioBufferList *bufferList); |
---|
36 | extern CFURLRef createURLFromPath(const char * path); |
---|
37 | char_t *getPrintableOSStatusError(char_t *str, OSStatus error); |
---|
38 | |
---|
39 | uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s); |
---|
40 | |
---|
41 | uint_t aubio_str_extension_matches(const char_t *ext, |
---|
42 | const char_t *pattern); |
---|
43 | const char_t *aubio_str_get_extension(const char_t *filename); |
---|
44 | |
---|
45 | #define MAX_SIZE 4096 // the maximum number of frames that can be written at a time |
---|
46 | |
---|
47 | void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write); |
---|
48 | |
---|
49 | struct _aubio_sink_apple_audio_t { |
---|
50 | uint_t samplerate; |
---|
51 | uint_t channels; |
---|
52 | char_t *path; |
---|
53 | |
---|
54 | uint_t max_frames; |
---|
55 | |
---|
56 | AudioBufferList bufferList; |
---|
57 | ExtAudioFileRef audioFile; |
---|
58 | bool async; |
---|
59 | AudioFileTypeID fileType; |
---|
60 | }; |
---|
61 | |
---|
62 | aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(const char_t * uri, uint_t samplerate) { |
---|
63 | aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t); |
---|
64 | s->max_frames = MAX_SIZE; |
---|
65 | s->async = false; |
---|
66 | |
---|
67 | if ( (uri == NULL) || (strnlen(uri, PATH_MAX) < 1) ) { |
---|
68 | AUBIO_ERROR("sink_apple_audio: Aborted opening null path\n"); |
---|
69 | goto beach; |
---|
70 | } |
---|
71 | |
---|
72 | s->path = AUBIO_ARRAY(char_t, strnlen(uri, PATH_MAX) + 1); |
---|
73 | strncpy(s->path, uri, strnlen(uri, PATH_MAX) + 1); |
---|
74 | |
---|
75 | s->samplerate = 0; |
---|
76 | s->channels = 0; |
---|
77 | |
---|
78 | aubio_sink_apple_audio_preset_format(s, aubio_str_get_extension(uri)); |
---|
79 | |
---|
80 | // zero samplerate given. do not open yet |
---|
81 | if ((sint_t)samplerate == 0) { |
---|
82 | return s; |
---|
83 | } |
---|
84 | |
---|
85 | // invalid samplerate given, abort |
---|
86 | if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) { |
---|
87 | goto beach; |
---|
88 | } |
---|
89 | |
---|
90 | s->samplerate = samplerate; |
---|
91 | s->channels = 1; |
---|
92 | |
---|
93 | if (aubio_sink_apple_audio_open(s) != AUBIO_OK) { |
---|
94 | // open failed, abort |
---|
95 | goto beach; |
---|
96 | } |
---|
97 | |
---|
98 | return s; |
---|
99 | beach: |
---|
100 | del_aubio_sink_apple_audio(s); |
---|
101 | return NULL; |
---|
102 | } |
---|
103 | |
---|
104 | uint_t aubio_sink_apple_audio_preset_samplerate(aubio_sink_apple_audio_t *s, uint_t samplerate) |
---|
105 | { |
---|
106 | if (aubio_io_validate_samplerate("sink_apple_audio", s->path, samplerate)) { |
---|
107 | return AUBIO_FAIL; |
---|
108 | } |
---|
109 | s->samplerate = samplerate; |
---|
110 | // automatically open when both samplerate and channels have been set |
---|
111 | if (/* s->samplerate != 0 && */ s->channels != 0) { |
---|
112 | return aubio_sink_apple_audio_open(s); |
---|
113 | } |
---|
114 | return AUBIO_OK; |
---|
115 | } |
---|
116 | |
---|
117 | uint_t aubio_sink_apple_audio_preset_channels(aubio_sink_apple_audio_t *s, uint_t channels) |
---|
118 | { |
---|
119 | if (aubio_io_validate_channels("sink_apple_audio", s->path, channels)) { |
---|
120 | return AUBIO_FAIL; |
---|
121 | } |
---|
122 | s->channels = channels; |
---|
123 | // automatically open when both samplerate and channels have been set |
---|
124 | if (s->samplerate != 0 /* && s->channels != 0 */) { |
---|
125 | return aubio_sink_apple_audio_open(s); |
---|
126 | } |
---|
127 | return AUBIO_OK; |
---|
128 | } |
---|
129 | |
---|
130 | uint_t aubio_sink_apple_audio_preset_format(aubio_sink_apple_audio_t *s, |
---|
131 | const char_t *fmt) |
---|
132 | { |
---|
133 | if (aubio_str_extension_matches(fmt, "wav")) { |
---|
134 | s->fileType = kAudioFileWAVEType; |
---|
135 | } else if (aubio_str_extension_matches(fmt, "m4a") |
---|
136 | || aubio_str_extension_matches(fmt, "mp4") ) { |
---|
137 | // use alac for "mp4" and "m4a" |
---|
138 | s->fileType = kAudioFileM4AType; |
---|
139 | } else if (aubio_str_extension_matches(fmt, "aac") ) { |
---|
140 | // only use lossy codec for "aac" |
---|
141 | s->fileType = kAudioFileMPEG4Type; |
---|
142 | } else if (aubio_str_extension_matches(fmt, "aiff") ) { |
---|
143 | // only use lossy codec for "aac" |
---|
144 | s->fileType = kAudioFileAIFFType; |
---|
145 | } else { |
---|
146 | s->fileType = kAudioFileWAVEType; |
---|
147 | if (fmt && strnlen(fmt, PATH_MAX)) { |
---|
148 | AUBIO_WRN("sink_apple_audio: could not guess format for %s," |
---|
149 | " using default (wav)\n", s->path); |
---|
150 | return AUBIO_FAIL; |
---|
151 | } |
---|
152 | } |
---|
153 | return AUBIO_OK; |
---|
154 | } |
---|
155 | |
---|
156 | static void aubio_sink_apple_audio_set_client_format(aubio_sink_apple_audio_t* s, |
---|
157 | AudioStreamBasicDescription *clientFormat) |
---|
158 | { |
---|
159 | memset(clientFormat, 0, sizeof(AudioStreamBasicDescription)); |
---|
160 | // always set samplerate and channels first |
---|
161 | clientFormat->mSampleRate = (Float64)(s->samplerate); |
---|
162 | clientFormat->mChannelsPerFrame = s->channels; |
---|
163 | |
---|
164 | switch (s->fileType) { |
---|
165 | case kAudioFileM4AType: |
---|
166 | clientFormat->mFormatID = kAudioFormatAppleLossless; |
---|
167 | break; |
---|
168 | case kAudioFileMPEG4Type: |
---|
169 | clientFormat->mFormatID = kAudioFormatMPEG4AAC; |
---|
170 | clientFormat->mFormatFlags = kMPEG4Object_AAC_Main; |
---|
171 | clientFormat->mFormatFlags |= kAppleLosslessFormatFlag_16BitSourceData; |
---|
172 | clientFormat->mFramesPerPacket = 1024; |
---|
173 | break; |
---|
174 | case kAudioFileWAVEType: |
---|
175 | clientFormat->mFormatID = kAudioFormatLinearPCM; |
---|
176 | clientFormat->mFormatFlags = kAudioFormatFlagIsSignedInteger; |
---|
177 | clientFormat->mFormatFlags |= kAudioFormatFlagIsPacked; |
---|
178 | clientFormat->mBitsPerChannel = sizeof(short) * 8; |
---|
179 | clientFormat->mFramesPerPacket = 1; |
---|
180 | clientFormat->mBytesPerFrame = clientFormat->mBitsPerChannel * clientFormat->mChannelsPerFrame / 8; |
---|
181 | clientFormat->mBytesPerPacket = clientFormat->mFramesPerPacket * clientFormat->mBytesPerFrame; |
---|
182 | break; |
---|
183 | case kAudioFileAIFFType: |
---|
184 | clientFormat->mFormatID = kAudioFormatLinearPCM; |
---|
185 | clientFormat->mFormatFlags = kAudioFormatFlagIsSignedInteger; |
---|
186 | clientFormat->mFormatFlags |= kAudioFormatFlagIsPacked; |
---|
187 | clientFormat->mFormatFlags |= kAudioFormatFlagIsBigEndian; |
---|
188 | clientFormat->mBitsPerChannel = sizeof(short) * 8; |
---|
189 | clientFormat->mFramesPerPacket = 1; |
---|
190 | clientFormat->mBytesPerFrame = clientFormat->mBitsPerChannel * clientFormat->mChannelsPerFrame / 8; |
---|
191 | clientFormat->mBytesPerPacket = clientFormat->mFramesPerPacket * clientFormat->mBytesPerFrame; |
---|
192 | break; |
---|
193 | default: |
---|
194 | break; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | uint_t aubio_sink_apple_audio_get_samplerate(const aubio_sink_apple_audio_t *s) |
---|
199 | { |
---|
200 | return s->samplerate; |
---|
201 | } |
---|
202 | |
---|
203 | uint_t aubio_sink_apple_audio_get_channels(const aubio_sink_apple_audio_t *s) |
---|
204 | { |
---|
205 | return s->channels; |
---|
206 | } |
---|
207 | |
---|
208 | uint_t aubio_sink_apple_audio_open(aubio_sink_apple_audio_t *s) { |
---|
209 | |
---|
210 | if (s->samplerate == 0 || s->channels == 0) return AUBIO_FAIL; |
---|
211 | |
---|
212 | CFURLRef fileURL = createURLFromPath(s->path); |
---|
213 | bool overwrite = true; |
---|
214 | |
---|
215 | // set the in-memory format |
---|
216 | AudioStreamBasicDescription inputFormat; |
---|
217 | memset(&inputFormat, 0, sizeof(AudioStreamBasicDescription)); |
---|
218 | inputFormat.mFormatID = kAudioFormatLinearPCM; |
---|
219 | inputFormat.mSampleRate = (Float64)(s->samplerate); |
---|
220 | inputFormat.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked; |
---|
221 | inputFormat.mChannelsPerFrame = s->channels; |
---|
222 | inputFormat.mBitsPerChannel = sizeof(smpl_t) * 8; |
---|
223 | inputFormat.mFramesPerPacket = 1; |
---|
224 | inputFormat.mBytesPerFrame = inputFormat.mBitsPerChannel * inputFormat.mChannelsPerFrame / 8; |
---|
225 | inputFormat.mBytesPerPacket = inputFormat.mFramesPerPacket * inputFormat.mBytesPerFrame; |
---|
226 | |
---|
227 | // get the in-file format |
---|
228 | AudioStreamBasicDescription clientFormat; |
---|
229 | aubio_sink_apple_audio_set_client_format(s, &clientFormat); |
---|
230 | |
---|
231 | OSStatus err = noErr; |
---|
232 | err = ExtAudioFileCreateWithURL(fileURL, s->fileType, &clientFormat, NULL, |
---|
233 | overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile); |
---|
234 | CFRelease(fileURL); |
---|
235 | if (err) { |
---|
236 | char_t errorstr[20]; |
---|
237 | AUBIO_ERR("sink_apple_audio: error when trying to create %s with " |
---|
238 | "ExtAudioFileCreateWithURL (%s)\n", s->path, |
---|
239 | getPrintableOSStatusError(errorstr, err)); |
---|
240 | goto beach; |
---|
241 | } |
---|
242 | |
---|
243 | #if defined(kAppleSoftwareAudioCodecManufacturer) |
---|
244 | // on iOS, set software based encoding before setting clientDataFormat |
---|
245 | UInt32 codecManf = kAppleSoftwareAudioCodecManufacturer; |
---|
246 | err = ExtAudioFileSetProperty(s->audioFile, |
---|
247 | kExtAudioFileProperty_CodecManufacturer, |
---|
248 | sizeof(UInt32), &codecManf); |
---|
249 | if (err) { |
---|
250 | char_t errorstr[20]; |
---|
251 | AUBIO_ERR("sink_apple_audio: error when trying to set sofware codec on %s " |
---|
252 | "(%s)\n", s->path, getPrintableOSStatusError(errorstr, err)); |
---|
253 | goto beach; |
---|
254 | } |
---|
255 | #endif |
---|
256 | |
---|
257 | err = ExtAudioFileSetProperty(s->audioFile, |
---|
258 | kExtAudioFileProperty_ClientDataFormat, |
---|
259 | sizeof(AudioStreamBasicDescription), &inputFormat); |
---|
260 | if (err) { |
---|
261 | char_t errorstr[20]; |
---|
262 | AUBIO_ERR("sink_apple_audio: error when trying to set output format on %s " |
---|
263 | "(%s)\n", s->path, getPrintableOSStatusError(errorstr, err)); |
---|
264 | goto beach; |
---|
265 | } |
---|
266 | |
---|
267 | if (createAudioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) { |
---|
268 | AUBIO_ERR("sink_apple_audio: error when creating buffer list for %s, " |
---|
269 | "out of memory? \n", s->path); |
---|
270 | goto beach; |
---|
271 | } |
---|
272 | return AUBIO_OK; |
---|
273 | |
---|
274 | beach: |
---|
275 | return AUBIO_FAIL; |
---|
276 | } |
---|
277 | |
---|
278 | void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) { |
---|
279 | UInt32 c, v; |
---|
280 | smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData; |
---|
281 | uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path, |
---|
282 | s->max_frames, write_data->length, write); |
---|
283 | |
---|
284 | for (c = 0; c < s->channels; c++) { |
---|
285 | for (v = 0; v < length; v++) { |
---|
286 | data[v * s->channels + c] = write_data->data[v]; |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | aubio_sink_apple_audio_write(s, length); |
---|
291 | } |
---|
292 | |
---|
293 | void aubio_sink_apple_audio_do_multi(aubio_sink_apple_audio_t * s, fmat_t * write_data, uint_t write) { |
---|
294 | UInt32 c, v; |
---|
295 | smpl_t *data = (smpl_t*)s->bufferList.mBuffers[0].mData; |
---|
296 | uint_t channels = aubio_sink_validate_input_channels("sink_apple_audio", |
---|
297 | s->path, s->channels, write_data->height); |
---|
298 | uint_t length = aubio_sink_validate_input_length("sink_apple_audio", s->path, |
---|
299 | s->max_frames, write_data->length, write); |
---|
300 | |
---|
301 | for (c = 0; c < channels; c++) { |
---|
302 | for (v = 0; v < length; v++) { |
---|
303 | data[v * s->channels + c] = write_data->data[c][v]; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | aubio_sink_apple_audio_write(s, length); |
---|
308 | } |
---|
309 | |
---|
310 | void aubio_sink_apple_audio_write(aubio_sink_apple_audio_t *s, uint_t write) { |
---|
311 | OSStatus err = noErr; |
---|
312 | // set mDataByteSize to match the number of frames to be written |
---|
313 | // see https://www.mail-archive.com/coreaudio-api@lists.apple.com/msg01109.html |
---|
314 | s->bufferList.mBuffers[0].mDataByteSize = write * s->channels |
---|
315 | * sizeof(smpl_t); |
---|
316 | if (s->async) { |
---|
317 | err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList); |
---|
318 | if (err) { |
---|
319 | char_t errorstr[20]; |
---|
320 | if (err == kExtAudioFileError_AsyncWriteBufferOverflow) { |
---|
321 | sprintf(errorstr,"buffer overflow"); |
---|
322 | } else if (err == kExtAudioFileError_AsyncWriteTooLarge) { |
---|
323 | sprintf(errorstr,"write too large"); |
---|
324 | } else { |
---|
325 | // unknown error |
---|
326 | getPrintableOSStatusError(errorstr, err); |
---|
327 | } |
---|
328 | AUBIO_ERROR("sink_apple_audio: error while writing %s " |
---|
329 | "in ExtAudioFileWriteAsync (%s)\n", s->path, errorstr); |
---|
330 | } |
---|
331 | } else { |
---|
332 | err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList); |
---|
333 | if (err) { |
---|
334 | char_t errorstr[20]; |
---|
335 | AUBIO_ERROR("sink_apple_audio: error while writing %s " |
---|
336 | "in ExtAudioFileWrite (%s)\n", s->path, |
---|
337 | getPrintableOSStatusError(errorstr, err)); |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | uint_t aubio_sink_apple_audio_close(aubio_sink_apple_audio_t * s) { |
---|
343 | OSStatus err = noErr; |
---|
344 | if (!s->audioFile) { |
---|
345 | return AUBIO_FAIL; |
---|
346 | } |
---|
347 | err = ExtAudioFileDispose(s->audioFile); |
---|
348 | if (err) { |
---|
349 | char_t errorstr[20]; |
---|
350 | AUBIO_ERROR("sink_apple_audio: error while closing %s " |
---|
351 | "in ExtAudioFileDispose (%s)\n", s->path, |
---|
352 | getPrintableOSStatusError(errorstr, err)); |
---|
353 | } |
---|
354 | s->audioFile = NULL; |
---|
355 | return err; |
---|
356 | } |
---|
357 | |
---|
358 | void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) { |
---|
359 | AUBIO_ASSERT(s); |
---|
360 | if (s->audioFile) |
---|
361 | aubio_sink_apple_audio_close (s); |
---|
362 | if (s->path) |
---|
363 | AUBIO_FREE(s->path); |
---|
364 | freeAudioBufferList(&s->bufferList); |
---|
365 | AUBIO_FREE(s); |
---|
366 | } |
---|
367 | |
---|
368 | #endif /* HAVE_SINK_APPLE_AUDIO */ |
---|