Search code examples
c++audiouwpaudio-recordingwasapi

How to capture audio on c++ UWP app with WASAPI?


I can not seem to find any tutorial on the internet for my question.
All the simple guide is not suitable for UWP.

For Example, To use WASAPI there are these steps

  • enumerate devices
  • capture audio
  • play (render) audio back

But the enumerating step, The client must call CoCreateInstance. But from my understanding this function is not support in UWP. Also I failed at Line 30 when following this code.

So, I try to understand This, C++ UWP using WASAPI, But I can't find any Enumerate part and this project is very complicate for me. It include a lot of other files (DeviceState.h, common.h)
And I failed to extract the code to create my own application.

My question is how can I capture audio on c++ UWP app with WASAPI?

If this question is too board, I will change my question to How to enumerate audio device in c++ UWP application?.

And the reason why I use WASAPI is because I want to access the data stored in the Buffer.

Edit:
For enumerating. https://github.com/Microsoft/Windows-universal-samples/blob/7c7832e1f144e4fc836603fd70e1352024a5fe1a/Samples/WindowsAudioSession/cpp/Scenario1.xaml.cpp#L85


Solution

  • Yes, you can use WASAPI to do audio capturing in UWP and this is what is done in the sample you have referenced (https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/WindowsAudioSession).

    For the enumeration, the main function is DeviceInformation::FindAllAsync with this selector MediaDevice::GetAudioCaptureSelector it will allow you to list the capture devices.

    For the stream capturing, the main function you need is ActivateAudioInterfaceAsync, it will allow you to create an IAudioClient from a device id (specific device) or a device class (render or capture) if you just need to use the default device. Once you have this IAudioClient you can use it to get an IAudioClientCapture, basically the things that you have seen in the sample.