DMRGateway/Voice.cpp

345 lines
8.3 KiB
C++
Raw Normal View History

2017-05-01 15:46:43 +02:00
/*
* Copyright (C) 2017 by Jonathan Naylor G4KLX
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
2017-05-01 21:05:41 +02:00
#include "DMRSlotType.h"
#include "DMRFullLC.h"
#include "DMREMB.h"
2017-05-01 15:46:43 +02:00
#include "Voice.h"
2017-05-01 21:05:41 +02:00
#include "Sync.h"
#include "Log.h"
2017-05-01 15:46:43 +02:00
2017-05-01 21:05:41 +02:00
#include <cstring>
2017-05-23 01:57:48 +02:00
#include <cstdlib>
2017-05-01 21:05:41 +02:00
#include <sys/stat.h>
const unsigned char SILENCE[] = {0xACU, 0xAAU, 0x40U, 0x20U, 0x00U, 0x44U, 0x40U, 0x80U, 0x80U};
2017-05-01 21:05:41 +02:00
const unsigned char COLOR_CODE = 3U;
const unsigned int SILENCE_LENGTH = 9U;
const unsigned int AMBE_LENGTH = 9U;
2017-05-01 21:05:41 +02:00
CVoice::CVoice(const std::string& directory, const std::string& language, unsigned int id, unsigned int slot, unsigned int tg) :
m_indxFile(),
m_ambeFile(),
2017-05-01 15:46:43 +02:00
m_slot(slot),
2017-05-01 21:05:41 +02:00
m_lc(FLCO_GROUP, id, tg),
m_embeddedLC(),
2017-05-01 15:46:43 +02:00
m_status(VS_NONE),
m_timer(1000U, 1U),
2017-05-01 21:05:41 +02:00
m_stopWatch(),
m_seqNo(0U),
m_streamId(0U),
m_sent(0U),
m_ambe(NULL),
m_positions(),
2017-05-01 21:05:41 +02:00
m_data(),
m_it()
2017-05-01 15:46:43 +02:00
{
2017-05-01 21:05:41 +02:00
m_embeddedLC.setLC(m_lc);
#if defined(_WIN32) || defined(_WIN64)
m_indxFile = directory + "\\" + language + ".indx";
m_ambeFile = directory + "\\" + language + ".ambe";
#else
m_indxFile = directory + "/" + language + ".indx";
m_ambeFile = directory + "/" + language + ".ambe";
#endif
2017-05-01 15:46:43 +02:00
}
CVoice::~CVoice()
{
2017-05-01 21:05:41 +02:00
for (std::vector<CDMRData*>::iterator it = m_data.begin(); it != m_data.end(); ++it)
delete *it;
for (std::unordered_map<std::string, CPositions*>::iterator it = m_positions.begin(); it != m_positions.end(); ++it)
delete it->second;
2017-05-01 21:05:41 +02:00
m_data.clear();
m_positions.clear();
2017-05-01 21:05:41 +02:00
delete[] m_ambe;
2017-05-01 15:46:43 +02:00
}
bool CVoice::open()
{
2017-05-01 21:05:41 +02:00
FILE* fpindx = ::fopen(m_indxFile.c_str(), "rt");
if (fpindx == NULL) {
LogError("Unable to open the index file - %s", m_indxFile.c_str());
return false;
}
struct stat statStruct;
int ret = ::stat(m_ambeFile.c_str(), &statStruct);
if (ret != 0) {
LogError("Unable to stat the AMBE file - %s", m_ambeFile.c_str());
::fclose(fpindx);
return false;
}
FILE* fpambe = ::fopen(m_ambeFile.c_str(), "rb");
if (fpambe == NULL) {
LogError("Unable to open the AMBE file - %s", m_ambeFile.c_str());
::fclose(fpindx);
return false;
}
m_ambe = new unsigned char[statStruct.st_size];
2017-05-06 18:17:34 +02:00
size_t sizeRead = ::fread(m_ambe, 1U, statStruct.st_size, fpambe);
if (sizeRead != 0U) {
char buffer[80U];
while (::fgets(buffer, 80, fpindx) != NULL) {
char* p1 = ::strtok(buffer, "\t\r\n");
char* p2 = ::strtok(NULL, "\t\r\n");
char* p3 = ::strtok(NULL, "\t\r\n");
if (p1 != NULL && p2 != NULL && p3 != NULL) {
std::string symbol = std::string(p1);
unsigned int start = ::atoi(p2) * AMBE_LENGTH;
unsigned int length = ::atoi(p3) * AMBE_LENGTH;
2017-05-06 18:17:34 +02:00
CPositions* pos = new CPositions;
pos->m_start = start;
pos->m_length = length;
m_positions[symbol] = pos;
}
2017-05-01 21:05:41 +02:00
}
}
::fclose(fpindx);
::fclose(fpambe);
2017-05-01 15:46:43 +02:00
return true;
}
void CVoice::linkedTo(unsigned int number, unsigned int room)
2017-05-01 15:46:43 +02:00
{
char letters[10U];
::sprintf(letters, "%03u", number);
std::vector<std::string> words;
2017-10-04 22:50:26 +02:00
if (m_positions.count("linkedto") == 0U) {
words.push_back("linked");
words.push_back("2");
} else {
words.push_back("linkedto");
}
words.push_back("X");
words.push_back("L");
words.push_back("X");
words.push_back(std::string(1U, letters[0U]));
words.push_back(std::string(1U, letters[1U]));
words.push_back(std::string(1U, letters[2U]));
2017-09-30 00:09:35 +02:00
2017-10-04 22:50:26 +02:00
// 4001 => 1 => A, 4002 => 2 => B, etc.
room %= 100U;
2017-10-02 13:51:26 +02:00
2017-09-30 00:09:35 +02:00
if (room >= 1U && room <= 26U)
words.push_back(std::string(1U, 'A' + room - 1U));
2017-05-01 21:05:41 +02:00
createVoice(words);
}
2017-05-01 21:05:41 +02:00
void CVoice::unlinked()
{
std::vector<std::string> words;
words.push_back("notlinked");
2017-05-01 21:05:41 +02:00
createVoice(words);
}
2017-05-01 21:05:41 +02:00
void CVoice::createVoice(const std::vector<std::string>& words)
{
unsigned int ambeLength = 0U;
for (std::vector<std::string>::const_iterator it = words.begin(); it != words.end(); ++it) {
if (m_positions.count(*it) > 0U) {
CPositions* position = m_positions.at(*it);
ambeLength += position->m_length;
} else {
LogWarning("Unable to find character/phrase \"%s\" in the index", (*it).c_str());
}
}
// Ensure that the AMBE is an integer number of DMR frames
if ((ambeLength % (3U * AMBE_LENGTH)) != 0U) {
unsigned int frames = ambeLength / (3U * AMBE_LENGTH);
frames++;
ambeLength = frames * (3U * AMBE_LENGTH);
}
2017-05-01 21:05:41 +02:00
2017-10-04 22:50:26 +02:00
// Add space for silence before and after the voice
ambeLength += SILENCE_LENGTH * AMBE_LENGTH;
ambeLength += SILENCE_LENGTH * AMBE_LENGTH;
unsigned char* ambeData = new unsigned char[ambeLength];
2017-05-01 21:05:41 +02:00
// Fill the AMBE data with silence
for (unsigned int i = 0U; i < ambeLength; i += AMBE_LENGTH)
::memcpy(ambeData + i, SILENCE, AMBE_LENGTH);
2017-05-01 15:46:43 +02:00
// Put offset in for silence at the beginning
unsigned int pos = SILENCE_LENGTH * AMBE_LENGTH;
for (std::vector<std::string>::const_iterator it = words.begin(); it != words.end(); ++it) {
if (m_positions.count(*it) > 0U) {
CPositions* position = m_positions.at(*it);
unsigned int start = position->m_start;
unsigned int length = position->m_length;
::memcpy(ambeData + pos, m_ambe + start, length);
pos += length;
}
}
2017-05-01 21:05:41 +02:00
for (std::vector<CDMRData*>::iterator it = m_data.begin(); it != m_data.end(); ++it)
delete *it;
m_data.clear();
m_streamId = ::rand() + 1U;
m_seqNo = 0U;
createHeaderTerminator(DT_VOICE_LC_HEADER);
createHeaderTerminator(DT_VOICE_LC_HEADER);
createHeaderTerminator(DT_VOICE_LC_HEADER);
unsigned char buffer[DMR_FRAME_LENGTH_BYTES];
unsigned int n = 0U;
for (unsigned int i = 0U; i < ambeLength; i += (3U * AMBE_LENGTH)) {
unsigned char* p = ambeData + i;
2017-05-01 21:05:41 +02:00
CDMRData* data = new CDMRData;
data->setSlotNo(m_slot);
data->setFLCO(FLCO_GROUP);
data->setSrcId(m_lc.getSrcId());
data->setDstId(m_lc.getDstId());
data->setN(n);
2017-05-01 21:05:41 +02:00
data->setSeqNo(m_seqNo++);
data->setStreamId(m_streamId);
::memcpy(buffer + 0U, p + 0U, AMBE_LENGTH);
::memcpy(buffer + 9U, p + 9U, AMBE_LENGTH);
::memcpy(buffer + 15U, p + 9U, AMBE_LENGTH);
::memcpy(buffer + 24U, p + 18U, AMBE_LENGTH);
2017-05-01 21:05:41 +02:00
if (n == 0U) {
CSync::addDMRAudioSync(buffer, true);
2017-05-01 21:05:41 +02:00
data->setDataType(DT_VOICE_SYNC);
} else {
unsigned char lcss = m_embeddedLC.getData(buffer, n);
2017-05-01 21:05:41 +02:00
CDMREMB emb;
emb.setColorCode(COLOR_CODE);
emb.setPI(false);
emb.setLCSS(lcss);
2017-05-01 21:05:41 +02:00
emb.getData(buffer);
data->setDataType(DT_VOICE);
}
n++;
if (n >= 6U)
n = 0U;
2017-05-01 21:05:41 +02:00
data->setData(buffer);
m_data.push_back(data);
}
createHeaderTerminator(DT_TERMINATOR_WITH_LC);
createHeaderTerminator(DT_TERMINATOR_WITH_LC);
delete[] ambeData;
2017-05-01 21:05:41 +02:00
2017-05-01 15:46:43 +02:00
m_status = VS_WAITING;
m_timer.start();
}
bool CVoice::read(CDMRData& data)
{
if (m_status != VS_SENDING)
return false;
2017-05-01 21:05:41 +02:00
unsigned int count = m_stopWatch.elapsed() / DMR_SLOT_TIME;
if (m_sent < count) {
data = *(*m_it);
++m_sent;
++m_it;
if (m_it == m_data.end()) {
for (std::vector<CDMRData*>::iterator it = m_data.begin(); it != m_data.end(); ++it)
delete *it;
m_data.clear();
m_timer.stop();
m_status = VS_NONE;
}
return true;
}
2017-05-01 15:46:43 +02:00
return false;
}
void CVoice::clock(unsigned int ms)
{
m_timer.clock(ms);
if (m_timer.isRunning() && m_timer.hasExpired()) {
if (m_status == VS_WAITING) {
m_stopWatch.start();
m_status = VS_SENDING;
2017-05-01 21:05:41 +02:00
m_it = m_data.begin();
m_sent = 0U;
2017-05-01 15:46:43 +02:00
}
}
}
2017-05-01 21:05:41 +02:00
void CVoice::createHeaderTerminator(unsigned char type)
{
CDMRData* data = new CDMRData;
data->setSlotNo(m_slot);
data->setFLCO(FLCO_GROUP);
data->setSrcId(m_lc.getSrcId());
data->setDstId(m_lc.getDstId());
data->setDataType(type);
data->setN(0U);
data->setSeqNo(m_seqNo++);
data->setStreamId(m_streamId);
unsigned char buffer[DMR_FRAME_LENGTH_BYTES];
CDMRFullLC fullLC;
fullLC.encode(m_lc, buffer, type);
CDMRSlotType slotType;
slotType.setColorCode(COLOR_CODE);
slotType.setDataType(type);
slotType.getData(buffer);
CSync::addDMRDataSync(buffer, true);
data->setData(buffer);
m_data.push_back(data);
}