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 */
57 * Tried with 3 buffers, but then any little window move is sufficient to
58 * stop the sound (OK, on a G3 400 with a Public Beta. Perhaps now we can
59 * go down to 2 buffers). With 16 buffers we have 1.5 seconds music
60 * buffered (or, if you're pessimistic, 1.5 seconds latency). Note 0
61 * buffers don't work much further than the Bus error.
63 #define NUMBER_BUFFERS 2
65 static void destroy_buffers(struct private_osx_writer_data *powd)
70 powd->to->next = NULL;
80 static void init_buffers(struct private_osx_writer_data *powd)
86 for (i = 0; i < NUMBER_BUFFERS; i++) {
87 *ptrptr = malloc(sizeof(osx_buffer));
89 (*ptrptr)->remaining = 0;
90 (*ptrptr)->buffer = NULL;
91 ptrptr = &(*ptrptr)->next;
92 /* This buffer is ready for filling (of course, it is empty!) */
93 sem_post(powd->semaphore);
95 *ptrptr = powd->from = powd->to;
98 static void fill_buffer(osx_buffer *b, short *source, long size)
102 if (b->remaining) /* Non empty buffer, must still be playing */
104 PARA_INFO_LOG("%ld\n", size);
105 if (b->size != size) {
107 * Hey! What's that? Coudn't this buffer size be fixed
108 * once (well, perhaps we just didn't allocate it yet)
112 b->buffer = malloc(size * sizeof(float));
117 char *tmp = (char *)source;
121 /* *dest++ = ((*source++) + 32768) / 65536.0; */
122 *dest++ = (*source++) / 32768.0;
125 b->remaining = b->size;
128 static OSStatus osx_callback(void * inClientData,
129 __a_unused AudioUnitRenderActionFlags *inActionFlags,
130 __a_unused const AudioTimeStamp *inTimeStamp,
131 __a_unused UInt32 inBusNumber,
132 __a_unused UInt32 inNumFrames,
133 AudioBufferList *outOutputData)
138 struct private_osx_writer_data *powd = inClientData;
140 // PARA_INFO_LOG("%p\n", powd);
141 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
142 /* what we have to fill */
143 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
144 dest = outOutputData->mBuffers[i].mData;
146 if ((n = powd->from->remaining) <= 0) {
147 /* no more bytes in the current read buffer! */
148 while ((n = powd->from->remaining) <= 0)
149 /* wait for the results */
152 PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
154 * we dump what we can. In fact, just the necessary
155 * should be sufficient
159 memcpy(dest, powd->from->ptr, n * sizeof(float));
161 /* remember all done work */
163 powd->from->ptr += n;
164 if ((powd->from->remaining -= n) <= 0) {
165 /* tell that there's a buffer to fill */
166 sem_post(powd->semaphore);
167 powd->from = powd->from->next;
174 static int osx_writer_open(struct writer_node *wn)
176 struct private_osx_writer_data *powd = para_calloc(
177 sizeof(struct private_osx_writer_data));
178 ComponentDescription desc;
180 AURenderCallbackStruct inputCallback = {osx_callback, powd};
181 AudioStreamBasicDescription format;
185 wn->private_data = powd;
186 /* where did that default audio output go? */
187 desc.componentType = kAudioUnitType_Output;
188 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
189 /* NOTE: and if default output isn't Apple? */
190 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
191 desc.componentFlags = 0;
192 desc.componentFlagsMask = 0;
193 ret = -E_DEFAULT_COMP;
194 comp = FindNextComponent(NULL, &desc);
198 if (OpenAComponent(comp, &powd->output))
201 if (AudioUnitInitialize(powd->output))
206 /* Hmmm, let's choose PCM format */
207 /* We tell the Output Unit what format we're going to supply data to it.
208 * This is necessary if you're providing data through an input callback
209 * AND you want the DefaultOutputUnit to do any format conversions
210 * necessary from your format to the device's format.
212 format.mSampleRate = 44100.0; /* The sample rate of the audio stream */
213 /* The specific encoding type of audio stream*/
214 format.mFormatID = kAudioFormatLinearPCM;
215 /* flags specific to each format */
216 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
217 | kLinearPCMFormatFlagIsPacked
218 | kLinearPCMFormatFlagIsBigEndian;
220 * We produce 2-channel audio. Now if we have a mega-super-hyper card for our
221 * audio, it is its problem to convert it to 8-, 16-, 32- or 1024-channel data.
223 format.mBytesPerFrame = (format.mFramesPerPacket = 1)
224 * (format.mBytesPerPacket = (format.mChannelsPerFrame = 2) * sizeof(float));
225 /* one of the most constant constants of the whole computer history */
226 format.mBitsPerChannel = sizeof(float) * 8;
227 ret = -E_STREAM_FORMAT;
228 if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_StreamFormat,
229 kAudioUnitScope_Input, 0, &format,
230 sizeof(AudioStreamBasicDescription)))
232 /* init the semaphore */
233 strcpy(s, "/mpg123-0000");
240 } while ((powd->semaphore = sem_open(s, O_CREAT | O_EXCL, 0644, 0))
241 == (sem_t *)SEM_FAILED);
243 ret = -E_ADD_CALLBACK;
244 if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_SetRenderCallback,
245 kAudioUnitScope_Input, 0, &inputCallback,
246 sizeof(inputCallback)) < 0)
250 destroy_buffers(powd);
252 AudioUnitUninitialize(powd->output);
254 CloseComponent(powd->output);
259 __malloc void *osx_write_parse_config(char *options)
261 struct osx_write_args_info *conf
262 = para_calloc(sizeof(struct osx_write_args_info));
263 PARA_INFO_LOG("options: %s\n", options);
264 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
274 static void osx_writer_close(struct writer_node *wn)
276 struct private_osx_writer_data *powd = wn->private_data;
278 PARA_INFO_LOG("closing writer node %p\n", wn);
279 AudioOutputUnitStop(powd->output);
280 AudioUnitUninitialize(powd->output);
281 CloseComponent(powd->output);
282 destroy_buffers(powd);
283 sem_close(powd->semaphore);
287 static int osx_write_post_select(__a_unused struct sched *s,
288 struct writer_node *wn)
290 struct private_osx_writer_data *powd = wn->private_data;
291 struct writer_node_group *wng = wn->wng;
292 short *data = (short*)wng->buf + wn->written;
294 if (*wng->loaded <= wn->written)
296 if (powd->to->remaining) /* Non empty buffer, must still be playing */
298 fill_buffer(powd->to, data, (*wng->loaded - wn->written) / sizeof(short));
299 powd->to = powd->to->next;
300 wn->written += (*wng->loaded - wn->written);
302 if (AudioOutputUnitStart(powd->output))
309 static int osx_write_pre_select(struct sched *s, struct writer_node *wn)
311 struct writer_node_group *wng = wn->wng;
312 struct private_osx_writer_data *powd = wn->private_data;
314 // if (!*wng->loaded)
316 // if (powd->to->remaining) /* Non empty buffer, must still be playing */
318 s->timeout.tv_sec = 0;
319 s->timeout.tv_usec = 20;
323 void osx_writer_init(struct writer *w)
325 w->open = osx_writer_open;
326 w->close = osx_writer_close;
327 w->pre_select = osx_write_pre_select;
328 w->post_select = osx_write_post_select;
329 w->parse_config = osx_write_parse_config;
330 w->shutdown = NULL; /* nothing to do */