Search code examples
c#windowsnaudiowasapi

Feed a capture device (e.g. microphone) with audio in C#


Is there any way to play audio directly into a capture device in C#? In my project I will have to feed later on a virtual capture driver with audio so I can use it in other programs and play the wanted audio anywhere else, but Im not sure it is possible in C#, I tried to do this with NAudio (which is truly amazing):

    var enumerator = new MMDeviceEnumerator();
    MMDevice captureDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Multimedia);
    WasapiOut wasapiOut = new WasapiOut(captureDevice, AudioClientShareMode.Shared, false, 0);

But ultimately it just throws a COMException with the code 0x88890003 which translates to the error "The AUDCLNT_STREAMFLAGS_LOOPBACK flag is set but the endpoint device is a capture device, not a rendering device". So in the end is there any possible solution or do I have to turn to another language like C++?


Solution

  • You cannot push audio to the device which generates audio on its own, "capture device".

    Loopback mode means that you can have a copy of audio stream from a rendering device, but this does not work the other way.

    The way things can work more or less as you assumed is when you have a special (and custom or third party, since no stock implementation of the kind exists) implementation of audio capture device, designed to generate audio supplied by external application such as your pushing the payload audio data via an API.

    Switching to C++ will be of no help with this challenge.