wfview/audiohandler.cpp

387 wiersze
11 KiB
C++
Czysty Zwykły widok Historia

2021-02-11 19:18:35 +00:00
/*
2021-05-23 15:09:41 +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.
2021-02-11 19:18:35 +00:00
*/
#include "audiohandler.h"
2021-05-16 20:16:59 +00:00
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
2021-02-11 19:18:35 +00:00
audioHandler::audioHandler(QObject* parent) :
2021-05-23 15:09:41 +00:00
isInitialized(false),
isUlaw(false),
audioLatency(0),
isInput(0),
chunkAvailable(false)
2021-02-11 19:18:35 +00:00
{
}
audioHandler::~audioHandler()
{
2021-05-23 15:09:41 +00:00
//stop();
if (resampler != Q_NULLPTR) {
2021-03-09 17:22:16 +00:00
speex_resampler_destroy(resampler);
}
2021-05-23 15:09:41 +00:00
if (audio.isStreamRunning())
{
audio.stopStream();
}
delete buf;
2021-02-11 19:18:35 +00:00
}
2021-05-23 15:09:41 +00:00
bool audioHandler::init(const quint8 bits, const quint8 channels, const quint16 samplerate, const quint16 latency, const bool ulaw, const bool isinput, int port, quint8 resampleQuality)
2021-02-11 19:18:35 +00:00
{
2021-05-23 15:09:41 +00:00
if (isInitialized) {
return false;
}
2021-02-11 19:18:35 +00:00
2021-05-16 20:16:59 +00:00
this->audioLatency = latency;
this->isUlaw = ulaw;
2021-05-23 15:09:41 +00:00
this->isInput = isinput;
this->radioSampleBits = bits;
this->radioSampleRate = samplerate;
2021-03-09 17:22:16 +00:00
this->radioChannels = channels;
// 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-23 15:09:41 +00:00
if (port > 0) {
aParams.deviceId = port;
}
else if (isInput) {
aParams.deviceId = audio.getDefaultInputDevice();
}
else {
aParams.deviceId = audio.getDefaultOutputDevice();
}
aParams.nChannels = channels;
aParams.firstChannel = 0;
2021-02-11 19:18:35 +00:00
2021-05-23 15:09:41 +00:00
info = audio.getDeviceInfo(aParams.deviceId);
2021-05-23 15:09:41 +00:00
buf = new quint16[960];
2021-05-23 15:09:41 +00:00
if (info.probed)
{
qInfo(logAudio()) << (isInput ? "Input" : "Output") << QString::fromStdString(info.name) << "(" << aParams.deviceId << ") successfully probed";
if (info.nativeFormats == 0)
qInfo(logAudio()) << " No natively supported data formats!";
else {
qDebug(logAudio()) << " Supported formats:" <<
(info.nativeFormats & RTAUDIO_SINT8 ? "8-bit int," : "") <<
(info.nativeFormats & RTAUDIO_SINT16 ? "16-bit int," : "") <<
(info.nativeFormats & RTAUDIO_SINT24 ? "24-bit int," : "") <<
(info.nativeFormats & RTAUDIO_SINT32 ? "32-bit int," : "") <<
(info.nativeFormats & RTAUDIO_FLOAT32 ? "32-bit float," : "") <<
(info.nativeFormats & RTAUDIO_FLOAT64 ? "64-bit float," : "");
qInfo(logAudio()) << " Preferred sample rate:" << info.preferredSampleRate;
}
2021-05-23 15:09:41 +00:00
qInfo(logAudio()) << " chunkSize: " << chunkSize;
}
else
{
2021-05-23 15:09:41 +00:00
qCritical(logAudio()) << (isInput ? "Input" : "Output") << QString::fromStdString(info.name) << "(" << aParams.deviceId << ") could not be probed, check audio configuration!";
}
2021-05-23 15:09:41 +00:00
int resample_error = 0;
2021-05-23 15:09:41 +00:00
if (isInput) {
resampler = wf_resampler_init(radioChannels, INTERNAL_SAMPLE_RATE, samplerate, resampleQuality, &resample_error);
try {
audio.openStream(NULL, &aParams, RTAUDIO_SINT16, INTERNAL_SAMPLE_RATE, &this->chunkSize, &staticWrite);
audio.startStream();
}
catch (RtAudioError& e) {
qInfo(logAudio()) << "Error opening:" << QString::fromStdString(e.getMessage());
return false;
}
}
2021-05-23 15:09:41 +00:00
else
{
resampler = wf_resampler_init(radioChannels, samplerate, INTERNAL_SAMPLE_RATE, resampleQuality, &resample_error);
try {
unsigned int length = chunkSize / 2;
audio.openStream(&aParams, NULL, RTAUDIO_SINT16, INTERNAL_SAMPLE_RATE, &length, &staticRead);
audio.startStream();
}
catch (RtAudioError& e) {
qInfo(logAudio()) << "Error opening:" << QString::fromStdString(e.getMessage());
return false;
}
}
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "device successfully opened";
2021-05-16 20:16:59 +00:00
wf_resampler_get_ratio(resampler, &ratioNum, &ratioDen);
2021-05-23 15:09:41 +00:00
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "wf_resampler_init() returned: " << resample_error << " ratioNum" << ratioNum << " ratioDen" << ratioDen;
2021-05-23 15:09:41 +00:00
return isInitialized;
2021-02-11 19:18:35 +00:00
}
void audioHandler::setVolume(unsigned char volume)
2021-03-22 16:02:22 +00:00
{
2021-05-23 15:09:41 +00:00
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "setVolume: " << volume << "(" << (qreal)(volume/255.0) << ")";
2021-02-11 19:18:35 +00:00
}
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-05-23 15:09:41 +00:00
int audioHandler::readData(void* outputBuffer, void* inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status)
2021-02-11 19:18:35 +00:00
{
2021-05-23 15:09:41 +00:00
// Calculate output length, always full samples
int sentlen = 0;
2021-05-23 15:09:41 +00:00
qint16* buffer = (qint16*)outputBuffer;
qDebug(logAudio()) << "looking for: " << nFrames;
2021-05-23 15:09:41 +00:00
for (int f = 0; f < nFrames; f++)
{
2021-05-23 15:09:41 +00:00
qDebug(logAudio()) << "*";
*buffer++ = getBuffer(f);
//*buffer++ = f;
2021-05-03 08:03:18 +00:00
}
2021-05-23 15:09:41 +00:00
return 0;
}
quint16 audioHandler::getBuffer(int i) {
return buf[i];
2021-02-11 19:18:35 +00:00
}
2021-05-23 15:09:41 +00:00
int audioHandler::writeData(void* outputBuffer, void* inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status)
2021-02-11 19:18:35 +00:00
{
2021-05-23 15:09:41 +00:00
int sentlen = 0;
/*
2021-02-27 10:13:59 +00:00
QMutexLocker locker(&mutex);
2021-05-23 15:09:41 +00:00
audioPacket* current;
2021-02-27 09:34:56 +00:00
while (sentlen < len) {
if (!audioBuffer.isEmpty())
{
if (audioBuffer.last().sent == chunkSize)
{
audioBuffer.append(audioPacket());
audioBuffer.last().sent = 0;
}
}
else
{
audioBuffer.append(audioPacket());
audioBuffer.last().sent = 0;
}
current = &audioBuffer.last();
2021-05-23 15:09:41 +00:00
int send = qMin((int)(len - sentlen), (int)chunkSize - current->sent);
2021-02-27 13:19:40 +00:00
2021-05-23 15:09:41 +00:00
current->datain.append(QByteArray::fromRawData(data + sentlen, send));
2021-03-09 17:22:16 +00:00
sentlen = sentlen + send;
2021-02-27 09:34:56 +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-05-23 15:09:41 +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-05-23 15:09:41 +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-05-23 15:09:41 +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-05-23 15:09:41 +00:00
return 0;
2021-02-11 19:18:35 +00:00
}
bool audioHandler::isSequential() const
{
2021-05-23 15:09:41 +00:00
return true;
2021-02-11 19:18:35 +00:00
}
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-05-23 15:09:41 +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
{
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-23 15:09:41 +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-23 15:09:41 +00:00
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Resampler error " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
2021-03-09 17:22:16 +00:00
}
}
else {
2021-05-23 15:09:41 +00:00
data.dataout = data.datain;
2021-03-09 17:22:16 +00:00
}
2021-05-23 15:09:41 +00:00
memcpy(buf, data.dataout.constData(), data.dataout.length());
qDebug(logAudio()) << "Got data: " << data.dataout.length();
//audioBuffer.insert({ data.seq, data });
2021-02-11 19:18:35 +00:00
}
void audioHandler::changeLatency(const quint16 newSize)
2021-02-11 19:18:35 +00:00
{
2021-05-23 15:09:41 +00:00
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Changing latency to: " << newSize << " from " << audioLatency;
audioLatency = newSize;
2021-02-11 19:18:35 +00:00
}
void audioHandler::getLatency()
2021-02-11 19:18:35 +00:00
{
2021-05-23 15:09:41 +00:00
emit sendLatency(audioLatency);
2021-02-11 19:18:35 +00:00
}
bool audioHandler::isChunkAvailable()
{
2021-05-23 15:09:41 +00:00
return (chunkAvailable);
}
2021-02-13 11:04:26 +00:00
void audioHandler::getNextAudioChunk(QByteArray& ret)
2021-02-12 20:42:56 +00:00
{
2021-05-23 15:09:41 +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-23 15:09:41 +00:00
if (packet->time.msecsTo(QTime::currentTime()) > 100) {
//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-05-23 15:09:41 +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.
2021-03-09 17:22:16 +00:00
2021-05-23 15:09:41 +00:00
if (ratioNum != 1)
2021-03-09 17:22:16 +00:00
{
// 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.
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) {
err = wf_resampler_process_int(resampler, 0, in, &inFrames, out, &outFrames);
2021-03-09 17:22:16 +00:00
}
else {
err = wf_resampler_process_interleaved_int(resampler, in, &inFrames, out, &outFrames);
2021-03-09 17:22:16 +00:00
}
if (err) {
qInfo(logAudio()) << (isInput ? "Input" : "Output") << "Resampler error " << err << " inFrames:" << inFrames << " outFrames:" << outFrames;
2021-03-09 17:22: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-05-23 15:09:41 +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
}
}
2021-05-23 15:09:41 +00:00
else if (radioSampleBits == 16) {
2021-03-09 17:22:16 +00:00
// 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-05-23 15:09:41 +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-05-23 15:09:41 +00:00
*/
return;
2021-02-12 20:42:56 +00:00
}
2021-05-23 15:09:41 +00:00
2021-02-11 19:18:35 +00:00