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
;
60 * Tried with 3 buffers, but then any little window move is sufficient to
61 * stop the sound (OK, on a G3 400 with a Public Beta. Perhaps now we can
62 * go down to 2 buffers). With 16 buffers we have 1.5 seconds music
63 * buffered (or, if you're pessimistic, 1.5 seconds latency). Note 0
64 * buffers don't work much further than the Bus error.
66 #define NUMBER_BUFFERS 2
68 static void destroy_buffers(struct private_osx_write_data
*powd
)
70 struct osx_buffer
*ptr
;
71 struct osx_buffer
*ptr2
;
73 powd
->to
->next
= NULL
;
82 static void init_buffers(struct private_osx_write_data
*powd
)
86 struct osx_buffer
**ptrptr
;
88 for (i
= 0; i
< NUMBER_BUFFERS
; i
++) {
89 *ptrptr
= malloc(sizeof(struct osx_buffer
));
91 (*ptrptr
)->remaining
= 0;
92 (*ptrptr
)->buffer
= NULL
;
93 ptrptr
= &(*ptrptr
)->next
;
95 *ptrptr
= powd
->from
= powd
->to
;
98 static void fill_buffer(struct osx_buffer
*b
, short *source
, long size
)
102 if (b
->remaining
) /* Non empty buffer, must still be playing */
104 if (b
->size
!= size
) {
105 b
->buffer
= para_realloc(b
->buffer
, size
* sizeof(float));
110 char *tmp
= (char *)source
;
114 /* *dest++ = ((*source++) + 32768) / 65536.0; */
115 *dest
++ = (*source
++) / 32768.0;
118 b
->remaining
= b
->size
;
121 static OSStatus
osx_callback(void * inClientData
,
122 __a_unused AudioUnitRenderActionFlags
*inActionFlags
,
123 __a_unused
const AudioTimeStamp
*inTimeStamp
,
124 __a_unused UInt32 inBusNumber
,
125 __a_unused UInt32 inNumFrames
,
126 AudioBufferList
*outOutputData
)
131 struct private_osx_write_data
*powd
= inClientData
;
133 // PARA_INFO_LOG("%p\n", powd);
134 for (i
= 0; i
< outOutputData
->mNumberBuffers
; ++i
) {
135 /* what we have to fill */
136 m
= outOutputData
->mBuffers
[i
].mDataByteSize
/ sizeof(float);
137 dest
= outOutputData
->mBuffers
[i
].mData
;
139 if ((n
= powd
->from
->remaining
) <= 0) {
140 PARA_INFO_LOG("%s", "buffer underrun\n");
141 /* no more bytes in the current read buffer! */
142 while ((n
= powd
->from
->remaining
) <= 0)
143 /* wait for the results */
146 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
148 * we dump what we can. In fact, just the necessary
149 * should be sufficient
153 memcpy(dest
, powd
->from
->ptr
, n
* sizeof(float));
155 /* remember all done work */
157 powd
->from
->ptr
+= n
;
158 if ((powd
->from
->remaining
-= n
) <= 0)
159 powd
->from
= powd
->from
->next
;
165 static int osx_write_open(struct writer_node
*wn
)
167 struct private_osx_write_data
*powd
= para_calloc(
168 sizeof(struct private_osx_write_data
));
169 ComponentDescription desc
;
171 AURenderCallbackStruct inputCallback
= {osx_callback
, powd
};
172 AudioStreamBasicDescription format
;
174 struct writer_node_group
*wng
= wn
->wng
;
175 struct osx_write_args_info
*conf
= wn
->conf
;
177 wn
->private_data
= powd
;
178 /* where did that default audio output go? */
179 desc
.componentType
= kAudioUnitType_Output
;
180 desc
.componentSubType
= kAudioUnitSubType_DefaultOutput
;
181 /* NOTE: and if default output isn't Apple? */
182 desc
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
183 desc
.componentFlags
= 0;
184 desc
.componentFlagsMask
= 0;
185 ret
= -E_DEFAULT_COMP
;
186 comp
= FindNextComponent(NULL
, &desc
);
190 if (OpenAComponent(comp
, &powd
->output
))
193 if (AudioUnitInitialize(powd
->output
))
197 /* Hmmm, let's choose PCM format */
198 /* We tell the Output Unit what format we're going to supply data to it.
199 * This is necessary if you're providing data through an input callback
200 * AND you want the DefaultOutputUnit to do any format conversions
201 * necessary from your format to the device's format.
203 if (!conf
->samplerate_given
&& wng
->samplerate
)
204 powd
->samplerate
= *wng
->samplerate
;
206 powd
->samplerate
= conf
->samplerate_arg
;
207 format
.mSampleRate
= powd
->samplerate
;
208 /* The specific encoding type of audio stream*/
209 format
.mFormatID
= kAudioFormatLinearPCM
;
210 /* flags specific to each format */
211 format
.mFormatFlags
= kLinearPCMFormatFlagIsFloat
212 | kLinearPCMFormatFlagIsPacked
213 | kLinearPCMFormatFlagIsBigEndian
;
214 if (!conf
->channels_given
&& wng
->channels
)
215 powd
->channels
= *wng
->channels
;
217 powd
->channels
= conf
->channels_arg
;
218 format
.mChannelsPerFrame
= powd
->channels
;
219 format
.mFramesPerPacket
= 1;
220 format
.mBytesPerPacket
= format
.mChannelsPerFrame
* sizeof(float);
221 format
.mBytesPerFrame
= format
.mFramesPerPacket
* format
.mBytesPerPacket
;
222 /* one of the most constant constants of the whole computer history */
223 format
.mBitsPerChannel
= sizeof(float) * 8;
224 ret
= -E_STREAM_FORMAT
;
225 if (AudioUnitSetProperty(powd
->output
, kAudioUnitProperty_StreamFormat
,
226 kAudioUnitScope_Input
, 0, &format
,
227 sizeof(AudioStreamBasicDescription
)))
230 ret
= -E_ADD_CALLBACK
;
231 if (AudioUnitSetProperty(powd
->output
, kAudioUnitProperty_SetRenderCallback
,
232 kAudioUnitScope_Input
, 0, &inputCallback
,
233 sizeof(inputCallback
)) < 0)
237 destroy_buffers(powd
);
239 AudioUnitUninitialize(powd
->output
);
241 CloseComponent(powd
->output
);
246 __malloc
void *osx_write_parse_config(char *options
)
248 struct osx_write_args_info
*conf
249 = para_calloc(sizeof(struct osx_write_args_info
));
250 PARA_INFO_LOG("options: %s\n", options
);
251 int ret
= osx_cmdline_parser_string(options
, conf
, "osx_write");
261 static void osx_write_close(struct writer_node
*wn
)
263 struct private_osx_write_data
*powd
= wn
->private_data
;
265 PARA_INFO_LOG("closing writer node %p\n", wn
);
266 AudioOutputUnitStop(powd
->output
);
267 AudioUnitUninitialize(powd
->output
);
268 CloseComponent(powd
->output
);
269 destroy_buffers(powd
);
273 static int need_new_buffer(struct writer_node
*wn
)
275 struct writer_node_group
*wng
= wn
->wng
;
276 struct private_osx_write_data
*powd
= wn
->private_data
;
278 if (*wng
->loaded
< sizeof(short))
280 if (powd
->to
->remaining
) /* Non empty buffer, must still be playing */
285 static int osx_write_post_select(__a_unused
struct sched
*s
,
286 struct writer_node
*wn
)
288 struct private_osx_write_data
*powd
= wn
->private_data
;
289 struct writer_node_group
*wng
= wn
->wng
;
290 short *data
= (short*)wng
->buf
;
292 if (!need_new_buffer(wn
))
294 fill_buffer(powd
->to
, data
, *wng
->loaded
/ sizeof(short));
295 powd
->to
= powd
->to
->next
;
296 wn
->written
= *wng
->loaded
;
298 if (AudioOutputUnitStart(powd
->output
))
299 return -E_UNIT_START
;
305 static int osx_write_pre_select(struct sched
*s
, __a_unused
struct writer_node
*wn
)
307 struct private_osx_write_data
*powd
= wn
->private_data
;
308 struct writer_node_group
*wng
= wn
->wng
;
309 size_t numbytes
= powd
->to
->remaining
* sizeof(short);
310 struct timeval tmp
= {.tv_sec
= 1, .tv_usec
= 0}, delay
= tmp
;
311 unsigned long divisor
;
313 if (!numbytes
&& *wng
->loaded
>= sizeof(short))
314 goto min_delay
; /* there's a buffer to fill */
317 divisor
= powd
->samplerate
* powd
->channels
* 2 / numbytes
;
319 tv_divide(divisor
, &tmp
, &delay
);
320 if (tv_diff(&s
->timeout
, &delay
, NULL
) > 0)
322 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
323 // (long unsigned) s->timeout.tv_usec);
326 PARA_DEBUG_LOG("%s\n", "minimal delay");
327 s
->timeout
.tv_sec
= 0;
328 s
->timeout
.tv_usec
= 1;
332 void osx_write_init(struct writer
*w
)
334 w
->open
= osx_write_open
;
335 w
->close
= osx_write_close
;
336 w
->pre_select
= osx_write_pre_select
;
337 w
->post_select
= osx_write_post_select
;
338 w
->parse_config
= osx_write_parse_config
;
339 w
->shutdown
= NULL
; /* nothing to do */