naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.37k stars 1.09k forks source link

Better way to convert WASAPI loopback to short-int PCM mono? #1114

Closed MV10 closed 5 months ago

MV10 commented 5 months ago

Right now I'm capturing WASAPI loopback in IEEE float with whatever channel-count the default device uses, then going through a bunch of my own code to convert the byte buffer to a float buffer, then I "manually" downmix the various channels to mono (via coefficients in Ogg RFC7845-5.1.1.5), then multiplying by the short min/max to convert the normalized float to the 16-bit integer values I need. The data has to be that way, it is feeding another program that already gets short[] mono PCM via OpenAL-Soft capture.

I've been trying to figure out if anything in NAudio can do this for me ... assuming it may perform better if it uses something like MediaFoundationResampler or some other part of the API that I haven't stumbled into yet.

I know I can bump the perf a little bit more by going to parallel processing for the downmix (I have lots of experience with that for image processing) but realistically I still won't touch what can be done if this is supported at the driver level or Media Foundation level or something like that.

MV10 commented 5 months ago

I just noticed this comment from May 2022:

There will be an even easier way in newer NAudio. WASAPI capture (including loopback) will let you set the wave format you want to capture directly, with the next release.

Is that in the current release? I don't see a way to set that.

MV10 commented 5 months ago

Neat. :)

using var capture = new WasapiLoopbackCapture();
capture.WaveFormat = new WaveFormat(22050, 1);
markheath commented 5 months ago

Lol, I'd forgotten that had gone in. Was about to explain how to use WaveBuffer, but yes, setting the capture format is the best way