Created
April 1, 2018 00:04
-
-
Save goonzoid/5bccc6889a47ea41737a73cec7b45e47 to your computer and use it in GitHub Desktop.
List audio devices on OS X
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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