How to set microphone gain in C # using WASAPI

I'm trying to use WASAPI in C # but I couldn't even find which dll to reference in Visual Studio. Should I reference the DLL in COM assemblies, or download it from the Microsoft website and reference it? Is there any documentation on using WASAPI in C #?

I want to use it to set the microphone gain level. I used NAudio for this, but it doesn't work correctly on Windows 8.1, see this . It sets the gain level through calls to winmm. I thought I could use WASAPI directly.

Edit

I tried CSCore, which has a WASAPI wrapper for adjusting the microphone gain. It sets the value successfully, but when the program crashes every time the access violation is disabled, it sets the value. Here is the code for CSCore:

MMDeviceEnumerator deviceEnumerator = new MMDeviceEnumerator();
MMDeviceCollection deviceCollection = deviceEnumerator.EnumerateAudioEndPoints(EDataFlow.eCapture, DEVICE_STATE.DEVICE_STATE_ACTIVE);
MMDevice microphone = null;

for (int i = 0; i < deviceCollection.Count; i++)
{
    MMDevice device = deviceCollection[i];

    if (device.FriendlyName.Contains("Plantronics"))
    {
        microphone = device;
    }
}

if (microphone != null && microphone.AudioSessionManager2.Sessions.Count < 1)
{
    return;
}

AudioSessionControl2 activeSession = null;

for (int i = 0; i < microphone.AudioSessionManager2.Sessions.Count; i++)
{
    if (microphone.AudioSessionManager2.Sessions[i].State == AudioSessionState.AudioSessionStateActive)
    {
        activeSession = microphone.AudioSessionManager2.Sessions[i];
    }
}

if (activeSession == null)
{
    return;
}

activeSession.SimpleAudioVolume.MasterVolume += 0.1f;

      

I also found the AudioSwitcher library which seems to have a wrapper too, however the microphone volume cannot be changed. It is always -1. Here is the code for AudioSwitcher:

CoreAudioController audioController = new CoreAudioController();
var devices = audioController.GetCaptureDevices(DeviceState.Active);

foreach (CoreAudioDevice device in devices)
{
    if (device.FullName.Contains("Plantronics"))
    {
         device.Volume = 49;
    }
}

      

+3


source to share





All Articles