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 @ref BACKEND_COREAUDIO
24 #if HAVE_COREAUDIO_AUDIOHARDWARE_H
26 #include "coreaudio.h"
28 #include <CoreFoundation/CFString.h>
30 /** @brief Return the default device ID */
31 static AudioDeviceID coreaudio_use_default(void) {
36 /* TODO should we use kAudioHardwarePropertyDefaultSystemOutputDevice
37 * instead? It is listed in the enumeration but is not documented, so we
38 * leave it alone until better information is available. */
39 propertySize = sizeof adid;
40 status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
41 &propertySize, &adid);
43 fatal(0, "AudioHardwareGetProperty kAudioHardwarePropertyDefaultOutputDevice: %d", (int)status);
44 if(adid == kAudioDeviceUnknown)
45 fatal(0, "no output device");
49 /** @brief Find a device by some string
50 * @param selector Selector for property to look for
51 * @param description Property description
52 * @param devs List of device IDs
53 * @param ndevs Number of device IDs in @p devs
54 * @param resultp Where to put device ID
55 * @return 1 if found, 0 if not found
57 static int coreaudio_find_device(AudioObjectPropertySelector selector,
58 //const char *description,
59 const AudioDeviceID *devs,
62 AudioDeviceID *resultp) {
66 for(unsigned n = 0; n < ndevs; ++n) {
68 propertySize = sizeof name;
69 status = AudioDeviceGetProperty(devs[n], 0, FALSE,
71 &propertySize, &name);
73 fatal(0, "AudioDeviceGetProperty: %d", (int)status);
77 CFRange r = { 0, CFStringGetLength(name) };
78 CFStringGetBytes(name, r, kCFStringEncodingUTF8,
79 '?', FALSE, output, sizeof output,
82 info("device %u %s: %s", n, description, (char *)output);
84 if(CFStringCompare(dev, name,
85 kCFCompareCaseInsensitive|kCFCompareNonliteral)
86 == kCFCompareEqualTo) {
93 return 0; /* didn't find it */
96 /** @brief Identify audio device
97 * @param name Device name
100 AudioDeviceID coreaudio_getdevice(const char *name) {
108 || !strcmp(name, "default"))
109 return coreaudio_use_default();
110 /* Convert the configured device name to a CFString */
112 dev = CFStringCreateWithCString(NULL/*default allocator*/,
114 kCFStringEncodingUTF8);
116 fatal(0, "CFStringCreateWithBytes failed");
117 /* Get a list of available devices */
118 AudioDeviceID devs[1024];
121 propertySize = sizeof devs;
122 status = AudioHardwareGetProperty(kAudioHardwarePropertyDevices,
123 &propertySize, devs);
125 fatal(0, "AudioHardwareGetProperty kAudioHardwarePropertyDevices: %d",
127 ndevs = propertySize / sizeof *devs;
129 fatal(0, "no sound devices found");
130 /* Try looking up by UID first */
131 found = coreaudio_find_device(kAudioDevicePropertyDeviceUID, //"UID",
132 devs, ndevs, dev, &adid);
133 /* Failing that try looking up by name */
135 found = coreaudio_find_device(kAudioObjectPropertyName, //"name",
136 devs, ndevs, dev, &adid);
139 fatal(0, "cannot find device '%s'", name);