2016-05-08 00:00:37 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2016 F4EXB //
|
|
|
|
// written by Edouard Griffiths //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation as version 3 of the License, or //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-11-12 11:22:19 -05:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2016-05-08 00:00:37 -04:00
|
|
|
|
|
|
|
#include "dsp/dvserialworker.h"
|
|
|
|
#include "audio/audiofifo.h"
|
|
|
|
|
|
|
|
MESSAGE_CLASS_DEFINITION(DVSerialWorker::MsgMbeDecode, Message)
|
|
|
|
MESSAGE_CLASS_DEFINITION(DVSerialWorker::MsgTest, Message)
|
|
|
|
|
|
|
|
DVSerialWorker::DVSerialWorker() :
|
|
|
|
m_running(false),
|
|
|
|
m_currentGainIn(0),
|
2016-10-28 02:37:27 -04:00
|
|
|
m_currentGainOut(0),
|
2018-06-23 20:29:54 -04:00
|
|
|
m_upsamplerLastValue(0.0f),
|
|
|
|
m_phase(0),
|
|
|
|
m_upsampling(1),
|
|
|
|
m_volume(1.0f)
|
2016-05-08 00:00:37 -04:00
|
|
|
{
|
2016-10-28 02:37:27 -04:00
|
|
|
m_audioBuffer.resize(48000);
|
|
|
|
m_audioBufferFill = 0;
|
|
|
|
m_audioFifo = 0;
|
2018-02-24 04:29:27 -05:00
|
|
|
memset(m_dvAudioSamples, 0, SerialDV::MBE_AUDIO_BLOCK_SIZE*sizeof(short));
|
2018-06-23 20:29:54 -04:00
|
|
|
setVolumeFactors();
|
2016-05-08 00:00:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DVSerialWorker::~DVSerialWorker()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DVSerialWorker::open(const std::string& serialDevice)
|
|
|
|
{
|
|
|
|
return m_dvController.open(serialDevice);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DVSerialWorker::close()
|
|
|
|
{
|
|
|
|
m_dvController.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DVSerialWorker::process()
|
|
|
|
{
|
|
|
|
m_running = true;
|
|
|
|
qDebug("DVSerialWorker::process: started");
|
|
|
|
|
|
|
|
while (m_running)
|
|
|
|
{
|
2018-11-12 11:22:19 -05:00
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
2016-05-08 00:00:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
qDebug("DVSerialWorker::process: stopped");
|
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DVSerialWorker::stop()
|
|
|
|
{
|
|
|
|
m_running = false;
|
|
|
|
}
|
|
|
|
|
2016-05-08 01:02:31 -04:00
|
|
|
void DVSerialWorker::handleInputMessages()
|
2016-05-08 00:00:37 -04:00
|
|
|
{
|
2016-05-08 01:02:31 -04:00
|
|
|
Message* message;
|
2016-10-28 02:37:27 -04:00
|
|
|
m_audioBufferFill = 0;
|
|
|
|
AudioFifo *audioFifo = 0;
|
2016-05-08 01:02:31 -04:00
|
|
|
|
|
|
|
while ((message = m_inputMessageQueue.pop()) != 0)
|
|
|
|
{
|
|
|
|
if (MsgMbeDecode::match(*message))
|
|
|
|
{
|
|
|
|
MsgMbeDecode *decodeMsg = (MsgMbeDecode *) message;
|
2018-06-27 03:15:10 -04:00
|
|
|
int dBVolume = (decodeMsg->getVolumeIndex() - 30) / 4;
|
2018-06-23 20:29:54 -04:00
|
|
|
float volume = pow(10.0, dBVolume / 10.0f);
|
|
|
|
int upsampling = decodeMsg->getUpsampling();
|
|
|
|
upsampling = upsampling > 6 ? 6 : upsampling < 1 ? 1 : upsampling;
|
|
|
|
|
|
|
|
if ((volume != m_volume) || (upsampling != m_upsampling))
|
|
|
|
{
|
|
|
|
m_volume = volume;
|
|
|
|
m_upsampling = upsampling;
|
|
|
|
setVolumeFactors();
|
|
|
|
}
|
2016-05-08 01:02:31 -04:00
|
|
|
|
2017-12-26 14:29:23 -05:00
|
|
|
m_upsampleFilter.useHP(decodeMsg->getUseHP());
|
|
|
|
|
2018-06-23 20:29:54 -04:00
|
|
|
if (m_dvController.decode(m_dvAudioSamples, decodeMsg->getMbeFrame(), decodeMsg->getMbeRate()))
|
2016-05-08 01:02:31 -04:00
|
|
|
{
|
2018-04-22 19:04:47 -04:00
|
|
|
if (upsampling > 1) {
|
|
|
|
upsample(upsampling, m_dvAudioSamples, SerialDV::MBE_AUDIO_BLOCK_SIZE, decodeMsg->getChannels());
|
2018-03-28 02:43:11 -04:00
|
|
|
} else {
|
|
|
|
noUpsample(m_dvAudioSamples, SerialDV::MBE_AUDIO_BLOCK_SIZE, decodeMsg->getChannels());
|
|
|
|
}
|
|
|
|
|
2016-10-28 02:37:27 -04:00
|
|
|
audioFifo = decodeMsg->getAudioFifo();
|
2016-05-08 01:02:31 -04:00
|
|
|
}
|
2016-05-09 03:24:28 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug("DVSerialWorker::handleInputMessages: MsgMbeDecode: decode failed");
|
|
|
|
}
|
2016-05-08 01:02:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
delete message;
|
|
|
|
}
|
2016-09-11 11:02:49 -04:00
|
|
|
|
2016-10-28 02:37:27 -04:00
|
|
|
if (audioFifo)
|
2016-09-11 13:58:38 -04:00
|
|
|
{
|
2018-09-12 09:30:53 -04:00
|
|
|
uint res = audioFifo->write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
|
2016-10-11 17:51:18 -04:00
|
|
|
|
2016-10-28 02:37:27 -04:00
|
|
|
if (res != m_audioBufferFill)
|
|
|
|
{
|
|
|
|
qDebug("DVSerialWorker::handleInputMessages: %u/%u audio samples written", res, m_audioBufferFill);
|
2016-09-11 13:58:38 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 02:37:27 -04:00
|
|
|
|
|
|
|
m_timestamp = QDateTime::currentDateTime();
|
2016-05-08 00:00:37 -04:00
|
|
|
}
|
2016-05-08 04:48:13 -04:00
|
|
|
|
2016-09-10 21:57:35 -04:00
|
|
|
void DVSerialWorker::pushMbeFrame(const unsigned char *mbeFrame,
|
|
|
|
int mbeRateIndex,
|
|
|
|
int mbeVolumeIndex,
|
2017-12-26 14:29:23 -05:00
|
|
|
unsigned char channels,
|
|
|
|
bool useHP,
|
2018-04-22 19:04:47 -04:00
|
|
|
int upsampling,
|
2017-12-26 14:29:23 -05:00
|
|
|
AudioFifo *audioFifo)
|
2016-09-10 21:57:35 -04:00
|
|
|
{
|
2016-10-28 02:37:27 -04:00
|
|
|
m_audioFifo = audioFifo;
|
2018-04-22 19:04:47 -04:00
|
|
|
m_inputMessageQueue.push(MsgMbeDecode::create(mbeFrame, mbeRateIndex, mbeVolumeIndex, channels, useHP, upsampling, audioFifo));
|
2016-09-10 21:57:35 -04:00
|
|
|
}
|
|
|
|
|
2016-10-28 02:37:27 -04:00
|
|
|
bool DVSerialWorker::isAvailable()
|
2016-09-10 21:57:35 -04:00
|
|
|
{
|
2016-10-28 02:37:27 -04:00
|
|
|
if (m_audioFifo == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-11 14:24:08 -04:00
|
|
|
|
2016-10-28 02:37:27 -04:00
|
|
|
return m_timestamp.time().msecsTo(QDateTime::currentDateTime().time()) > 1000; // 1 second inactivity timeout
|
2016-10-11 14:24:08 -04:00
|
|
|
}
|
|
|
|
|
2016-10-28 02:37:27 -04:00
|
|
|
bool DVSerialWorker::hasFifo(AudioFifo *audioFifo)
|
2016-10-11 14:24:08 -04:00
|
|
|
{
|
2016-10-28 02:37:27 -04:00
|
|
|
return m_audioFifo == audioFifo;
|
2016-09-10 21:57:35 -04:00
|
|
|
}
|
|
|
|
|
2018-04-22 19:04:47 -04:00
|
|
|
void DVSerialWorker::upsample(int upsampling, short *in, int nbSamplesIn, unsigned char channels)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < nbSamplesIn; i++)
|
|
|
|
{
|
2018-07-04 14:01:02 -04:00
|
|
|
//float cur = m_upsampleFilter.usesHP() ? m_upsampleFilter.runHP((float) m_compressor.compress(in[i])) : (float) m_compressor.compress(in[i]);
|
|
|
|
float cur = m_upsampleFilter.usesHP() ? m_upsampleFilter.runHP((float) in[i]) : (float) in[i];
|
2018-06-23 20:29:54 -04:00
|
|
|
float prev = m_upsamplerLastValue;
|
2018-04-22 19:04:47 -04:00
|
|
|
qint16 upsample;
|
|
|
|
|
|
|
|
for (int j = 1; j <= upsampling; j++)
|
|
|
|
{
|
2018-06-23 20:29:54 -04:00
|
|
|
upsample = (qint16) m_upsampleFilter.runLP(cur*m_upsamplingFactors[j] + prev*m_upsamplingFactors[upsampling-j]);
|
2018-07-04 14:01:02 -04:00
|
|
|
m_audioBuffer[m_audioBufferFill].l = channels & 1 ? m_compressor.compress(upsample) : 0;
|
|
|
|
m_audioBuffer[m_audioBufferFill].r = (channels>>1) & 1 ? m_compressor.compress(upsample) : 0;
|
2018-04-22 19:04:47 -04:00
|
|
|
|
2016-10-28 02:37:27 -04:00
|
|
|
if (m_audioBufferFill < m_audioBuffer.size() - 1)
|
2016-05-08 19:26:39 -04:00
|
|
|
{
|
2016-10-28 02:37:27 -04:00
|
|
|
++m_audioBufferFill;
|
2016-09-11 13:58:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug("DVSerialWorker::upsample6: audio buffer is full check its size");
|
2016-05-08 19:26:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 20:29:54 -04:00
|
|
|
m_upsamplerLastValue = cur;
|
2016-05-08 19:26:39 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-11 18:53:26 -04:00
|
|
|
|
2018-03-28 02:43:11 -04:00
|
|
|
void DVSerialWorker::noUpsample(short *in, int nbSamplesIn, unsigned char channels)
|
2016-10-11 18:53:26 -04:00
|
|
|
{
|
2016-10-28 02:37:27 -04:00
|
|
|
for (int i = 0; i < nbSamplesIn; i++)
|
|
|
|
{
|
2018-06-23 20:29:54 -04:00
|
|
|
float cur = m_upsampleFilter.usesHP() ? m_upsampleFilter.runHP((float) in[i]) : (float) in[i];
|
|
|
|
m_audioBuffer[m_audioBufferFill].l = channels & 1 ? cur*m_upsamplingFactors[0] : 0;
|
|
|
|
m_audioBuffer[m_audioBufferFill].r = (channels>>1) & 1 ? cur*m_upsamplingFactors[0] : 0;
|
2016-10-28 02:37:27 -04:00
|
|
|
|
2018-03-28 02:43:11 -04:00
|
|
|
if (m_audioBufferFill < m_audioBuffer.size() - 1)
|
|
|
|
{
|
|
|
|
++m_audioBufferFill;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug("DVSerialWorker::noUpsample: audio buffer is full check its size");
|
|
|
|
}
|
2016-10-28 02:37:27 -04:00
|
|
|
}
|
2016-10-11 18:53:26 -04:00
|
|
|
}
|
2018-03-28 02:43:11 -04:00
|
|
|
|
2018-06-23 20:29:54 -04:00
|
|
|
void DVSerialWorker::setVolumeFactors()
|
|
|
|
{
|
|
|
|
m_upsamplingFactors[0] = m_volume;
|
|
|
|
|
|
|
|
for (int i = 1; i <= m_upsampling; i++) {
|
|
|
|
m_upsamplingFactors[i] = (i*m_volume) / (float) m_upsampling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 02:43:11 -04:00
|
|
|
//void DVSerialWorker::upsample6(short *in, short *out, int nbSamplesIn)
|
|
|
|
//{
|
|
|
|
// for (int i = 0; i < nbSamplesIn; i++)
|
|
|
|
// {
|
|
|
|
// int cur = (int) in[i];
|
|
|
|
// int prev = (int) m_upsamplerLastValue;
|
|
|
|
// short up;
|
|
|
|
//
|
|
|
|
//// DEBUG:
|
|
|
|
//// for (int j = 0; j < 6; j++)
|
|
|
|
//// {
|
|
|
|
//// up = 32768.0f * cos(m_phase);
|
|
|
|
//// *out = up;
|
|
|
|
//// out ++;
|
|
|
|
//// *out = up;
|
|
|
|
//// out ++;
|
|
|
|
//// m_phase += M_PI / 6.0;
|
|
|
|
//// }
|
|
|
|
////
|
|
|
|
//// if ((i % 2) == 1)
|
|
|
|
//// {
|
|
|
|
//// m_phase = 0.0f;
|
|
|
|
//// }
|
|
|
|
//
|
|
|
|
// up = m_upsampleFilter.run((cur*1 + prev*5) / 6);
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
//
|
|
|
|
// up = m_upsampleFilter.run((cur*2 + prev*4) / 6);
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
//
|
|
|
|
// up = m_upsampleFilter.run((cur*3 + prev*3) / 6);
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
//
|
|
|
|
// up = m_upsampleFilter.run((cur*4 + prev*2) / 6);
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
//
|
|
|
|
// up = m_upsampleFilter.run((cur*5 + prev*1) / 6);
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
//
|
|
|
|
// up = m_upsampleFilter.run(in[i]);
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
// *out = up;
|
|
|
|
// out++;
|
|
|
|
//
|
|
|
|
// m_upsamplerLastValue = in[i];
|
|
|
|
// }
|
|
|
|
//}
|