fix: Prevents a crash if no audio device is connected

This commit is contained in:
Thomas Zobler
2026-06-23 10:23:43 +02:00
committed by Lars Toenning
parent d8ce95e3f1
commit e75dad4448
2 changed files with 11 additions and 11 deletions

View File

@@ -96,21 +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,47 +73,47 @@ namespace swift::sound
{
const QAudioDevice defDevice =
device.isInputDevice() ? QMediaDevices::defaultAudioInput() : QMediaDevices::defaultAudioOutput();
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 {};
}