How to programmatically configure speakers using the Core Audio API on Mac OS X?

I have a 7.1 channel audio output device and a custom kext to control this. My user application needs to send 7.1 audio data on the rear channel to the device, but the device receives only 2 audio channels. I checked the “Speaker Setup” option in the “Audio MIDI setup” application and set the stereo. When I set it to "7.1 Rear Surround" everything works fine. In my final product, I do not want the user to do all this manually. So the question is: is there any Core Audio API or any other tools for this programmatically?

enter image description here

+4
source share
1 answer

Well, after playing with some Core Audio APIs, finally I could do it.

  • Get AudioDeviceID:

    AudioDeviceID audioDevice = getMyAwesomeDeviceID(); 
  • Create the AudioObjectPropertyAddress attribute:

     AudioObjectPropertyAddress propertyAddress; propertyAddress.mSelector = kAudioDevicePropertyPreferredChannelLayout; propertyAddress.mScope = kAudioDevicePropertyScopeOutput; propertyAddress.mElement = kAudioObjectPropertyElementMaster; 
  • Request if an audio object has this property:

     AudioObjectHasProperty(audioDevice, &propertyAddress) 
  • Get the data size of this property and create an AudioChannelLayout:

     UInt32 propSize(0); AudioObjectGetPropertyDataSize(audioDevice, &propertyAddress, 0, NULL, &propSize); AudioChannelLayout* layout = (AudioChannelLayout*)malloc(propSize); 
  • Configure the structure of AudioChannelLayout (for example: stereo layout):

     AudioChannelLabel labels[2] = {kAudioChannelLabel_Right, kAudioChannelLabel_Left}; layout->mNumberChannelDescriptions = 2; for (UInt32 i = 2; i < layout->mNumberChannelDescriptions; i++) { layout->mChannelDescriptions[i].mChannelLabel = labels[i]; layout->mChannelDescriptions[i].mChannelFlags = kAudioChannelFlags_AllOff; } 
  • Set the properties of the AudioObject:

     AudioObjectSetPropertyData(audioDevice, &propertyAddress, 0, NULL, propSize, layout); 
+4
source

Source: https://habr.com/ru/post/1480844/


All Articles