#import "audioNose.h"
@implementation audioNose
+(void)getAllDevices{
NSMutableArray* deviceNames = [[NSMutableArray alloc] init];
NSMutableArray* deviceManufacturers = [[NSMutableArray alloc] init];
NSMutableArray* deviceUIDs = [[NSMutableArray alloc] init];
NSMutableArray* deviceAudioDeviceIDs = [[NSMutableArray alloc] init];
NSString* deviceDetail;
AudioObjectPropertyAddress propertyAddress = [AcpGetProperty getPropertyAddressWithGlobalScope:kAudioHardwarePropertyDevices]; // identifies which property is being queried
AudioObjectID *deviceIDs; // array to store device IDs
UInt32 propertySize; // the size of the returned property information, needed to allocate the correct amount of memory
NSInteger numDevices; // needed to know the size of the device ID array
if (AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize) == noErr) { // find the size of the property, kAudioSystemObject is a generic AudioDevice to use
numDevices = propertySize / sizeof(AudioDeviceID); // find the number of devices (by dividing returned size by the known size of each device
deviceIDs = (AudioDeviceID*)calloc(numDevices, sizeof(AudioDeviceID)); // allocate memory for the deviceIDs array, based on the number of devices and the size of one audio device ID
if (AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize, deviceIDs) == noErr) { // get the property data into the memory allocated for what this queried property returns (an array of audioDeviceIDs)
CFStringRef deviceName;
CFStringRef deviceManufacturer;
CFStringRef deviceUID;
for (NSInteger index = 0; index < numDevices; index++) {
// get device name
propertySize = sizeof(deviceName);
AudioObjectPropertyAddress deviceNameAddress = [AcpGetProperty getPropertyAddressWithGlobalScope:kAudioObjectPropertyName];
if (AudioObjectGetPropertyData(deviceIDs[index], &deviceNameAddress, 0, NULL, &propertySize, &deviceName) == noErr) {
deviceDetail = (NSString*)deviceName; // toll free bridge
[deviceNames addObject: deviceDetail];
// get device manufacturer
propertySize = sizeof(deviceManufacturer);
AudioObjectPropertyAddress deviceManufacturerAddress = [AcpGetProperty getPropertyAddressWithGlobalScope:kAudioObjectPropertyManufacturer];
if (AudioObjectGetPropertyData(deviceIDs[index], &deviceManufacturerAddress, 0, NULL, &propertySize, &deviceManufacturer) == noErr) {
deviceDetail = (NSString*)deviceManufacturer; // toll free bridge
[deviceManufacturers addObject: deviceDetail];
// get UID of device
propertySize = sizeof(deviceUID);
AudioObjectPropertyAddress deviceUIDAddress = [AcpGetProperty getPropertyAddressWithGlobalScope:kAudioDevicePropertyDeviceUID];
if (AudioObjectGetPropertyData(deviceIDs[index], &deviceUIDAddress, 0, NULL, &propertySize, &deviceUID) == noErr) {
deviceDetail = (NSString*)deviceUID;
[deviceUIDs addObject:deviceDetail];
// get number which represents AudioDeviceID
[deviceAudioDeviceIDs addObject:[NSNumber numberWithInt:((UInt32)deviceIDs[index]) ] ];
} else {
NSLog(@"Error getting UID");
}
} else {
NSLog(@"Error getting device manufacturer");
}
} else {
NSLog(@"Error getting device name");
}
};
CFRelease(deviceName);
CFRelease(deviceManufacturer);
CFRelease(deviceUID);
};
};
// print information to console
for (NSInteger index = 0; index < numDevices; index++) {
NSLog(@"Audio Object #%i:\nDevice Name: %@\nManufacturer: %@\nUID: %@\naudioDeviceID: %@\n\n",
index,
[deviceNames objectAtIndex:index],
[deviceManufacturers objectAtIndex:index],
[deviceUIDs objectAtIndex:index],
[deviceAudioDeviceIDs objectAtIndex:index] );
}
// clean up
[deviceNames release];
[deviceManufacturers release];
[deviceUIDs release];
[deviceAudioDeviceIDs release];
free(deviceIDs);
};