Skip to content

Instantly share code, notes, and snippets.

@goonzoid
Created April 1, 2018 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goonzoid/5bccc6889a47ea41737a73cec7b45e47 to your computer and use it in GitHub Desktop.
Save goonzoid/5bccc6889a47ea41737a73cec7b45e47 to your computer and use it in GitHub Desktop.
List audio devices on OS X
NSArray* audioDevices() {
AudioObjectPropertyAddress propertyAddress = {
kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
UInt32 dataSize = 0;
OSStatus status = 0;
status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);
if (kAudioHardwareNoError != status) {
NSLog(@"AudioObjectGetPropertyDataSize (kAudioHardwarePropertyDevices) failed: %i", status);
}
AudioDeviceID *audioDevices = malloc(dataSize);
status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, audioDevices);
if (kAudioHardwareNoError != status) {
NSLog(@"AudioObjectGetPropertyData (kAudioHardwarePropertyDevices) failed: %i", status);
}
if (sizeof(audioDevices) != dataSize) {
NSLog(@"AudioObjectGetPropertyData (kAudioHardwarePropertyDevices) did not fill the outData buffer");
}
UInt32 deviceCount = dataSize / sizeof(AudioDeviceID);
NSMutableArray *inputInterfaces = [NSMutableArray arrayWithCapacity:deviceCount];
propertyAddress.mSelector = kAudioObjectPropertyName;
for (UInt32 i = 0; i < deviceCount; ++i) {
CFStringRef deviceName = NULL;
dataSize = sizeof(deviceName);
status = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &deviceName);
if (kAudioHardwareNoError != status) {
NSLog(@"AudioObjectGetPropertyData (kAudioObjectPropertyName) failed: %i", status);
continue;
}
if (sizeof(deviceName) != dataSize) {
NSLog(@"AudioObjectGetPropertyData (kAudioObjectPropertyName) did not fill the outData buffer");
}
[inputInterfaces addObject:(__bridge NSString *)deviceName];
}
free(audioDevices), audioDevices = NULL;
return [NSArray arrayWithArray:inputInterfaces];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment