mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-01 13:36:48 +08:00
[AFV] Select audio device based on situational need
For AFV itself, a low latency device is required (which on Windows most likely will pick WASAPI). For notifications and effects, the most compatible device is required (which on Windows will most likely fall back to QWindowsAudio).
This commit is contained in:
committed by
Mat Sutcliffe
parent
18ec101391
commit
90e87835fc
@@ -7,6 +7,10 @@
|
||||
*/
|
||||
|
||||
#include "audioutilities.h"
|
||||
#include <QAudioInput>
|
||||
#include <QAudioOutput>
|
||||
|
||||
using namespace BlackMisc::Audio;
|
||||
|
||||
namespace BlackSound
|
||||
{
|
||||
@@ -76,4 +80,98 @@ namespace BlackSound
|
||||
return output;
|
||||
}
|
||||
|
||||
QAudioDeviceInfo getLowestLatencyDevice(const CAudioDeviceInfo &device, QAudioFormat &format)
|
||||
{
|
||||
if (device.isDefault())
|
||||
{
|
||||
if (device.getType() == CAudioDeviceInfo::InputDevice) { return QAudioDeviceInfo::defaultInputDevice(); }
|
||||
else { return QAudioDeviceInfo::defaultOutputDevice(); }
|
||||
}
|
||||
|
||||
QAudio::Mode mode = device.getType() == CAudioDeviceInfo::InputDevice ? QAudio::AudioInput : QAudio::AudioOutput;
|
||||
const QList<QAudioDeviceInfo> allQtDevices = QAudioDeviceInfo::availableDevices(mode);
|
||||
|
||||
// Find the one with lowest latency.
|
||||
QList<QAudioDeviceInfo> supportedDevices;
|
||||
for (const QAudioDeviceInfo &d : allQtDevices)
|
||||
{
|
||||
if (d.deviceName() == device.getName())
|
||||
{
|
||||
if (! d.isFormatSupported(format))
|
||||
{
|
||||
// Check whether the nearest format is acceptable for our needs
|
||||
QAudioFormat nearestFormat = d.nearestFormat(format);
|
||||
if (nearestFormat.sampleRate() != format.sampleRate() ||
|
||||
nearestFormat.sampleSize() != format.sampleSize() ||
|
||||
nearestFormat.sampleType() != format.sampleType() ||
|
||||
nearestFormat.byteOrder() != format.byteOrder() ||
|
||||
nearestFormat.codec() != format.codec())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
supportedDevices.push_back(d);
|
||||
}
|
||||
}
|
||||
|
||||
if (supportedDevices.empty()) { return {}; }
|
||||
|
||||
QAudioDeviceInfo deviceWithLowestLatency = supportedDevices.at(0);
|
||||
|
||||
if (supportedDevices.size() > 1)
|
||||
{
|
||||
QAudioFormat nearestFormat = format;
|
||||
int lowestBufferSize = std::numeric_limits<int>::max();
|
||||
for (const QAudioDeviceInfo &d : supportedDevices)
|
||||
{
|
||||
int bufferSize = 0;
|
||||
if (device.getType() == CAudioDeviceInfo::InputDevice)
|
||||
{
|
||||
QAudioInput input(d, d.nearestFormat(format));
|
||||
input.start();
|
||||
input.stop();
|
||||
bufferSize = input.bufferSize();
|
||||
}
|
||||
else
|
||||
{
|
||||
QAudioOutput output(d, d.nearestFormat(format));
|
||||
output.start();
|
||||
output.stop();
|
||||
bufferSize = output.bufferSize();
|
||||
}
|
||||
|
||||
if (bufferSize < lowestBufferSize)
|
||||
{
|
||||
deviceWithLowestLatency = d;
|
||||
nearestFormat = d.nearestFormat(format);
|
||||
lowestBufferSize = bufferSize;
|
||||
}
|
||||
}
|
||||
format = nearestFormat;
|
||||
}
|
||||
return deviceWithLowestLatency;
|
||||
}
|
||||
|
||||
QAudioDeviceInfo getHighestCompatibleOutputDevice(const CAudioDeviceInfo &device, QAudioFormat &format)
|
||||
{
|
||||
if (device.isDefault()) { return QAudioDeviceInfo::defaultOutputDevice(); }
|
||||
|
||||
const QList<QAudioDeviceInfo> allQtDevices = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
|
||||
|
||||
QList<QAudioDeviceInfo> supportedDevices;
|
||||
for (const QAudioDeviceInfo &d : allQtDevices)
|
||||
{
|
||||
if (d.deviceName() == device.getName())
|
||||
{
|
||||
if (d.isFormatSupported(format))
|
||||
{
|
||||
supportedDevices.push_back(d);
|
||||
}
|
||||
}
|
||||
|
||||
if (supportedDevices.size() > 0) { return supportedDevices.at(0); }
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // ns
|
||||
|
||||
Reference in New Issue
Block a user