Skip to content

Instantly share code, notes, and snippets.

@novi
Created April 21, 2016 05:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save novi/e706e714c3dfd87ca84aeba4d9dbfb6e to your computer and use it in GitHub Desktop.
Save novi/e706e714c3dfd87ca84aeba4d9dbfb6e to your computer and use it in GitHub Desktop.
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