From 89725c3bb4c162ce07d60222bab6d00aac82d08a Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 2 Oct 2019 19:54:02 +0200 Subject: [PATCH] Ref T730, also return an output device if there is no exact match --- src/blacksound/audioutilities.cpp | 42 +++++++++++++++++++------------ 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/blacksound/audioutilities.cpp b/src/blacksound/audioutilities.cpp index f718a530e..f25cfe048 100644 --- a/src/blacksound/audioutilities.cpp +++ b/src/blacksound/audioutilities.cpp @@ -23,7 +23,7 @@ namespace BlackSound for (int n = 0; n < inputSamples; n++) { output[n] = *reinterpret_cast(input.data() + n * 2); - output[n] /= 32767.0; + output[n] /= 32767.0f; } return output; } @@ -75,7 +75,7 @@ namespace BlackSound QVector output; for (auto sample : input) { - output.push_back(sample / 32768.0); + output.push_back(sample / 32768.0f); } return output; } @@ -88,7 +88,7 @@ namespace BlackSound else { return QAudioDeviceInfo::defaultOutputDevice(); } } - QAudio::Mode mode = device.getType() == CAudioDeviceInfo::InputDevice ? QAudio::AudioInput : QAudio::AudioOutput; + QAudio::Mode mode = (device.getType() == CAudioDeviceInfo::InputDevice) ? QAudio::AudioInput : QAudio::AudioOutput; const QList allQtDevices = QAudioDeviceInfo::availableDevices(mode); // Find the one with lowest latency. @@ -97,15 +97,15 @@ namespace BlackSound { if (d.deviceName() == device.getName()) { - if (! d.isFormatSupported(format)) + if (!d.isFormatSupported(format)) { // Check whether the nearest format is acceptable for our needs - QAudioFormat nearestFormat = d.nearestFormat(format); - if (nearestFormat.sampleRate() != format.sampleRate() || + const QAudioFormat nearestFormat = d.nearestFormat(format); + if (nearestFormat.sampleRate() != format.sampleRate() || nearestFormat.sampleSize() != format.sampleSize() || nearestFormat.sampleType() != format.sampleType() || - nearestFormat.byteOrder() != format.byteOrder() || - nearestFormat.codec() != format.codec()) + nearestFormat.byteOrder() != format.byteOrder() || + nearestFormat.codec() != format.codec()) { continue; } @@ -155,7 +155,6 @@ namespace BlackSound QAudioDeviceInfo getHighestCompatibleOutputDevice(const CAudioDeviceInfo &device, QAudioFormat &format) { if (device.isDefault()) { return QAudioDeviceInfo::defaultOutputDevice(); } - const QList allQtDevices = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput); QList supportedDevices; @@ -163,15 +162,26 @@ namespace BlackSound { if (d.deviceName() == device.getName()) { - if (d.isFormatSupported(format)) - { - supportedDevices.push_back(d); - } + // exact match, format supported + if (d.isFormatSupported(format)) { return d; } } - - if (supportedDevices.size() > 0) { return supportedDevices.at(0); } + supportedDevices.push_back(d); } - return {}; + + // no suitable device + if (supportedDevices.isEmpty()) + { + format = QAudioFormat(); + return QAudioDeviceInfo(); + } + + // here we could "search the best device", currently only first is taken + QAudioDeviceInfo usedDevice = supportedDevices.front(); + if (!usedDevice.isFormatSupported(format)) + { + format = usedDevice.nearestFormat(format); + } + return usedDevice; } } // ns