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>
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 struct private_osx_write_data {
56 /** the main control structure for audio data manipulation */
58 /** non-zero if playback has started */
60 /** callback reads audio data from this buffer */
61 struct osx_buffer *from;
62 /* the post_select writes audio data here */
63 struct osx_buffer *to;
64 /** sample rate of the current audio stream */
66 /** number of channels of the current audio stream */
70 static void destroy_buffers(struct private_osx_write_data *powd)
72 struct osx_buffer *ptr;
73 struct osx_buffer *ptr2;
75 powd->to->next = NULL;
84 static void init_buffers(struct writer_node *wn)
86 struct private_osx_write_data *powd = wn->private_data;
87 struct osx_write_args_info *conf = wn->conf;
88 struct osx_buffer **ptrptr;
92 for (i = 0; i < conf->numbuffers_arg; i++) {
93 *ptrptr = malloc(sizeof(struct osx_buffer));
95 (*ptrptr)->remaining = 0;
96 (*ptrptr)->buffer = NULL;
97 ptrptr = &(*ptrptr)->next;
99 *ptrptr = powd->from = powd->to;
102 static void fill_buffer(struct osx_buffer *b, short *source, long size)
106 if (b->remaining) /* Non empty buffer, must still be playing */
108 if (b->size != size) {
109 b->buffer = para_realloc(b->buffer, size * sizeof(float));
114 char *tmp = (char *)source;
118 /* *dest++ = ((*source++) + 32768) / 65536.0; */
119 *dest++ = (*source++) / 32768.0;
122 b->remaining = b->size;
125 static OSStatus osx_callback(void * inClientData,
126 __a_unused AudioUnitRenderActionFlags *inActionFlags,
127 __a_unused const AudioTimeStamp *inTimeStamp,
128 __a_unused UInt32 inBusNumber,
129 __a_unused UInt32 inNumFrames,
130 AudioBufferList *outOutputData)
135 struct private_osx_write_data *powd = inClientData;
137 // PARA_INFO_LOG("%p\n", powd);
138 for (i = 0; i < outOutputData->mNumberBuffers; ++i) {
139 /* what we have to fill */
140 m = outOutputData->mBuffers[i].mDataByteSize / sizeof(float);
141 dest = outOutputData->mBuffers[i].mData;
143 if ((n = powd->from->remaining) <= 0) {
144 PARA_INFO_LOG("%s", "buffer underrun\n");
145 /* no more bytes in the current read buffer! */
146 while ((n = powd->from->remaining) <= 0)
147 /* wait for the results */
150 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
152 * we dump what we can. In fact, just the necessary
153 * should be sufficient
157 memcpy(dest, powd->from->ptr, n * sizeof(float));
159 /* remember all done work */
161 powd->from->ptr += n;
162 if ((powd->from->remaining -= n) <= 0)
163 powd->from = powd->from->next;
169 static int osx_write_open(struct writer_node *wn)
171 struct private_osx_write_data *powd = para_calloc(
172 sizeof(struct private_osx_write_data));
173 ComponentDescription desc;
175 AURenderCallbackStruct inputCallback = {osx_callback, powd};
176 AudioStreamBasicDescription format;
178 struct writer_node_group *wng = wn->wng;
179 struct osx_write_args_info *conf = wn->conf;
181 wn->private_data = powd;
182 /* where did that default audio output go? */
183 desc.componentType = kAudioUnitType_Output;
184 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
185 /* NOTE: and if default output isn't Apple? */
186 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
187 desc.componentFlags = 0;
188 desc.componentFlagsMask = 0;
189 ret = -E_DEFAULT_COMP;
190 comp = FindNextComponent(NULL, &desc);
194 if (OpenAComponent(comp, &powd->audio_unit))
197 if (AudioUnitInitialize(powd->audio_unit))
200 /* Hmmm, let's choose PCM format */
201 /* We tell the Output Unit what format we're going to supply data to it.
202 * This is necessary if you're providing data through an input callback
203 * AND you want the DefaultOutputUnit to do any format conversions
204 * necessary from your format to the device's format.
206 if (!conf->samplerate_given && wng->samplerate)
207 powd->samplerate = *wng->samplerate;
209 powd->samplerate = conf->samplerate_arg;
210 format.mSampleRate = powd->samplerate;
211 /* The specific encoding type of audio stream*/
212 format.mFormatID = kAudioFormatLinearPCM;
213 /* flags specific to each format */
214 format.mFormatFlags = kLinearPCMFormatFlagIsFloat
215 | kLinearPCMFormatFlagIsPacked
216 | kLinearPCMFormatFlagIsBigEndian;
217 if (!conf->channels_given && wng->channels)
218 powd->channels = *wng->channels;
220 powd->channels = conf->channels_arg;
221 format.mChannelsPerFrame = powd->channels;
222 format.mFramesPerPacket = 1;
223 format.mBytesPerPacket = format.mChannelsPerFrame * sizeof(float);
224 format.mBytesPerFrame = format.mFramesPerPacket * format.mBytesPerPacket;
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->audio_unit, kAudioUnitProperty_StreamFormat,
229 kAudioUnitScope_Input, 0, &format,
230 sizeof(AudioStreamBasicDescription)))
233 ret = -E_ADD_CALLBACK;
234 if (AudioUnitSetProperty(powd->audio_unit, kAudioUnitProperty_SetRenderCallback,
235 kAudioUnitScope_Input, 0, &inputCallback,
236 sizeof(inputCallback)) < 0)
240 destroy_buffers(powd);
242 AudioUnitUninitialize(powd->audio_unit);
244 CloseComponent(powd->audio_unit);
249 __malloc void *osx_write_parse_config(char *options)
251 struct osx_write_args_info *conf
252 = para_calloc(sizeof(struct osx_write_args_info));
253 PARA_INFO_LOG("options: %s\n", options);
254 int ret = osx_cmdline_parser_string(options, conf, "osx_write");
264 static void osx_write_close(struct writer_node *wn)
266 struct private_osx_write_data *powd = wn->private_data;
268 PARA_INFO_LOG("closing writer node %p\n", wn);
269 AudioOutputUnitStop(powd->audio_unit);
270 AudioUnitUninitialize(powd->audio_unit);
271 CloseComponent(powd->audio_unit);
272 destroy_buffers(powd);
276 static int need_new_buffer(struct writer_node *wn)
278 struct writer_node_group *wng = wn->wng;
279 struct private_osx_write_data *powd = wn->private_data;
281 if (*wng->loaded < sizeof(short))
283 if (powd->to->remaining) /* Non empty buffer, must still be playing */
288 static int osx_write_post_select(__a_unused struct sched *s,
289 struct writer_node *wn)
291 struct private_osx_write_data *powd = wn->private_data;
292 struct writer_node_group *wng = wn->wng;
293 short *data = (short*)wng->buf;
295 if (!need_new_buffer(wn))
297 fill_buffer(powd->to, data, *wng->loaded / sizeof(short));
298 powd->to = powd->to->next;
299 wn->written = *wng->loaded;
301 if (AudioOutputUnitStart(powd->audio_unit))
302 return -E_UNIT_START;
308 static int osx_write_pre_select(struct sched *s, __a_unused struct writer_node *wn)
310 struct private_osx_write_data *powd = wn->private_data;
311 struct writer_node_group *wng = wn->wng;
312 size_t numbytes = powd->to->remaining * sizeof(short);
313 struct timeval tmp = {.tv_sec = 1, .tv_usec = 0}, delay = tmp;
314 unsigned long divisor;
316 if (!numbytes && *wng->loaded >= sizeof(short))
317 goto min_delay; /* there's a buffer to fill */
320 divisor = powd->samplerate * powd->channels * 2 / numbytes;
322 tv_divide(divisor, &tmp, &delay);
323 if (tv_diff(&s->timeout, &delay, NULL) > 0)
325 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
326 // (long unsigned) s->timeout.tv_usec);
329 PARA_DEBUG_LOG("%s\n", "minimal delay");
330 s->timeout.tv_sec = 0;
331 s->timeout.tv_usec = 1;
335 /** the init function of the osx writer */
336 void osx_write_init(struct writer *w)
338 w->open = osx_write_open;
339 w->close = osx_write_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 */