调用Windows API函数来设置自动增益控制

时间:2019-05-17 09:57:21

标签: c# winforms winapi audio core-audio

enter image description here https://docs.microsoft.com/en-us/windows/desktop/api/devicetopology/nf-devicetopology-iaudioautogaincontrol-setenabled

我正在尝试执行C#包装程序/调用来设置此低级设备设置值。基本上,它禁用/启用AGC麦克风设置。我找到了此链接,但不确定如何将其连接起来,如下所示:

 // http://msdn.microsoft.com/en-us/library/dd757304%28VS.85%29.aspx
    [DllImport("winmm.dll", CharSet = CharSet.Ansi)]
    public static extern Int32 mixerGetNumDevs();

基本上,我想禁用(取消选中)此增强功能

2 个答案:

答案 0 :(得分:4)

answer讨论了“如何在C#中使用C ++ DLL导出的接口指针”。但是,我认为您想要的更多内容如下。

您不需要使用winapi。自动增益控制功能在AudioQualityEnhancer类中实现,该类是媒体处理程序。它使用AutoGainControl bool属性来启用或禁用增益控制功能。

C#中的自动增益控制示例:

using System;
using Ozeki.Media;

namespace Automatic_Gain_Control
{
    class Program
    {
        static Microphone microphone;
        static Speaker speaker;
        static MediaConnector connector;
        static AudioQualityEnhancer audioProcessor;

        static void Main(string[] args)
        {
            microphone = Microphone.GetDefaultDevice();
            speaker = Speaker.GetDefaultDevice();
            connector = new MediaConnector();
            audioProcessor = new AudioQualityEnhancer();

            audioProcessor.AutoGainControl = true;//enable
            audioProcessor.GainSpeed = 12;
            audioProcessor.MaxGain = 30;

            connector.Connect(microphone, audioProcessor);
            connector.Connect(audioProcessor, speaker);

            microphone.Start();
            speaker.Start();

            Console.ReadLine();
        }
    }
}

更新:

要仅通过接口禁用AGC,可以将所有接口过程封装在自己的DLL函数中,例如:

HRESULT EnableAGC()
{
    CComPtr<IMMDeviceEnumerator>    m_pIMMEnumerator;
    CComPtr<IAudioVolumeLevel>  m_pMicBoost;
    CComPtr<IAudioAutoGainControl>  m_pAGC;

    HRESULT hr = S_OK;
    hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, IID_IMMDeviceEnumerator, (void**)&m_pIMMEnumerator);
    if (FAILED(hr)) return hr;
    CComPtr<IMMDevice> pIMMDeivce = NULL;
    std::wstring strEndPointID;//String of the Device ID 
    if (strEndPointID.empty())
    {
        hr = m_pIMMEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &pIMMDeivce);
    }
    else
    {
        hr = m_pIMMEnumerator->GetDevice(strEndPointID.c_str(), &pIMMDeivce);
    }
    if (FAILED(hr)) return hr;

    CComPtr<IDeviceTopology> pTopo = NULL;
    hr = pIMMDeivce->Activate(IID_IDeviceTopology, CLSCTX_INPROC_SERVER, 0, (void**)&pTopo);
    if (FAILED(hr)) return hr;

    CComPtr<IConnector> pConn = NULL;
    hr = pTopo->GetConnector(0, &pConn);
    if (FAILED(hr)) return hr;

    CComPtr<IConnector> pConnNext = NULL;
    hr = pConn->GetConnectedTo(&pConnNext);
    if (FAILED(hr)) return hr;

    CComPtr<IPart> pPart = NULL;
    hr = pConnNext->QueryInterface(IID_IPart, (void**)&pPart);
    if (FAILED(hr)) return hr;


    hr = pPart->Activate(CLSCTX_INPROC_SERVER, IID_IAudioAutoGainControl, (void**)&m_pAGC);
    if (SUCCEEDED(hr) && m_pAGC)
    {
        //Hardware Supports Microphone AGC
        BOOL bEnable = TRUE;
        hr = m_pAGC->SetEnabled(bEnable, NULL);
    }
    else
    {
        //Hardware not Supports Microphone AGC
    }
    return hr;
}

答案 1 :(得分:0)

这里是纯C#控制台应用程序示例,等效于@Drake的C / C ++代码。我使用来自名为DirectN的开源项目中的代码编写了该代码,该项目定义了数千种c#Windows互操作类型(DirectX等),包括Code Audio API等。

class Program
{
    static void Main(string[] args)
    {
        // using DirectN
        var enumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator();

        // or call GetDevice(...) with an id
        enumerator.GetDefaultAudioEndpoint(
            __MIDL___MIDL_itf_mmdeviceapi_0000_0000_0001.eCapture,
            __MIDL___MIDL_itf_mmdeviceapi_0000_0000_0002.eConsole,
            out var device).ThrowOnError();

        const int CLSCTX_ALL = 23;
        device.Activate(typeof(IDeviceTopology).GUID, CLSCTX_ALL, null, out var iface).ThrowOnError();

        var topology = (IDeviceTopology)iface;
        topology.GetConnector(0, out var connector).ThrowOnError();

        var part = (IPart)connector;
        if (part.Activate(CLSCTX_ALL, typeof(IAudioAutoGainControl).GUID, out iface).IsError)
        {
            Console.WriteLine("AGC not supported.");
            return;
        }

        var control = (IAudioAutoGainControl)iface;
        control.SetEnabled(true, IntPtr.Zero);
    }

    [ComImport]
    [Guid("bcde0395-e52f-467c-8e3d-c4579291692e")] // CLSID_MMDeviceEnumerator
    class MMDeviceEnumerator
    {
    }
}

您可以使用DirectN的nuget软件包,也可以仅将所需的.cs文件(及其依赖项)复制到项目中。在这里,您将需要以下内容:

HRESULT.cs                                          
HRESULTS.cs                                         
IAudioAutoGainControl.cs                            
IAudioVolumeLevel.cs                                
IConnector.cs                                       
IControlChangeNotify.cs                             
IControlInterface.cs                                
IDeviceTopology.cs                                  
IMMDevice.cs                                        
IMMDeviceCollection.cs                              
IMMDeviceEnumerator.cs                              
IMMNotificationClient.cs                            
IPart.cs                                            
IPartsList.cs                                       
IPerChannelDbLevel.cs                               
ISubunit.cs                                         
PROPERTYKEY.cs                                      
PropertyType.cs                                     
PropVariant.cs                                      
_tagpropertykey.cs                                  
__MIDL___MIDL_itf_devicetopology_0000_0000_0011.cs  
__MIDL___MIDL_itf_devicetopology_0000_0000_0012.cs  
__MIDL___MIDL_itf_devicetopology_0000_0000_0013.cs  
__MIDL___MIDL_itf_mmdeviceapi_0000_0000_0001.cs     
__MIDL___MIDL_itf_mmdeviceapi_0000_0000_0002.cs