mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-06 18:25:37 +08:00
Replace QAudioDeviceInfo with CAudioDeviceInfo where possible
QAudioDeviceInfo is a low level technical class, which shouldn't be used in higher level code. Remove it from all APIs where possible and just create it in order to interface with QAudio
This commit is contained in:
committed by
Mat Sutcliffe
parent
8656131eb1
commit
a2e3700739
@@ -15,6 +15,7 @@
|
||||
#include <QtGlobal>
|
||||
#include <QStringBuilder>
|
||||
#include <QDebug>
|
||||
#include <QAudioDeviceInfo>
|
||||
#include <cmath>
|
||||
|
||||
using namespace BlackMisc;
|
||||
@@ -90,21 +91,40 @@ namespace BlackCore
|
||||
m_encoder.setBitRate(16 * 1024);
|
||||
}
|
||||
|
||||
void CInput::start(const QAudioDeviceInfo &inputDevice)
|
||||
void CInput::start(const BlackMisc::Audio::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);
|
||||
m_inputFormat.setSampleSize(16);
|
||||
m_inputFormat.setSampleType(QAudioFormat::SignedInt);
|
||||
m_inputFormat.setByteOrder(QAudioFormat::LittleEndian);
|
||||
m_inputFormat.setCodec("audio/pcm");
|
||||
if (!inputDevice.isFormatSupported(m_inputFormat))
|
||||
if (!selectedDevice.isFormatSupported(m_inputFormat))
|
||||
{
|
||||
m_inputFormat = inputDevice.nearestFormat(m_inputFormat);
|
||||
m_inputFormat = selectedDevice.nearestFormat(m_inputFormat);
|
||||
const QString w =
|
||||
inputDevice.deviceName() %
|
||||
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()) %
|
||||
@@ -115,7 +135,7 @@ namespace BlackCore
|
||||
CLogMessage(this).warning(w);
|
||||
}
|
||||
|
||||
m_audioInput.reset(new QAudioInput(inputDevice, m_inputFormat));
|
||||
m_audioInput.reset(new QAudioInput(selectedDevice, m_inputFormat));
|
||||
m_audioInputBuffer.start();
|
||||
|
||||
m_audioInput->start(&m_audioInputBuffer);
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
|
||||
#include "blacksound/sampleprovider/bufferedwaveprovider.h"
|
||||
#include "blacksound/codecs/opusencoder.h"
|
||||
#include "blackmisc/audio/audiodeviceinfo.h"
|
||||
|
||||
#include <QAudioDeviceInfo>
|
||||
#include <QAudioInput>
|
||||
#include <QString>
|
||||
#include <QDateTime>
|
||||
@@ -70,7 +70,6 @@ namespace BlackCore
|
||||
|
||||
struct InputVolumeStreamArgs
|
||||
{
|
||||
QAudioDeviceInfo DeviceNumber;
|
||||
double PeakRaw = 0.0;
|
||||
double PeakDB = -1.0 * std::numeric_limits<double>::infinity();
|
||||
double PeakVU = 0.0;
|
||||
@@ -101,13 +100,13 @@ namespace BlackCore
|
||||
bool started() const { return m_started; }
|
||||
|
||||
//! Start
|
||||
void start(const QAudioDeviceInfo &inputDevice);
|
||||
void start(const BlackMisc::Audio::CAudioDeviceInfo &inputDevice);
|
||||
|
||||
//! Stop
|
||||
void stop();
|
||||
|
||||
//! Corresponding device
|
||||
const QAudioDeviceInfo &device() const { return m_device; }
|
||||
const BlackMisc::Audio::CAudioDeviceInfo &device() const { return m_device; }
|
||||
|
||||
signals:
|
||||
//! Volume stream data
|
||||
@@ -124,7 +123,7 @@ namespace BlackCore
|
||||
|
||||
BlackSound::Codecs::COpusEncoder m_encoder;
|
||||
QScopedPointer<QAudioInput> m_audioInput;
|
||||
QAudioDeviceInfo m_device;
|
||||
BlackMisc::Audio::CAudioDeviceInfo m_device;
|
||||
QAudioFormat m_inputFormat;
|
||||
|
||||
bool m_started = false;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <cmath>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Audio;
|
||||
using namespace BlackSound;
|
||||
using namespace BlackSound::SampleProvider;
|
||||
|
||||
@@ -81,11 +82,32 @@ namespace BlackCore
|
||||
Output::Output(QObject *parent) : QObject(parent)
|
||||
{ }
|
||||
|
||||
void Output::start(const QAudioDeviceInfo &outputDevice, ISampleProvider *sampleProvider)
|
||||
void Output::start(const CAudioDeviceInfo &outputDevice, ISampleProvider *sampleProvider)
|
||||
{
|
||||
if (m_started) { return; }
|
||||
|
||||
m_audioOutputBuffer = new CAudioOutputBuffer(sampleProvider, this);
|
||||
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);
|
||||
outputFormat.setChannelCount(1);
|
||||
@@ -94,11 +116,11 @@ namespace BlackCore
|
||||
outputFormat.setByteOrder(QAudioFormat::LittleEndian);
|
||||
outputFormat.setCodec("audio/pcm");
|
||||
|
||||
if (!outputDevice.isFormatSupported(outputFormat))
|
||||
if (!selectedDevice.isFormatSupported(outputFormat))
|
||||
{
|
||||
outputFormat = outputDevice.nearestFormat(outputFormat);
|
||||
outputFormat = selectedDevice.nearestFormat(outputFormat);
|
||||
const QString w =
|
||||
outputDevice.deviceName() %
|
||||
selectedDevice.deviceName() %
|
||||
": Default OUTPUT format not supported - trying to use nearest" %
|
||||
" Sample rate: " % QString::number(outputFormat.sampleRate()) %
|
||||
" Sample size: " % QString::number(outputFormat.sampleSize()) %
|
||||
@@ -109,7 +131,7 @@ namespace BlackCore
|
||||
CLogMessage(this).warning(w);
|
||||
}
|
||||
|
||||
m_audioOutputCom.reset(new QAudioOutput(outputDevice, outputFormat));
|
||||
m_audioOutputCom.reset(new QAudioOutput(selectedDevice, outputFormat));
|
||||
// m_audioOutput->setBufferSize(bufferSize);
|
||||
m_audioOutputBuffer->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
|
||||
m_audioOutputBuffer->setAudioFormat(outputFormat);
|
||||
|
||||
@@ -12,9 +12,9 @@
|
||||
#define BLACKCORE_AFV_AUDIO_OUTPUT_H
|
||||
|
||||
#include "blacksound/sampleprovider/sampleprovider.h"
|
||||
#include "blackmisc/audio/audiodeviceinfo.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QAudioDeviceInfo>
|
||||
#include <QAudioOutput>
|
||||
|
||||
namespace BlackCore
|
||||
@@ -26,7 +26,6 @@ namespace BlackCore
|
||||
//! Stream args
|
||||
struct OutputVolumeStreamArgs
|
||||
{
|
||||
QAudioDeviceInfo DeviceNumber;
|
||||
double PeakRaw = 0.0;
|
||||
double PeakDB = -1 * std::numeric_limits<double>::infinity();
|
||||
double PeakVU = 0.0;
|
||||
@@ -82,13 +81,13 @@ namespace BlackCore
|
||||
}
|
||||
|
||||
//! Start output
|
||||
void start(const QAudioDeviceInfo &outputDevice, BlackSound::SampleProvider::ISampleProvider *sampleProvider);
|
||||
void start(const BlackMisc::Audio::CAudioDeviceInfo &outputDevice, BlackSound::SampleProvider::ISampleProvider *sampleProvider);
|
||||
|
||||
//! Stop output
|
||||
void stop();
|
||||
|
||||
//! Corresponding device
|
||||
const QAudioDeviceInfo &device() const { return m_device; }
|
||||
const BlackMisc::Audio::CAudioDeviceInfo &device() const { return m_device; }
|
||||
|
||||
signals:
|
||||
//! Streaming data
|
||||
@@ -97,7 +96,7 @@ namespace BlackCore
|
||||
private:
|
||||
bool m_started = false;
|
||||
|
||||
QAudioDeviceInfo m_device;
|
||||
BlackMisc::Audio::CAudioDeviceInfo m_device;
|
||||
QScopedPointer<QAudioOutput> m_audioOutputCom;
|
||||
CAudioOutputBuffer *m_audioOutputBuffer = nullptr;
|
||||
};
|
||||
|
||||
@@ -106,12 +106,12 @@ namespace BlackCore
|
||||
|
||||
QStringList CAfvClient::availableInputDevices() const
|
||||
{
|
||||
return CAudioDeviceInfoList::allQtInputDevices().getDeviceNames();
|
||||
return CAudioDeviceInfoList::allInputDevices().getDeviceNames();
|
||||
}
|
||||
|
||||
QStringList CAfvClient::availableOutputDevices() const
|
||||
{
|
||||
return CAudioDeviceInfoList::allQtOutputDevices().getDeviceNames();
|
||||
return CAudioDeviceInfoList::allOutputDevices().getDeviceNames();
|
||||
}
|
||||
|
||||
void CAfvClient::setBypassEffects(bool value)
|
||||
@@ -132,14 +132,14 @@ namespace BlackCore
|
||||
Q_UNUSED(mute)
|
||||
}
|
||||
|
||||
bool CAfvClient::restartWithNewDevices(const QAudioDeviceInfo &inputDevice, const QAudioDeviceInfo &outputDevice)
|
||||
bool CAfvClient::restartWithNewDevices(const CAudioDeviceInfo &inputDevice, const CAudioDeviceInfo &outputDevice)
|
||||
{
|
||||
this->stop();
|
||||
this->start(inputDevice, outputDevice, allTransceiverIds());
|
||||
return true;
|
||||
}
|
||||
|
||||
void CAfvClient::start(const QAudioDeviceInfo &inputDevice, const QAudioDeviceInfo &outputDevice, const QVector<quint16> &transceiverIDs)
|
||||
void CAfvClient::start(const CAudioDeviceInfo &inputDevice, const CAudioDeviceInfo &outputDevice, const QVector<quint16> &transceiverIDs)
|
||||
{
|
||||
if (m_isStarted)
|
||||
{
|
||||
@@ -154,21 +154,27 @@ namespace BlackCore
|
||||
outputSampleProvider = new CVolumeSampleProvider(soundcardSampleProvider, this);
|
||||
outputSampleProvider->setVolume(m_outputVolume);
|
||||
|
||||
m_output->start(outputDevice.isNull() ? QAudioDeviceInfo::defaultOutputDevice() : outputDevice, outputSampleProvider);
|
||||
m_input->start(inputDevice.isNull() ? QAudioDeviceInfo::defaultInputDevice() : inputDevice);
|
||||
m_output->start(outputDevice, outputSampleProvider);
|
||||
m_input->start(inputDevice);
|
||||
|
||||
m_startDateTimeUtc = QDateTime::currentDateTimeUtc();
|
||||
m_connection->setReceiveAudio(true);
|
||||
m_voiceServerPositionTimer->start(5000);
|
||||
this->onSettingsChanged(); // make sure all settings are applied
|
||||
m_isStarted = true;
|
||||
CLogMessage(this).info(u"Started [Input: %1] [Output: %2]") << inputDevice.deviceName() << outputDevice.deviceName();
|
||||
CLogMessage(this).info(u"Started [Input: %1] [Output: %2]") << inputDevice.getName() << outputDevice.getName();
|
||||
}
|
||||
|
||||
void CAfvClient::start(const QString &inputDeviceName, const QString &outputDeviceName)
|
||||
{
|
||||
const QAudioDeviceInfo i = CAudioDeviceInfoList::allQtInputDevices().findByName(inputDeviceName).toAudioDeviceInfo();
|
||||
const QAudioDeviceInfo o = CAudioDeviceInfoList::allQtOutputDevices().findByName(outputDeviceName).toAudioDeviceInfo();
|
||||
if (QThread::currentThread() != this->thread())
|
||||
{
|
||||
QMetaObject::invokeMethod(this, "start", Q_ARG(QString, inputDeviceName), Q_ARG(QString, outputDeviceName));
|
||||
return;
|
||||
}
|
||||
|
||||
const CAudioDeviceInfo i(CAudioDeviceInfo::InputDevice, inputDeviceName);
|
||||
const CAudioDeviceInfo o(CAudioDeviceInfo::OutputDevice, outputDeviceName);
|
||||
this->start(i, o, allTransceiverIds());
|
||||
}
|
||||
|
||||
@@ -581,17 +587,17 @@ namespace BlackCore
|
||||
}
|
||||
}
|
||||
|
||||
const QAudioDeviceInfo &CAfvClient::getInputDevice() const
|
||||
const CAudioDeviceInfo &CAfvClient::getInputDevice() const
|
||||
{
|
||||
if (m_input) { return m_input->device(); }
|
||||
static const QAudioDeviceInfo null = QAudioDeviceInfo();
|
||||
return null;
|
||||
static const CAudioDeviceInfo nullDevice;
|
||||
return nullDevice;
|
||||
}
|
||||
|
||||
const QAudioDeviceInfo &CAfvClient::getOutputDevice() const
|
||||
const CAudioDeviceInfo &CAfvClient::getOutputDevice() const
|
||||
{
|
||||
if (m_output) { return m_output->device(); }
|
||||
static const QAudioDeviceInfo nullDevice = QAudioDeviceInfo();
|
||||
static const CAudioDeviceInfo nullDevice;
|
||||
return nullDevice;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,11 +23,11 @@
|
||||
#include "blackmisc/aviation/comsystem.h"
|
||||
#include "blackmisc/audio/audiosettings.h"
|
||||
#include "blackmisc/audio/ptt.h"
|
||||
#include "blackmisc/audio/audiodeviceinfo.h"
|
||||
#include "blackmisc/logcategorylist.h"
|
||||
#include "blackmisc/identifiable.h"
|
||||
#include "blackmisc/settingscache.h"
|
||||
|
||||
#include <QAudioDeviceInfo>
|
||||
#include <QDateTime>
|
||||
#include <QAudioInput>
|
||||
#include <QAudioOutput>
|
||||
@@ -90,8 +90,8 @@ namespace BlackCore
|
||||
void setMuted(bool mute);
|
||||
//! @}
|
||||
|
||||
bool restartWithNewDevices(const QAudioDeviceInfo &inputDevice, const QAudioDeviceInfo &outputDevice);
|
||||
void start(const QAudioDeviceInfo &inputDevice, const QAudioDeviceInfo &outputDevice, const QVector<quint16> &transceiverIDs);
|
||||
bool restartWithNewDevices(const BlackMisc::Audio::CAudioDeviceInfo &inputDevice, const BlackMisc::Audio::CAudioDeviceInfo &outputDevice);
|
||||
void start(const BlackMisc::Audio::CAudioDeviceInfo &inputDevice, const BlackMisc::Audio::CAudioDeviceInfo &outputDevice, const QVector<quint16> &transceiverIDs);
|
||||
Q_INVOKABLE void start(const QString &inputDeviceName, const QString &outputDeviceName);
|
||||
void stop();
|
||||
|
||||
@@ -155,10 +155,13 @@ namespace BlackCore
|
||||
//! @}
|
||||
|
||||
//! Recently used device @{
|
||||
const QAudioDeviceInfo &getInputDevice() const;
|
||||
const QAudioDeviceInfo &getOutputDevice() const;
|
||||
const BlackMisc::Audio::CAudioDeviceInfo &getInputDevice() const;
|
||||
const BlackMisc::Audio::CAudioDeviceInfo &getOutputDevice() const;
|
||||
//! @}
|
||||
|
||||
QString getReceivingCallsignsCom1();
|
||||
QString getReceivingCallsignsCom2();
|
||||
|
||||
signals:
|
||||
//! Receiving callsigns have been changed
|
||||
//! \remark callsigns I do receive
|
||||
@@ -183,8 +186,7 @@ namespace BlackCore
|
||||
void audioOutDataAvailable(const AudioRxOnTransceiversDto &dto);
|
||||
void inputVolumeStream(const Audio::InputVolumeStreamArgs &args);
|
||||
void outputVolumeStream(const Audio::OutputVolumeStreamArgs &args);
|
||||
QString getReceivingCallsignsCom1();
|
||||
QString getReceivingCallsignsCom2();
|
||||
|
||||
|
||||
void inputOpusDataAvailable();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user