2 * QEMU OS X CoreAudio audio driver
4 * Copyright (c) 2005 Mike Kronenberg
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include <CoreAudio/CoreAudio.h>
26 #include <string.h> /* strerror */
27 #include <pthread.h> /* pthread_X */
29 #include "qemu-common.h"
32 #define AUDIO_CAP "coreaudio"
33 #include "audio_int.h"
45 typedef struct coreaudioVoiceOut {
47 pthread_mutex_t mutex;
49 AudioDeviceID outputDeviceID;
50 UInt32 audioDevicePropertyBufferFrameSize;
51 AudioStreamBasicDescription outputStreamBasicDescription;
57 static void coreaudio_logstatus (OSStatus status)
62 case kAudioHardwareNoError:
63 str = "kAudioHardwareNoError";
66 case kAudioHardwareNotRunningError:
67 str = "kAudioHardwareNotRunningError";
70 case kAudioHardwareUnspecifiedError:
71 str = "kAudioHardwareUnspecifiedError";
74 case kAudioHardwareUnknownPropertyError:
75 str = "kAudioHardwareUnknownPropertyError";
78 case kAudioHardwareBadPropertySizeError:
79 str = "kAudioHardwareBadPropertySizeError";
82 case kAudioHardwareIllegalOperationError:
83 str = "kAudioHardwareIllegalOperationError";
86 case kAudioHardwareBadDeviceError:
87 str = "kAudioHardwareBadDeviceError";
90 case kAudioHardwareBadStreamError:
91 str = "kAudioHardwareBadStreamError";
94 case kAudioHardwareUnsupportedOperationError:
95 str = "kAudioHardwareUnsupportedOperationError";
98 case kAudioDeviceUnsupportedFormatError:
99 str = "kAudioDeviceUnsupportedFormatError";
102 case kAudioDevicePermissionsError:
103 str = "kAudioDevicePermissionsError";
107 AUD_log (AUDIO_CAP, "Reason: status code %ld\n", status);
111 AUD_log (AUDIO_CAP, "Reason: %s\n", str);
114 static void GCC_FMT_ATTR (2, 3) coreaudio_logerr (
123 AUD_log (AUDIO_CAP, fmt, ap);
126 coreaudio_logstatus (status);
129 static void GCC_FMT_ATTR (3, 4) coreaudio_logerr2 (
138 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
141 AUD_vlog (AUDIO_CAP, fmt, ap);
144 coreaudio_logstatus (status);
147 static inline UInt32 isPlaying (AudioDeviceID outputDeviceID)
151 UInt32 propertySize = sizeof(outputDeviceID);
152 status = AudioDeviceGetProperty(
153 outputDeviceID, 0, 0,
154 kAudioDevicePropertyDeviceIsRunning, &propertySize, &result);
155 if (status != kAudioHardwareNoError) {
156 coreaudio_logerr(status,
157 "Could not determine whether Device is playing\n");
162 static void coreaudio_atexit (void)
167 static int coreaudio_lock (coreaudioVoiceOut *core, const char *fn_name)
171 err = pthread_mutex_lock (&core->mutex);
173 dolog ("Could not lock voice for %s\nReason: %s\n",
174 fn_name, strerror (err));
180 static int coreaudio_unlock (coreaudioVoiceOut *core, const char *fn_name)
184 err = pthread_mutex_unlock (&core->mutex);
186 dolog ("Could not unlock voice for %s\nReason: %s\n",
187 fn_name, strerror (err));
193 static int coreaudio_run_out (HWVoiceOut *hw)
196 coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
198 if (coreaudio_lock (core, "coreaudio_run_out")) {
202 live = audio_pcm_hw_get_live_out (hw);
204 if (core->decr > live) {
205 ldebug ("core->decr %d live %d core->live %d\n",
211 decr = audio_MIN (core->decr, live);
214 core->live = live - decr;
215 hw->rpos = core->rpos;
217 coreaudio_unlock (core, "coreaudio_run_out");
221 /* callback to feed audiooutput buffer */
222 static OSStatus audioDeviceIOProc(
223 AudioDeviceID inDevice,
224 const AudioTimeStamp* inNow,
225 const AudioBufferList* inInputData,
226 const AudioTimeStamp* inInputTime,
227 AudioBufferList* outOutputData,
228 const AudioTimeStamp* inOutputTime,
231 UInt32 frame, frameCount;
232 float *out = outOutputData->mBuffers[0].mData;
233 HWVoiceOut *hw = hwptr;
234 coreaudioVoiceOut *core = (coreaudioVoiceOut *) hwptr;
236 struct st_sample *src;
239 const float scale = 1.f / UINT_MAX;
241 const float scale = UINT_MAX;
245 if (coreaudio_lock (core, "audioDeviceIOProc")) {
250 frameCount = core->audioDevicePropertyBufferFrameSize;
253 /* if there are not enough samples, set signal and return */
254 if (live < frameCount) {
256 coreaudio_unlock (core, "audioDeviceIOProc(empty)");
261 src = hw->mix_buf + rpos;
264 for (frame = 0; frame < frameCount; frame++) {
266 *out++ = src[frame].l; /* left channel */
267 *out++ = src[frame].r; /* right channel */
270 *out++ = src[frame].l * scale; /* left channel */
271 *out++ = src[frame].r * scale; /* right channel */
273 *out++ = src[frame].l / scale; /* left channel */
274 *out++ = src[frame].r / scale; /* right channel */
279 rpos = (rpos + frameCount) % hw->samples;
280 core->decr += frameCount;
283 coreaudio_unlock (core, "audioDeviceIOProc");
287 static int coreaudio_write (SWVoiceOut *sw, void *buf, int len)
289 return audio_pcm_sw_write (sw, buf, len);
292 static int coreaudio_init_out (HWVoiceOut *hw, struct audsettings *as)
295 coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
298 const char *typ = "playback";
299 AudioValueRange frameRange;
302 err = pthread_mutex_init(&core->mutex, NULL);
304 dolog("Could not create mutex\nReason: %s\n", strerror (err));
308 audio_pcm_init_info (&hw->info, as);
310 /* open default output device */
311 propertySize = sizeof(core->outputDeviceID);
312 status = AudioHardwareGetProperty(
313 kAudioHardwarePropertyDefaultOutputDevice,
315 &core->outputDeviceID);
316 if (status != kAudioHardwareNoError) {
317 coreaudio_logerr2 (status, typ,
318 "Could not get default output Device\n");
321 if (core->outputDeviceID == kAudioDeviceUnknown) {
322 dolog ("Could not initialize %s - Unknown Audiodevice\n", typ);
326 /* get minimum and maximum buffer frame sizes */
327 propertySize = sizeof(frameRange);
328 status = AudioDeviceGetProperty(
329 core->outputDeviceID,
332 kAudioDevicePropertyBufferFrameSizeRange,
335 if (status != kAudioHardwareNoError) {
336 coreaudio_logerr2 (status, typ,
337 "Could not get device buffer frame range\n");
341 if (frameRange.mMinimum > conf.buffer_frames) {
342 core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
343 dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum);
345 else if (frameRange.mMaximum < conf.buffer_frames) {
346 core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
347 dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
350 core->audioDevicePropertyBufferFrameSize = conf.buffer_frames;
353 /* set Buffer Frame Size */
354 propertySize = sizeof(core->audioDevicePropertyBufferFrameSize);
355 status = AudioDeviceSetProperty(
356 core->outputDeviceID,
360 kAudioDevicePropertyBufferFrameSize,
362 &core->audioDevicePropertyBufferFrameSize);
363 if (status != kAudioHardwareNoError) {
364 coreaudio_logerr2 (status, typ,
365 "Could not set device buffer frame size %ld\n",
366 core->audioDevicePropertyBufferFrameSize);
370 /* get Buffer Frame Size */
371 propertySize = sizeof(core->audioDevicePropertyBufferFrameSize);
372 status = AudioDeviceGetProperty(
373 core->outputDeviceID,
376 kAudioDevicePropertyBufferFrameSize,
378 &core->audioDevicePropertyBufferFrameSize);
379 if (status != kAudioHardwareNoError) {
380 coreaudio_logerr2 (status, typ,
381 "Could not get device buffer frame size\n");
384 hw->samples = conf.nbuffers * core->audioDevicePropertyBufferFrameSize;
386 /* get StreamFormat */
387 propertySize = sizeof(core->outputStreamBasicDescription);
388 status = AudioDeviceGetProperty(
389 core->outputDeviceID,
392 kAudioDevicePropertyStreamFormat,
394 &core->outputStreamBasicDescription);
395 if (status != kAudioHardwareNoError) {
396 coreaudio_logerr2 (status, typ,
397 "Could not get Device Stream properties\n");
398 core->outputDeviceID = kAudioDeviceUnknown;
403 core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq;
404 propertySize = sizeof(core->outputStreamBasicDescription);
405 status = AudioDeviceSetProperty(
406 core->outputDeviceID,
410 kAudioDevicePropertyStreamFormat,
412 &core->outputStreamBasicDescription);
413 if (status != kAudioHardwareNoError) {
414 coreaudio_logerr2 (status, typ, "Could not set samplerate %d\n",
416 core->outputDeviceID = kAudioDeviceUnknown;
421 status = AudioDeviceAddIOProc(core->outputDeviceID, audioDeviceIOProc, hw);
422 if (status != kAudioHardwareNoError) {
423 coreaudio_logerr2 (status, typ, "Could not set IOProc\n");
424 core->outputDeviceID = kAudioDeviceUnknown;
429 if (!isPlaying(core->outputDeviceID)) {
430 status = AudioDeviceStart(core->outputDeviceID, audioDeviceIOProc);
431 if (status != kAudioHardwareNoError) {
432 coreaudio_logerr2 (status, typ, "Could not start playback\n");
433 AudioDeviceRemoveIOProc(core->outputDeviceID, audioDeviceIOProc);
434 core->outputDeviceID = kAudioDeviceUnknown;
442 static void coreaudio_fini_out (HWVoiceOut *hw)
446 coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
448 if (!conf.isAtexit) {
450 if (isPlaying(core->outputDeviceID)) {
451 status = AudioDeviceStop(core->outputDeviceID, audioDeviceIOProc);
452 if (status != kAudioHardwareNoError) {
453 coreaudio_logerr (status, "Could not stop playback\n");
457 /* remove callback */
458 status = AudioDeviceRemoveIOProc(core->outputDeviceID,
460 if (status != kAudioHardwareNoError) {
461 coreaudio_logerr (status, "Could not remove IOProc\n");
464 core->outputDeviceID = kAudioDeviceUnknown;
467 err = pthread_mutex_destroy(&core->mutex);
469 dolog("Could not destroy mutex\nReason: %s\n", strerror (err));
473 static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
476 coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
481 if (!isPlaying(core->outputDeviceID)) {
482 status = AudioDeviceStart(core->outputDeviceID, audioDeviceIOProc);
483 if (status != kAudioHardwareNoError) {
484 coreaudio_logerr (status, "Could not resume playback\n");
491 if (!conf.isAtexit) {
492 if (isPlaying(core->outputDeviceID)) {
493 status = AudioDeviceStop(core->outputDeviceID, audioDeviceIOProc);
494 if (status != kAudioHardwareNoError) {
495 coreaudio_logerr (status, "Could not pause playback\n");
504 static void *coreaudio_audio_init (void)
506 atexit(coreaudio_atexit);
507 return &coreaudio_audio_init;
510 static void coreaudio_audio_fini (void *opaque)
515 static struct audio_option coreaudio_options[] = {
516 {"BUFFER_SIZE", AUD_OPT_INT, &conf.buffer_frames,
517 "Size of the buffer in frames", NULL, 0},
518 {"BUFFER_COUNT", AUD_OPT_INT, &conf.nbuffers,
519 "Number of buffers", NULL, 0},
520 {NULL, 0, NULL, NULL, NULL, 0}
523 static struct audio_pcm_ops coreaudio_pcm_ops = {
537 struct audio_driver coreaudio_audio_driver = {
538 INIT_FIELD (name = ) "coreaudio",
539 INIT_FIELD (descr = )
540 "CoreAudio http://developer.apple.com/audio/coreaudio.html",
541 INIT_FIELD (options = ) coreaudio_options,
542 INIT_FIELD (init = ) coreaudio_audio_init,
543 INIT_FIELD (fini = ) coreaudio_audio_fini,
544 INIT_FIELD (pcm_ops = ) &coreaudio_pcm_ops,
545 INIT_FIELD (can_be_default = ) 1,
546 INIT_FIELD (max_voices_out = ) 1,
547 INIT_FIELD (max_voices_in = ) 0,
548 INIT_FIELD (voice_size_out = ) sizeof (coreaudioVoiceOut),
549 INIT_FIELD (voice_size_in = ) 0