mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-01 21:56:43 +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
@@ -19,6 +19,7 @@
|
||||
#include <cmath>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Audio;
|
||||
using namespace BlackSound;
|
||||
|
||||
namespace BlackCore
|
||||
@@ -27,6 +28,7 @@ namespace BlackCore
|
||||
{
|
||||
namespace Audio
|
||||
{
|
||||
|
||||
CAudioInputBuffer::CAudioInputBuffer(QObject *parent) :
|
||||
QIODevice(parent)
|
||||
{}
|
||||
@@ -34,7 +36,6 @@ namespace BlackCore
|
||||
void CAudioInputBuffer::start()
|
||||
{
|
||||
open(QIODevice::WriteOnly | QIODevice::Unbuffered);
|
||||
// m_timerId = startTimer(5, Qt::PreciseTimer);
|
||||
}
|
||||
|
||||
void CAudioInputBuffer::stop()
|
||||
@@ -91,28 +92,11 @@ namespace BlackCore
|
||||
m_encoder.setBitRate(16 * 1024);
|
||||
}
|
||||
|
||||
void CInput::start(const BlackMisc::Audio::CAudioDeviceInfo &inputDevice)
|
||||
void CInput::start(const CAudioDeviceInfo &inputDevice)
|
||||
{
|
||||
if (m_started) { return; }
|
||||
|
||||
m_device = inputDevice;
|
||||
QAudioDeviceInfo selectedDevice;
|
||||
if (inputDevice.isDefault())
|
||||
{
|
||||
selectedDevice = QAudioDeviceInfo::defaultInputDevice();
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: Add smart algorithm to find the device with lowest latency
|
||||
const QList<QAudioDeviceInfo> inputDevices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
|
||||
for (const QAudioDeviceInfo &d : inputDevices)
|
||||
{
|
||||
if (d.deviceName() == inputDevice.getName())
|
||||
{
|
||||
selectedDevice = d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_inputFormat.setSampleRate(m_sampleRate);
|
||||
m_inputFormat.setChannelCount(1);
|
||||
@@ -120,21 +104,8 @@ namespace BlackCore
|
||||
m_inputFormat.setSampleType(QAudioFormat::SignedInt);
|
||||
m_inputFormat.setByteOrder(QAudioFormat::LittleEndian);
|
||||
m_inputFormat.setCodec("audio/pcm");
|
||||
if (!selectedDevice.isFormatSupported(m_inputFormat))
|
||||
{
|
||||
m_inputFormat = selectedDevice.nearestFormat(m_inputFormat);
|
||||
const QString w =
|
||||
selectedDevice.deviceName() %
|
||||
": Default INPUT format not supported - trying to use nearest" %
|
||||
" Sample rate: " % QString::number(m_inputFormat.sampleRate()) %
|
||||
" Sample size: " % QString::number(m_inputFormat.sampleSize()) %
|
||||
" Sample type: " % QString::number(m_inputFormat.sampleType()) %
|
||||
" Byte order: " % QString::number(m_inputFormat.byteOrder()) %
|
||||
" Codec: " % m_inputFormat.codec() %
|
||||
" Channel count: " % QString::number(m_inputFormat.channelCount());
|
||||
CLogMessage(this).warning(w);
|
||||
}
|
||||
|
||||
QAudioDeviceInfo selectedDevice = getLowestLatencyDevice(inputDevice, m_inputFormat);
|
||||
m_audioInput.reset(new QAudioInput(selectedDevice, m_inputFormat));
|
||||
m_audioInputBuffer.start();
|
||||
|
||||
|
||||
@@ -90,23 +90,6 @@ namespace BlackCore
|
||||
connect(m_audioOutputBuffer, &CAudioOutputBuffer::outputVolumeStream, this, &Output::outputVolumeStream);
|
||||
|
||||
m_device = outputDevice;
|
||||
QAudioDeviceInfo selectedDevice;
|
||||
if (outputDevice.isDefault())
|
||||
{
|
||||
selectedDevice = QAudioDeviceInfo::defaultOutputDevice();
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: Add smart algorithm to find the device with lowest latency
|
||||
const QList<QAudioDeviceInfo> outputDevices = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
|
||||
for (const QAudioDeviceInfo &d : outputDevices)
|
||||
{
|
||||
if (d.deviceName() == outputDevice.getName())
|
||||
{
|
||||
selectedDevice = d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QAudioFormat outputFormat;
|
||||
outputFormat.setSampleRate(48000);
|
||||
@@ -116,23 +99,8 @@ namespace BlackCore
|
||||
outputFormat.setByteOrder(QAudioFormat::LittleEndian);
|
||||
outputFormat.setCodec("audio/pcm");
|
||||
|
||||
if (!selectedDevice.isFormatSupported(outputFormat))
|
||||
{
|
||||
outputFormat = selectedDevice.nearestFormat(outputFormat);
|
||||
const QString w =
|
||||
selectedDevice.deviceName() %
|
||||
": Default OUTPUT format not supported - trying to use nearest" %
|
||||
" Sample rate: " % QString::number(outputFormat.sampleRate()) %
|
||||
" Sample size: " % QString::number(outputFormat.sampleSize()) %
|
||||
" Sample type: " % QString::number(outputFormat.sampleType()) %
|
||||
" Byte order: " % QString::number(outputFormat.byteOrder()) %
|
||||
" Codec: " % outputFormat.codec() %
|
||||
" Channel count: " % QString::number(outputFormat.channelCount());
|
||||
CLogMessage(this).warning(w);
|
||||
}
|
||||
|
||||
QAudioDeviceInfo selectedDevice = getLowestLatencyDevice(outputDevice, outputFormat);
|
||||
m_audioOutputCom.reset(new QAudioOutput(selectedDevice, outputFormat));
|
||||
// m_audioOutput->setBufferSize(bufferSize);
|
||||
m_audioOutputBuffer->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
|
||||
m_audioOutputBuffer->setAudioFormat(outputFormat);
|
||||
m_audioOutputCom->start(m_audioOutputBuffer);
|
||||
|
||||
Reference in New Issue
Block a user