Handmade Network»Forums
Simon Anciaux
1266 posts
Is there a way to get ressampling with WASAPI ?

Hello,

When we create a buffer using WASAPI in shared mode, we can't choose the sampling rate and number/setup of speaker. For example if Windows is setup to use 48Khz audio (or maybe the first application to initialize WASAPI requested 48Khz) and my game/application request a 44.1Khz waveformat (because it's source audio is at that frequency), WASAPI initialization will return a 48Khz buffer, which requires me to do the resampling before submitting.

Is there a way to either force WASAPI (in shared mode) to return a buffer in the requested format, or to re-sample on the fly when I submit data ? If not, does anyone know a single header library to do audio resampling ?

Mārtiņš Možeiko
2446 posts / 2 projects
Is there a way to get ressampling with WASAPI ?

Yes, it can do automatic resampling. AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM flag when you call IAudioClient::Initialize()

Simon Anciaux
1266 posts
Is there a way to get ressampling with WASAPI ?
Edited by Simon Anciaux on
Replying to mmozeiko (#29134)

I experimented with that flag a few month ago but I wasn't able to make things work. I tried again today and found a potential issue in my code and now I get sound, but it's playing at twice the speed, which suggests I'm not filling the buffer correctly.

It seems to simply be that I'm filling only 1 channel, but when I try to fill the memory for two channels, I get an access violation in the audio buffer memory. I'll take a closer look at what's going on when I got some time.

Thanks.

Mārtiņš Možeiko
2446 posts / 2 projects
Is there a way to get ressampling with WASAPI ?
Replying to mrmixer (#29138)

This flag should work if you're on Win7 or newer. It was not available in WinVista.

If you want to do manual resampling you can do it with Media Foundation api using CLSID_CResamplerMediaObject object. wcap code uses it to resample captured wasapi audio for audio encoding to mp4.

Simon Anciaux
1266 posts
Is there a way to get ressampling with WASAPI ?
Edited by Simon Anciaux on
Replying to mmozeiko (#29139)

Thanks, I didn't know about the Media Foundation resampling.

There were two errors in the function that fills the buffer on my side, one that made me fill twice the size required (I was trying to fill a stereo buffer with a mono source and messed up), and one where I tried to fill the stereo buffer with a mono source and copied a byte at a time in each channel instead of copying the full sample size in each channel (lrlrlr|lrlrlr instead of lllrrr|lllrrr... for 24 bits samples).

Thank for the help.