Skip to content

Instantly share code, notes, and snippets.

@novi novi/devices.c
Created Apr 21, 2016

Embed
What would you like to do?
Retrieval audio devices of yours with CoreAudio HAL.
// $ clang -framework CoreAudio devices.c
#include <stdio.h>
#include <stdlib.h>
#include <CoreAudio/CoreAudio.h>
int main(int argc, char *argv[]) {
AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster };
UInt32 theDataSize = 0;
OSStatus err;
err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &theAddress, 0, NULL, &theDataSize);
if (err != noErr) {
return -1;
}
printf("the data size is %d bytes.\n", theDataSize);
UInt32 numOfDevices = theDataSize / (UInt32)sizeof(AudioDeviceID);
printf("device count is %d.\n", numOfDevices);
AudioDeviceID* deviceIDs = malloc(theDataSize);
memset(deviceIDs, 0, theDataSize);
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &theDataSize, (void *)deviceIDs);
if (err != noErr) {
printf("could not get devices %d\n", err);
return -1;
}
for(size_t i = 0; i < numOfDevices; i++) {
printf("device %ld is %u.\n", i, deviceIDs[i]);
}
free(deviceIDs);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.