2 * This file is part of DisOrder
3 * Copyright (C) 2009 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file lib/coreaudio.c
19 * @brief Support for Apple Core Audio
24 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
26 #include "coreaudio.h"
29 #include <CoreFoundation/CFString.h>
30 /* evil bodge to work around broken header file */
32 #include <CoreServices/CoreServices.h>
35 /** @brief Report an error with an OSStatus */
36 void coreaudio_fatal(OSStatus err, const char *fmt, ...) {
41 byte_vasprintf(&msg, fmt, ap);
44 disorder_fatal(0, "%s: error %u", msg, (unsigned)err);
47 /** @brief Return the default device ID */
48 static AudioDeviceID coreaudio_use_default(void) {
53 /* TODO should we use kAudioHardwarePropertyDefaultSystemOutputDevice
54 * instead? It is listed in the enumeration but is not documented, so we
55 * leave it alone until better information is available. */
56 propertySize = sizeof adid;
57 status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
58 &propertySize, &adid);
60 coreaudio_fatal(status, "AudioHardwareGetProperty kAudioHardwarePropertyDefaultOutputDevice");
61 if(adid == kAudioDeviceUnknown)
62 disorder_fatal(0, "no output device");
66 /** @brief Find a device by some string
67 * @param selector Selector for property to look for
68 * @param devs List of device IDs
69 * @param ndevs Number of device IDs in @p devs
70 * @param dev Desired device name
71 * @param resultp Where to put device ID
72 * @return 1 if found, 0 if not found
74 static int coreaudio_find_device(AudioObjectPropertySelector selector,
75 //const char *description,
76 const AudioDeviceID *devs,
79 AudioDeviceID *resultp) {
83 for(unsigned n = 0; n < ndevs; ++n) {
85 propertySize = sizeof name;
86 status = AudioDeviceGetProperty(devs[n], 0, FALSE,
88 &propertySize, &name);
90 coreaudio_fatal(status, "AudioDeviceGetProperty");
94 CFRange r = { 0, CFStringGetLength(name) };
95 CFStringGetBytes(name, r, kCFStringEncodingUTF8,
96 '?', FALSE, output, sizeof output,
99 disorder_info("device %u %s: %s", n, description, (char *)output);
101 if(CFStringCompare(dev, name,
102 kCFCompareCaseInsensitive|kCFCompareNonliteral)
103 == kCFCompareEqualTo) {
110 return 0; /* didn't find it */
113 /** @brief Identify audio device
114 * @param name Device name
117 AudioDeviceID coreaudio_getdevice(const char *name) {
125 || !strcmp(name, "default"))
126 return coreaudio_use_default();
127 /* Convert the configured device name to a CFString */
129 dev = CFStringCreateWithCString(NULL/*default allocator*/,
131 kCFStringEncodingUTF8);
133 disorder_fatal(0, "CFStringCreateWithBytes failed");
134 /* Get a list of available devices */
135 AudioDeviceID devs[1024];
138 propertySize = sizeof devs;
139 status = AudioHardwareGetProperty(kAudioHardwarePropertyDevices,
140 &propertySize, devs);
142 coreaudio_fatal(status, "AudioHardwareGetProperty kAudioHardwarePropertyDevices");
143 ndevs = propertySize / sizeof *devs;
145 disorder_fatal(0, "no sound devices found");
146 /* Try looking up by UID first */
147 found = coreaudio_find_device(-1*kAudioDevicePropertyDeviceUID, //"UID",
148 devs, ndevs, dev, &adid);
149 /* Failing that try looking up by name */
151 found = coreaudio_find_device(kAudioObjectPropertyName, //"name",
152 devs, ndevs, dev, &adid);
155 disorder_fatal(0, "cannot find device '%s'", name);