src/io/sink_apple_audio.c: switch to sync mode if async fails
[aubio.git] / src / io / sink_apple_audio.c
1 /*
2   Copyright (C) 2012 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 "config.h"
22
23 #ifdef __APPLE__
24
25 #include "aubio_priv.h"
26 #include "fvec.h"
27 #include "io/sink_apple_audio.h"
28
29 // CFURLRef, CFURLCreateWithFileSystemPath, ...
30 #include <CoreFoundation/CoreFoundation.h>
31 // ExtAudioFileRef, AudioStreamBasicDescription, AudioBufferList, ...
32 #include <AudioToolbox/AudioToolbox.h>
33
34 #define FLOAT_TO_SHORT(x) (short)(x * 32768)
35
36 extern int createAubioBufferList(AudioBufferList *bufferList, int channels, int segmentSize);
37 extern void freeAudioBufferList(AudioBufferList *bufferList);
38 extern CFURLRef getURLFromPath(const char * path);
39
40 #define MAX_SIZE 4096 // the maximum number of frames that can be written at a time
41
42 struct _aubio_sink_apple_audio_t {
43   uint_t samplerate;
44   uint_t channels;
45   char_t *path;
46
47   uint_t max_frames;
48
49   AudioBufferList bufferList;
50   ExtAudioFileRef audioFile;
51   bool async;
52 };
53
54 aubio_sink_apple_audio_t * new_aubio_sink_apple_audio(char_t * uri, uint_t samplerate) {
55   aubio_sink_apple_audio_t * s = AUBIO_NEW(aubio_sink_apple_audio_t);
56   s->samplerate = samplerate;
57   s->channels = 1;
58   s->path = uri;
59   s->max_frames = MAX_SIZE;
60   s->async = true;
61
62   AudioStreamBasicDescription clientFormat;
63   memset(&clientFormat, 0, sizeof(AudioStreamBasicDescription));
64   clientFormat.mFormatID         = kAudioFormatLinearPCM;
65   clientFormat.mSampleRate       = (Float64)(s->samplerate);
66   clientFormat.mFormatFlags      = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
67   clientFormat.mChannelsPerFrame = s->channels;
68   clientFormat.mBitsPerChannel   = sizeof(short) * 8;
69   clientFormat.mFramesPerPacket  = 1;
70   clientFormat.mBytesPerFrame    = clientFormat.mBitsPerChannel * clientFormat.mChannelsPerFrame / 8;
71   clientFormat.mBytesPerPacket   = clientFormat.mFramesPerPacket * clientFormat.mBytesPerFrame;
72   clientFormat.mReserved         = 0;
73
74   AudioFileTypeID fileType = kAudioFileWAVEType;
75   CFURLRef fileURL = getURLFromPath(uri);
76   bool overwrite = true;
77   OSStatus err = noErr;
78   err = ExtAudioFileCreateWithURL(fileURL, fileType, &clientFormat, NULL,
79      overwrite ? kAudioFileFlags_EraseFile : 0, &s->audioFile);
80   if (err) {
81     AUBIO_ERR("error when trying to create %s, in ExtAudioFileCreateWithURL, %d\n", s->path, (int)err);
82     goto beach;
83   }
84   if (createAubioBufferList(&s->bufferList, s->channels, s->max_frames * s->channels)) {
85     AUBIO_ERR("error when creating buffer list for %s, out of memory? \n", s->path);
86     goto beach;
87   }
88   return s;
89
90 beach:
91   AUBIO_FREE(s);
92   return NULL;
93 }
94
95 void aubio_sink_apple_audio_do(aubio_sink_apple_audio_t * s, fvec_t * write_data, uint_t write) {
96   OSStatus err = noErr;
97   UInt32 c, v;
98   short *data = (short*)s->bufferList.mBuffers[0].mData;
99   if (write > s->max_frames) {
100     AUBIO_WRN("sink_apple_audio: trying to write %d frames, max %d\n", write, s->max_frames);
101     write = s->max_frames;
102   }
103   smpl_t *buf = write_data->data;
104
105   if (buf) {
106       for (c = 0; c < s->channels; c++) {
107           for (v = 0; v < write; v++) {
108               data[v * s->channels + c] =
109                   FLOAT_TO_SHORT(buf[ v * s->channels + c]);
110           }
111       }
112   }
113   if (s->async) {
114     err = ExtAudioFileWriteAsync(s->audioFile, write, &s->bufferList);
115
116     if (err) {
117       AUBIO_ERROR("in aubio_sink_apple_audio_do, writing %s\n", s->path);
118       AUBIO_ERROR("ExtAudioFileWriteAsync failed with %d, switching to sync\n", (int)err);
119       s->async = false;
120     } else {
121       return;
122     }
123
124   } else {
125     err = ExtAudioFileWrite(s->audioFile, write, &s->bufferList);
126
127     if (err) {
128       AUBIO_ERROR("in aubio_sink_apple_audio_do, writing %s\n", s->path);
129       AUBIO_ERROR("ExtAudioFileWrite failed with %d, aborting\n", (int)err);
130     }
131   }
132   return;
133 }
134
135 void del_aubio_sink_apple_audio(aubio_sink_apple_audio_t * s) {
136   OSStatus err = noErr;
137   if (!s || !s->audioFile) {
138     AUBIO_ERR("failed erasing sink_apple_audio\n");
139     return;
140   }
141   err = ExtAudioFileDispose(s->audioFile);
142   if (err) AUBIO_ERROR("error in ExtAudioFileDispose, %d\n", (int)err);
143   s->audioFile = NULL;
144   freeAudioBufferList(&s->bufferList);
145   AUBIO_FREE(s);
146   return;
147 }
148
149 #endif /* __APPLE__ */