2 Copyright (C) 2012 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/>.
25 #include "aubio_priv.h"
27 #include "io/sink_apple_audio.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 getURLFromPath(const char * path);
40 #define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
42 struct _aubio_sink_apple_audio_t {
49 AudioBufferList bufferList;
50 ExtAudioFileRef audioFile;
53 aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate) {
54 aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t);
58 s->max_frames = MAX_SIZE;
60 AudioStreamBasicDescription clientFormat;
61 UInt32 propSize = sizeof(clientFormat);
62 memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
63 clientFormat.mFormatID = kAudioFormatLinearPCM;
64 clientFormat.mSampleRate = (Float64)(s->samplerate);
65 clientFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
66 clientFormat.mChannelsPerFrame = s->channels;
67 clientFormat.mBitsPerChannel = sizeof(short) * 8;
68 clientFormat.mFramesPerPacket = 1;
69 clientFormat.mBytesPerFrame = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
70 clientFormat.mBytesPerPacket = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
71 clientFormat.mReserved = 0;
73 AudioFileTypeID fileType = kAudioFileWAVEType;
74 CFURLRef fileURL = getURLFromPath(uri);
75 bool overwrite = true;
77 err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
78 overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
80 AUBIO_ERR("error when trying to access %s, in ExtAudioFileOpenURL, %d\n", s->path, (int)err);
83 if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
84 AUBIO_ERR("error when creating buffer list for %s, out of memory? \n", s->path);
94 void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
98 short *data = (short*)s->bufferList.mBuffers[0].mData;
99 if (write > s->max_frames) {
100 write = s->max_frames;
101 AUBIO_WRN("trying to write %d frames, but only %d can be written at a time",
102 write, s->max_frames);
104 smpl_t *buf = write_data->data;
107 for (c = 0; c < s->channels; c++) {
108 for (v = 0; v < write; v++) {
109 data[v * s->channels + c] =
110 FLOAT_TO_SHORT(buf[ v * s->channels + c]);
115 err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
116 if (err) { AUBIO_ERROR("error in ExtAudioFileWriteAsync, %d\n", (int)err); }
118 err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
119 if (err) { AUBIO_ERROR("error in ExtAudioFileWrite, %d\n", (int)err); }
124 void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
125 OSStatus err = noErr;
126 if (!s || !s->audioFile) { return; }
127 err = ExtAudioFileDispose(s->audioFile);
128 if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err);
130 freeAudioBufferList(&s->bufferList);
135 #endif /* __APPLE__ */