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_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>
43 float *ptr; /* Where in the buffer are we? */
45 struct osx_buffer *next;
48 struct private_osx_write_data {
52 struct osx_buffer *from; /* Current buffers */
53 struct osx_buffer *to;
58 static void destroy_buffers(struct private_osx_write_data *powd)
60 struct osx_buffer *ptr;
61 struct osx_buffer *ptr2;
63 powd->to->next = NULL;
72 static void init_buffers(struct writer_node *wn)
74 struct private_osx_write_data *powd = wn->private_data;
75 struct osx_write_args_info *conf = wn->conf;
76 struct osx_buffer **ptrptr;
80 for (i = 0; i < conf->numbuffers_arg; i++) {
81 *ptrptr = malloc(sizeof(struct osx_buffer));
83 (*ptrptr)->remaining = 0;
84 (*ptrptr)->buffer = NULL;
85 ptrptr = &(*ptrptr)->next;
87 *ptrptr = powd->from = powd->to;
90 static void fill_buffer(struct osx_buffer *b, short *source, long size)
94 if (b->remaining) /* Non empty buffer, must still be playing */
96 if (b->size != size) {
97 b->buffer = para_realloc(b->buffer, size * sizeof(float));
102 char *tmp = (char *)source;
106 /* *dest++ = ((*source++) + 32768) / 65536.0; */
107 *dest++ = (*source++) / 32768.0;
110 b->remaining = b->size;
113 static OSStatus osx_callback(void * inClientData,
114 __a_unused AudioUnitRenderActionFlags *inActionFlags,
115 __a_unused const AudioTimeStamp *inTimeStamp,
116 __a_unused UInt32 inBusNumber,
117 __a_unused UInt32 inNumFrames,
118 AudioBufferList *outOutputData)
123 struct private_osx_write_data *powd = inClientData;
125 // PARA_INFO_LOG("%p\n", powd);
126 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
127 /* what we have to fill */
128 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
129 dest = outOutputData->mBuffers[i].mData;
131 if ((n = powd->from->remaining) <= 0) {
132 PARA_INFO_LOG("%s", "buffer underrun\n");
133 /* no more bytes in the current read buffer! */
134 while ((n = powd->from->remaining) <= 0)
135 /* wait for the results */
138 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
140 * we dump what we can. In fact, just the necessary
141 * should be sufficient
145 memcpy(dest, powd->from->ptr, n * sizeof(float));
147 /* remember all done work */
149 powd->from->ptr += n;
150 if ((powd->from->remaining -= n) <= 0)
151 powd->from = powd->from->next;
157 static int osx_write_open(struct writer_node *wn)
159 struct private_osx_write_data *powd = para_calloc(
160 sizeof(struct private_osx_write_data));
161 ComponentDescription desc;
163 AURenderCallbackStruct inputCallback = {osx_callback, powd};
164 AudioStreamBasicDescription format;
166 struct writer_node_group *wng = wn->wng;
167 struct osx_write_args_info *conf = wn->conf;
169 wn->private_data = powd;
170 /* where did that default audio output go? */
171 desc.componentType = kAudioUnitType_Output;
172 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
173 /* NOTE: and if default output isn't Apple? */
174 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
175 desc.componentFlags = 0;
176 desc.componentFlagsMask = 0;
177 ret = -E_DEFAULT_COMP;
178 comp = FindNextComponent(NULL, &desc);
182 if (OpenAComponent(comp, &powd->output))
185 if (AudioUnitInitialize(powd->output))
189 /* Hmmm, let's choose PCM format */
190 /* We tell the Output Unit what format we're going to supply data to it.
191 * This is necessary if you're providing data through an input callback
192 * AND you want the DefaultOutputUnit to do any format conversions
193 * necessary from your format to the device's format.
195 if (!conf->samplerate_given && wng->samplerate)
196 powd->samplerate = *wng->samplerate;
198 powd->samplerate = conf->samplerate_arg;
199 format.mSampleRate = powd->samplerate;
200 /* The specific encoding type of audio stream*/
201 format.mFormatID = kAudioFormatLinearPCM;
202 /* flags specific to each format */
203 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
204 | kLinearPCMFormatFlagIsPacked
205 | kLinearPCMFormatFlagIsBigEndian;
206 if (!conf->channels_given && wng->channels)
207 powd->channels = *wng->channels;
209 powd->channels = conf->channels_arg;
210 format.mChannelsPerFrame = powd->channels;
211 format.mFramesPerPacket = 1;
212 format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
213 format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
214 /* one of the most constant constants of the whole computer history */
215 format.mBitsPerChannel = sizeof(float) * 8;
216 ret = -E_STREAM_FORMAT;
217 if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_StreamFormat,
218 kAudioUnitScope_Input, 0, &format,
219 sizeof(AudioStreamBasicDescription)))
222 ret = -E_ADD_CALLBACK;
223 if (AudioUnitSetProperty(powd->output, kAudioUnitProperty_SetRenderCallback,
224 kAudioUnitScope_Input, 0, &inputCallback,
225 sizeof(inputCallback)) < 0)
229 destroy_buffers(powd);
231 AudioUnitUninitialize(powd->output);
233 CloseComponent(powd->output);
238 __malloc void *osx_write_parse_config(char *options)
240 struct osx_write_args_info *conf
241 = para_calloc(sizeof(struct osx_write_args_info));
242 PARA_INFO_LOG("options: %s\n", options);
243 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
253 static void osx_write_close(struct writer_node *wn)
255 struct private_osx_write_data *powd = wn->private_data;
257 PARA_INFO_LOG("closing writer node %p\n", wn);
258 AudioOutputUnitStop(powd->output);
259 AudioUnitUninitialize(powd->output);
260 CloseComponent(powd->output);
261 destroy_buffers(powd);
265 static int need_new_buffer(struct writer_node *wn)
267 struct writer_node_group *wng = wn->wng;
268 struct private_osx_write_data *powd = wn->private_data;
270 if (*wng->loaded < sizeof(short))
272 if (powd->to->remaining) /* Non empty buffer, must still be playing */
277 static int osx_write_post_select(__a_unused struct sched *s,
278 struct writer_node *wn)
280 struct private_osx_write_data *powd = wn->private_data;
281 struct writer_node_group *wng = wn->wng;
282 short *data = (short*)wng->buf;
284 if (!need_new_buffer(wn))
286 fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
287 powd->to = powd->to->next;
288 wn->written = *wng->loaded;
290 if (AudioOutputUnitStart(powd->output))
291 return -E_UNIT_START;
297 static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
299 struct private_osx_write_data *powd = wn->private_data;
300 struct writer_node_group *wng = wn->wng;
301 size_t numbytes = powd->to->remaining * sizeof(short);
302 struct timeval tmp = {.tv_sec = 1, .tv_usec = 0}, delay = tmp;
303 unsigned long divisor;
305 if (!numbytes && *wng->loaded >= sizeof(short))
306 goto min_delay; /* there's a buffer to fill */
309 divisor = powd->samplerate * powd->channels * 2 / numbytes;
311 tv_divide(divisor, &tmp, &delay);
312 if (tv_diff(&s->timeout, &delay, NULL) > 0)
314 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
315 // (long unsigned) s->timeout.tv_usec);
318 PARA_DEBUG_LOG("%s\n", "minimal delay");
319 s->timeout.tv_sec = 0;
320 s->timeout.tv_usec = 1;
324 void osx_write_init(struct writer *w)
326 w->open = osx_write_open;
327 w->close = osx_write_close;
328 w->pre_select = osx_write_pre_select;
329 w->post_select = osx_write_post_select;
330 w->parse_config = osx_write_parse_config;
331 w->shutdown = NULL; /* nothing to do */