2021-02-11 19:18:35 +00:00
|
|
|
/*
|
|
|
|
This class handles both RX and TX audio, each is created as a seperate instance of the class
|
|
|
|
but as the setup/handling if output (RX) and input (TX) devices is so similar I have combined them.
|
|
|
|
*/
|
|
|
|
#include "audiohandler.h"
|
2021-05-16 20:16:59 +00:00
|
|
|
|
|
|
|
#ifndef USE_RTAUDIO
|
2021-02-23 21:21:22 +00:00
|
|
|
#include "logcategories.h"
|
2021-02-11 19:18:35 +00:00
|
|
|
|
2021-02-13 00:45:59 +00:00
|
|
|
#define MULAW_BIAS 33
|
|
|
|
#define MULAW_MAX 0x1fff
|
|
|
|
|
2021-02-11 19:18:35 +00:00
|
|
|
audioHandler::audioHandler(QObject* parent) :
|
|
|
|
QIODevice(parent),
|
|
|
|
isInitialized(false),
|
2021-02-12 12:58:37 +00:00
|
|
|
audioOutput(Q_NULLPTR),
|
2021-02-12 12:59:33 +00:00
|
|
|
audioInput(Q_NULLPTR),
|
2021-02-11 19:18:35 +00:00
|
|
|
isUlaw(false),
|
2021-05-16 20:16:59 +00:00
|
|
|
audioLatency(0),
|
2021-02-12 12:58:37 +00:00
|
|
|
isInput(0),
|
2021-03-03 09:49:49 +00:00
|
|
|
chunkAvailable(false)
|
2021-02-11 19:18:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
audioHandler::~audioHandler()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
if (audioOutput != Q_NULLPTR) {
|
2021-03-22 15:16:41 +00:00
|
|
|
audioOutput->stop();
|
2021-02-11 19:18:35 +00:00
|
|
|
delete audioOutput;
|
|
|
|
}
|
|
|
|
if (audioInput != Q_NULLPTR) {
|
2021-03-22 15:16:41 +00:00
|
|
|
audioInput->stop();
|
2021-02-11 19:18:35 +00:00
|
|
|
delete audioInput;
|
|
|
|
}
|
2021-03-09 17:22:16 +00:00
|
|
|
|
2021-05-18 08:32:56 +00:00
|
|
|
if (resampler != NULL) {
|
2021-03-09 17:22:16 +00:00
|
|
|
speex_resampler_destroy(resampler);
|
|
|
|
}
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 20:09:04 +00:00
|
|
|
bool audioHandler::init(const quint8 bits, const quint8 channels, const quint16 samplerate, const quint16 audioLatency, const bool ulaw, const bool isinput, int device, quint8 resampleQuality)
|
2021-02-11 19:18:35 +00:00
|
|
|
{
|
|
|
|
if (isInitialized) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-16 20:16:59 +00:00
|
|
|
|
|
|
|
/* Always use 16 bit 48K samples internally*/
|
2021-02-12 14:28:55 +00:00
|
|
|
format.setSampleSize(16);
|
2021-02-11 19:18:35 +00:00
|
|
|
format.setChannelCount(channels);
|
2021-03-09 17:22:16 +00:00
|
|
|
format.setSampleRate(INTERNAL_SAMPLE_RATE);
|
2021-02-11 19:18:35 +00:00
|
|
|
format.setCodec("audio/pcm");
|
|
|
|
format.setByteOrder(QAudioFormat::LittleEndian);
|
|
|
|
format.setSampleType(QAudioFormat::SignedInt);
|
|
|
|
|
2021-05-16 20:16:59 +00:00
|
|
|
this->audioLatency = latency;
|
2021-02-27 00:37:00 +00:00
|
|
|
this->isUlaw = ulaw;
|
2021-02-11 19:18:35 +00:00
|
|
|
this->isInput = isinput;
|
2021-02-12 14:28:55 +00:00
|
|
|
this->radioSampleBits = bits;
|
|
|
|
this->radioSampleRate = samplerate;
|
2021-03-09 17:22:16 +00:00
|
|
|
this->radioChannels = channels;
|
|
|
|
|
2021-03-09 19:53:28 +00:00
|
|
|
// chunk size is always relative to Internal Sample Rate.
|
|
|
|
this->chunkSize = (INTERNAL_SAMPLE_RATE / 25) * radioChannels;
|
2021-03-09 17:22:16 +00:00
|
|
|
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "chunkSize: " << this->chunkSize;
|
2021-05-21 23:32:09 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "bufferLength (latency): " << this->latency;
|
2021-03-09 17:22:16 +00:00
|
|
|
|
|
|
|
int resample_error=0;
|
2021-02-11 19:18:35 +00:00
|
|
|
|
2021-03-01 20:31:05 +00:00
|
|
|
if (isInput) {
|
2021-03-13 09:50:43 +00:00
|
|
|
resampler = wf_resampler_init(radioChannels, INTERNAL_SAMPLE_RATE, samplerate, resampleQuality, &resample_error);
|
|
|
|
|
2021-05-22 09:43:57 +00:00
|
|
|
isInitialized = setDevice(port);
|
|
|
|
|
2021-03-01 20:44:09 +00:00
|
|
|
if (!isInitialized) {
|
2021-05-22 13:58:52 +00:00
|
|
|
qInfo(logAudio()) << "Input device " << port.deviceName() << " not found, using default";
|
2021-03-01 20:44:09 +00:00
|
|
|
isInitialized = setDevice(QAudioDeviceInfo::defaultInputDevice());
|
|
|
|
}
|
2021-03-01 20:31:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-13 09:50:43 +00:00
|
|
|
resampler = wf_resampler_init(radioChannels, samplerate, INTERNAL_SAMPLE_RATE, resampleQuality, &resample_error);
|
|
|
|
|
2021-05-22 09:43:57 +00:00
|
|
|
isInitialized = setDevice(port);
|
|
|
|
|
2021-03-01 20:44:09 +00:00
|
|
|
if (!isInitialized) {
|
2021-05-22 13:58:52 +00:00
|
|
|
qInfo(logAudio()) << "Output device " << deviceInfo.deviceName() << " not found, using default";
|
2021-03-01 20:44:09 +00:00
|
|
|
isInitialized = setDevice(QAudioDeviceInfo::defaultOutputDevice());
|
|
|
|
}
|
2021-03-01 20:31:05 +00:00
|
|
|
}
|
2021-03-13 09:50:43 +00:00
|
|
|
|
2021-05-16 20:16:59 +00:00
|
|
|
|
2021-03-13 09:50:43 +00:00
|
|
|
wf_resampler_get_ratio(resampler, &ratioNum, &ratioDen);
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "wf_resampler_init() returned: " << resample_error << " ratioNum" << ratioNum << " ratioDen" << ratioDen;
|
2021-03-13 09:50:43 +00:00
|
|
|
|
2021-05-22 13:58:52 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "audio port name: " << deviceInfo.deviceName();
|
2021-03-23 18:19:47 +00:00
|
|
|
return isInitialized;
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 18:53:34 +00:00
|
|
|
void audioHandler::setVolume(unsigned char volume)
|
2021-03-22 16:02:22 +00:00
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
//qInfo(logAudio()) << (isInput ? "Input" : "Output") << "setVolume: " << volume << "(" << (qreal)(volume/255.0) << ")";
|
2021-05-16 20:16:59 +00:00
|
|
|
|
2021-03-22 16:02:22 +00:00
|
|
|
if (audioOutput != Q_NULLPTR) {
|
|
|
|
audioOutput->setVolume((qreal)(volume / 255.0));
|
|
|
|
}
|
|
|
|
else if (audioInput != Q_NULLPTR) {
|
|
|
|
audioInput->setVolume((qreal)(volume / 255.0));
|
|
|
|
}
|
2021-05-16 20:16:59 +00:00
|
|
|
|
2021-03-22 16:02:22 +00:00
|
|
|
}
|
2021-02-11 19:18:35 +00:00
|
|
|
|
2021-05-16 20:16:59 +00:00
|
|
|
|
2021-02-11 19:18:35 +00:00
|
|
|
bool audioHandler::setDevice(QAudioDeviceInfo deviceInfo)
|
|
|
|
{
|
2021-03-02 10:07:08 +00:00
|
|
|
bool ret = true;
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "setDevice() running :" << deviceInfo.deviceName();
|
2021-02-11 19:18:35 +00:00
|
|
|
if (!deviceInfo.isFormatSupported(format)) {
|
|
|
|
if (deviceInfo.isNull())
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << "No audio device was found. You probably need to install libqt5multimedia-plugins.";
|
2021-03-02 10:07:08 +00:00
|
|
|
ret = false;
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-03-02 10:07:08 +00:00
|
|
|
/*
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << "Audio Devices found: ";
|
2021-02-11 19:18:35 +00:00
|
|
|
const auto deviceInfos = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
|
|
|
|
for (const QAudioDeviceInfo& deviceInfo : deviceInfos)
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << "Device name: " << deviceInfo.deviceName();
|
|
|
|
qInfo(logAudio()) << "is null (probably not good):" << deviceInfo.isNull();
|
|
|
|
qInfo(logAudio()) << "channel count:" << deviceInfo.supportedChannelCounts();
|
|
|
|
qInfo(logAudio()) << "byte order:" << deviceInfo.supportedByteOrders();
|
|
|
|
qInfo(logAudio()) << "supported codecs:" << deviceInfo.supportedCodecs();
|
|
|
|
qInfo(logAudio()) << "sample rates:" << deviceInfo.supportedSampleRates();
|
|
|
|
qInfo(logAudio()) << "sample sizes:" << deviceInfo.supportedSampleSizes();
|
|
|
|
qInfo(logAudio()) << "sample types:" << deviceInfo.supportedSampleTypes();
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << "----- done with audio info -----";
|
2021-03-02 10:07:08 +00:00
|
|
|
*/
|
|
|
|
ret=false;
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << "Format not supported, choosing nearest supported format - which may not work!";
|
2021-02-11 19:18:35 +00:00
|
|
|
deviceInfo.nearestFormat(format);
|
|
|
|
}
|
|
|
|
this->deviceInfo = deviceInfo;
|
|
|
|
this->reinit();
|
2021-03-02 10:07:08 +00:00
|
|
|
return ret;
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void audioHandler::reinit()
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "reinit() running";
|
2021-03-13 09:23:06 +00:00
|
|
|
if (audioOutput != Q_NULLPTR && audioOutput->state() != QAudio::StoppedState) {
|
2021-02-21 14:53:42 +00:00
|
|
|
this->stop();
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this->isInput)
|
|
|
|
{
|
|
|
|
// (Re)initialize audio input
|
2021-02-13 00:45:59 +00:00
|
|
|
if (audioInput != Q_NULLPTR)
|
|
|
|
delete audioInput;
|
2021-02-11 19:18:35 +00:00
|
|
|
audioInput = new QAudioInput(deviceInfo, format, this);
|
2021-02-12 20:42:56 +00:00
|
|
|
|
2021-02-11 19:18:35 +00:00
|
|
|
connect(audioInput, SIGNAL(notify()), SLOT(notified()));
|
|
|
|
connect(audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(stateChanged(QAudio::State)));
|
2021-02-12 20:42:56 +00:00
|
|
|
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// (Re)initialize audio output
|
2021-02-13 00:45:59 +00:00
|
|
|
if (audioOutput != Q_NULLPTR)
|
|
|
|
delete audioOutput;
|
2021-02-11 19:18:35 +00:00
|
|
|
audioOutput = Q_NULLPTR;
|
|
|
|
audioOutput = new QAudioOutput(deviceInfo, format, this);
|
2021-03-09 19:53:28 +00:00
|
|
|
|
|
|
|
// This seems to only be needed on Linux but is critical in aligning buffer sizes.
|
2021-05-22 08:24:19 +00:00
|
|
|
//#ifdef Q_OS_MAC
|
2021-05-21 23:10:22 +00:00
|
|
|
audioOutput->setBufferSize(chunkSize*8);
|
2021-05-22 08:24:19 +00:00
|
|
|
//#else
|
|
|
|
// audioOutput->setBufferSize(chunkSize*4);
|
|
|
|
//#endif
|
2021-03-09 19:53:28 +00:00
|
|
|
|
2021-03-09 17:22:16 +00:00
|
|
|
connect(audioOutput, SIGNAL(notify()), SLOT(notified()));
|
2021-02-11 19:18:35 +00:00
|
|
|
connect(audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(stateChanged(QAudio::State)));
|
|
|
|
}
|
|
|
|
|
2021-02-21 14:53:42 +00:00
|
|
|
this->start();
|
2021-02-13 23:25:24 +00:00
|
|
|
this->flush();
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void audioHandler::start()
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "start() running";
|
2021-02-11 19:18:35 +00:00
|
|
|
|
|
|
|
if ((audioOutput == Q_NULLPTR || audioOutput->state() != QAudio::StoppedState) &&
|
|
|
|
(audioInput == Q_NULLPTR || audioInput->state() != QAudio::StoppedState) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInput) {
|
2021-05-22 14:17:26 +00:00
|
|
|
//this->open(QIODevice::WriteOnly | QIODevice::Unbuffered);
|
|
|
|
this->open(QIODevice::WriteOnly);
|
|
|
|
audioInput->start(this);
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-05-22 14:17:26 +00:00
|
|
|
//this->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
|
|
|
|
this->open(QIODevice::ReadOnly);
|
2021-02-26 09:49:31 +00:00
|
|
|
audioOutput->start(this);
|
2021-02-27 09:34:56 +00:00
|
|
|
}
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void audioHandler::flush()
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "flush() running";
|
2021-02-11 19:18:35 +00:00
|
|
|
this->stop();
|
|
|
|
if (isInput) {
|
|
|
|
audioInput->reset();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
audioOutput->reset();
|
|
|
|
}
|
2021-02-27 09:34:56 +00:00
|
|
|
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
audioBuffer.clear();
|
2021-02-11 19:18:35 +00:00
|
|
|
this->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void audioHandler::stop()
|
|
|
|
{
|
2021-03-13 09:23:06 +00:00
|
|
|
if (audioOutput != Q_NULLPTR && audioOutput->state() != QAudio::StoppedState) {
|
2021-02-11 19:18:35 +00:00
|
|
|
// Stop audio output
|
|
|
|
audioOutput->stop();
|
|
|
|
this->close();
|
|
|
|
}
|
|
|
|
|
2021-03-13 09:23:06 +00:00
|
|
|
if (audioInput != Q_NULLPTR && audioInput->state() != QAudio::StoppedState) {
|
2021-02-11 19:18:35 +00:00
|
|
|
// Stop audio output
|
|
|
|
audioInput->stop();
|
|
|
|
this->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 17:22:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// This function processes the incoming audio FROM the radio and pushes it into the playback buffer *data
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data"></param>
|
|
|
|
/// <param name="maxlen"></param>
|
|
|
|
/// <returns></returns>
|
2021-02-11 19:18:35 +00:00
|
|
|
qint64 audioHandler::readData(char* data, qint64 maxlen)
|
|
|
|
{
|
|
|
|
// Calculate output length, always full samples
|
2021-02-27 00:37:00 +00:00
|
|
|
int sentlen = 0;
|
|
|
|
|
2021-05-15 17:53:16 +00:00
|
|
|
//qInfo(logAudio()) << "Looking for: " << maxlen << " bytes";
|
2021-03-02 12:00:13 +00:00
|
|
|
|
2021-03-03 09:49:49 +00:00
|
|
|
// We must lock the mutex for the entire time that the buffer may be modified.
|
2021-02-27 00:37:00 +00:00
|
|
|
// Get next packet from buffer.
|
2021-05-17 15:19:36 +00:00
|
|
|
if (!inputBuffer.isEmpty())
|
2021-02-27 00:37:00 +00:00
|
|
|
{
|
2021-03-03 09:49:49 +00:00
|
|
|
|
2021-02-27 00:37:00 +00:00
|
|
|
// Output buffer is ALWAYS 16 bit.
|
2021-03-13 09:23:06 +00:00
|
|
|
QMutexLocker locker(&mutex);
|
2021-05-17 15:19:36 +00:00
|
|
|
auto packet = inputBuffer.begin();
|
2021-03-13 09:23:06 +00:00
|
|
|
|
2021-05-17 15:19:36 +00:00
|
|
|
while (packet != inputBuffer.end() && sentlen < maxlen)
|
2021-02-27 00:37:00 +00:00
|
|
|
{
|
2021-03-02 11:50:42 +00:00
|
|
|
int timediff = packet->time.msecsTo(QTime::currentTime());
|
2021-03-14 20:06:20 +00:00
|
|
|
|
2021-05-16 20:16:59 +00:00
|
|
|
if (timediff > (int)audioLatency * 2) {
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Packet " << hex << packet->seq <<
|
2021-03-22 09:10:03 +00:00
|
|
|
" arrived too late (increase output latency!) " <<
|
2021-03-14 08:44:30 +00:00
|
|
|
dec << packet->time.msecsTo(QTime::currentTime()) << "ms";
|
2021-05-22 20:09:04 +00:00
|
|
|
|
2021-05-16 20:16:59 +00:00
|
|
|
while (packet !=audioBuffer.end() && timediff > (int)audioLatency) {
|
2021-03-14 20:06:20 +00:00
|
|
|
timediff = packet->time.msecsTo(QTime::currentTime());
|
|
|
|
lastSeq=packet->seq;
|
2021-05-17 15:19:36 +00:00
|
|
|
packet = inputBuffer.erase(packet); // returns next packet
|
2021-03-14 20:06:20 +00:00
|
|
|
}
|
2021-05-17 15:19:36 +00:00
|
|
|
if (packet == inputBuffer.end()) {
|
2021-03-14 20:06:20 +00:00
|
|
|
break;
|
2021-03-14 08:44:30 +00:00
|
|
|
}
|
2021-02-27 00:37:00 +00:00
|
|
|
}
|
2021-03-14 20:06:20 +00:00
|
|
|
|
|
|
|
// If we got here then packet time must be within latency threshold
|
|
|
|
|
|
|
|
if (packet->seq == lastSeq+1 || packet->seq <= lastSeq)
|
2021-02-27 00:37:00 +00:00
|
|
|
{
|
2021-03-09 17:22:16 +00:00
|
|
|
int send = qMin((int)maxlen-sentlen, packet->dataout.length() - packet->sent);
|
2021-03-02 14:35:33 +00:00
|
|
|
lastSeq = packet->seq;
|
2021-05-15 17:53:16 +00:00
|
|
|
//qInfo(logAudio()) << "Packet " << hex << packet->seq << " arrived on time " << Qt::dec << packet->time.msecsTo(QTime::currentTime()) << "ms";
|
2021-02-27 00:37:00 +00:00
|
|
|
|
2021-03-09 17:22:16 +00:00
|
|
|
memcpy(data + sentlen, packet->dataout.constData() + packet->sent, send);
|
2021-02-27 00:37:00 +00:00
|
|
|
|
2021-03-09 17:22:16 +00:00
|
|
|
sentlen = sentlen + send;
|
2021-03-02 10:19:08 +00:00
|
|
|
|
2021-03-13 09:23:06 +00:00
|
|
|
if (send == packet->dataout.length() - packet->sent)
|
2021-02-27 00:37:00 +00:00
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
//qInfo(logAudio()) << "Get next packet";
|
2021-05-17 15:19:36 +00:00
|
|
|
packet = inputBuffer.erase(packet); // returns next packet
|
2021-02-27 00:37:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-14 20:06:20 +00:00
|
|
|
// Store sent amount (could be zero if audioOutput buffer full) then break.
|
|
|
|
packet->sent = send;
|
|
|
|
break;
|
|
|
|
}
|
2021-03-02 14:35:33 +00:00
|
|
|
} else {
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Missing audio packet(s) from: " << hex << lastSeq + 1 << " to " << hex << packet->seq - 1;
|
2021-03-02 14:35:33 +00:00
|
|
|
lastSeq = packet->seq;
|
|
|
|
}
|
2021-02-27 00:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-03 08:03:18 +00:00
|
|
|
else {
|
|
|
|
// Fool audio system into thinking it has valid data, this seems to be required
|
|
|
|
// for MacOS Built in audio but shouldn't cause any issues with other platforms.
|
|
|
|
memset(data, 0x0, maxlen);
|
|
|
|
return maxlen;
|
|
|
|
}
|
2021-02-27 00:37:00 +00:00
|
|
|
|
|
|
|
return sentlen;
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 audioHandler::writeData(const char* data, qint64 len)
|
|
|
|
{
|
2021-02-27 13:26:59 +00:00
|
|
|
qint64 sentlen = 0;
|
2021-02-27 10:13:59 +00:00
|
|
|
QMutexLocker locker(&mutex);
|
2021-03-01 19:53:12 +00:00
|
|
|
audioPacket *current;
|
2021-02-27 09:34:56 +00:00
|
|
|
|
|
|
|
while (sentlen < len) {
|
2021-02-27 11:00:44 +00:00
|
|
|
if (!audioBuffer.isEmpty())
|
|
|
|
{
|
|
|
|
if (audioBuffer.last().sent == chunkSize)
|
|
|
|
{
|
2021-03-01 19:53:12 +00:00
|
|
|
audioBuffer.append(audioPacket());
|
2021-02-27 11:00:44 +00:00
|
|
|
audioBuffer.last().sent = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-01 19:53:12 +00:00
|
|
|
audioBuffer.append(audioPacket());
|
2021-02-27 11:00:44 +00:00
|
|
|
audioBuffer.last().sent = 0;
|
|
|
|
}
|
|
|
|
current = &audioBuffer.last();
|
|
|
|
|
2021-03-09 17:22:16 +00:00
|
|
|
int send = qMin((int)(len - sentlen), (int)chunkSize-current->sent);
|
2021-02-27 13:19:40 +00:00
|
|
|
|
2021-03-09 17:22:16 +00:00
|
|
|
current->datain.append(QByteArray::fromRawData(data + sentlen, send ));
|
|
|
|
|
|
|
|
sentlen = sentlen + send;
|
2021-02-27 09:34:56 +00:00
|
|
|
|
2021-02-27 11:00:44 +00:00
|
|
|
current->seq = 0; // Not used in TX
|
|
|
|
current->time = QTime::currentTime();
|
2021-03-09 17:22:16 +00:00
|
|
|
current->sent = current->datain.length();
|
2021-02-27 12:38:12 +00:00
|
|
|
|
2021-02-27 13:13:27 +00:00
|
|
|
if (current->sent == chunkSize)
|
2021-02-27 09:34:56 +00:00
|
|
|
{
|
2021-02-27 12:38:12 +00:00
|
|
|
chunkAvailable = true;
|
2021-02-27 11:04:08 +00:00
|
|
|
}
|
2021-03-22 09:10:03 +00:00
|
|
|
else if (audioBuffer.length()<=1 && current->sent != chunkSize) {
|
2021-02-27 12:38:12 +00:00
|
|
|
chunkAvailable = false;
|
2021-02-27 09:34:56 +00:00
|
|
|
}
|
2021-02-27 12:38:12 +00:00
|
|
|
|
2021-02-27 10:11:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 09:34:56 +00:00
|
|
|
return (sentlen); // Always return the same number as we received
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 audioHandler::bytesAvailable() const
|
|
|
|
{
|
2021-02-27 09:34:56 +00:00
|
|
|
return 0;
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool audioHandler::isSequential() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void audioHandler::notified()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-12 23:56:02 +00:00
|
|
|
|
2021-02-11 19:18:35 +00:00
|
|
|
void audioHandler::stateChanged(QAudio::State state)
|
|
|
|
{
|
2021-03-02 11:12:12 +00:00
|
|
|
// Process the state
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case QAudio::IdleState:
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Audio now in idle state: " << audioBuffer.length() << " packets in buffer";
|
2021-03-13 09:23:06 +00:00
|
|
|
if (audioOutput != Q_NULLPTR && audioOutput->error() == QAudio::UnderrunError)
|
2021-03-02 11:12:12 +00:00
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "buffer underrun";
|
2021-03-02 11:12:12 +00:00
|
|
|
audioOutput->suspend();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QAudio::ActiveState:
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Audio now in active state: " << audioBuffer.length() << " packets in buffer";
|
2021-03-02 11:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QAudio::SuspendedState:
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Audio now in suspended state: " << audioBuffer.length() << " packets in buffer";
|
2021-03-02 11:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QAudio::StoppedState:
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Audio now in stopped state: " << audioBuffer.length() << " packets in buffer";
|
2021-03-02 11:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Unhandled audio state: " << audioBuffer.length() << " packets in buffer";
|
2021-03-02 11:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-09 17:22:16 +00:00
|
|
|
void audioHandler::incomingAudio(audioPacket data)
|
2021-02-11 19:18:35 +00:00
|
|
|
{
|
|
|
|
if (audioOutput != Q_NULLPTR && audioOutput->state() != QAudio::StoppedState) {
|
2021-02-24 22:56:40 +00:00
|
|
|
QMutexLocker locker(&mutex);
|
2021-03-09 17:22:16 +00:00
|
|
|
|
|
|
|
// Incoming data is 8bits?
|
|
|
|
if (radioSampleBits == 8)
|
|
|
|
{
|
|
|
|
QByteArray inPacket((int)data.datain.length() * 2, (char)0xff);
|
|
|
|
qint16* in = (qint16*)inPacket.data();
|
|
|
|
for (int f = 0; f < data.datain.length(); f++)
|
|
|
|
{
|
|
|
|
if (isUlaw)
|
|
|
|
{
|
|
|
|
in[f] = ulaw_decode[(quint8)data.datain[f]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Convert 8-bit sample to 16-bit
|
|
|
|
in[f] = (qint16)(((quint8)data.datain[f] << 8) - 32640);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.datain = inPacket; // Replace incoming data with converted.
|
|
|
|
}
|
|
|
|
|
2021-05-15 17:53:16 +00:00
|
|
|
//qInfo(logAudio()) << "Adding packet to buffer:" << data.seq << ": " << data.datain.length();
|
2021-03-09 17:22:16 +00:00
|
|
|
|
|
|
|
/* We now have an array of 16bit samples in the NATIVE samplerate of the radio
|
|
|
|
If the radio sample rate is below 48000, we need to resample.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ratioDen != 1) {
|
|
|
|
|
|
|
|
// We need to resample
|
|
|
|
quint32 outFrames = ((data.datain.length() / 2) * ratioDen) / radioChannels;
|
|
|
|
quint32 inFrames = (data.datain.length() / 2) / radioChannels;
|
|
|
|
data.dataout.resize(outFrames * 2 * radioChannels); // Preset the output buffer size.
|
|
|
|
|
|
|
|
int err = 0;
|
|
|
|
if (this->radioChannels == 1) {
|
|
|
|
err = wf_resampler_process_int(resampler, 0, (const qint16*)data.datain.constData(), &inFrames, (qint16*)data.dataout.data(), &outFrames);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err = wf_resampler_process_interleaved_int(resampler, (const qint16*)data.datain.constData(), &inFrames, (qint16*)data.dataout.data(), &outFrames);
|
|
|
|
}
|
|
|
|
if (err) {
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) <<(isInput ? "Input" : "Output") << "Resampler error " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
|
2021-03-09 17:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data.dataout = data.datain;
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:19:36 +00:00
|
|
|
inputBuffer.insert(data.seq, data);
|
2021-02-28 20:10:07 +00:00
|
|
|
|
2021-03-02 12:06:46 +00:00
|
|
|
// Restart playback
|
|
|
|
if (audioOutput->state() == QAudio::SuspendedState)
|
|
|
|
{
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << "Output Audio Suspended, Resuming...";
|
2021-03-02 12:06:46 +00:00
|
|
|
audioOutput->resume();
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 00:37:00 +00:00
|
|
|
void audioHandler::changeLatency(const quint16 newSize)
|
2021-02-11 19:18:35 +00:00
|
|
|
{
|
2021-05-16 20:16:59 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Changing latency to: " << newSize << " from " << audioLatency;
|
|
|
|
audioLatency = newSize;
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 00:37:00 +00:00
|
|
|
void audioHandler::getLatency()
|
2021-02-11 19:18:35 +00:00
|
|
|
{
|
2021-05-16 20:16:59 +00:00
|
|
|
emit sendLatency(audioLatency);
|
2021-02-11 19:18:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 14:53:42 +00:00
|
|
|
bool audioHandler::isChunkAvailable()
|
|
|
|
{
|
2021-02-27 12:38:12 +00:00
|
|
|
return (chunkAvailable);
|
2021-02-21 14:53:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 11:04:26 +00:00
|
|
|
void audioHandler::getNextAudioChunk(QByteArray& ret)
|
2021-02-12 20:42:56 +00:00
|
|
|
{
|
2021-02-27 12:38:12 +00:00
|
|
|
if (!audioBuffer.isEmpty() && chunkAvailable)
|
2021-02-27 09:34:56 +00:00
|
|
|
{
|
2021-02-27 13:43:53 +00:00
|
|
|
|
2021-02-27 09:34:56 +00:00
|
|
|
QMutexLocker locker(&mutex);
|
2021-02-27 13:43:53 +00:00
|
|
|
|
|
|
|
// Skip through audio buffer deleting any old entry.
|
2021-02-27 09:34:56 +00:00
|
|
|
auto packet = audioBuffer.begin();
|
2021-02-27 13:43:53 +00:00
|
|
|
while (packet != audioBuffer.end())
|
2021-02-27 09:34:56 +00:00
|
|
|
{
|
2021-05-21 09:51:25 +00:00
|
|
|
if (packet->time.msecsTo(QTime::currentTime()) > latency) {
|
|
|
|
//qInfo(logAudio()) << "TX Packet too old " << dec << packet->time.msecsTo(QTime::currentTime()) << "ms";
|
2021-02-27 13:43:53 +00:00
|
|
|
packet = audioBuffer.erase(packet); // returns next packet
|
|
|
|
}
|
|
|
|
else {
|
2021-03-09 17:22:16 +00:00
|
|
|
if (packet->datain.length() == chunkSize && ret.length() == 0)
|
2021-02-27 13:43:53 +00:00
|
|
|
{
|
2021-03-09 17:22:16 +00:00
|
|
|
/* We now have an array of samples in the computer native format (48000)
|
|
|
|
If the radio sample rate is below 48000, we need to resample.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ratioNum != 1)
|
|
|
|
{
|
|
|
|
// We need to resample (we are STILL 16 bit!)
|
|
|
|
quint32 outFrames = ((packet->datain.length() / 2) / ratioNum) / radioChannels;
|
|
|
|
quint32 inFrames = (packet->datain.length() / 2) / radioChannels;
|
|
|
|
packet->dataout.resize(outFrames * 2 * radioChannels); // Preset the output buffer size.
|
|
|
|
|
2021-03-09 19:53:28 +00:00
|
|
|
const qint16* in = (qint16*)packet->datain.constData();
|
|
|
|
qint16* out = (qint16*)packet->dataout.data();
|
2021-03-09 17:22:16 +00:00
|
|
|
int err = 0;
|
|
|
|
if (this->radioChannels == 1) {
|
2021-03-09 19:53:28 +00:00
|
|
|
err = wf_resampler_process_int(resampler, 0, in, &inFrames, out, &outFrames);
|
2021-03-09 17:22:16 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-03-09 19:53:28 +00:00
|
|
|
err = wf_resampler_process_interleaved_int(resampler, in, &inFrames, out, &outFrames);
|
2021-03-09 17:22:16 +00:00
|
|
|
}
|
|
|
|
if (err) {
|
2021-05-15 17:53:16 +00:00
|
|
|
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Resampler error " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
|
2021-03-09 17:22:16 +00:00
|
|
|
}
|
2021-05-15 17:53:16 +00:00
|
|
|
//qInfo(logAudio()) << "Resampler run " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
|
|
|
|
//qInfo(logAudio()) << "Resampler run inLen:" << packet->datain.length() << " outLen:" << packet->dataout.length();
|
2021-03-09 17:22:16 +00:00
|
|
|
if (radioSampleBits == 8)
|
|
|
|
{
|
2021-03-09 19:53:28 +00:00
|
|
|
packet->datain=packet->dataout; // Copy output packet back to input buffer.
|
|
|
|
packet->dataout.clear(); // Buffer MUST be cleared ready to be re-filled by the upsampling below.
|
2021-03-09 17:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (radioSampleBits == 16 ){
|
|
|
|
// Only copy buffer if radioSampleBits is 16, as it will be handled below otherwise.
|
|
|
|
packet->dataout = packet->datain;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do we need to convert 16-bit to 8-bit?
|
|
|
|
if (radioSampleBits == 8) {
|
|
|
|
packet->dataout.resize(packet->datain.length() / 2);
|
|
|
|
qint16* in = (qint16*)packet->datain.data();
|
|
|
|
for (int f = 0; f < packet->dataout.length(); f++)
|
|
|
|
{
|
|
|
|
quint8 outdata = 0;
|
|
|
|
if (isUlaw) {
|
|
|
|
qint16 enc = qFromLittleEndian<quint16>(in + f);
|
|
|
|
if (enc >= 0)
|
|
|
|
outdata = ulaw_encode[enc];
|
|
|
|
else
|
|
|
|
outdata = 0x7f & ulaw_encode[-enc];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
outdata = (quint8)(((qFromLittleEndian<qint16>(in + f) >> 8) ^ 0x80) & 0xff);
|
|
|
|
}
|
|
|
|
packet->dataout[f] = (char)outdata;
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 19:53:28 +00:00
|
|
|
ret=packet->dataout;
|
2021-02-27 13:43:53 +00:00
|
|
|
packet = audioBuffer.erase(packet); // returns next packet
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
packet++;
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 09:34:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-13 11:04:26 +00:00
|
|
|
return;
|
2021-02-12 20:42:56 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 20:16:59 +00:00
|
|
|
#endif
|
2021-02-11 19:18:35 +00:00
|
|
|
|