andrewrk / libsoundio

C library for cross-platform real-time audio input and output
http://libsound.io/
MIT License
1.92k stars 229 forks source link

Fix WASAPI resampling when the sample rate significantly differs from the mix format #194

Closed cgutman closed 5 years ago

cgutman commented 5 years ago

My application is decoding incoming audio at a fixed 48 KHz sample rate and I'm depending on WASAPI resampling to handle conversion to the mix format. Right now, libsoundio is using AUDCLNT_STREAMFLAGS_RATEADJUST and IAudioClockAdjustment::SetSampleRate() to handle sample rates that differ from the native mix format.

However, when setting my mix format in the Sound Control Panel to 16-bit, 192 KHz, SetSampleRate() fails with E_INVALIDARG meaning "The sample rate is out of the range for the Audio Processing Object." according to MSDN. The amount of sample rate adjustment available using SetSampleRate() appears to be limited.

It seems that what we really want here is AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM which automatically handles sample rate conversion to the mix format. Using this flag fixes the 48 KHz -> 192 KHz resampling case.

To reproduce:

  1. Open Control Panel and click Sound
  2. Choose your default audio device and click Properties then the Advanced tab
  3. Select "16 bit, 192000 Hz"
  4. Play audio with libsoundio using a 48 KHz sample rate
  5. Note that soundio_outstream_open() fails with SoundIoErrorOpeningDevice
cgutman commented 5 years ago

I've been shipping this code in production for a few weeks across several thousand Windows users (Win7 to Win10) and haven't received any more complaints from users with 192 KHz audio.

andrewrk commented 5 years ago

Hey thanks for submitting your changes upstream, and thanks for the follow-up to report that user testing worked.