2020-05-07 20:49:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "FMControl.h"
|
|
|
|
|
|
|
|
#include <string>
|
2020-05-08 12:12:37 +00:00
|
|
|
|
2020-05-24 05:43:22 +00:00
|
|
|
#if defined(DUMP_RF_AUDIO)
|
2020-05-24 08:47:11 +00:00
|
|
|
#include <stdio.h>
|
2020-05-24 05:43:22 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-24 17:07:59 +00:00
|
|
|
const float EMPHASIS_GAIN_DB = 0.0F; //Gain needs to be the same for pre an deeemphasis
|
|
|
|
const float FILTER_GAIN_DB = 0.0F;
|
|
|
|
const unsigned int FM_MASK = 0x00000FFFU;
|
2020-05-07 20:49:18 +00:00
|
|
|
|
|
|
|
CFMControl::CFMControl(CFMNetwork* network) :
|
|
|
|
m_network(network),
|
2020-05-08 19:16:02 +00:00
|
|
|
m_enabled(false),
|
2020-05-14 20:01:10 +00:00
|
|
|
m_incomingRFAudio(1600U, "Incoming RF FM Audio"),
|
2020-05-24 17:07:59 +00:00
|
|
|
m_preemphasis (NULL),
|
|
|
|
m_deemphasis (NULL),
|
|
|
|
m_filterStage1(NULL),
|
|
|
|
m_filterStage2(NULL),
|
|
|
|
m_filterStage3(NULL)
|
2020-05-07 20:49:18 +00:00
|
|
|
{
|
2020-05-24 17:07:59 +00:00
|
|
|
m_preemphasis = new CIIRDirectForm1Filter(0.38897032f, -0.32900053f, 0.0f, 1.0f, 0.28202918f, 0.0f, EMPHASIS_GAIN_DB);
|
|
|
|
m_deemphasis = new CIIRDirectForm1Filter(1.0f,0.28202918f, 0.0f, 0.38897032f, -0.32900053f, 0.0f, EMPHASIS_GAIN_DB);
|
|
|
|
|
2020-05-30 05:31:48 +00:00
|
|
|
//cheby type 1 0.2dB cheby type 1 3rd order 300-2700Hz fs=8000
|
2020-05-24 17:07:59 +00:00
|
|
|
m_filterStage1 = new CIIRDirectForm1Filter(0.29495028f, 0.0f, -0.29495028f, 1.0f, -0.61384624f, -0.057158668f, FILTER_GAIN_DB);
|
|
|
|
m_filterStage2 = new CIIRDirectForm1Filter(1.0f, 2.0f, 1.0f, 1.0f, 0.9946123f, 0.6050482f, FILTER_GAIN_DB);
|
|
|
|
m_filterStage3 = new CIIRDirectForm1Filter(1.0f, -2.0f, 1.0f, 1.0f, -1.8414584f, 0.8804949f, FILTER_GAIN_DB);
|
2020-05-07 20:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CFMControl::~CFMControl()
|
|
|
|
{
|
2020-05-24 17:07:59 +00:00
|
|
|
delete m_preemphasis ;
|
|
|
|
delete m_deemphasis ;
|
|
|
|
|
|
|
|
delete m_filterStage1;
|
|
|
|
delete m_filterStage2;
|
|
|
|
delete m_filterStage3;
|
2020-05-07 20:49:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 12:12:37 +00:00
|
|
|
bool CFMControl::writeModem(const unsigned char* data, unsigned int length)
|
2020-05-07 20:49:18 +00:00
|
|
|
{
|
|
|
|
assert(data != NULL);
|
2020-05-08 12:12:37 +00:00
|
|
|
assert(length > 0U);
|
2020-05-10 08:31:53 +00:00
|
|
|
|
2020-05-20 16:07:57 +00:00
|
|
|
if (m_network == NULL)
|
|
|
|
return true;
|
|
|
|
|
2020-05-11 11:59:28 +00:00
|
|
|
if (data[0U] == TAG_HEADER)
|
|
|
|
return true;
|
|
|
|
|
2020-05-15 18:19:57 +00:00
|
|
|
if (data[0U] == TAG_EOT)
|
2020-05-16 12:09:28 +00:00
|
|
|
return m_network->writeEOT();
|
2020-05-15 18:19:57 +00:00
|
|
|
|
2020-05-11 11:59:28 +00:00
|
|
|
if (data[0U] != TAG_DATA)
|
2020-05-10 08:31:53 +00:00
|
|
|
return false;
|
2020-05-08 12:12:37 +00:00
|
|
|
|
2020-05-11 11:59:28 +00:00
|
|
|
m_incomingRFAudio.addData(data + 1U, length - 1U);
|
2020-05-10 19:49:11 +00:00
|
|
|
unsigned int bufferLength = m_incomingRFAudio.dataSize();
|
2020-05-16 12:09:28 +00:00
|
|
|
if (bufferLength > 255U)
|
2020-05-11 16:38:07 +00:00
|
|
|
bufferLength = 255U;
|
2020-05-08 12:12:37 +00:00
|
|
|
|
2020-05-11 11:59:28 +00:00
|
|
|
if (bufferLength >= 3U) {
|
2020-05-24 08:47:11 +00:00
|
|
|
#if defined(DUMP_RF_AUDIO)
|
|
|
|
FILE * audiofile = fopen("./audiodump.bin", "ab");
|
|
|
|
#endif
|
|
|
|
|
2020-05-11 11:59:28 +00:00
|
|
|
bufferLength = bufferLength - bufferLength % 3U; //round down to nearest multiple of 3
|
2020-05-14 20:01:10 +00:00
|
|
|
unsigned char bufferData[255U];
|
2020-05-10 19:49:11 +00:00
|
|
|
m_incomingRFAudio.getData(bufferData, bufferLength);
|
2020-05-14 20:01:10 +00:00
|
|
|
|
|
|
|
unsigned int nSamples = 0;
|
|
|
|
float samples[85U]; // 255 / 3;
|
2020-05-08 12:12:37 +00:00
|
|
|
|
2020-05-10 19:49:11 +00:00
|
|
|
// Unpack the serial data into float values.
|
2020-05-10 20:08:36 +00:00
|
|
|
for (unsigned int i = 0U; i < bufferLength; i += 3U) {
|
2020-05-24 05:43:22 +00:00
|
|
|
short sample1 = 0U;
|
|
|
|
short sample2 = 0U;
|
2020-05-08 12:12:37 +00:00
|
|
|
|
2020-05-10 19:49:11 +00:00
|
|
|
unsigned int pack = 0U;
|
|
|
|
unsigned char* packPointer = (unsigned char*)&pack;
|
2020-05-08 12:12:37 +00:00
|
|
|
|
2020-05-24 05:43:22 +00:00
|
|
|
packPointer[0U] = bufferData[i];
|
|
|
|
packPointer[1U] = bufferData[i + 1U];
|
|
|
|
packPointer[2U] = bufferData[i + 2U];
|
2020-05-10 19:49:11 +00:00
|
|
|
|
2020-05-24 05:43:22 +00:00
|
|
|
//extract unsigned 12 bit samples to 16 bit signed
|
|
|
|
sample2 = short(int(pack & FM_MASK) - 2048);
|
|
|
|
sample1 = short(int(pack >> 12) - 2048);
|
2020-05-10 19:49:11 +00:00
|
|
|
|
|
|
|
// Convert from unsigned short (0 - +4095) to float (-1.0 - +1.0)
|
2020-05-24 05:43:22 +00:00
|
|
|
samples[nSamples++] = float(sample1) / 2048.0F;
|
|
|
|
samples[nSamples++] = float(sample2) / 2048.0F;
|
2020-05-10 19:49:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 05:31:48 +00:00
|
|
|
//De-emphasise the data and remove CTCSS
|
2020-05-24 17:07:59 +00:00
|
|
|
for (unsigned int i = 0U; i < nSamples; i++) {
|
|
|
|
samples[i] = m_deemphasis->filter(samples[i]);
|
|
|
|
samples[i] = m_filterStage3->filter(m_filterStage2->filter(m_filterStage1->filter(samples[i])));
|
|
|
|
}
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-24 05:43:22 +00:00
|
|
|
#if defined(DUMP_RF_AUDIO)
|
2020-05-24 08:47:11 +00:00
|
|
|
if(audiofile != NULL)
|
|
|
|
fwrite(samples, sizeof(float), nSamples, audiofile);
|
2020-05-24 05:43:22 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-14 20:01:10 +00:00
|
|
|
unsigned short out[170U]; // 85 * 2
|
2020-05-10 19:49:11 +00:00
|
|
|
unsigned int nOut = 0U;
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-10 19:49:11 +00:00
|
|
|
// Repack the data (8-bit unsigned values containing unsigned 16-bit data)
|
|
|
|
for (unsigned int i = 0U; i < nSamples; i++) {
|
|
|
|
unsigned short sample = (unsigned short)((samples[i] + 1.0F) * 32767.0F + 0.5F);
|
|
|
|
out[nOut++] = (sample >> 8) & 0xFFU;
|
|
|
|
out[nOut++] = (sample >> 0) & 0xFFU;
|
|
|
|
}
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-24 05:43:22 +00:00
|
|
|
#if defined(DUMP_RF_AUDIO)
|
2020-05-24 08:47:11 +00:00
|
|
|
if(audiofile != NULL)
|
|
|
|
fclose(audiofile);
|
2020-05-24 05:43:22 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-24 08:47:11 +00:00
|
|
|
return m_network->writeData((unsigned char*)out, nOut);
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:59:28 +00:00
|
|
|
return true;
|
2020-05-07 20:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CFMControl::readModem(unsigned char* data, unsigned int space)
|
|
|
|
{
|
|
|
|
assert(data != NULL);
|
|
|
|
assert(space > 0U);
|
2020-05-11 11:59:28 +00:00
|
|
|
|
|
|
|
if (m_network == NULL)
|
|
|
|
return 0U;
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-14 20:01:10 +00:00
|
|
|
if(space > 252U)
|
|
|
|
space = 252U;
|
|
|
|
|
|
|
|
unsigned char netData[168U];//84 * 2 modem can handle up to 84 samples (252 bytes) at a time
|
|
|
|
unsigned int length = m_network->read(netData, 168U);
|
2020-05-08 12:12:37 +00:00
|
|
|
if (length == 0U)
|
|
|
|
return 0U;
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-14 20:01:10 +00:00
|
|
|
float samples[84U];
|
2020-05-08 12:12:37 +00:00
|
|
|
unsigned int nSamples = 0U;
|
|
|
|
// Convert the unsigned 16-bit data (+65535 - 0) to float (+1.0 - -1.0)
|
|
|
|
for (unsigned int i = 0U; i < length; i += 2U) {
|
|
|
|
unsigned short sample = (netData[i + 0U] << 8) | netData[i + 1U];
|
2020-05-30 05:31:48 +00:00
|
|
|
samples[nSamples++] = (float(sample) / 32768.0F) - 1.0F;
|
2020-05-08 12:12:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 20:01:10 +00:00
|
|
|
//Pre-emphasise the data and other stuff.
|
2020-05-30 05:31:48 +00:00
|
|
|
//for (unsigned int i = 0U; i < nSamples; i++)
|
|
|
|
// samples[i] = m_preemphasis->filter(samples[i]);
|
2020-05-08 12:12:37 +00:00
|
|
|
|
|
|
|
// Pack the floating point data (+1.0 to -1.0) to packed 12-bit samples (+2047 - -2048)
|
2020-05-08 15:05:04 +00:00
|
|
|
unsigned int pack = 0U;
|
|
|
|
unsigned char* packPointer = (unsigned char*)&pack;
|
2020-05-08 14:41:06 +00:00
|
|
|
unsigned int j = 0U;
|
|
|
|
unsigned int i = 0U;
|
2020-05-08 15:05:04 +00:00
|
|
|
for (; i < nSamples && j < space; i += 2U, j += 3U) {
|
2020-05-08 14:41:06 +00:00
|
|
|
unsigned short sample1 = (unsigned short)((samples[i] + 1.0F) * 2048.0F + 0.5F);
|
|
|
|
unsigned short sample2 = (unsigned short)((samples[i + 1] + 1.0F) * 2048.0F + 0.5F);
|
|
|
|
|
2020-05-30 05:31:48 +00:00
|
|
|
pack = 0U;
|
2020-05-08 15:05:04 +00:00
|
|
|
pack = ((unsigned int)sample1) << 12;
|
2020-05-08 14:41:06 +00:00
|
|
|
pack |= sample2;
|
|
|
|
|
2020-05-24 06:00:27 +00:00
|
|
|
data[j] = packPointer[0U];
|
|
|
|
data[j + 1U] = packPointer[1U];
|
|
|
|
data[j + 2U] = packPointer[2U];
|
2020-05-08 12:12:37 +00:00
|
|
|
}
|
2020-05-07 20:49:18 +00:00
|
|
|
|
2020-05-08 14:41:06 +00:00
|
|
|
return j;//return the number of bytes written
|
2020-05-07 20:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFMControl::clock(unsigned int ms)
|
|
|
|
{
|
|
|
|
// May not be needed
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFMControl::enable(bool enabled)
|
|
|
|
{
|
|
|
|
// May not be needed
|
|
|
|
}
|