mirror of
https://github.com/JulianGro/overte.git
synced 2025-05-04 20:32:41 +02:00
Refactor the audio device format detection. Only use the native format when Qt can correctly determine it.
This commit is contained in:
parent
c81b0fc018
commit
d69d77d0ee
1 changed files with 37 additions and 26 deletions
|
@ -375,41 +375,53 @@ QAudioDeviceInfo defaultAudioDeviceForMode(QAudio::Mode mode) {
|
||||||
return (mode == QAudio::AudioInput) ? QAudioDeviceInfo::defaultInputDevice() : QAudioDeviceInfo::defaultOutputDevice();
|
return (mode == QAudio::AudioInput) ? QAudioDeviceInfo::defaultInputDevice() : QAudioDeviceInfo::defaultOutputDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// attempt to use the native sample rate and channel count
|
||||||
|
bool nativeFormatForAudioDevice(const QAudioDeviceInfo& audioDevice,
|
||||||
|
QAudioFormat& audioFormat) {
|
||||||
|
|
||||||
|
audioFormat = audioDevice.preferredFormat();
|
||||||
|
|
||||||
|
audioFormat.setCodec("audio/pcm");
|
||||||
|
audioFormat.setSampleSize(16);
|
||||||
|
audioFormat.setSampleType(QAudioFormat::SignedInt);
|
||||||
|
audioFormat.setByteOrder(QAudioFormat::LittleEndian);
|
||||||
|
|
||||||
|
if (!audioDevice.isFormatSupported(audioFormat)) {
|
||||||
|
qCDebug(audioclient) << "WARNING: The native format is" << audioFormat << "but isFormatSupported() failed.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// converting to/from this rate must produce an integral number of samples
|
||||||
|
if (audioFormat.sampleRate() * AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL % AudioConstants::SAMPLE_RATE != 0) {
|
||||||
|
qCDebug(audioclient) << "WARNING: The native sample rate [" << audioFormat.sampleRate() << "] is not supported.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool adjustedFormatForAudioDevice(const QAudioDeviceInfo& audioDevice,
|
bool adjustedFormatForAudioDevice(const QAudioDeviceInfo& audioDevice,
|
||||||
const QAudioFormat& desiredAudioFormat,
|
const QAudioFormat& desiredAudioFormat,
|
||||||
QAudioFormat& adjustedAudioFormat) {
|
QAudioFormat& adjustedAudioFormat) {
|
||||||
|
|
||||||
qCDebug(audioclient) << "The desired format for audio I/O is" << desiredAudioFormat;
|
qCDebug(audioclient) << "The desired format for audio I/O is" << desiredAudioFormat;
|
||||||
|
|
||||||
adjustedAudioFormat = desiredAudioFormat;
|
#if defined(Q_OS_ANDROID) || defined(Q_OS_OSX)
|
||||||
|
// As of Qt5.6, Android returns the native OpenSLES sample rate when possible, else 48000
|
||||||
|
// Mac OSX returns the preferred CoreAudio format
|
||||||
|
if (nativeFormatForAudioDevice(audioDevice, adjustedAudioFormat)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(Q_OS_WIN)
|
#if defined(Q_OS_WIN)
|
||||||
|
if (IsWindows8OrGreater()) {
|
||||||
// On Windows, using WASAPI shared mode, the sample rate and channel count must
|
// On Windows using WASAPI shared-mode, returns the internal mix format
|
||||||
// exactly match the internal mix format. Any other format will fail to open.
|
if (nativeFormatForAudioDevice(audioDevice, adjustedAudioFormat)) {
|
||||||
|
return true;
|
||||||
adjustedAudioFormat = audioDevice.preferredFormat(); // returns mixFormat
|
}
|
||||||
|
|
||||||
adjustedAudioFormat.setCodec("audio/pcm");
|
|
||||||
adjustedAudioFormat.setSampleSize(16);
|
|
||||||
adjustedAudioFormat.setSampleType(QAudioFormat::SignedInt);
|
|
||||||
adjustedAudioFormat.setByteOrder(QAudioFormat::LittleEndian);
|
|
||||||
|
|
||||||
if (!audioDevice.isFormatSupported(adjustedAudioFormat)) {
|
|
||||||
qCDebug(audioclient) << "WARNING: The mix format is" << adjustedAudioFormat << "but isFormatSupported() failed.";
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
// converting to/from this rate must produce an integral number of samples
|
#endif
|
||||||
if (adjustedAudioFormat.sampleRate() * AudioConstants::NETWORK_FRAME_SAMPLES_PER_CHANNEL % AudioConstants::SAMPLE_RATE != 0) {
|
|
||||||
qCDebug(audioclient) << "WARNING: The current sample rate [" << adjustedAudioFormat.sampleRate() << "] is not supported.";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
|
|
||||||
#elif defined(Q_OS_ANDROID)
|
adjustedAudioFormat = desiredAudioFormat;
|
||||||
// FIXME: query the native sample rate of the device?
|
|
||||||
adjustedAudioFormat.setSampleRate(48000);
|
|
||||||
#else
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Attempt the device sample rate in decreasing order of preference.
|
// Attempt the device sample rate in decreasing order of preference.
|
||||||
|
@ -433,7 +445,6 @@ bool adjustedFormatForAudioDevice(const QAudioDeviceInfo& audioDevice,
|
||||||
} else if (audioDevice.supportedSampleRates().contains(176400)) {
|
} else if (audioDevice.supportedSampleRates().contains(176400)) {
|
||||||
adjustedAudioFormat.setSampleRate(176400);
|
adjustedAudioFormat.setSampleRate(176400);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
if (adjustedAudioFormat != desiredAudioFormat) {
|
if (adjustedAudioFormat != desiredAudioFormat) {
|
||||||
// return the nearest in case it needs 2 channels
|
// return the nearest in case it needs 2 channels
|
||||||
|
|
Loading…
Reference in a new issue