Skip to content

Commit 1ecaa82

Browse files
committed
More work-around for Qt6.8.2 audio format bug.
1 parent c6c29bc commit 1ecaa82

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

SerialPrograms/Source/CommonFramework/AudioPipeline/AudioInfo.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,28 @@ AudioSampleFormat get_sample_format(QAudioFormat& native_format){
146146
}
147147

148148

149+
std::string format_to_str(const QAudioFormat& format){
150+
std::string str;
151+
str += "Preferred Format:\n";
152+
str += " Channels: " + std::to_string(format.channelCount()) + "\n";
153+
str += " Sample Rate: " + std::to_string(format.sampleRate()) + "\n";
154+
#if QT_VERSION_MAJOR == 5
155+
str += " Sample Format: " + std::to_string(format.sampleType()) + "\n";
156+
#else
157+
str += " Sample Format: " + std::to_string(format.sampleFormat()) + "\n";
158+
#endif
159+
return str;
160+
}
161+
162+
149163
// Return a list of our formats that are supported by this device.
150164
// "preferred_index" is set to the index of the list that is preferred by the device.
151165
// If no preferred format matches our formats, -1 is returned.
152166
std::vector<AudioChannelFormat> supported_input_formats(int& preferred_index, const NativeAudioInfo& info, const std::string& display_name){
153167
QAudioFormat preferred_format = info.preferredFormat();
154168

155169
std::string str = display_name + "\n";
156-
str += "Preferred Format:\n";
157-
str += " Channels: " + std::to_string(preferred_format.channelCount()) + "\n";
158-
str += " Sample Rate: " + std::to_string(preferred_format.sampleRate()) + "\n";
159-
#if QT_VERSION_MAJOR == 5
160-
str += " Sample Format: " + std::to_string(preferred_format.sampleType()) + "\n";
161-
#else
162-
str += " Sample Format: " + std::to_string(preferred_format.sampleFormat()) + "\n";
163-
#endif
170+
str += format_to_str(preferred_format);
164171
global_logger_tagged().log(str);
165172

166173
// preferred_format.setSampleSize(16);
@@ -420,19 +427,31 @@ std::vector<AudioDeviceInfo> AudioDeviceInfo::all_output_devices(){
420427
Data& data = *list.back().m_body;
421428

422429
std::string name = device.deviceName().toStdString();
430+
431+
std::string str = name + "\n";
432+
str += format_to_str(device.preferredFormat());
433+
global_logger_tagged().log(str);
434+
423435
data.device_name = name;
424436
data.display_name = std::move(name);
425437
data.info = std::move(device);
426438

427439
data.supported_formats = supported_output_formats(data.preferred_format_index, data.info);
440+
428441
}
429442
#elif QT_VERSION_MAJOR == 6
430443
for (NativeAudioInfo& device : QMediaDevices::audioOutputs()){
431444
list.emplace_back();
432445
Data& data = *list.back().m_body;
433446

447+
std::string name = device.description().toStdString();
448+
449+
std::string str = name + "\n";
450+
str += format_to_str(device.preferredFormat());
451+
global_logger_tagged().log(str);
452+
434453
data.device_name = device.id().toStdString();
435-
data.display_name = device.description().toStdString();
454+
data.display_name = std::move(name);
436455
data.info = std::move(device);
437456

438457
data.supported_formats = supported_output_formats(data.preferred_format_index, data.info);

SerialPrograms/Source/CommonFramework/AudioPipeline/IO/AudioSink.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,21 @@ AudioSink::~AudioSink(){}
102102
AudioSink::AudioSink(Logger& logger, const AudioDeviceInfo& device, AudioChannelFormat format, double volume){
103103
NativeAudioInfo native_info = device.native_info();
104104
QAudioFormat native_format = native_info.preferredFormat();
105+
QAudioFormat target_format = native_format;
105106

106-
set_format(native_format, format);
107+
set_format(target_format, format);
107108

108-
AudioSampleFormat sample_format = get_sample_format(native_format);
109+
AudioSampleFormat sample_format = get_sample_format(target_format);
109110
if (sample_format == AudioSampleFormat::INVALID){
110111
sample_format = AudioSampleFormat::FLOAT32;
111-
setSampleFormatToFloat(native_format);
112+
setSampleFormatToFloat(target_format);
112113
}
113114

114-
logger.log("AudioOutputDevice(): " + dumpAudioFormat(native_format));
115-
if (!native_info.isFormatSupported(native_format)){
115+
logger.log("AudioOutputDevice(): Target: " + dumpAudioFormat(target_format));
116+
logger.log("AudioOutputDevice(): Native: " + dumpAudioFormat(native_format));
117+
if (!native_info.isFormatSupported(native_format) &&
118+
native_format != target_format
119+
){
116120
logger.log("Audio output device does not support the requested audio format.", COLOR_RED);
117121
return;
118122
}

0 commit comments

Comments
 (0)