I am trying to use WASAPI in C# but I could not even find which dll to reference in Visual Studio. Should I reference a dll in COM assemblies or download one from Microsoft website and reference it? Is there any documentation how to use the WASAPI in C#?
I want to use it to set microphone boost level. I have been using NAudio for that, but in Windows 8.1 it does not function properly, see this. It sets the boost level via winmm calls. I thought I could use WASAPI directly.
Edit
I have tried CSCore which has a wrapper for WASAPI calls for setting microphone boost. It successfully sets the value, but the program crashes everytime with Access Violation exception after setting 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 have also found 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;
}
}