mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-23 15:25:35 +08:00
Ref T730, input/output
* style * fload -> double * CLogMessage
This commit is contained in:
committed by
Mat Sutcliffe
parent
661a6576c5
commit
fbc1efd7c7
@@ -9,12 +9,16 @@
|
||||
//! \file
|
||||
|
||||
#include "input.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
#include "blacksound/audioutilities.h"
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QStringBuilder>
|
||||
#include <QDebug>
|
||||
#include <cmath>
|
||||
|
||||
using namespace BlackMisc;
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
namespace Afv
|
||||
@@ -53,7 +57,7 @@ namespace BlackCore
|
||||
m_buffer.append(data, static_cast<int>(len));
|
||||
while (m_buffer.size() > 1920)
|
||||
{
|
||||
qDebug() << QDateTime::currentMSecsSinceEpoch() << "CAudioInputBuffer::writeData " << m_buffer.size();
|
||||
// qDebug() << QDateTime::currentMSecsSinceEpoch() << "CAudioInputBuffer::writeData " << m_buffer.size();
|
||||
emit frameAvailable(m_buffer.left(1920));
|
||||
m_buffer.remove(0, 1920);
|
||||
}
|
||||
@@ -70,8 +74,7 @@ namespace BlackCore
|
||||
const qint64 delta = now - m_lastFrameSent;
|
||||
if (delta >= 19)
|
||||
{
|
||||
qDebug() << now << "[signal] frameAvailable - buffer size" << m_buffer.size();
|
||||
|
||||
// qDebug() << now << "[signal] frameAvailable - buffer size" << m_buffer.size();
|
||||
m_buffer.remove(0, 1920);
|
||||
m_lastFrameSent = now;
|
||||
emit frameAvailable(m_buffer.left(1920));
|
||||
@@ -91,7 +94,6 @@ namespace BlackCore
|
||||
if (m_started) { return; }
|
||||
|
||||
QAudioFormat inputFormat;
|
||||
|
||||
inputFormat.setSampleRate(m_sampleRate);
|
||||
inputFormat.setChannelCount(1);
|
||||
inputFormat.setSampleSize(16);
|
||||
@@ -100,14 +102,17 @@ namespace BlackCore
|
||||
inputFormat.setCodec("audio/pcm");
|
||||
if (!inputDevice.isFormatSupported(inputFormat))
|
||||
{
|
||||
qWarning() << "Default INPUT format not supported - trying to use nearest";
|
||||
qWarning() << "Default format not supported - trying to use nearest:";
|
||||
qWarning() << "Sample rate: " << inputFormat.sampleRate();
|
||||
qWarning() << "Sample size: " << inputFormat.sampleSize();
|
||||
qWarning() << "Sample type: " << inputFormat.sampleType();
|
||||
qWarning() << "Byte order: " << inputFormat.byteOrder();
|
||||
qWarning() << "Codec: " << inputFormat.codec();
|
||||
qWarning() << "Channel count: " << inputFormat.channelCount();
|
||||
inputFormat = inputDevice.nearestFormat(inputFormat);
|
||||
const QString w =
|
||||
inputDevice.deviceName() %
|
||||
": Default INPUT format not supported - trying to use nearest" %
|
||||
" Sample rate: " % QString::number(inputFormat.sampleRate()) %
|
||||
" Sample size: " % QString::number(inputFormat.sampleSize()) %
|
||||
" Sample type: " % QString::number(inputFormat.sampleType()) %
|
||||
" Byte order: " % QString::number(inputFormat.byteOrder()) %
|
||||
" Codec: " % inputFormat.codec() %
|
||||
" Channel count: " % QString::number(inputFormat.channelCount());
|
||||
CLogMessage(this).warning(w);
|
||||
}
|
||||
|
||||
m_audioInput.reset(new QAudioInput(inputDevice, inputFormat));
|
||||
@@ -152,18 +157,16 @@ namespace BlackCore
|
||||
m_opusBytesEncoded += length;
|
||||
|
||||
m_sampleCount += samples.size();
|
||||
if (m_sampleCount >= c_sampleCountPerEvent)
|
||||
if (m_sampleCount >= SampleCountPerEvent)
|
||||
{
|
||||
InputVolumeStreamArgs inputVolumeStreamArgs;
|
||||
qint16 maxInt = std::numeric_limits<qint16>::max();
|
||||
inputVolumeStreamArgs.PeakRaw = static_cast<float>(m_maxSampleInput) / maxInt;
|
||||
inputVolumeStreamArgs.PeakDB = static_cast<float>(20 * std::log10(inputVolumeStreamArgs.PeakRaw));
|
||||
float db = qBound(minDb, inputVolumeStreamArgs.PeakDB, maxDb);
|
||||
float ratio = (db - minDb) / (maxDb - minDb);
|
||||
if (ratio < 0.30)
|
||||
ratio = 0;
|
||||
if (ratio > 1.0)
|
||||
ratio = 1;
|
||||
double db = qBound(minDb, inputVolumeStreamArgs.PeakDB, maxDb);
|
||||
double ratio = (db - minDb) / (maxDb - minDb);
|
||||
if (ratio < 0.30) { ratio = 0.0; }
|
||||
if (ratio > 1.0) { ratio = 1.0; }
|
||||
inputVolumeStreamArgs.PeakVU = ratio;
|
||||
emit inputVolumeStream(inputVolumeStreamArgs);
|
||||
m_sampleCount = 0;
|
||||
|
||||
@@ -71,9 +71,9 @@ namespace BlackCore
|
||||
struct InputVolumeStreamArgs
|
||||
{
|
||||
QAudioDeviceInfo DeviceNumber;
|
||||
float PeakRaw = 0.0;
|
||||
float PeakDB = -1 * std::numeric_limits<float>::infinity();
|
||||
float PeakVU = 0.0;
|
||||
double PeakRaw = 0.0;
|
||||
double PeakDB = -1.0 * std::numeric_limits<double>::infinity();
|
||||
double PeakVU = 0.0;
|
||||
};
|
||||
|
||||
//! Input
|
||||
@@ -85,16 +85,24 @@ namespace BlackCore
|
||||
//! Ctor
|
||||
CInput(int sampleRate, QObject *parent = nullptr);
|
||||
|
||||
bool started() const { return m_started; }
|
||||
|
||||
int opusBytesEncoded() const { return m_opusBytesEncoded; }
|
||||
void setOpusBytesEncoded(int opusBytesEncoded) { m_opusBytesEncoded = opusBytesEncoded; }
|
||||
|
||||
double volume() const { return m_volume; }
|
||||
void setVolume(double volume) { m_volume = volume; }
|
||||
|
||||
//! Started?
|
||||
bool started() const { return m_started; }
|
||||
|
||||
//! Start
|
||||
void start(const QAudioDeviceInfo &inputDevice);
|
||||
|
||||
//! Stop
|
||||
void stop();
|
||||
|
||||
//! Corresponding device
|
||||
const QAudioDeviceInfo &device() const { return m_device; }
|
||||
|
||||
signals:
|
||||
//! Volume stream data
|
||||
void inputVolumeStream(const InputVolumeStreamArgs &args);
|
||||
@@ -110,6 +118,7 @@ namespace BlackCore
|
||||
|
||||
BlackSound::Codecs::COpusEncoder m_encoder;
|
||||
QScopedPointer<QAudioInput> m_audioInput;
|
||||
QAudioDeviceInfo m_device;
|
||||
|
||||
bool m_started = false;
|
||||
int m_opusBytesEncoded = 0;
|
||||
@@ -117,9 +126,9 @@ namespace BlackCore
|
||||
double m_volume = 1.0;
|
||||
qint16 m_maxSampleInput = 0.0;
|
||||
|
||||
const int c_sampleCountPerEvent = 4800;
|
||||
const float maxDb = 0;
|
||||
const float minDb = -40;
|
||||
const int SampleCountPerEvent = 4800;
|
||||
const double maxDb = 0;
|
||||
const double minDb = -40;
|
||||
|
||||
uint m_audioSequenceCounter = 0;
|
||||
|
||||
|
||||
@@ -9,10 +9,13 @@
|
||||
//! \file
|
||||
|
||||
#include "output.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QStringBuilder>
|
||||
#include <cmath>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackSound::SampleProvider;
|
||||
|
||||
namespace BlackCore
|
||||
@@ -38,23 +41,20 @@ namespace BlackCore
|
||||
{
|
||||
qint16 sampleInput = sample;
|
||||
sampleInput = qAbs(sampleInput);
|
||||
if (sampleInput > m_maxSampleOutput)
|
||||
m_maxSampleOutput = sampleInput;
|
||||
if (sampleInput > m_maxSampleOutput) { m_maxSampleOutput = sampleInput; }
|
||||
}
|
||||
|
||||
m_sampleCount += buffer.size();
|
||||
if (m_sampleCount >= c_sampleCountPerEvent)
|
||||
if (m_sampleCount >= SampleCountPerEvent)
|
||||
{
|
||||
OutputVolumeStreamArgs outputVolumeStreamArgs;
|
||||
qint16 maxInt = std::numeric_limits<qint16>::max();
|
||||
outputVolumeStreamArgs.PeakRaw = m_maxSampleOutput / maxInt;
|
||||
outputVolumeStreamArgs.PeakDB = (float)(20 * std::log10(outputVolumeStreamArgs.PeakRaw));
|
||||
float db = qBound(minDb, outputVolumeStreamArgs.PeakDB, maxDb);
|
||||
float ratio = (db - minDb) / (maxDb - minDb);
|
||||
if (ratio < 0.30)
|
||||
ratio = 0;
|
||||
if (ratio > 1.0)
|
||||
ratio = 1;
|
||||
outputVolumeStreamArgs.PeakDB = static_cast<float>(20 * std::log10(outputVolumeStreamArgs.PeakRaw));
|
||||
const double db = qBound(minDb, outputVolumeStreamArgs.PeakDB, maxDb);
|
||||
double ratio = (db - minDb) / (maxDb - minDb);
|
||||
if (ratio < 0.30) { ratio = 0.0; }
|
||||
if (ratio > 1.0) { ratio = 1.0; }
|
||||
outputVolumeStreamArgs.PeakVU = ratio;
|
||||
emit outputVolumeStream(outputVolumeStreamArgs);
|
||||
m_sampleCount = 0;
|
||||
@@ -84,7 +84,7 @@ namespace BlackCore
|
||||
Output::Output(QObject *parent) : QObject(parent)
|
||||
{ }
|
||||
|
||||
void Output::start(const QAudioDeviceInfo &device, ISampleProvider *sampleProvider)
|
||||
void Output::start(const QAudioDeviceInfo &outputDevice, ISampleProvider *sampleProvider)
|
||||
{
|
||||
m_audioOutputBuffer = new CAudioOutputBuffer(sampleProvider, this);
|
||||
connect(m_audioOutputBuffer, &CAudioOutputBuffer::outputVolumeStream, this, &Output::outputVolumeStream);
|
||||
@@ -97,20 +97,22 @@ namespace BlackCore
|
||||
outputFormat.setByteOrder(QAudioFormat::LittleEndian);
|
||||
outputFormat.setCodec("audio/pcm");
|
||||
|
||||
if (!device.isFormatSupported(outputFormat))
|
||||
if (!outputDevice.isFormatSupported(outputFormat))
|
||||
{
|
||||
qWarning() << "Default OUTPUT format not supported - trying to use nearest";
|
||||
outputFormat = device.nearestFormat(outputFormat);
|
||||
qWarning() << "Default format not supported - trying to use nearest:";
|
||||
qWarning() << "Sample rate: " << outputFormat.sampleRate();
|
||||
qWarning() << "Sample size: " << outputFormat.sampleSize();
|
||||
qWarning() << "Sample type: " << outputFormat.sampleType();
|
||||
qWarning() << "Byte order: " << outputFormat.byteOrder();
|
||||
qWarning() << "Codec: " << outputFormat.codec();
|
||||
qWarning() << "Channel count: " << outputFormat.channelCount();
|
||||
outputFormat = outputDevice.nearestFormat(outputFormat);
|
||||
const QString w =
|
||||
outputDevice.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);
|
||||
}
|
||||
|
||||
m_audioOutputCom1.reset(new QAudioOutput(device, outputFormat));
|
||||
m_audioOutputCom1.reset(new QAudioOutput(outputDevice, outputFormat));
|
||||
// m_audioOutput->setBufferSize(bufferSize);
|
||||
m_audioOutputBuffer->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
|
||||
m_audioOutputBuffer->setAudioFormat(outputFormat);
|
||||
@@ -123,6 +125,8 @@ namespace BlackCore
|
||||
{
|
||||
if (!m_started) { return; }
|
||||
m_started = false;
|
||||
m_audioOutputBuffer->deleteLater();
|
||||
m_audioOutputBuffer = nullptr;
|
||||
}
|
||||
} // ns
|
||||
} // ns
|
||||
|
||||
@@ -27,9 +27,9 @@ namespace BlackCore
|
||||
struct OutputVolumeStreamArgs
|
||||
{
|
||||
QAudioDeviceInfo DeviceNumber;
|
||||
float PeakRaw = 0.0;
|
||||
float PeakDB = -1 * std::numeric_limits<float>::infinity();
|
||||
float PeakVU = 0.0;
|
||||
double PeakRaw = 0.0;
|
||||
double PeakDB = -1 * std::numeric_limits<double>::infinity();
|
||||
double PeakVU = 0.0;
|
||||
};
|
||||
|
||||
class CAudioOutputBuffer : public QIODevice
|
||||
@@ -40,8 +40,10 @@ namespace BlackCore
|
||||
//! Ctor
|
||||
CAudioOutputBuffer(BlackSound::SampleProvider::ISampleProvider *sampleProvider, QObject *parent = nullptr);
|
||||
|
||||
//! Related provider
|
||||
BlackSound::SampleProvider::ISampleProvider *m_sampleProvider = nullptr;
|
||||
|
||||
//! Set the format
|
||||
void setAudioFormat(const QAudioFormat &format) { m_outputFormat = format; }
|
||||
|
||||
signals:
|
||||
@@ -54,30 +56,40 @@ namespace BlackCore
|
||||
private:
|
||||
QAudioFormat m_outputFormat;
|
||||
|
||||
float m_maxSampleOutput = 0;
|
||||
double m_maxSampleOutput = 0;
|
||||
int m_sampleCount = 0;
|
||||
const int c_sampleCountPerEvent = 4800;
|
||||
const float maxDb = 0;
|
||||
const float minDb = -40;
|
||||
const int SampleCountPerEvent = 4800;
|
||||
const double maxDb = 0;
|
||||
const double minDb = -40;
|
||||
};
|
||||
|
||||
//! Output
|
||||
class Output : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
//! Ctor
|
||||
Output(QObject *parent = nullptr);
|
||||
|
||||
void start(const QAudioDeviceInfo &device, BlackSound::SampleProvider::ISampleProvider *sampleProvider);
|
||||
//! Start output
|
||||
void start(const QAudioDeviceInfo &outputDevice, BlackSound::SampleProvider::ISampleProvider *sampleProvider);
|
||||
|
||||
//! Stop output
|
||||
void stop();
|
||||
|
||||
//! Corresponding device
|
||||
const QAudioDeviceInfo &device() const { return m_device; }
|
||||
|
||||
signals:
|
||||
//! Streaming data
|
||||
void outputVolumeStream(const OutputVolumeStreamArgs &args);
|
||||
|
||||
private:
|
||||
bool m_started = false;
|
||||
|
||||
QAudioDeviceInfo m_device;
|
||||
QScopedPointer<QAudioOutput> m_audioOutputCom1;
|
||||
CAudioOutputBuffer *m_audioOutputBuffer;
|
||||
CAudioOutputBuffer *m_audioOutputBuffer = nullptr;
|
||||
};
|
||||
} // ns
|
||||
} // ns
|
||||
|
||||
Reference in New Issue
Block a user