diff --git a/src/core/afv/audio/input.cpp b/src/core/afv/audio/input.cpp index a0c1d67e5..7d0bb5e96 100644 --- a/src/core/afv/audio/input.cpp +++ b/src/core/afv/audio/input.cpp @@ -96,6 +96,21 @@ namespace swift::core::afv::audio inputFormat.setSampleFormat(QAudioFormat::Int16); QAudioDevice selectedDevice = getLowestLatencyDevice(inputDevice, inputFormat); + + if (selectedDevice.isNull()) + { + CLogMessage(this).error(u"No suitable audio input device found for '%1'. Required format: %2") + << inputDevice.getName() << toQString(inputFormat); + return; + } + + if (!selectedDevice.isFormatSupported(inputFormat)) + { + CLogMessage(this).error(u"Selected device '%1' does not support required format: %2") + << selectedDevice.description() << toQString(inputFormat); + return; + } + m_inputFormat = inputFormat; m_audioInput.reset(new QAudioSource(selectedDevice, m_inputFormat)); if (!m_audioInputBuffer) { m_audioInputBuffer = new CAudioInputBuffer(this); } diff --git a/src/sound/audioutilities.cpp b/src/sound/audioutilities.cpp index 7886ca70f..1c572c947 100644 --- a/src/sound/audioutilities.cpp +++ b/src/sound/audioutilities.cpp @@ -73,7 +73,50 @@ namespace swift::sound { const QAudioDevice defDevice = device.isInputDevice() ? QMediaDevices::defaultAudioInput() : QMediaDevices::defaultAudioOutput(); - Q_ASSERT_X(defDevice.isFormatSupported(format), Q_FUNC_INFO, "Device does not support format"); + + if (defDevice.isNull()) + { + // No default device available + return {}; + } + + if (!defDevice.isFormatSupported(format)) + { + // Try to find a supported format + QAudioFormat adjustedFormat = defDevice.preferredFormat(); + + // Try to keep as many original settings as possible + adjustedFormat.setSampleRate(format.sampleRate()); + adjustedFormat.setChannelCount(format.channelCount()); + adjustedFormat.setSampleFormat(format.sampleFormat()); + + if (defDevice.isFormatSupported(adjustedFormat)) + { + format = adjustedFormat; + return defDevice; + } + + // Fallback: Try with preferred format's sample rate but keep other settings + adjustedFormat = defDevice.preferredFormat(); + adjustedFormat.setChannelCount(format.channelCount()); + adjustedFormat.setSampleFormat(format.sampleFormat()); + + if (defDevice.isFormatSupported(adjustedFormat)) + { + format = adjustedFormat; + return defDevice; + } + + // Last resort: Use the device's preferred format entirely + if (defDevice.isFormatSupported(defDevice.preferredFormat())) + { + format = defDevice.preferredFormat(); + return defDevice; + } + + // Device doesn't support any reasonable format + return {}; + } return defDevice; }