2 * Copyright (C) 2006 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_writer.c paraslash's output plugin for MacOs */
21 #include <CoreAudio/CoreAudio.h>
28 #include "osx_write.cmdline.h"
32 #include <CoreAudio/CoreAudio.h>
33 #include <AudioUnit/AudioUnit.h>
34 #include <AudioToolbox/DefaultAudioOutput.h>
35 #include <semaphore.h>
39 float *ptr; /* Where in the buffer are we? */
41 struct osx_buffer *next;
43 typedef struct osx_buffer osx_buffer;
45 struct private_osx_writer_data {
51 osx_buffer *from; /* Current buffers */
59 * Tried with 3 buffers, but then any little window move is sufficient to
60 * stop the sound (OK, on a G3 400 with a Public Beta. Perhaps now we can
61 * go down to 2 buffers). With 16 buffers we have 1.5 seconds music
62 * buffered (or, if you're pessimistic, 1.5 seconds latency). Note 0
63 * buffers don't work much further than the Bus error.
65 #define NUMBER_BUFFERS 2
67 static void destroy_buffers(struct private_osx_writer_data *powd)
72 powd->to->next = NULL;
82 static void init_buffers(struct private_osx_writer_data *powd)
88 for (i = 0; i < NUMBER_BUFFERS; i++) {
89 *ptrptr = malloc(sizeof(osx_buffer));
91 (*ptrptr)->remaining = 0;
92 (*ptrptr)->buffer = NULL;
93 ptrptr = &(*ptrptr)->next;
94 /* This buffer is ready for filling (of course, it is empty!) */
95 sem_post(powd->semaphore);
97 *ptrptr = powd->from = powd->to;
100 static void fill_buffer(osx_buffer *b, short *source, long size)
104 if (b->remaining) /* Non empty buffer, must still be playing */
106 PARA_INFO_LOG("%ld\n", size);
107 if (b->size != size) {
109 * Hey! What's that? Coudn't this buffer size be fixed
110 * once (well, perhaps we just didn't allocate it yet)
114 b->buffer = malloc(size * sizeof(float));
119 char *tmp = (char *)source;
123 /* *dest++ = ((*source++) + 32768) / 65536.0; */
124 *dest++ = (*source++) / 32768.0;
127 b->remaining = b->size;
130 static OSStatus osx_callback(void * inClientData,
131 __a_unused AudioUnitRenderActionFlags *inActionFlags,
132 __a_unused const AudioTimeStamp *inTimeStamp,
133 __a_unused UInt32 inBusNumber,
134 __a_unused UInt32 inNumFrames,
135 AudioBufferList *outOutputData)
140 struct private_osx_writer_data *powd = inClientData;
142 // PARA_INFO_LOG("%p\n", powd);
143 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
144 /* what we have to fill */
145 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
146 dest = outOutputData->mBuffers[i].mData;
148 if ((n = powd->from->remaining) <= 0) {
149 /* no more bytes in the current read buffer! */
150 while ((n = powd->from->remaining) <= 0)
151 /* wait for the results */
154 PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
156 * we dump what we can. In fact, just the necessary
157 * should be sufficient
161 memcpy(dest, powd->from->ptr, n * sizeof(float));
163 /* remember all done work */
165 powd->from->ptr += n;
166 if ((powd->from->remaining -= n) <= 0) {
167 /* tell that there's a buffer to fill */
168 sem_post(powd->semaphore);
169 powd->from = powd->from->next;
176 static int osx_writer_open(struct writer_node *wn)
178 struct private_osx_writer_data *powd = para_calloc(
179 sizeof(struct private_osx_writer_data));
180 ComponentDescription desc;
182 AURenderCallbackStruct inputCallback = {osx_callback, powd};
183 AudioStreamBasicDescription format;
186 struct writer_node_group *wng = wn->wng;
187 struct osx_write_args_info *conf = wn->conf;
189 wn->private_data = powd;
190 /* where did that default audio output go? */
191 desc.componentType = kAudioUnitType_Output;
192 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
193 /* NOTE: and if default output isn't Apple? */
194 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
195 desc.componentFlags = 0;
196 desc.componentFlagsMask = 0;
197 ret = -E_DEFAULT_COMP;
198 comp = FindNextComponent(NULL, &desc);
202 if (OpenAComponent(comp, &powd->output))
205 if (AudioUnitInitialize(powd->output))
210 /* Hmmm, let's choose PCM format */
211 /* We tell the Output Unit what format we're going to supply data to it.
212 * This is necessary if you're providing data through an input callback
213 * AND you want the DefaultOutputUnit to do any format conversions
214 * necessary from your format to the device's format.
216 if (!conf->samplerate_given && wng->samplerate)
217 powd->samplerate = *wng->samplerate;
219 powd->samplerate = conf->samplerate_arg;
220 format.mSampleRate = powd->samplerate;
221 /* The specific encoding type of audio stream*/
222 format.mFormatID = kAudioFormatLinearPCM;
223 /* flags specific to each format */
224 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
225 | kLinearPCMFormatFlagIsPacked
226 | kLinearPCMFormatFlagIsBigEndian;
227 if (!conf->channels_given && wng->channels)
228 powd->channels = *wng->channels;
230 powd->channels = conf->channels_arg;
231 format.mChannelsPerFrame = powd->channels;
232 format.mFramesPerPacket = 1;
233 format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
234 format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
235 /* one of the most constant constants of the whole computer history */
236 format.mBitsPerChannel = sizeof(float) * 8;
237 ret = -E_STREAM_FORMAT;
238 if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_StreamFormat,
239 kAudioUnitScope_Input, 0, &format,
240 sizeof(AudioStreamBasicDescription)))
242 /* init the semaphore */
243 strcpy(s, "/mpg123-0000");
250 } while ((powd->semaphore = sem_open(s, O_CREAT | O_EXCL, 0644, 0))
251 == (sem_t *)SEM_FAILED);
253 ret = -E_ADD_CALLBACK;
254 if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_SetRenderCallback,
255 kAudioUnitScope_Input, 0, &inputCallback,
256 sizeof(inputCallback)) < 0)
260 destroy_buffers(powd);
262 AudioUnitUninitialize(powd->output);
264 CloseComponent(powd->output);
269 __malloc void *osx_write_parse_config(char *options)
271 struct osx_write_args_info *conf
272 = para_calloc(sizeof(struct osx_write_args_info));
273 PARA_INFO_LOG("options: %s\n", options);
274 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
284 static void osx_writer_close(struct writer_node *wn)
286 struct private_osx_writer_data *powd = wn->private_data;
288 PARA_INFO_LOG("closing writer node %p\n", wn);
289 AudioOutputUnitStop(powd->output);
290 AudioUnitUninitialize(powd->output);
291 CloseComponent(powd->output);
292 destroy_buffers(powd);
293 sem_close(powd->semaphore);
297 static int need_new_buffer(struct writer_node *wn)
299 struct writer_node_group *wng = wn->wng;
300 struct private_osx_writer_data *powd = wn->private_data;
302 if (*wng->loaded < sizeof(short))
304 if (powd->to->remaining) /* Non empty buffer, must still be playing */
309 static int osx_write_post_select(__a_unused struct sched *s,
310 struct writer_node *wn)
312 struct private_osx_writer_data *powd = wn->private_data;
313 struct writer_node_group *wng = wn->wng;
314 short *data = (short*)wng->buf;
316 if (!need_new_buffer(wn))
318 fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
319 powd->to = powd->to->next;
320 wn->written = *wng->loaded;
322 if (AudioOutputUnitStart(powd->output))
329 static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
331 s->timeout.tv_sec = 0;
332 s->timeout.tv_usec = 20;
336 void osx_writer_init(struct writer *w)
338 w->open = osx_writer_open;
339 w->close = osx_writer_close;
340 w->pre_select = osx_write_pre_select;
341 w->post_select = osx_write_post_select;
342 w->parse_config = osx_write_parse_config;
343 w->shutdown = NULL; /* nothing to do */