fix: Fixes a crash when no audio device is connected

This commit is contained in:
Thomas Zobler
2026-06-23 10:07:50 +02:00
committed by Lars Toenning
parent 87528d9086
commit d8ce95e3f1
2 changed files with 59 additions and 1 deletions

View File

@@ -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); }

View File

@@ -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;
}