get channels support from specific AudioID, please help me see what is wrong in the code
get channels support from specific AudioID, please help me see what is wrong in the code
- Subject: get channels support from specific AudioID, please help me see what is wrong in the code
- From: Anonymous <email@hidden>
- Date: Fri, 23 Mar 2012 11:49:30 -0500
Hi, There,
I was trying to query my Mac Pro to get a list of audio devices, with
their physical device ID and their channels supported.
Now I got the following partially working, partially not working. And
I get this error code 2003332927, I do not seem to be able to find out
what this code means. Can anyone help. My following code is ready to
compile and run if add CoreAudio framework.
I split it from my lengthy code and made it simple. Your help is
highly appreciated!
Result:
xiaofengmaclap:Debug wolfram$ ./DeviceTest
Error in AudioObjectGetPropertyData in getting theChannelNum: 2003332927
Error in AudioObjectGetPropertyData in getting theChannelNum: 2003332927
Error in AudioObjectGetPropertyData in getting theChannelNum: 2003332927
Error in AudioObjectGetPropertyData in getting theChannelNum: 2003332927
{{"DeviceID"->260,"DeviceName"->"Built-in
Microphone"},{"DeviceID"->258,"DeviceName"->"Built-in
Input"},{"DeviceID"->262,"DeviceName"->"Built-in Output"}}
////Code//////////////////////////////////////////////////////////////////////////
#include <CoreFoundation/CoreFoundation.h>
#include <CoreAudio/CoreAudio.h>
#define LENGTH 2048
char* deviceinfo;
char * MYCFStringCopyUTF8String(CFStringRef aString)
{
if (aString == NULL) {
return NULL;
}
CFIndex length = CFStringGetLength(aString);
CFIndex maxSize =
CFStringGetMaximumSizeForEncoding(length,
kCFStringEncodingUTF8);
char *buffer = (char *)malloc(maxSize);
if (CFStringGetCString(aString, buffer, maxSize,
kCFStringEncodingUTF8)) {
return buffer;
}
return NULL;
}
void getDeviceInfo()
{
OSStatus result;
UInt32 thePropSize;
AudioDeviceID *theDeviceIDs = NULL;
CFStringRef theDeviceName;
CFStringRef theChannelNum;
char* CDeviceName;
char* CChannelNum;
UInt32 theNumDevices = 0; //this is number of devices
AudioObjectPropertyAddress thePropertyAddress ;
char buffer[LENGTH];
deviceinfo=(char*)malloc(sizeof(char)*LENGTH);
deviceinfo[0]='\0';
int counter; //use internally to
help concatenating the comma seperator.
UInt32 i; //interator
thePropertyAddress.mSelector=kAudioHardwarePropertyDevices;
thePropertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
thePropertyAddress.mElement = kAudioObjectPropertyElementMaster;
result = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject,
&thePropertyAddress, 0, NULL, &thePropSize);
if (result)
{
printf("Error in AudioObjectGetPropertyDataSize: %d\n", (int)result);
}
// Find out how many devices are on the system
theNumDevices = thePropSize / sizeof(AudioDeviceID);
theDeviceIDs = (AudioDeviceID*)calloc(theNumDevices, sizeof(AudioDeviceID));
//pass in thePropertyAddress, and return theDeviceList
result = AudioObjectGetPropertyData(kAudioObjectSystemObject,
&thePropertyAddress, 0, NULL, &thePropSize, theDeviceIDs);
if (result)
{
printf("Error in AudioObjectGetPropertyData: %d\n", (int)result);
}
// get the channel numbers
thePropSize = sizeof(CFStringRef);
thePropertyAddress.mSelector=kAudioDevicePropertyChannelNameCFString;
thePropertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
thePropertyAddress.mElement = kAudioObjectPropertyElementMaster;
result = AudioObjectGetPropertyData(theDeviceIDs[i],
&thePropertyAddress, 0, NULL, &thePropSize, &theChannelNum);
if (result)
{
printf("Error in AudioObjectGetPropertyData in getting
theChannelNum: %d\n", (int)result);
}
strcat(deviceinfo,"{");
for (i=0; i < theNumDevices; i++)
{
// get the device name
thePropSize = sizeof(CFStringRef);
thePropertyAddress.mSelector = kAudioObjectPropertyName;
thePropertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
thePropertyAddress.mElement = kAudioObjectPropertyElementMaster;
// pass in theProSize, the PropertyAddress, and device ID, then
output theDeviceName
result = AudioObjectGetPropertyData(theDeviceIDs[i],
&thePropertyAddress, 0, NULL, &thePropSize, &theDeviceName);
if (result)
{
printf("Error in AudioObjectGetPropertyData in getting
theDeviceName: %d\n", (int)result);
}
// data type conversion from CFStringRef to char*
CDeviceName=MYCFStringCopyUTF8String(theDeviceName);
// get the channel numbers
thePropSize = sizeof(CFStringRef);
thePropertyAddress.mSelector=kAudioDevicePropertyChannelNameCFString;
thePropertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
thePropertyAddress.mElement = kAudioObjectPropertyElementMaster;
result = AudioObjectGetPropertyData(theDeviceIDs[i],
&thePropertyAddress, 0, NULL, &thePropSize, &theChannelNum);
if (result)
{
printf("Error in AudioObjectGetPropertyData in getting
theChannelNum: %d\n", (int)result);
}
//convert channelNum from CFStringRef to char*
CChannelNum=MYCFStringCopyUTF8String(theChannelNum);
strcat(deviceinfo,"{");
strcat(deviceinfo,"\"DeviceID\"->");
sprintf(buffer,"%d",(int)theDeviceIDs[i]);
strcat(deviceinfo,buffer);
strcat(deviceinfo,",");
strcat(deviceinfo,"\"DeviceName\"->");
strcat(deviceinfo,"\"");
strcat(deviceinfo,CDeviceName);
strcat(deviceinfo,"\"");
//strcat(deviceinfo,",");
//strcat(deviceinfo,"\"Channels\"->");
//strcat(deviceinfo,CChannelNum);
strcat(deviceinfo,"}");
counter=i;
if(++counter!=theNumDevices)
{
strcat(deviceinfo,",");
}
CFRelease(theDeviceName);
if(theChannelNum!=NULL)
CFRelease(theChannelNum);
} //end of for loop
strcat(deviceinfo,"}");
puts(deviceinfo);
free(deviceinfo);
}
int main(void)
{
getDeviceInfo();
}
--
Xiaofeng Li
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden