Skip to content

Commit 241ea78

Browse files
committed
Try to fix Qt6.8.2 audio format issue.
1 parent f2b736b commit 241ea78

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

SerialPrograms/Source/CommonFramework/AudioPipeline/AudioInfo.cpp

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
#else
2929
#error "Unsupported Qt version."
3030
#endif
31+
32+
33+
#if QT_VERSION_MAJOR != 5
34+
#define PA_AUDIO_USE_CHANNEL_CONFIG
35+
#endif
36+
37+
3138
namespace PokemonAutomation{
3239

3340

@@ -45,35 +52,59 @@ const char* AUDIO_FORMAT_LABELS[] = {
4552
void set_format(QAudioFormat& native_format, AudioChannelFormat format){
4653
switch (format){
4754
case AudioChannelFormat::MONO_48000:
55+
#ifdef PA_AUDIO_USE_CHANNEL_CONFIG
56+
if (native_format.channelConfig() != QAudioFormat::ChannelConfigMono){
57+
native_format.setChannelConfig(QAudioFormat::ChannelConfigMono);
58+
}
59+
#else
4860
if (native_format.channelCount() != 1){
4961
native_format.setChannelCount(1);
5062
}
63+
#endif
5164
if (native_format.sampleRate() != 48000){
5265
native_format.setSampleRate(48000);
5366
}
5467
break;
5568
case AudioChannelFormat::DUAL_44100:
69+
#ifdef PA_AUDIO_USE_CHANNEL_CONFIG
70+
if (native_format.channelConfig() != QAudioFormat::ChannelConfigStereo){
71+
native_format.setChannelConfig(QAudioFormat::ChannelConfigStereo);
72+
}
73+
#else
5674
if (native_format.channelCount() != 2){
5775
native_format.setChannelCount(2);
5876
}
77+
#endif
5978
if (native_format.sampleRate() != 44100){
6079
native_format.setSampleRate(44100);
6180
}
6281
break;
6382
case AudioChannelFormat::DUAL_48000:
83+
#ifdef PA_AUDIO_USE_CHANNEL_CONFIG
84+
if (native_format.channelConfig() != QAudioFormat::ChannelConfigStereo){
85+
native_format.setChannelConfig(QAudioFormat::ChannelConfigStereo);
86+
}
87+
#else
6488
if (native_format.channelCount() != 2){
6589
native_format.setChannelCount(2);
6690
}
91+
#endif
6792
if (native_format.sampleRate() != 48000){
6893
native_format.setSampleRate(48000);
6994
}
7095
break;
7196
case AudioChannelFormat::MONO_96000:
7297
case AudioChannelFormat::INTERLEAVE_LR_96000:
7398
case AudioChannelFormat::INTERLEAVE_RL_96000:
99+
#ifdef PA_AUDIO_USE_CHANNEL_CONFIG
100+
if (native_format.channelConfig() != QAudioFormat::ChannelConfigMono){
101+
native_format.setChannelConfig(QAudioFormat::ChannelConfigMono);
102+
}
103+
#else
74104
if (native_format.channelCount() != 1){
75105
native_format.setChannelCount(1);
76106
}
107+
#endif
77108
if (native_format.sampleRate() != 96000){
78109
native_format.setSampleRate(96000);
79110
}
@@ -121,18 +152,36 @@ AudioSampleFormat get_sample_format(QAudioFormat& native_format){
121152
std::vector<AudioChannelFormat> supported_input_formats(int& preferred_index, const NativeAudioInfo& info, const std::string& display_name){
122153
QAudioFormat preferred_format = info.preferredFormat();
123154

155+
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
164+
global_logger_tagged().log(str);
165+
124166
// preferred_format.setSampleSize(16);
125167
// preferred_format.setSampleType(QAudioFormat::SampleType::SignedInt);
126168

127169
int preferred_channels = preferred_format.channelCount();
128170
int preferred_rate = preferred_format.sampleRate();
129-
// cout << "display_name = " << display_name << endl;
130-
// cout << "channelCount = " << preferred_format.channelCount() << endl;
131-
// cout << "sample_rate = " << preferred_format.sample_rate() << endl;
132-
// cout << "sampleSize = " << preferred_format.sampleSize() << endl;
133-
// cout << "sampleFormat = " << preferred_format.sampleType() << endl;
134-
// cout << "preferred_format = " << info.isFormatSupported(preferred_format) << endl;
135171

172+
#if 0
173+
cout << "display_name = " << display_name << endl;
174+
cout << "channelCount = " << preferred_format.channelCount() << endl;
175+
cout << "sampleRate = " << preferred_format.sampleRate() << endl;
176+
cout << "sampleFormat = " << (int)preferred_format.sampleFormat() << endl;
177+
cout << "preferred_format = " << info.isFormatSupported(preferred_format) << endl;
178+
179+
for (const QAudioFormat::SampleFormat& format : info.supportedSampleFormats()){
180+
cout << "supported format: " << (int)format << endl;
181+
}
182+
#endif
183+
184+
#if 0
136185
#if QT_VERSION_MAJOR == 6 || (QT_VERSION_MAJOR == 5 && __GNUC__)
137186
// On Qt6, "QAudioFormat::preferredFormat()" always returns stereo 44.1 kHz.
138187
// Unlike Qt5, it does not return the native format of the device
@@ -141,6 +190,7 @@ std::vector<AudioChannelFormat> supported_input_formats(int& preferred_index, co
141190
// mono 96000 which will be the case for standard capture cards.
142191
preferred_channels = 1;
143192
preferred_rate = 96000;
193+
#endif
144194
#endif
145195

146196
std::vector<AudioChannelFormat> ret;

0 commit comments

Comments
 (0)