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
;
47 typedef struct osx_buffer osx_buffer
;
49 struct private_osx_write_data
{
54 osx_buffer
*from
; /* Current buffers */
62 * Tried with 3 buffers, but then any little window move is sufficient to
63 * stop the sound (OK, on a G3 400 with a Public Beta. Perhaps now we can
64 * go down to 2 buffers). With 16 buffers we have 1.5 seconds music
65 * buffered (or, if you're pessimistic, 1.5 seconds latency). Note 0
66 * buffers don't work much further than the Bus error.
68 #define NUMBER_BUFFERS 2
70 static void destroy_buffers(struct private_osx_write_data
*powd
)
75 powd
->to
->next
= NULL
;
84 static void init_buffers(struct private_osx_write_data
*powd
)
90 for (i
= 0; i
< NUMBER_BUFFERS
; i
++) {
91 *ptrptr
= malloc(sizeof(osx_buffer
));
93 (*ptrptr
)->remaining
= 0;
94 (*ptrptr
)->buffer
= NULL
;
95 ptrptr
= &(*ptrptr
)->next
;
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)
113 b
->buffer
= malloc(size
* sizeof(float));
118 char *tmp
= (char *)source
;
122 /* *dest++ = ((*source++) + 32768) / 65536.0; */
123 *dest
++ = (*source
++) / 32768.0;
126 b
->remaining
= b
->size
;
129 static OSStatus
osx_callback(void * inClientData
,
130 __a_unused AudioUnitRenderActionFlags
*inActionFlags
,
131 __a_unused
const AudioTimeStamp
*inTimeStamp
,
132 __a_unused UInt32 inBusNumber
,
133 __a_unused UInt32 inNumFrames
,
134 AudioBufferList
*outOutputData
)
139 struct private_osx_write_data
*powd
= inClientData
;
141 // PARA_INFO_LOG("%p\n", powd);
142 for (i
= 0; i
< outOutputData
->mNumberBuffers
; ++i
) {
143 /* what we have to fill */
144 m
= outOutputData
->mBuffers
[i
].mDataByteSize
/ sizeof(float);
145 dest
= outOutputData
->mBuffers
[i
].mData
;
147 if ((n
= powd
->from
->remaining
) <= 0) {
148 PARA_INFO_LOG("%s", "buffer underrun\n");
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 powd
->from
= powd
->from
->next
;
173 static int osx_write_open(struct writer_node
*wn
)
175 struct private_osx_write_data
*powd
= para_calloc(
176 sizeof(struct private_osx_write_data
));
177 ComponentDescription desc
;
179 AURenderCallbackStruct inputCallback
= {osx_callback
, powd
};
180 AudioStreamBasicDescription format
;
182 struct writer_node_group
*wng
= wn
->wng
;
183 struct osx_write_args_info
*conf
= wn
->conf
;
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 if (!conf
->samplerate_given
&& wng
->samplerate
)
213 powd
->samplerate
= *wng
->samplerate
;
215 powd
->samplerate
= conf
->samplerate_arg
;
216 format
.mSampleRate
= powd
->samplerate
;
217 /* The specific encoding type of audio stream*/
218 format
.mFormatID
= kAudioFormatLinearPCM
;
219 /* flags specific to each format */
220 format
.mFormatFlags
= kLinearPCMFormatFlagIsFloat
221 | kLinearPCMFormatFlagIsPacked
222 | kLinearPCMFormatFlagIsBigEndian
;
223 if (!conf
->channels_given
&& wng
->channels
)
224 powd
->channels
= *wng
->channels
;
226 powd
->channels
= conf
->channels_arg
;
227 format
.mChannelsPerFrame
= powd
->channels
;
228 format
.mFramesPerPacket
= 1;
229 format
.mBytesPerPacket
= format
.mChannelsPerFrame
* sizeof(float);
230 format
.mBytesPerFrame
= format
.mFramesPerPacket
* format
.mBytesPerPacket
;
231 /* one of the most constant constants of the whole computer history */
232 format
.mBitsPerChannel
= sizeof(float) * 8;
233 ret
= -E_STREAM_FORMAT
;
234 if (AudioUnitSetProperty(powd
->output
, kAudioUnitProperty_StreamFormat
,
235 kAudioUnitScope_Input
, 0, &format
,
236 sizeof(AudioStreamBasicDescription
)))
239 ret
= -E_ADD_CALLBACK
;
240 if (AudioUnitSetProperty(powd
->output
, kAudioUnitProperty_SetRenderCallback
,
241 kAudioUnitScope_Input
, 0, &inputCallback
,
242 sizeof(inputCallback
)) < 0)
246 destroy_buffers(powd
);
248 AudioUnitUninitialize(powd
->output
);
250 CloseComponent(powd
->output
);
255 __malloc
void *osx_write_parse_config(char *options
)
257 struct osx_write_args_info
*conf
258 = para_calloc(sizeof(struct osx_write_args_info
));
259 PARA_INFO_LOG("options: %s\n", options
);
260 int ret
= osx_cmdline_parser_string(options
, conf
, "osx_write");
270 static void osx_write_close(struct writer_node
*wn
)
272 struct private_osx_write_data
*powd
= wn
->private_data
;
274 PARA_INFO_LOG("closing writer node %p\n", wn
);
275 AudioOutputUnitStop(powd
->output
);
276 AudioUnitUninitialize(powd
->output
);
277 CloseComponent(powd
->output
);
278 destroy_buffers(powd
);
282 static int need_new_buffer(struct writer_node
*wn
)
284 struct writer_node_group
*wng
= wn
->wng
;
285 struct private_osx_write_data
*powd
= wn
->private_data
;
287 if (*wng
->loaded
< sizeof(short))
289 if (powd
->to
->remaining
) /* Non empty buffer, must still be playing */
294 static int osx_write_post_select(__a_unused
struct sched
*s
,
295 struct writer_node
*wn
)
297 struct private_osx_write_data
*powd
= wn
->private_data
;
298 struct writer_node_group
*wng
= wn
->wng
;
299 short *data
= (short*)wng
->buf
;
301 if (!need_new_buffer(wn
))
303 fill_buffer(powd
->to
, data
, *wng
->loaded
/ sizeof(short));
304 powd
->to
= powd
->to
->next
;
305 wn
->written
= *wng
->loaded
;
307 if (AudioOutputUnitStart(powd
->output
))
308 return -E_UNIT_START
;
314 static int osx_write_pre_select(struct sched
*s
, __a_unused
struct writer_node
*wn
)
316 s
->timeout
.tv_sec
= 0;
317 s
->timeout
.tv_usec
= 20;
321 void osx_write_init(struct writer
*w
)
323 w
->open
= osx_write_open
;
324 w
->close
= osx_write_close
;
325 w
->pre_select
= osx_write_pre_select
;
326 w
->post_select
= osx_write_post_select
;
327 w
->parse_config
= osx_write_parse_config
;
328 w
->shutdown
= NULL
; /* nothing to do */