Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Windows API function to set Auto Gain Control

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

I'm trying to do a C# wrapper/call to set this low level device setting value. It basically disables/enables AGC microphone setting. I've found this link but i'm not sure how to wire it up to look like this:

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

Essentially, I want to disable (uncheck) this enhancement

like image 741
Ayson Baxter Avatar asked May 17 '19 09:05

Ayson Baxter


1 Answers

This answer has discussed "How to use interface pointer exported by C++ DLL in C#". But, I think what you want more is as below.

You don't need to use the winapi. The auto gain control functionality is implemented in the AudioQualityEnhancer class, which is a mediahandler. It uses the AutoGainControl bool property to enable or disable the gain control feature.

Automatic Gain Control example in 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();
        }
    }
}

UPDATE:

To disable the AGC with interface simply, You can encapsulate all interface procedures in your own DLL functions, like:

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;
}
like image 77
Drake Wu Avatar answered Sep 18 '22 15:09

Drake Wu