From 86adc6099b4ed8b6791666fe16be96d7d8f7123a Mon Sep 17 00:00:00 2001 From: Ken Cooke Date: Mon, 5 Aug 2019 12:14:45 -0700 Subject: [PATCH] Attempt audio device format with native sample rate and channels forced to 2/1 before failing --- libraries/audio-client/src/AudioClient.cpp | 27 +++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/libraries/audio-client/src/AudioClient.cpp b/libraries/audio-client/src/AudioClient.cpp index c16e297c28..09b8b495f3 100644 --- a/libraries/audio-client/src/AudioClient.cpp +++ b/libraries/audio-client/src/AudioClient.cpp @@ -597,11 +597,17 @@ bool AudioClient::getNamedAudioDeviceForModeExists(QAudio::Mode mode, const QStr // attempt to use the native sample rate and channel count -bool nativeFormatForAudioDevice(const QAudioDeviceInfo& audioDevice, - QAudioFormat& audioFormat) { +bool nativeFormatForAudioDevice(const QAudioDeviceInfo& audioDevice, QAudioFormat& audioFormat) { audioFormat = audioDevice.preferredFormat(); + // converting to/from this rate must produce an integral number of samples + if ((audioFormat.sampleRate() <= 0) || + (audioFormat.sampleRate() * AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL % AudioConstants::SAMPLE_RATE != 0)) { + qCWarning(audioclient) << "The native sample rate [" << audioFormat.sampleRate() << "] is not supported."; + return false; + } + audioFormat.setCodec("audio/pcm"); audioFormat.setSampleSize(16); audioFormat.setSampleType(QAudioFormat::SignedInt); @@ -609,12 +615,17 @@ bool nativeFormatForAudioDevice(const QAudioDeviceInfo& audioDevice, if (!audioDevice.isFormatSupported(audioFormat)) { qCWarning(audioclient) << "The native format is" << audioFormat << "but isFormatSupported() failed."; - return false; - } - // converting to/from this rate must produce an integral number of samples - if (audioFormat.sampleRate() * AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL % AudioConstants::SAMPLE_RATE != 0) { - qCWarning(audioclient) << "The native sample rate [" << audioFormat.sampleRate() << "] is not supported."; - return false; + + // attempt the native sample rate, with channels forced to 2 + audioFormat.setChannelCount(2); + if (!audioDevice.isFormatSupported(audioFormat)) { + + // attempt the native sample rate, with channels forced to 1 + audioFormat.setChannelCount(1); + if (!audioDevice.isFormatSupported(audioFormat)) { + return false; + } + } } return true; }