How do I utilize AudioObjectGetPropertyData in OS X to retrieve a list of the system\'s input devices? I currently have the following dummy code for retrieving
Swift 3.0 Xcode 8 Beta 5
Struggled with this for a good while but this seems to work fine for now.
func handle(_ errorCode: OSStatus) throws {
if errorCode != kAudioHardwareNoError {
let error = NSError(domain: NSOSStatusErrorDomain, code: Int(errorCode), userInfo: [NSLocalizedDescriptionKey : "CAError: \(errorCode)" ])
NSApplication.shared().presentError(error)
throw error
}
}
func getInputDevices() throws -> [AudioDeviceID] {
var inputDevices: [AudioDeviceID] = []
// Construct the address of the property which holds all available devices
var devicesPropertyAddress = AudioObjectPropertyAddress(mSelector: kAudioHardwarePropertyDevices, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMaster)
var propertySize = UInt32(0)
// Get the size of the property in the kAudioObjectSystemObject so we can make space to store it
try handle(AudioObjectGetPropertyDataSize(AudioObjectID(kAudioObjectSystemObject), &devicesPropertyAddress, 0, nil, &propertySize))
// Get the number of devices by dividing the property address by the size of AudioDeviceIDs
let numberOfDevices = Int(propertySize) / sizeof(AudioDeviceID.self)
// Create space to store the values
var deviceIDs: [AudioDeviceID] = []
for _ in 0 ..< numberOfDevices {
deviceIDs.append(AudioDeviceID())
}
// Get the available devices
try handle(AudioObjectGetPropertyData(AudioObjectID(kAudioObjectSystemObject), &devicesPropertyAddress, 0, nil, &propertySize, &deviceIDs))
// Iterate
for id in deviceIDs {
// Get the device name for fun
var name: CFString = ""
var propertySize = UInt32(sizeof(CFString.self))
var deviceNamePropertyAddress = AudioObjectPropertyAddress(mSelector: kAudioDevicePropertyDeviceNameCFString, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMaster)
try handle(AudioObjectGetPropertyData(id, &deviceNamePropertyAddress, 0, nil, &propertySize, &name))
// Check the input scope of the device for any channels. That would mean it's an input device
// Get the stream configuration of the device. It's a list of audio buffers.
var streamConfigAddress = AudioObjectPropertyAddress(mSelector: kAudioDevicePropertyStreamConfiguration, mScope: kAudioDevicePropertyScopeInput, mElement: 0)
// Get the size so we can make room again
try handle(AudioObjectGetPropertyDataSize(id, &streamConfigAddress, 0, nil, &propertySize))
// Create a buffer list with the property size we just got and let core audio fill it
let audioBufferList = AudioBufferList.allocate(maximumBuffers: Int(propertySize))
try handle(AudioObjectGetPropertyData(id, &streamConfigAddress, 0, nil, &propertySize, audioBufferList.unsafeMutablePointer))
// Get the number of channels in all the audio buffers in the audio buffer list
var channelCount = 0
for i in 0 ..< Int(audioBufferList.unsafeMutablePointer.pointee.mNumberBuffers) {
channelCount = channelCount + Int(audioBufferList[i].mNumberChannels)
}
free(audioBufferList.unsafeMutablePointer)
// If there are channels, it's an input device
if channelCount > 0 {
Swift.print("Found input device '\(name)' with \(channelCount) channels")
inputDevices.append(id)
}
}
return inputDevices
}