Calling Windows API function to set Auto Gain Control
See the question and my original answer on StackOverflowHere is a pure C# console app sample, equivalent of @Drake's C/C++ code. I've written it using code from an open source project called DirectN that defines thousands of c# Windows interop types (DirectX, etc.), including Code Audio API, etc.
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
{
}
}
You can use either the DirectN's nuget package, or copy to your project only the needed .cs files (and their dependencies). Here, you would need the following:
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