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
{
58 struct osx_buffer
*from
; /* Current buffers */
59 struct osx_buffer
*to
;
64 static void destroy_buffers(struct private_osx_write_data
*powd
)
66 struct osx_buffer
*ptr
;
67 struct osx_buffer
*ptr2
;
69 powd
->to
->next
= NULL
;
78 static void init_buffers(struct writer_node
*wn
)
80 struct private_osx_write_data
*powd
= wn
->private_data
;
81 struct osx_write_args_info
*conf
= wn
->conf
;
82 struct osx_buffer
**ptrptr
;
86 for (i
= 0; i
< conf
->numbuffers_arg
; i
++) {
87 *ptrptr
= malloc(sizeof(struct osx_buffer
));
89 (*ptrptr
)->remaining
= 0;
90 (*ptrptr
)->buffer
= NULL
;
91 ptrptr
= &(*ptrptr
)->next
;
93 *ptrptr
= powd
->from
= powd
->to
;
96 static void fill_buffer(struct osx_buffer
*b
, short *source
, long size
)
100 if (b
->remaining
) /* Non empty buffer, must still be playing */
102 if (b
->size
!= size
) {
103 b
->buffer
= para_realloc(b
->buffer
, size
* sizeof(float));
108 char *tmp
= (char *)source
;
112 /* *dest++ = ((*source++) + 32768) / 65536.0; */
113 *dest
++ = (*source
++) / 32768.0;
116 b
->remaining
= b
->size
;
119 static OSStatus
osx_callback(void * inClientData
,
120 __a_unused AudioUnitRenderActionFlags
*inActionFlags
,
121 __a_unused
const AudioTimeStamp
*inTimeStamp
,
122 __a_unused UInt32 inBusNumber
,
123 __a_unused UInt32 inNumFrames
,
124 AudioBufferList
*outOutputData
)
129 struct private_osx_write_data
*powd
= inClientData
;
131 // PARA_INFO_LOG("%p\n", powd);
132 for (i
= 0; i
< outOutputData
->mNumberBuffers
; ++i
) {
133 /* what we have to fill */
134 m
= outOutputData
->mBuffers
[i
].mDataByteSize
/ sizeof(float);
135 dest
= outOutputData
->mBuffers
[i
].mData
;
137 if ((n
= powd
->from
->remaining
) <= 0) {
138 PARA_INFO_LOG("%s", "buffer underrun\n");
139 /* no more bytes in the current read buffer! */
140 while ((n
= powd
->from
->remaining
) <= 0)
141 /* wait for the results */
144 // PARA_INFO_LOG("buf %p: n = %ld, m= %ld\n", powd->from->buffer, n, m);
146 * we dump what we can. In fact, just the necessary
147 * should be sufficient
151 memcpy(dest
, powd
->from
->ptr
, n
* sizeof(float));
153 /* remember all done work */
155 powd
->from
->ptr
+= n
;
156 if ((powd
->from
->remaining
-= n
) <= 0)
157 powd
->from
= powd
->from
->next
;
163 static int osx_write_open(struct writer_node
*wn
)
165 struct private_osx_write_data
*powd
= para_calloc(
166 sizeof(struct private_osx_write_data
));
167 ComponentDescription desc
;
169 AURenderCallbackStruct inputCallback
= {osx_callback
, powd
};
170 AudioStreamBasicDescription format
;
172 struct writer_node_group
*wng
= wn
->wng
;
173 struct osx_write_args_info
*conf
= wn
->conf
;
175 wn
->private_data
= powd
;
176 /* where did that default audio output go? */
177 desc
.componentType
= kAudioUnitType_Output
;
178 desc
.componentSubType
= kAudioUnitSubType_DefaultOutput
;
179 /* NOTE: and if default output isn't Apple? */
180 desc
.componentManufacturer
= kAudioUnitManufacturer_Apple
;
181 desc
.componentFlags
= 0;
182 desc
.componentFlagsMask
= 0;
183 ret
= -E_DEFAULT_COMP
;
184 comp
= FindNextComponent(NULL
, &desc
);
188 if (OpenAComponent(comp
, &powd
->output
))
191 if (AudioUnitInitialize(powd
->output
))
194 /* Hmmm, let's choose PCM format */
195 /* We tell the Output Unit what format we're going to supply data to it.
196 * This is necessary if you're providing data through an input callback
197 * AND you want the DefaultOutputUnit to do any format conversions
198 * necessary from your format to the device's format.
200 if (!conf
->samplerate_given
&& wng
->samplerate
)
201 powd
->samplerate
= *wng
->samplerate
;
203 powd
->samplerate
= conf
->samplerate_arg
;
204 format
.mSampleRate
= powd
->samplerate
;
205 /* The specific encoding type of audio stream*/
206 format
.mFormatID
= kAudioFormatLinearPCM
;
207 /* flags specific to each format */
208 format
.mFormatFlags
= kLinearPCMFormatFlagIsFloat
209 | kLinearPCMFormatFlagIsPacked
210 | kLinearPCMFormatFlagIsBigEndian
;
211 if (!conf
->channels_given
&& wng
->channels
)
212 powd
->channels
= *wng
->channels
;
214 powd
->channels
= conf
->channels_arg
;
215 format
.mChannelsPerFrame
= powd
->channels
;
216 format
.mFramesPerPacket
= 1;
217 format
.mBytesPerPacket
= format
.mChannelsPerFrame
* sizeof(float);
218 format
.mBytesPerFrame
= format
.mFramesPerPacket
* format
.mBytesPerPacket
;
219 /* one of the most constant constants of the whole computer history */
220 format
.mBitsPerChannel
= sizeof(float) * 8;
221 ret
= -E_STREAM_FORMAT
;
222 if (AudioUnitSetProperty(powd
->output
, kAudioUnitProperty_StreamFormat
,
223 kAudioUnitScope_Input
, 0, &format
,
224 sizeof(AudioStreamBasicDescription
)))
227 ret
= -E_ADD_CALLBACK
;
228 if (AudioUnitSetProperty(powd
->output
, kAudioUnitProperty_SetRenderCallback
,
229 kAudioUnitScope_Input
, 0, &inputCallback
,
230 sizeof(inputCallback
)) < 0)
234 destroy_buffers(powd
);
236 AudioUnitUninitialize(powd
->output
);
238 CloseComponent(powd
->output
);
243 __malloc
void *osx_write_parse_config(char *options
)
245 struct osx_write_args_info
*conf
246 = para_calloc(sizeof(struct osx_write_args_info
));
247 PARA_INFO_LOG("options: %s\n", options
);
248 int ret
= osx_cmdline_parser_string(options
, conf
, "osx_write");
258 static void osx_write_close(struct writer_node
*wn
)
260 struct private_osx_write_data
*powd
= wn
->private_data
;
262 PARA_INFO_LOG("closing writer node %p\n", wn
);
263 AudioOutputUnitStop(powd
->output
);
264 AudioUnitUninitialize(powd
->output
);
265 CloseComponent(powd
->output
);
266 destroy_buffers(powd
);
270 static int need_new_buffer(struct writer_node
*wn
)
272 struct writer_node_group
*wng
= wn
->wng
;
273 struct private_osx_write_data
*powd
= wn
->private_data
;
275 if (*wng
->loaded
< sizeof(short))
277 if (powd
->to
->remaining
) /* Non empty buffer, must still be playing */
282 static int osx_write_post_select(__a_unused
struct sched
*s
,
283 struct writer_node
*wn
)
285 struct private_osx_write_data
*powd
= wn
->private_data
;
286 struct writer_node_group
*wng
= wn
->wng
;
287 short *data
= (short*)wng
->buf
;
289 if (!need_new_buffer(wn
))
291 fill_buffer(powd
->to
, data
, *wng
->loaded
/ sizeof(short));
292 powd
->to
= powd
->to
->next
;
293 wn
->written
= *wng
->loaded
;
295 if (AudioOutputUnitStart(powd
->output
))
296 return -E_UNIT_START
;
302 static int osx_write_pre_select(struct sched
*s
, __a_unused
struct writer_node
*wn
)
304 struct private_osx_write_data
*powd
= wn
->private_data
;
305 struct writer_node_group
*wng
= wn
->wng
;
306 size_t numbytes
= powd
->to
->remaining
* sizeof(short);
307 struct timeval tmp
= {.tv_sec
= 1, .tv_usec
= 0}, delay
= tmp
;
308 unsigned long divisor
;
310 if (!numbytes
&& *wng
->loaded
>= sizeof(short))
311 goto min_delay
; /* there's a buffer to fill */
314 divisor
= powd
->samplerate
* powd
->channels
* 2 / numbytes
;
316 tv_divide(divisor
, &tmp
, &delay
);
317 if (tv_diff(&s
->timeout
, &delay
, NULL
) > 0)
319 // PARA_DEBUG_LOG("delay: %lu:%lu\n", (long unsigned) s->timeout.tv_sec,
320 // (long unsigned) s->timeout.tv_usec);
323 PARA_DEBUG_LOG("%s\n", "minimal delay");
324 s
->timeout
.tv_sec
= 0;
325 s
->timeout
.tv_usec
= 1;
329 void osx_write_init(struct writer
*w
)
331 w
->open
= osx_write_open
;
332 w
->close
= osx_write_close
;
333 w
->pre_select
= osx_write_pre_select
;
334 w
->post_select
= osx_write_post_select
;
335 w
->parse_config
= osx_write_parse_config
;
336 w
->shutdown
= NULL
; /* nothing to do */