P25Clients/P25Gateway/Voice.cpp

429 lines
12 KiB
C++
Raw Normal View History

2019-02-04 09:17:26 -05:00
/*
2019-02-04 09:57:20 -05:00
* Copyright (C) 2017,2018,2019 by Jonathan Naylor G4KLX
2019-02-04 09:17:26 -05:00
*
* 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.
*/
#include "Voice.h"
#include "Log.h"
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <sys/stat.h>
2019-02-04 09:57:20 -05:00
const unsigned char REC62[] = {
0x62U, 0x02U, 0x02U, 0x0CU, 0x0BU, 0x12U, 0x64U, 0x00U, 0x00U, 0x80U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U };
const unsigned char REC63[] = {
0x63U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC64[] = {
0x64U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC65[] = {
0x65U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC66[] = {
0x66U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC67[] = {
0x67U, 0xF0U, 0x9DU, 0x6AU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC68[] = {
0x68U, 0x19U, 0xD4U, 0x26U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC69[] = {
0x69U, 0xE0U, 0xEBU, 0x7BU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC6A[] = {
0x6AU, 0x00U, 0x00U, 0x02U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U };
const unsigned char REC6B[] = {
0x6BU, 0x02U, 0x02U, 0x0CU, 0x0BU, 0x12U, 0x64U, 0x00U, 0x00U, 0x80U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U };
const unsigned char REC6C[] = {
0x6CU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC6D[] = {
0x6DU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC6E[] = {
0x6EU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC6F[] = {
0x6FU, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC70[] = {
0x70U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC71[] = {
0x71U, 0xACU, 0xB8U, 0xA4U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC72[] = {
0x72U, 0x9BU, 0xDCU, 0x75U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U };
const unsigned char REC73[] = {
0x73U, 0x00U, 0x00U, 0x02U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U };
const unsigned char REC80[] = {
0x80U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U };
2019-02-04 09:17:26 -05:00
2019-02-05 08:47:43 -05:00
const unsigned char SILENCE[] = { 0x04U, 0x0CU, 0xFDU, 0x7BU, 0xFBU, 0x7DU, 0xF2U, 0x7BU, 0x3DU, 0x9EU, 0x44U };
2019-02-04 09:17:26 -05:00
2019-02-04 10:30:19 -05:00
const unsigned int P25_FRAME_TIME = 20U;
2019-02-04 09:17:26 -05:00
const unsigned int SILENCE_LENGTH = 4U;
2019-02-04 10:30:19 -05:00
const unsigned int IMBE_LENGTH = 11U;
2019-02-04 09:17:26 -05:00
2019-02-05 08:47:43 -05:00
const unsigned int LDU_LENGTH = 9U;
2019-02-04 09:17:26 -05:00
CVoice::CVoice(const std::string& directory, const std::string& language, unsigned int srcId) :
m_language(language),
m_indxFile(),
2019-02-04 09:57:20 -05:00
m_imbeFile(),
2019-02-04 09:17:26 -05:00
m_srcId(srcId),
m_status(VS_NONE),
m_timer(1000U, 1U),
m_stopWatch(),
m_sent(0U),
2019-02-04 10:30:19 -05:00
m_n(0x62U),
2019-02-05 02:58:38 -05:00
m_dstId(0U),
2019-02-04 09:57:20 -05:00
m_imbe(NULL),
2019-02-04 09:17:26 -05:00
m_voiceData(NULL),
m_voiceLength(0U),
m_positions()
{
assert(!directory.empty());
assert(!language.empty());
#if defined(_WIN32) || defined(_WIN64)
m_indxFile = directory + "\\" + language + ".indx";
m_imbeFile = directory + "\\" + language + ".imbe";
2019-02-04 09:17:26 -05:00
#else
m_indxFile = directory + "/" + language + ".indx";
m_imbeFile = directory + "/" + language + ".imbe";
2019-02-04 09:17:26 -05:00
#endif
// Approximately 10 seconds worth
2019-02-05 02:58:38 -05:00
m_voiceData = new unsigned char[10U * 50U * IMBE_LENGTH];
2019-02-04 09:17:26 -05:00
}
CVoice::~CVoice()
{
for (std::unordered_map<std::string, CPositions*>::iterator it = m_positions.begin(); it != m_positions.end(); ++it)
delete it->second;
m_positions.clear();
2019-02-04 09:57:20 -05:00
delete[] m_imbe;
2019-02-04 09:17:26 -05:00
delete[] m_voiceData;
}
bool CVoice::open()
{
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;
2019-02-04 09:57:20 -05:00
int ret = ::stat(m_imbeFile.c_str(), &statStruct);
2019-02-04 09:17:26 -05:00
if (ret != 0) {
2019-02-05 02:58:38 -05:00
LogError("Unable to stat the IMBE file - %s", m_imbeFile.c_str());
2019-02-04 09:17:26 -05:00
::fclose(fpindx);
return false;
}
2019-02-04 10:30:19 -05:00
FILE* fpimbe = ::fopen(m_imbeFile.c_str(), "rb");
if (fpimbe == NULL) {
2019-02-05 02:58:38 -05:00
LogError("Unable to open the IMBE file - %s", m_imbeFile.c_str());
2019-02-04 09:17:26 -05:00
::fclose(fpindx);
return false;
}
2019-02-04 09:57:20 -05:00
m_imbe = new unsigned char[statStruct.st_size];
2019-02-04 09:17:26 -05:00
2019-02-04 09:57:20 -05:00
size_t sizeRead = ::fread(m_imbe, 1U, statStruct.st_size, fpimbe);
2019-02-04 09:17:26 -05:00
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);
2019-02-04 09:57:20 -05:00
unsigned int start = ::atoi(p2) * IMBE_LENGTH;
unsigned int length = ::atoi(p3) * IMBE_LENGTH;
2019-02-04 09:17:26 -05:00
CPositions* pos = new CPositions;
pos->m_start = start;
pos->m_length = length;
m_positions[symbol] = pos;
}
}
}
::fclose(fpindx);
2019-02-04 09:57:20 -05:00
::fclose(fpimbe);
2019-02-04 09:17:26 -05:00
LogInfo("Loaded the audio and index file for %s", m_language.c_str());
return true;
}
void CVoice::linkedTo(unsigned int tg)
{
char letters[10U];
::sprintf(letters, "%u", tg);
std::vector<std::string> words;
if (m_positions.count("linkedto") == 0U) {
words.push_back("linked");
words.push_back("2");
} else {
words.push_back("linkedto");
}
for (unsigned int i = 0U; letters[i] != 0x00U; i++)
words.push_back(std::string(1U, letters[i]));
createVoice(tg, words);
}
void CVoice::unlinked()
{
std::vector<std::string> words;
words.push_back("notlinked");
createVoice(9999U, words);
}
void CVoice::createVoice(unsigned int tg, const std::vector<std::string>& words)
{
2019-02-05 02:58:38 -05:00
m_dstId = tg;
2019-02-04 10:30:19 -05:00
m_voiceLength = 0U;
2019-02-04 09:17:26 -05:00
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);
2019-02-05 15:03:36 -05:00
m_voiceLength += position->m_length;
2019-02-04 09:17:26 -05:00
} else {
LogWarning("Unable to find character/phrase \"%s\" in the index", (*it).c_str());
}
}
// Add space for silence before and after the voice
2019-02-04 10:30:19 -05:00
m_voiceLength += SILENCE_LENGTH * IMBE_LENGTH;
m_voiceLength += SILENCE_LENGTH * IMBE_LENGTH;
2019-02-04 09:17:26 -05:00
2019-02-05 02:58:38 -05:00
// Round to the next highest LDU frame length
2019-02-05 08:47:43 -05:00
unsigned int n = (m_voiceLength / IMBE_LENGTH) % LDU_LENGTH;
2019-02-05 02:58:38 -05:00
if (n > 0U)
2019-02-05 08:47:43 -05:00
m_voiceLength += (LDU_LENGTH - n) * IMBE_LENGTH;
2019-02-05 02:58:38 -05:00
2019-02-04 10:30:19 -05:00
// Fill the IMBE data with silence
2019-02-04 09:17:26 -05:00
unsigned int offset = 0U;
2019-02-04 10:30:19 -05:00
for (unsigned int i = 0U; i < (m_voiceLength / IMBE_LENGTH); i++, offset += IMBE_LENGTH)
::memcpy(m_voiceData + offset, SILENCE, IMBE_LENGTH);
2019-02-04 09:17:26 -05:00
// Put offset in for silence at the beginning
2019-02-04 10:30:19 -05:00
unsigned int pos = SILENCE_LENGTH * IMBE_LENGTH;
2019-02-04 09:17:26 -05:00
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;
2019-02-04 10:30:19 -05:00
::memcpy(m_voiceData + pos, m_imbe + start, length);
2019-02-04 09:17:26 -05:00
pos += length;
}
}
}
unsigned int CVoice::read(unsigned char* data)
{
assert(data != NULL);
if (m_status != VS_SENDING)
return 0U;
2019-02-05 05:20:44 -05:00
unsigned int offset = m_sent * IMBE_LENGTH;
if (offset >= m_voiceLength) {
2019-02-05 02:58:38 -05:00
::memcpy(data, REC80, 17U);
m_timer.stop();
m_voiceLength = 0U;
m_status = VS_NONE;
return 17U;
}
2019-02-04 09:17:26 -05:00
2019-02-05 02:58:38 -05:00
unsigned int count = m_stopWatch.elapsed() / P25_FRAME_TIME;
2019-02-04 09:17:26 -05:00
2019-02-05 02:58:38 -05:00
unsigned int length = 0U;
2019-02-04 09:17:26 -05:00
2019-02-05 02:58:38 -05:00
if (m_sent < count) {
switch (m_n) {
case 0x62U:
::memcpy(data, REC62, 22U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 10U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 22U;
m_n = 0x63U;
break;
case 0x63U:
::memcpy(data, REC63, 14U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 1U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 14U;
m_n = 0x64U;
break;
case 0x64U:
::memcpy(data, REC64, 17U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x65U;
break;
case 0x65U:
::memcpy(data, REC65, 17U);
data[1U] = (m_dstId >> 16) & 0xFFU;
data[2U] = (m_dstId >> 8) & 0xFFU;
data[3U] = (m_dstId >> 0) & 0xFFU;
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x66U;
break;
case 0x66U:
::memcpy(data, REC66, 17U);
data[1U] = (m_srcId >> 16) & 0xFFU;
data[2U] = (m_srcId >> 8) & 0xFFU;
data[3U] = (m_srcId >> 0) & 0xFFU;
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x67U;
break;
case 0x67U:
::memcpy(data, REC67, 17U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x68U;
break;
case 0x68U:
::memcpy(data, REC68, 17U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x69U;
break;
case 0x69U:
::memcpy(data, REC69, 17U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x6AU;
break;
case 0x6AU:
::memcpy(data, REC6A, 16U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 4U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 16U;
m_n = 0x6BU;
break;
case 0x6BU:
::memcpy(data, REC6B, 22U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 10U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 22U;
m_n = 0x6CU;
break;
case 0x6CU:
::memcpy(data, REC6C, 14U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 1U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 14U;
m_n = 0x6DU;
break;
case 0x6DU:
::memcpy(data, REC6D, 17U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x6EU;
break;
case 0x6EU:
::memcpy(data, REC6E, 17U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x6FU;
break;
case 0x6FU:
::memcpy(data, REC6F, 17U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x70U;
break;
case 0x70U:
::memcpy(data, REC70, 17U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x71U;
break;
case 0x71U:
::memcpy(data, REC71, 17U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x72U;
break;
case 0x72U:
::memcpy(data, REC72, 17U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 5U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 17U;
m_n = 0x73U;
break;
default:
::memcpy(data, REC73, 16U);
2019-02-05 05:20:44 -05:00
::memcpy(data + 4U, m_voiceData + offset, IMBE_LENGTH);
2019-02-05 02:58:38 -05:00
length = 16U;
m_n = 0x62U;
break;
2019-02-04 09:17:26 -05:00
}
2019-02-05 02:58:38 -05:00
m_sent++;
2019-02-04 09:17:26 -05:00
}
2019-02-05 02:58:38 -05:00
return length;
2019-02-04 09:17:26 -05:00
}
void CVoice::eof()
{
if (m_voiceLength == 0U)
return;
m_status = VS_WAITING;
m_timer.start();
}
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;
m_sent = 0U;
2019-02-04 10:30:19 -05:00
m_n = 0x62U;
2019-02-04 09:17:26 -05:00
}
}
}