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>
38 float *ptr
; /* Where in the buffer are we? */
40 struct osx_buffer
*next
;
42 typedef struct osx_buffer osx_buffer
;
44 struct private_osx_writer_data
{
49 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!) */
94 *ptrptr
= powd
->from
= powd
->to
;
97 static void fill_buffer(osx_buffer
*b
, short *source
, long size
)
101 if (b
->remaining
) /* Non empty buffer, must still be playing */
103 PARA_INFO_LOG("%ld\n", size
);
104 if (b
->size
!= size
) {
106 * Hey! What's that? Coudn't this buffer size be fixed
107 * once (well, perhaps we just didn't allocate it yet)
111 b
->buffer
= malloc(size
* sizeof(float));
116 char *tmp
= (char *)source
;
120 /* *dest++ = ((*source++) + 32768) / 65536.0; */
121 *dest
++ = (*source
++) / 32768.0;
124 b
->remaining
= b
->size
;
127 static OSStatus
osx_callback(void * inClientData
,
128 __a_unused AudioUnitRenderActionFlags
*inActionFlags
,
129 __a_unused
const AudioTimeStamp
*inTimeStamp
,
130 __a_unused UInt32 inBusNumber
,
131 __a_unused UInt32 inNumFrames
,
132 AudioBufferList
*outOutputData
)
137 struct private_osx_writer_data
*powd
= inClientData
;
139 // PARA_INFO_LOG("%p\n", powd);
140 for (i
= 0; i
< outOutputData
->mNumberBuffers
; ++i
) {
141 /* what we have to fill */
142 m
= outOutputData
->mBuffers
[i
].mDataByteSize
/ sizeof(float);
143 dest
= outOutputData
->mBuffers
[i
].mData
;
145 if ((n
= powd
->from
->remaining
) <= 0) {
146 PARA_INFO_LOG("%s", "buffer underrun\n");
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 powd
->from
= powd
->from
->next
;
171 static int osx_writer_open(struct writer_node
*wn
)
173 struct private_osx_writer_data
*powd
= para_calloc(
174 sizeof(struct private_osx_writer_data
));
175 ComponentDescription desc
;
177 AURenderCallbackStruct inputCallback
= {osx_callback
, powd
};
178 AudioStreamBasicDescription format
;
180 struct writer_node_group
*wng
= wn
->wng
;
181 struct osx_write_args_info
*conf
= wn
->conf
;
183 wn
->private_data
= powd
;
184 /* where did that default audio output go? */
185 desc
.componentType
= kAudioUnitType_Output
;
186 desc
.componentSubType
= kAudioUnitSubType_DefaultOutput
;
187 /* NOTE: and if default output isn't Apple? */
188 desc
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
189 desc
.componentFlags
= 0;
190 desc
.componentFlagsMask
= 0;
191 ret
= -E_DEFAULT_COMP
;
192 comp
= FindNextComponent(NULL
, &desc
);
196 if (OpenAComponent(comp
, &powd
->output
))
199 if (AudioUnitInitialize(powd
->output
))
204 /* Hmmm, let's choose PCM format */
205 /* We tell the Output Unit what format we're going to supply data to it.
206 * This is necessary if you're providing data through an input callback
207 * AND you want the DefaultOutputUnit to do any format conversions
208 * necessary from your format to the device's format.
210 if (!conf
->samplerate_given
&& wng
->samplerate
)
211 powd
->samplerate
= *wng
->samplerate
;
213 powd
->samplerate
= conf
->samplerate_arg
;
214 format
.mSampleRate
= powd
->samplerate
;
215 /* The specific encoding type of audio stream*/
216 format
.mFormatID
= kAudioFormatLinearPCM
;
217 /* flags specific to each format */
218 format
.mFormatFlags
= kLinearPCMFormatFlagIsFloat
219 | kLinearPCMFormatFlagIsPacked
220 | kLinearPCMFormatFlagIsBigEndian
;
221 if (!conf
->channels_given
&& wng
->channels
)
222 powd
->channels
= *wng
->channels
;
224 powd
->channels
= conf
->channels_arg
;
225 format
.mChannelsPerFrame
= powd
->channels
;
226 format
.mFramesPerPacket
= 1;
227 format
.mBytesPerPacket
= format
.mChannelsPerFrame
* sizeof(float);
228 format
.mBytesPerFrame
= format
.mFramesPerPacket
* format
.mBytesPerPacket
;
229 /* one of the most constant constants of the whole computer history */
230 format
.mBitsPerChannel
= sizeof(float) * 8;
231 ret
= -E_STREAM_FORMAT
;
232 if (AudioUnitSetProperty(powd
->output
, kAudioUnitProperty_StreamFormat
,
233 kAudioUnitScope_Input
, 0, &format
,
234 sizeof(AudioStreamBasicDescription
)))
237 ret
= -E_ADD_CALLBACK
;
238 if (AudioUnitSetProperty(powd
->output
, kAudioUnitProperty_SetRenderCallback
,
239 kAudioUnitScope_Input
, 0, &inputCallback
,
240 sizeof(inputCallback
)) < 0)
244 destroy_buffers(powd
);
246 AudioUnitUninitialize(powd
->output
);
248 CloseComponent(powd
->output
);
253 __malloc
void *osx_write_parse_config(char *options
)
255 struct osx_write_args_info
*conf
256 = para_calloc(sizeof(struct osx_write_args_info
));
257 PARA_INFO_LOG("options: %s\n", options
);
258 int ret
= osx_cmdline_parser_string(options
, conf
, "osx_write");
268 static void osx_writer_close(struct writer_node
*wn
)
270 struct private_osx_writer_data
*powd
= wn
->private_data
;
272 PARA_INFO_LOG("closing writer node %p\n", wn
);
273 AudioOutputUnitStop(powd
->output
);
274 AudioUnitUninitialize(powd
->output
);
275 CloseComponent(powd
->output
);
276 destroy_buffers(powd
);
280 static int need_new_buffer(struct writer_node
*wn
)
282 struct writer_node_group
*wng
= wn
->wng
;
283 struct private_osx_writer_data
*powd
= wn
->private_data
;
285 if (*wng
->loaded
< sizeof(short))
287 if (powd
->to
->remaining
) /* Non empty buffer, must still be playing */
292 static int osx_write_post_select(__a_unused
struct sched
*s
,
293 struct writer_node
*wn
)
295 struct private_osx_writer_data
*powd
= wn
->private_data
;
296 struct writer_node_group
*wng
= wn
->wng
;
297 short *data
= (short*)wng
->buf
;
299 if (!need_new_buffer(wn
))
301 fill_buffer(powd
->to
, data
, *wng
->loaded
/ sizeof(short));
302 powd
->to
= powd
->to
->next
;
303 wn
->written
= *wng
->loaded
;
305 if (AudioOutputUnitStart(powd
->output
))
312 static int osx_write_pre_select(struct sched
*s
, __a_unused
struct writer_node
*wn
)
314 s
->timeout
.tv_sec
= 0;
315 s
->timeout
.tv_usec
= 20;
319 void osx_writer_init(struct writer
*w
)
321 w
->open
= osx_writer_open
;
322 w
->close
= osx_writer_close
;
323 w
->pre_select
= osx_write_pre_select
;
324 w
->post_select
= osx_write_post_select
;
325 w
->parse_config
= osx_write_parse_config
;
326 w
->shutdown
= NULL
; /* nothing to do */