2013-05-15 30 views
4

我有一個7.1聲道音頻輸出設備和一個自定義kext來驅動它。我的自定義應用程序需要將7.1後置聲道音頻數據發送到設備,但設備僅接收2聲道音頻數據。我在「音頻MIDI設置」應用程序中選中了「配置揚聲器」選項,並將其設置爲立體聲。當我將其設置爲「7.1後置環繞聲」時,一切正常。在我的最終產品中,我不希望用戶必須手動完成所有這些操作。所以,問題是 - 是否有任何核心音頻API或以任何其他方式編程?如何在Mac OS X上使用Core Audio API以編程方式設置揚聲器配置?

enter image description here

回答

4

好,具有一定的核心音頻API的玩耍後,終於我可以完成這件事。

  1. 獲取AudioDeviceID:

    AudioDeviceID audioDevice = getMyAwesomeDeviceID(); 
    
  2. 創建AudioObjectPropertyAddress:

    AudioObjectPropertyAddress propertyAddress; 
    propertyAddress.mSelector = kAudioDevicePropertyPreferredChannelLayout; 
    propertyAddress.mScope = kAudioDevicePropertyScopeOutput; 
    propertyAddress.mElement = kAudioObjectPropertyElementMaster; 
    
  3. 查詢如果音頻對象有此屬性:

    AudioObjectHasProperty(audioDevice, &propertyAddress) 
    
  4. 得到這個屬性的數據大小和創建AudioChannelLayout:

    UInt32 propSize(0); 
    AudioObjectGetPropertyDataSize(audioDevice, &propertyAddress, 0, NULL, &propSize); 
    AudioChannelLayout* layout = (AudioChannelLayout*)malloc(propSize); 
    
  5. 配置您的AudioChannelLayout結構(如:立體佈局):

    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; 
    } 
    
  6. 設置AudioObject屬性數據:

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