3

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;
    }
}
Community
  • 1
  • 1
Yusuf Tarık Günaydın
  • 3,016
  • 2
  • 27
  • 41
  • 2
    It is a very unfriendly api, directly usable only from C++. The NAudio project attempts some wrapping, whatever you *really* want to do will surely be covered by that library. [Read this](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Hans Passant Jun 30 '15 at 08:26
  • 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.](http://stackoverflow.com/q/29622213/3670437) It sets the boost level via winmm calls. I thought I can use WASAPI directly. – Yusuf Tarık Günaydın Jun 30 '15 at 08:33
  • Why are you telling me? Put it where it belongs, in your question. – Hans Passant Jun 30 '15 at 08:35
  • @qqww2 yes the mixer... classes in NAudio do wrap the winmm APIs. That's not a bug, it's just what those classes do (they were created in 2001 well before WASAPI existed). If you want to use the wrappers for WASAPI instead, then you can, although you'd need to find out how to adjust the microphone boost via WASAPI calls. This isn't something I've done myself though, so I'm afraid I can't tell you if its possible or how. – Mark Heath Jul 02 '15 at 10:14
  • I don't think it is a bug in NAudio. It does work on the other versions of Windows as expected. Maybe it is a bug in winmm library? – Yusuf Tarık Günaydın Jul 02 '15 at 10:22

0 Answers0