mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-05-04 08:55:43 +08:00
refactor: Port to Qt 6
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
#include <QtGlobal>
|
||||
#include <QStringBuilder>
|
||||
#include <QDebug>
|
||||
#include <QAudioDeviceInfo>
|
||||
#include <QAudioDevice>
|
||||
#include <cmath>
|
||||
|
||||
using namespace BlackMisc;
|
||||
@@ -101,14 +101,11 @@ namespace BlackCore::Afv::Audio
|
||||
QAudioFormat inputFormat;
|
||||
inputFormat.setSampleRate(m_sampleRate); // normally 48000
|
||||
inputFormat.setChannelCount(1);
|
||||
inputFormat.setSampleSize(16);
|
||||
inputFormat.setSampleType(QAudioFormat::SignedInt);
|
||||
inputFormat.setByteOrder(QAudioFormat::LittleEndian);
|
||||
inputFormat.setCodec("audio/pcm");
|
||||
inputFormat.setSampleFormat(QAudioFormat::Int16);
|
||||
|
||||
QAudioDeviceInfo selectedDevice = getLowestLatencyDevice(inputDevice, inputFormat);
|
||||
QAudioDevice selectedDevice = getLowestLatencyDevice(inputDevice, inputFormat);
|
||||
m_inputFormat = inputFormat;
|
||||
m_audioInput.reset(new QAudioInput(selectedDevice, m_inputFormat));
|
||||
m_audioInput.reset(new QAudioSource(selectedDevice, m_inputFormat));
|
||||
if (!m_audioInputBuffer) { m_audioInputBuffer = new CAudioInputBuffer(this); }
|
||||
else { m_audioInputBuffer->disconnect(); } // make sure disconnected in any case
|
||||
m_audioInputBuffer->start(m_inputFormat);
|
||||
@@ -139,7 +136,7 @@ namespace BlackCore::Afv::Audio
|
||||
m_started = true;
|
||||
#endif
|
||||
const QString format = toQString(m_inputFormat);
|
||||
CLogMessage(this).info(u"Starting: '%1' with: %2") << selectedDevice.deviceName() << format;
|
||||
CLogMessage(this).info(u"Starting: '%1' with: %2") << selectedDevice.description() << format;
|
||||
}
|
||||
|
||||
void CInput::stop()
|
||||
@@ -158,6 +155,7 @@ namespace BlackCore::Afv::Audio
|
||||
|
||||
void CInput::audioInDataAvailable(const QByteArray &frame)
|
||||
{
|
||||
static_assert(Q_BYTE_ORDER == Q_LITTLE_ENDIAN);
|
||||
QVector<qint16> samples = convertBytesTo16BitPCM(frame);
|
||||
|
||||
if (m_inputFormat.channelCount() == 2)
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
# include "blackmisc/macos/microphoneaccess.h"
|
||||
#endif
|
||||
|
||||
#include <QAudioInput>
|
||||
#include <QAudioSource>
|
||||
#include <QString>
|
||||
#include <QDateTime>
|
||||
#include <QSharedPointer>
|
||||
@@ -138,7 +138,7 @@ namespace BlackCore::Afv::Audio
|
||||
int m_sampleRate = 0;
|
||||
|
||||
BlackSound::Codecs::COpusEncoder m_encoder;
|
||||
QScopedPointer<QAudioInput> m_audioInput;
|
||||
QScopedPointer<QAudioSource> m_audioInput;
|
||||
BlackMisc::Audio::CAudioDeviceInfo m_device;
|
||||
QAudioFormat m_inputFormat;
|
||||
|
||||
|
||||
@@ -26,9 +26,22 @@ namespace BlackCore::Afv::Audio
|
||||
this->setObjectName(on);
|
||||
}
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
qint64 CAudioOutputBuffer::bytesAvailable() const
|
||||
{
|
||||
// Workaround to mimic the pre-Qt6 behavior.
|
||||
// With Qt6, the QAudioSink on Windows uses the bytesAvailable function to trigger
|
||||
// a call to readData() only when data is available. Other platforms still use a
|
||||
// pull procedure that automatically calls readData() afer a specific period. Until
|
||||
// a proper solution for the bytesAvailable() is implemented, this uses a fixed number.
|
||||
// readData() will handle it itself if actually no data is available.
|
||||
return 3840 + QIODevice::bytesAvailable();
|
||||
}
|
||||
#endif
|
||||
|
||||
qint64 CAudioOutputBuffer::readData(char *data, qint64 maxlen)
|
||||
{
|
||||
const int sampleBytes = m_outputFormat.sampleSize() / 8;
|
||||
const int sampleBytes = m_outputFormat.bytesPerSample();
|
||||
const int channelCount = m_outputFormat.channelCount();
|
||||
const qint64 count = maxlen / (sampleBytes * channelCount);
|
||||
QVector<float> buffer;
|
||||
@@ -92,16 +105,14 @@ namespace BlackCore::Afv::Audio
|
||||
QAudioFormat outputFormat;
|
||||
outputFormat.setSampleRate(48000);
|
||||
outputFormat.setChannelCount(1);
|
||||
outputFormat.setSampleSize(32);
|
||||
outputFormat.setSampleType(QAudioFormat::Float);
|
||||
outputFormat.setByteOrder(QAudioFormat::LittleEndian);
|
||||
outputFormat.setCodec("audio/pcm");
|
||||
outputFormat.setSampleFormat(QAudioFormat::Float);
|
||||
static_assert(Q_BYTE_ORDER == Q_LITTLE_ENDIAN);
|
||||
|
||||
const QString format = toQString(outputFormat);
|
||||
const QAudioDeviceInfo selectedDevice = getLowestLatencyDevice(outputDevice, outputFormat);
|
||||
CLogMessage(this).info(u"Starting: '%1' with: %2") << selectedDevice.deviceName() << format;
|
||||
const QAudioDevice selectedDevice = getLowestLatencyDevice(outputDevice, outputFormat);
|
||||
CLogMessage(this).info(u"Starting: '%1' with: %2") << selectedDevice.description() << format;
|
||||
|
||||
m_audioOutput.reset(new QAudioOutput(selectedDevice, outputFormat));
|
||||
m_audioOutput.reset(new QAudioSink(selectedDevice, outputFormat));
|
||||
m_audioOutputBuffer->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
|
||||
m_audioOutputBuffer->setAudioFormat(outputFormat);
|
||||
m_audioOutput->start(m_audioOutputBuffer);
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#include "blackmisc/audio/audiodeviceinfo.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QAudioOutput>
|
||||
#include <QAudioSink>
|
||||
|
||||
namespace BlackCore::Afv::Audio
|
||||
{
|
||||
@@ -39,6 +39,11 @@ namespace BlackCore::Afv::Audio
|
||||
void outputVolumeStream(const OutputVolumeStreamArgs &args);
|
||||
|
||||
protected:
|
||||
#ifdef Q_OS_WIN
|
||||
//! \copydoc QIODevice::bytesAvailable
|
||||
qint64 bytesAvailable() const override;
|
||||
#endif
|
||||
|
||||
//! \copydoc QIODevice::readData
|
||||
virtual qint64 readData(char *data, qint64 maxlen) override;
|
||||
|
||||
@@ -95,7 +100,7 @@ namespace BlackCore::Afv::Audio
|
||||
private:
|
||||
bool m_started = false;
|
||||
BlackMisc::Audio::CAudioDeviceInfo m_device;
|
||||
QScopedPointer<QAudioOutput> m_audioOutput;
|
||||
QScopedPointer<QAudioSink> m_audioOutput;
|
||||
CAudioOutputBuffer *m_audioOutputBuffer = nullptr;
|
||||
};
|
||||
} // ns
|
||||
|
||||
@@ -19,10 +19,8 @@ namespace BlackCore::Afv::Audio
|
||||
|
||||
m_waveFormat.setSampleRate(sampleRate);
|
||||
m_waveFormat.setChannelCount(1);
|
||||
m_waveFormat.setSampleSize(16);
|
||||
m_waveFormat.setSampleType(QAudioFormat::SignedInt);
|
||||
m_waveFormat.setByteOrder(QAudioFormat::LittleEndian);
|
||||
m_waveFormat.setCodec("audio/pcm");
|
||||
m_waveFormat.setSampleFormat(QAudioFormat::Int16);
|
||||
static_assert(Q_BYTE_ORDER == Q_LITTLE_ENDIAN);
|
||||
|
||||
m_mixer = new CMixingSampleProvider(this);
|
||||
m_receiverIDs = transceiverIDs;
|
||||
@@ -148,7 +146,7 @@ namespace BlackCore::Afv::Audio
|
||||
return p->getId() == radioTransceiver.id;
|
||||
});
|
||||
|
||||
if (it)
|
||||
if (it != m_receiverInputs.end())
|
||||
{
|
||||
(*it)->setFrequency(radioTransceiver.frequencyHz);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user