From e9bfbdf3862c62c065550292965514cf97eaacb2 Mon Sep 17 00:00:00 2001 From: Mooneer Salem Date: Sun, 25 May 2025 12:43:59 -0700 Subject: [PATCH] Windows: determine minimum number of audio channels supported. --- src/audio/WASAPIAudioEngine.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/audio/WASAPIAudioEngine.cpp b/src/audio/WASAPIAudioEngine.cpp index 647a0378..7960331b 100644 --- a/src/audio/WASAPIAudioEngine.cpp +++ b/src/audio/WASAPIAudioEngine.cpp @@ -419,6 +419,26 @@ AudioDeviceSpecification WASAPIAudioEngine::getDeviceSpecification_(IMMDevice* d spec.defaultSampleRate = streamFormat->nSamplesPerSec; spec.maxChannels = streamFormat->nChannels; spec.minChannels = streamFormat->nChannels; + + // Determine minimum number of channels supported. + for (int channel = spec.maxChannels - 1; channel >= 1; channel--) + { + WAVEFORMATEX* closestFormat = nullptr; + streamFormat->nChannels = channel; + if (audioClient->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, streamFormat, &closestFormat) == S_OK) + { + spec.minChannels = channel; + } + if (closestFormat != nullptr) + { + CoTaskMemFree(closestFormat); + } + + if (spec.minChannels != channel) + { + break; + } + } CoTaskMemFree(streamFormat); audioClient->Release();