sdrangel/sdrbase/dsp/dvserialworker.cpp

193 lines
6.0 KiB
C++
Raw Normal View History

///////////////////////////////////////////////////////////////////////////////////
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <unistd.h>
2016-10-11 18:53:26 -04:00
#include <sys/time.h>
#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-11 18:19:44 -04:00
m_currentGainOut(0)
{
}
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)
{
sleep(1);
}
qDebug("DVSerialWorker::process: stopped");
emit finished();
}
void DVSerialWorker::stop()
{
m_running = false;
}
void DVSerialWorker::handleInputMessages()
{
Message* message;
2016-10-11 17:51:18 -04:00
AudioFifo *audioFifo[m_nbFifoSlots];
for (int i = 0; i < m_nbFifoSlots; i++)
{
audioFifo[i] = 0;
2016-10-11 18:02:36 -04:00
m_fifoSlots[i].m_audioBufferFill = 0;
2016-10-11 17:51:18 -04:00
}
while ((message = m_inputMessageQueue.pop()) != 0)
{
if (MsgMbeDecode::match(*message))
{
MsgMbeDecode *decodeMsg = (MsgMbeDecode *) message;
int dBVolume = (decodeMsg->getVolumeIndex() - 30) / 2;
2016-10-11 18:19:44 -04:00
unsigned int fifoSlot = decodeMsg->getFifoSlot();
2016-10-11 18:19:44 -04:00
if (m_dvController.decode(m_fifoSlots[fifoSlot].m_dvAudioSamples, decodeMsg->getMbeFrame(), decodeMsg->getMbeRate(), dBVolume))
{
2016-10-11 18:19:44 -04:00
upsample6(m_fifoSlots[fifoSlot].m_dvAudioSamples,
SerialDV::MBE_AUDIO_BLOCK_SIZE,
decodeMsg->getChannels(),
fifoSlot);
audioFifo[fifoSlot] = decodeMsg->getAudioFifo();
}
2016-05-09 03:24:28 -04:00
else
{
qDebug("DVSerialWorker::handleInputMessages: MsgMbeDecode: decode failed");
}
}
delete message;
}
2016-10-11 17:51:18 -04:00
for (int i = 0; i < m_nbFifoSlots; i++)
{
2016-10-11 17:51:18 -04:00
if (audioFifo[i])
{
2016-10-11 18:02:36 -04:00
uint res = audioFifo[i]->write((const quint8*)&m_fifoSlots[i].m_audioBuffer[0], m_fifoSlots[i].m_audioBufferFill, 10);
2016-10-11 17:51:18 -04:00
2016-10-11 18:02:36 -04:00
if (res != m_fifoSlots[i].m_audioBufferFill)
2016-10-11 17:51:18 -04:00
{
2016-10-11 18:02:36 -04:00
qDebug("DVSerialWorker::handleInputMessages: %u/%u audio samples written", res, m_fifoSlots[i].m_audioBufferFill);
2016-10-11 17:51:18 -04:00
}
m_fifoSlots[i].m_timestamp = QDateTime::currentDateTime();
}
}
}
2016-05-08 04:48:13 -04:00
void DVSerialWorker::pushMbeFrame(const unsigned char *mbeFrame,
int mbeRateIndex,
int mbeVolumeIndex,
2016-10-11 17:51:18 -04:00
unsigned char channels, AudioFifo *audioFifo,
unsigned int fifoSlot)
{
2016-10-11 16:52:45 -04:00
m_fifoSlots[0].m_audioFifo = audioFifo;
2016-10-11 17:51:18 -04:00
m_inputMessageQueue.push(MsgMbeDecode::create(mbeFrame, mbeRateIndex, mbeVolumeIndex, channels, audioFifo, fifoSlot));
}
2016-10-11 17:51:18 -04:00
bool DVSerialWorker::isAvailable(unsigned int& fifoSlot)
{
2016-10-11 17:51:18 -04:00
for (fifoSlot = 0; fifoSlot < m_nbFifoSlots; fifoSlot++)
{
if (m_fifoSlots[fifoSlot].m_audioFifo == 0)
{
return true;
}
else if (m_fifoSlots[fifoSlot].m_timestamp.time().msecsTo(QDateTime::currentDateTime().time()) > 1000)
{
return true;
}
}
2016-10-11 17:51:18 -04:00
return false;
}
2016-10-11 17:51:18 -04:00
bool DVSerialWorker::hasFifo(AudioFifo *audioFifo, unsigned int& fifoSlot)
{
2016-10-11 17:51:18 -04:00
for (fifoSlot = 0; fifoSlot < m_nbFifoSlots; fifoSlot++)
{
if (m_fifoSlots[fifoSlot].m_audioFifo == audioFifo)
{
return true;
}
}
return false;
}
2016-10-11 18:02:36 -04:00
void DVSerialWorker::upsample6(short *in, int nbSamplesIn, unsigned char channels, unsigned int fifoSlot)
{
for (int i = 0; i < nbSamplesIn; i++)
{
int cur = (int) in[i];
2016-10-11 18:02:36 -04:00
int prev = (int) m_fifoSlots[fifoSlot].m_upsamplerLastValue;
qint16 upsample;
for (int j = 1; j < 7; j++)
{
2016-10-11 18:02:36 -04:00
upsample = m_fifoSlots[fifoSlot].m_upsampleFilter.run((qint16) ((cur*j + prev*(6-j)) / 6));
m_fifoSlots[fifoSlot].m_audioBuffer[m_fifoSlots[fifoSlot].m_audioBufferFill].l = channels & 1 ? upsample : 0;
m_fifoSlots[fifoSlot].m_audioBuffer[m_fifoSlots[fifoSlot].m_audioBufferFill].r = (channels>>1) & 1 ? upsample : 0;
2016-10-11 18:02:36 -04:00
if (m_fifoSlots[fifoSlot].m_audioBufferFill < m_fifoSlots[fifoSlot].m_audioBuffer.size() - 1)
{
2016-10-11 18:02:36 -04:00
++m_fifoSlots[fifoSlot].m_audioBufferFill;
}
else
{
qDebug("DVSerialWorker::upsample6: audio buffer is full check its size");
}
}
2016-10-11 18:02:36 -04:00
m_fifoSlots[fifoSlot].m_upsamplerLastValue = in[i];
}
}
2016-10-11 18:53:26 -04:00
long long DVSerialWorker::getUSecs()
{
struct timeval tp;
gettimeofday(&tp, 0);
return (long long) tp.tv_sec * 1000000L + tp.tv_usec;
}