2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file osx_write.c paraslash's output plugin for MacOs */
22 * based on mosx-mpg123, by Guillaume Outters and Steven A. Kortze
23 * <skortze@sourceforge.net>
26 #include <CoreAudio/CoreAudio.h>
33 #include "osx_write.cmdline.h"
37 #include <CoreAudio/CoreAudio.h>
38 #include <AudioUnit/AudioUnit.h>
39 #include <AudioToolbox/DefaultAudioOutput.h>
41 /** describes one input buffer for the osx writer */
43 /** pointer to the beginning of the buffer */
45 /** the size of this buffer */
47 /** current position in the buffer */
49 /** number of floats not yet consuned */
51 /** pointer to the next audio buffer */
52 struct osx_buffer *next;
55 /** data specific to the osx writer */
56 struct private_osx_write_data {
57 /** the main control structure for audio data manipulation */
59 /** non-zero if playback has started */
61 /** callback reads audio data from this buffer */
62 struct osx_buffer *from;
63 /** the post_select writes audio data here */
64 struct osx_buffer *to;
65 /** sample rate of the current audio stream */
67 /** number of channels of the current audio stream */
71 static void destroy_buffers(struct private_osx_write_data *powd)
73 struct osx_buffer *ptr;
74 struct osx_buffer *ptr2;
76 powd->to->next = NULL;
85 static void init_buffers(struct writer_node *wn)
87 struct private_osx_write_data *powd = wn->private_data;
88 struct osx_write_args_info *conf = wn->conf;
89 struct osx_buffer **ptrptr;
93 for (i = 0; i < conf->numbuffers_arg; i++) {
94 *ptrptr = malloc(sizeof(struct osx_buffer));
96 (*ptrptr)->remaining = 0;
97 (*ptrptr)->buffer = NULL;
98 ptrptr = &(*ptrptr)->next;
100 *ptrptr = powd->from = powd->to;
103 static void fill_buffer(struct osx_buffer *b, short *source, long size)
107 if (b->remaining) /* Non empty buffer, must still be playing */
109 if (b->size != size) {
110 b->buffer = para_realloc(b->buffer, size * sizeof(float));
115 char *tmp = (char *)source;
119 /* *dest++ = ((*source++) + 32768) / 65536.0; */
120 *dest++ = (*source++) / 32768.0;
123 b->remaining = b->size;
126 static OSStatus osx_callback(void * inClientData,
127 __a_unused AudioUnitRenderActionFlags *inActionFlags,
128 __a_unused const AudioTimeStamp *inTimeStamp,
129 __a_unused UInt32 inBusNumber,
130 __a_unused UInt32 inNumFrames,
131 AudioBufferList *outOutputData)
136 struct private_osx_write_data *powd = inClientData;
138 // PARA_INFO_LOG("%p\n", powd);
139 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
140 /* what we have to fill */
141 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
142 dest = outOutputData->mBuffers[i].mData;
144 if ((n = powd->from->remaining) <= 0) {
145 PARA_INFO_LOG("%s", "buffer underrun\n");
148 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
150 * we dump what we can. In fact, just the necessary
151 * should be sufficient
155 memcpy(dest, powd->from->ptr, n * sizeof(float));
157 /* remember all done work */
159 powd->from->ptr += n;
160 if ((powd->from->remaining -= n) <= 0)
161 powd->from = powd->from->next;
167 static int osx_write_open(struct writer_node *wn)
169 struct private_osx_write_data *powd = para_calloc(
170 sizeof(struct private_osx_write_data));
171 ComponentDescription desc;
173 AURenderCallbackStruct inputCallback = {osx_callback, powd};
174 AudioStreamBasicDescription format;
176 struct writer_node_group *wng = wn->wng;
177 struct osx_write_args_info *conf = wn->conf;
179 wn->private_data = powd;
180 /* where did that default audio output go? */
181 desc.componentType = kAudioUnitType_Output;
182 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
183 /* NOTE: and if default output isn't Apple? */
184 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
185 desc.componentFlags = 0;
186 desc.componentFlagsMask = 0;
187 ret = -E_DEFAULT_COMP;
188 comp = FindNextComponent(NULL, &desc);
192 if (OpenAComponent(comp, &powd->audio_unit))
195 if (AudioUnitInitialize(powd->audio_unit))
198 /* Hmmm, let's choose PCM format */
199 /* We tell the Output Unit what format we're going to supply data to it.
200 * This is necessary if you're providing data through an input callback
201 * AND you want the DefaultOutputUnit to do any format conversions
202 * necessary from your format to the device's format.
204 if (!conf->samplerate_given && wng->samplerate)
205 powd->samplerate = *wng->samplerate;
207 powd->samplerate = conf->samplerate_arg;
208 format.mSampleRate = powd->samplerate;
209 /* The specific encoding type of audio stream*/
210 format.mFormatID = kAudioFormatLinearPCM;
211 /* flags specific to each format */
212 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
213 | kLinearPCMFormatFlagIsPacked
214 | kLinearPCMFormatFlagIsBigEndian;
215 if (!conf->channels_given && wng->channels)
216 powd->channels = *wng->channels;
218 powd->channels = conf->channels_arg;
219 format.mChannelsPerFrame = powd->channels;
220 format.mFramesPerPacket = 1;
221 format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
222 format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
223 /* one of the most constant constants of the whole computer history */
224 format.mBitsPerChannel = sizeof(float) * 8;
225 ret = -E_STREAM_FORMAT;
226 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_StreamFormat,
227 kAudioUnitScope_Input, 0, &format,
228 sizeof(AudioStreamBasicDescription)))
231 ret = -E_ADD_CALLBACK;
232 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_SetRenderCallback,
233 kAudioUnitScope_Input, 0, &inputCallback,
234 sizeof(inputCallback)) < 0)
238 destroy_buffers(powd);
240 AudioUnitUninitialize(powd->audio_unit);
242 CloseComponent(powd->audio_unit);
247 __malloc static void *osx_write_parse_config(const char *options)
249 struct osx_write_args_info *conf
250 = para_calloc(sizeof(struct osx_write_args_info));
251 PARA_INFO_LOG("options: %s\n", options);
252 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
262 static void osx_write_close(struct writer_node *wn)
264 struct private_osx_write_data *powd = wn->private_data;
266 PARA_INFO_LOG("closing writer node %p\n", wn);
267 AudioOutputUnitStop(powd->audio_unit);
268 AudioUnitUninitialize(powd->audio_unit);
269 CloseComponent(powd->audio_unit);
270 destroy_buffers(powd);
274 static int need_new_buffer(struct writer_node *wn)
276 struct writer_node_group *wng = wn->wng;
277 struct private_osx_write_data *powd = wn->private_data;
279 if (*wng->loaded < sizeof(short))
281 if (powd->to->remaining) /* Non empty buffer, must still be playing */
286 static int osx_write_post_select(__a_unused struct sched *s,
287 struct writer_node *wn)
289 struct private_osx_write_data *powd = wn->private_data;
290 struct writer_node_group *wng = wn->wng;
291 short *data = (short*)wng->buf;
293 if (!need_new_buffer(wn))
295 fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
296 powd->to = powd->to->next;
297 wn->written = *wng->loaded;
299 if (AudioOutputUnitStart(powd->audio_unit))
300 return -E_UNIT_START;
306 static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
308 struct private_osx_write_data *powd = wn->private_data;
309 struct writer_node_group *wng = wn->wng;
310 size_t numbytes = powd->to->remaining * sizeof(short);
311 struct timeval tmp = {.tv_sec = 1, .tv_usec = 0}, delay = tmp;
312 unsigned long divisor;
314 if (!numbytes && *wng->loaded >= sizeof(short))
315 goto min_delay; /* there's a buffer to fill */
318 divisor = powd->samplerate * powd->channels * 2 / numbytes;
320 tv_divide(divisor, &tmp, &delay);
321 if (tv_diff(&s->timeout, &delay, NULL) > 0)
323 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
324 // (long unsigned) s->timeout.tv_usec);
327 PARA_DEBUG_LOG("%s\n", "minimal delay");
328 s->timeout.tv_sec = 0;
329 s->timeout.tv_usec = 1;
333 /** the init function of the osx writer */
334 void osx_write_init(struct writer *w)
336 w->open = osx_write_open;
337 w->close = osx_write_close;
338 w->pre_select = osx_write_pre_select;
339 w->post_select = osx_write_post_select;
340 w->parse_config = osx_write_parse_config;
341 w->shutdown = NULL; /* nothing to do */