merge-requests/5/head
Phil Taylor 2021-05-27 13:54:52 +01:00
rodzic f726073e22
commit 0c7892bd82
3 zmienionych plików z 85 dodań i 118 usunięć

Wyświetl plik

@ -60,7 +60,6 @@ bool audioHandler::init(const quint8 bits, const quint8 channels, const quint16
else { else {
aParams.deviceId = audio.getDefaultOutputDevice(); aParams.deviceId = audio.getDefaultOutputDevice();
} }
aParams.nChannels = 2; // Internally this is always 2 channels
aParams.firstChannel = 0; aParams.firstChannel = 0;
try { try {
@ -99,6 +98,7 @@ bool audioHandler::init(const quint8 bits, const quint8 channels, const quint16
if (isInput) { if (isInput) {
resampler = wf_resampler_init(radioChannels, INTERNAL_SAMPLE_RATE, samplerate, resampleQuality, &resample_error); resampler = wf_resampler_init(radioChannels, INTERNAL_SAMPLE_RATE, samplerate, resampleQuality, &resample_error);
try { try {
aParams.nChannels = 1; // Internally this is always 2 channels
audio.openStream(NULL, &aParams, RTAUDIO_SINT16, INTERNAL_SAMPLE_RATE, &this->chunkSize, &staticWrite, this); audio.openStream(NULL, &aParams, RTAUDIO_SINT16, INTERNAL_SAMPLE_RATE, &this->chunkSize, &staticWrite, this);
audio.startStream(); audio.startStream();
} }
@ -112,6 +112,7 @@ bool audioHandler::init(const quint8 bits, const quint8 channels, const quint16
resampler = wf_resampler_init(radioChannels, samplerate, INTERNAL_SAMPLE_RATE, resampleQuality, &resample_error); resampler = wf_resampler_init(radioChannels, samplerate, INTERNAL_SAMPLE_RATE, resampleQuality, &resample_error);
try { try {
unsigned int length = chunkSize / 2; unsigned int length = chunkSize / 2;
aParams.nChannels = 2; // Internally this is always 2 channels
audio.openStream(&aParams, NULL, RTAUDIO_SINT16, INTERNAL_SAMPLE_RATE, &length, &staticRead, this); audio.openStream(&aParams, NULL, RTAUDIO_SINT16, INTERNAL_SAMPLE_RATE, &length, &staticRead, this);
audio.startStream(); audio.startStream();
} }
@ -131,6 +132,7 @@ bool audioHandler::init(const quint8 bits, const quint8 channels, const quint16
void audioHandler::setVolume(unsigned char volume) void audioHandler::setVolume(unsigned char volume)
{ {
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "setVolume: " << volume << "(" << (qreal)(volume/255.0) << ")"; qInfo(logAudio()) << (isInput ? "Input" : "Output") << "setVolume: " << volume << "(" << (qreal)(volume/255.0) << ")";
this->volume = (qreal)(volume / 255.0);
} }
@ -217,47 +219,29 @@ int audioHandler::readData(void* outputBuffer, void* inputBuffer, unsigned int n
int audioHandler::writeData(void* outputBuffer, void* inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status) int audioHandler::writeData(void* outputBuffer, void* inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status)
{ {
int sentlen = 0; int sentlen = 0;
/* unsigned int nBytes = nFrames * 2; // This is ALWAYS 2 bytes per sample and 1 channels
QMutexLocker locker(&mutex); const char* data = (const char*)inputBuffer;
audioPacket* current; while (sentlen < nBytes) {
if (tempBuf.sent != chunkSize)
while (sentlen < len) {
if (!audioBuffer.isEmpty())
{ {
if (audioBuffer.last().sent == chunkSize) int send = qMin((int)(nBytes - sentlen), (int)chunkSize - tempBuf.sent);
tempBuf.data.append(QByteArray::fromRawData(data + sentlen, send));
sentlen = sentlen + send;
tempBuf.seq = 0; // Not used in TX
tempBuf.time = QTime::currentTime();
tempBuf.sent = tempBuf.data.length();
}
else {
if (!ringBuf->try_write(tempBuf))
{ {
audioBuffer.append(audioPacket()); qDebug(logAudio()) << "outgoing audio buffer full!";
audioBuffer.last().sent = 0;
} }
tempBuf.data.clear();
tempBuf.sent = 0;
} }
else
{
audioBuffer.append(audioPacket());
audioBuffer.last().sent = 0;
}
current = &audioBuffer.last();
int send = qMin((int)(len - sentlen), (int)chunkSize - current->sent);
current->data.append(QByteArray::fromRawData(data + sentlen, send));
sentlen = sentlen + send;
current->seq = 0; // Not used in TX
current->time = QTime::currentTime();
current->sent = current->data.length();
if (current->sent == chunkSize)
{
chunkAvailable = true;
}
else if (audioBuffer.length() <= 1 && current->sent != chunkSize) {
chunkAvailable = false;
}
} }
*/
return (sentlen); // Always return the same number as we received return 0;
} }
qint64 audioHandler::bytesAvailable() const qint64 audioHandler::bytesAvailable() const
@ -338,21 +322,28 @@ int audioHandler::incomingAudio(audioPacket data)
data.data = outPacket; // Replace incoming data with converted. data.data = outPacket; // Replace incoming data with converted.
} }
if (radioChannels == 1) if (radioChannels == 1) {
{ // Convert to stereo and set volume.
// Convert to stereo
QByteArray outPacket(data.data.length()*2, 0xff); // Preset the output buffer size. QByteArray outPacket(data.data.length()*2, 0xff); // Preset the output buffer size.
qint16* in = (qint16*)data.data.data(); qint16* in = (qint16*)data.data.data();
qint16* out = (qint16*)outPacket.data(); qint16* out = (qint16*)outPacket.data();
for (int f = 0; f < data.data.length()/2; f++) for (int f = 0; f < data.data.length()/2; f++)
{ {
*out++ = *in; *out++ = *in * volume;
*out++ = *in++; *out++ = *in++ * volume;
} }
data.data.clear(); data.data.clear();
data.data = outPacket; // Replace incoming data with converted. data.data = outPacket; // Replace incoming data with converted.
} else {
// We already have two channels so just update volume.
qint16* in = (qint16*)data.data.data();
for (int f = 0; f < data.data.length() / 2; f++)
{
in[f] = in[f] * volume;
}
} }
if (!ringBuf->try_write(data)) if (!ringBuf->try_write(data))
{ {
qDebug(logAudio()) << "Buffer full! capacity:" << ringBuf->capacity() << "length" << ringBuf->size(); qDebug(logAudio()) << "Buffer full! capacity:" << ringBuf->capacity() << "length" << ringBuf->size();
@ -378,90 +369,62 @@ bool audioHandler::isChunkAvailable()
void audioHandler::getNextAudioChunk(QByteArray& ret) void audioHandler::getNextAudioChunk(QByteArray& ret)
{ {
/* audioPacket packet;
if (!audioBuffer.isEmpty() && chunkAvailable) packet.sent = 0;
if (ringBuf != Q_NULLPTR && ringBuf->try_read(packet))
{ {
if (ratioNum != 1)
QMutexLocker locker(&mutex);
// Skip through audio buffer deleting any old entry.
auto packet = audioBuffer.begin();
while (packet != audioBuffer.end())
{ {
if (packet->time.msecsTo(QTime::currentTime()) > 100) { // We need to resample (we are STILL 16 bit!)
//qInfo(logAudio()) << "TX Packet too old " << dec << packet->time.msecsTo(QTime::currentTime()) << "ms"; quint32 outFrames = ((packet.data.length() / 2) / ratioNum) / radioChannels;
packet = audioBuffer.erase(packet); // returns next packet quint32 inFrames = (packet.data.length() / 2) / radioChannels;
QByteArray inPacket((int)outFrames * 2 * radioChannels, (char)0xff);
const qint16* in = (qint16*)packet.data.constData();
qint16* out = (qint16*)inPacket.data();
int err = 0;
if (this->radioChannels == 1) {
err = wf_resampler_process_int(resampler, 0, in, &inFrames, out, &outFrames);
} }
else { else {
if (packet->data.length() == chunkSize && ret.length() == 0) err = wf_resampler_process_interleaved_int(resampler, in, &inFrames, out, &outFrames);
{ }
// We now have an array of samples in the computer native format (48000) if (err) {
// If the radio sample rate is below 48000, we need to resample. qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Resampler error " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
}
//qInfo(logAudio()) << "Resampler run " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
//qInfo(logAudio()) << "Resampler run inLen:" << packet->datain.length() << " outLen:" << packet->dataout.length();
packet.data.clear();
packet.data = inPacket; // Copy output packet back to input buffer.
}
if (ratioNum != 1) // Do we need to convert 16-bit to 8-bit?
{ if (radioSampleBits == 8) {
// We need to resample (we are STILL 16 bit!) QByteArray inPacket((int)packet.data.length() / 2, (char)0xff);
quint32 outFrames = ((packet->data.length() / 2) / ratioNum) / radioChannels; qint16* in = (qint16*)packet.data.data();
quint32 inFrames = (packet->data.length() / 2) / radioChannels; for (int f = 0; f < inPacket.length(); f++)
packet->dataout.resize(outFrames * 2 * radioChannels); // Preset the output buffer size. {
quint8 outdata = 0;
const qint16* in = (qint16*)packet->data.constData(); if (isUlaw) {
qint16* out = (qint16*)packet->dataout.data(); qint16 enc = qFromLittleEndian<quint16>(in + f);
int err = 0; if (enc >= 0)
if (this->radioChannels == 1) { outdata = ulaw_encode[enc];
err = wf_resampler_process_int(resampler, 0, in, &inFrames, out, &outFrames); else
} outdata = 0x7f & ulaw_encode[-enc];
else {
err = wf_resampler_process_interleaved_int(resampler, in, &inFrames, out, &outFrames);
}
if (err) {
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Resampler error " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
}
//qInfo(logAudio()) << "Resampler run " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
//qInfo(logAudio()) << "Resampler run inLen:" << packet->data.length() << " outLen:" << packet->dataout.length();
if (radioSampleBits == 8)
{
packet->data = 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.
}
}
else if (radioSampleBits == 16) {
// Only copy buffer if radioSampleBits is 16, as it will be handled below otherwise.
packet->dataout = packet->data;
}
// Do we need to convert 16-bit to 8-bit?
if (radioSampleBits == 8) {
packet->dataout.resize(packet->data.length() / 2);
qint16* in = (qint16*)packet->data.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;
}
}
ret = packet->dataout;
packet = audioBuffer.erase(packet); // returns next packet
} }
else { else {
packet++; outdata = (quint8)(((qFromLittleEndian<qint16>(in + f) >> 8) ^ 0x80) & 0xff);
} }
inPacket[f] = (char)outdata;
} }
packet.data.clear();
packet.data = inPacket; // Copy output packet back to input buffer.
} }
ret = packet.data;
} }
*/
return; return;
} }

Wyświetl plik

@ -6,6 +6,7 @@
#include <QtMultimedia/QAudioOutput> #include <QtMultimedia/QAudioOutput>
#include <QByteArray> #include <QByteArray>
#include <QMutex> #include <QMutex>
#include <QtEndian>
#include "rtaudio/RtAudio.h" #include "rtaudio/RtAudio.h"
typedef signed short MY_TYPE; typedef signed short MY_TYPE;
@ -116,6 +117,7 @@ private:
volatile bool ready = false; volatile bool ready = false;
audioPacket tempBuf; audioPacket tempBuf;
quint16 currentLatency; quint16 currentLatency;
qreal volume=1.0;
}; };

Wyświetl plik

@ -858,10 +858,12 @@ void udpAudio::watchdog()
void udpAudio::sendTxAudio() void udpAudio::sendTxAudio()
{ {
if (txaudio == Q_NULLPTR) {
if (txaudio && txaudio->isChunkAvailable()) { return;
QByteArray audio; }
txaudio->getNextAudioChunk(audio); QByteArray audio;
txaudio->getNextAudioChunk(audio);
if (audio.length() > 0) {
int counter = 1; int counter = 1;
int len = 0; int len = 0;