From 1290f9c49eaea3a69b09408b1e09f5ccc536a548 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Wed, 20 May 2020 18:07:57 +0200 Subject: [PATCH 1/5] do not write EOT when network is not set --- FMControl.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/FMControl.cpp b/FMControl.cpp index 18d90c5..0867914 100644 --- a/FMControl.cpp +++ b/FMControl.cpp @@ -41,6 +41,9 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length) assert(data != NULL); assert(length > 0U); + if (m_network == NULL) + return true; + if (data[0U] == TAG_HEADER) return true; @@ -50,8 +53,6 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length) if (data[0U] != TAG_DATA) return false; - if (m_network == NULL) - return true; m_incomingRFAudio.addData(data + 1U, length - 1U); unsigned int bufferLength = m_incomingRFAudio.dataSize(); From ad843e72835e05f2a2b6b60ad42c7ec21770cd6b Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Wed, 20 May 2020 18:07:57 +0200 Subject: [PATCH 2/5] do not write EOT when network is not set --- FMControl.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/FMControl.cpp b/FMControl.cpp index 18d90c5..0867914 100644 --- a/FMControl.cpp +++ b/FMControl.cpp @@ -41,6 +41,9 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length) assert(data != NULL); assert(length > 0U); + if (m_network == NULL) + return true; + if (data[0U] == TAG_HEADER) return true; @@ -50,8 +53,6 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length) if (data[0U] != TAG_DATA) return false; - if (m_network == NULL) - return true; m_incomingRFAudio.addData(data + 1U, length - 1U); unsigned int bufferLength = m_incomingRFAudio.dataSize(); From ed3299a51376a46f5c9c8442c534ff19ea1437fc Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 24 May 2020 07:43:22 +0200 Subject: [PATCH 3/5] Fix unpack, add audio dump --- FMControl.cpp | 37 ++++++++++++++++++++++++++++--------- FMControl.h | 5 +++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/FMControl.cpp b/FMControl.cpp index 0867914..5405a11 100644 --- a/FMControl.cpp +++ b/FMControl.cpp @@ -20,6 +20,11 @@ #include +#if defined(DUMP_RF_AUDIO) +#include +#include +#endif + const float EMPHASIS_GAIN_DB = 0.0F; //Gain needs to be the same for pre an deeemphasis const unsigned int FM_MASK = 0x00000FFFU; @@ -38,6 +43,11 @@ CFMControl::~CFMControl() bool CFMControl::writeModem(const unsigned char* data, unsigned int length) { +#if defined(DUMP_RF_AUDIO) + std::ofstream audiofile; + audiofile.open("audiodump.bin", std::ios::out | std::ios::app | std::ios::binary); +#endif + assert(data != NULL); assert(length > 0U); @@ -69,28 +79,33 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length) // Unpack the serial data into float values. for (unsigned int i = 0U; i < bufferLength; i += 3U) { - unsigned short sample1 = 0U; - unsigned short sample2 = 0U; + short sample1 = 0U; + short sample2 = 0U; unsigned int pack = 0U; unsigned char* packPointer = (unsigned char*)&pack; - packPointer[1U] = bufferData[i]; - packPointer[2U] = bufferData[i + 1U]; - packPointer[3U] = bufferData[i + 2U]; + packPointer[0U] = bufferData[i]; + packPointer[1U] = bufferData[i + 1U]; + packPointer[2U] = bufferData[i + 2U]; - sample2 = short(pack & FM_MASK); - sample1 = short(pack >> 12); + //extract unsigned 12 bit samples to 16 bit signed + sample2 = short(int(pack & FM_MASK) - 2048); + sample1 = short(int(pack >> 12) - 2048); // Convert from unsigned short (0 - +4095) to float (-1.0 - +1.0) - samples[nSamples++] = (float(sample1) - 2048.0F) / 2048.0F; - samples[nSamples++] = (float(sample2) - 2048.0F) / 2048.0F; + samples[nSamples++] = float(sample1) / 2048.0F; + samples[nSamples++] = float(sample2) / 2048.0F; } //De-emphasise the data and any other processing needed (maybe a low-pass filter to remove the CTCSS) for (unsigned int i = 0U; i < nSamples; i++) samples[i] = m_deemphasis.filter(samples[i]); +#if defined(DUMP_RF_AUDIO) + audiofile.write((char*)(void*)samples, nSamples * sizeof(float)); +#endif + unsigned short out[170U]; // 85 * 2 unsigned int nOut = 0U; @@ -104,6 +119,10 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length) return m_network->writeData((unsigned char*)out, nOut); } +#if defined(DUMP_RF_AUDIO) + audiofile.close(); +#endif + return true; } diff --git a/FMControl.h b/FMControl.h index 5a4a8cd..9067d89 100644 --- a/FMControl.h +++ b/FMControl.h @@ -23,6 +23,11 @@ #include "Defines.h" #include "IIRDirectForm1Filter.h" +// Uncomment this to dump audio to a raw audio file +// The file will be written in same folder as executable +// Toplay the file : aplay -f FLOAT_LE -c1 -r8000 -t raw audiodump.bin +//#define DUMP_RF_AUDIO + class CFMControl { public: CFMControl(CFMNetwork* network); From c0a9bb81a30063c046ec80e5c76721094882785c Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 24 May 2020 08:00:27 +0200 Subject: [PATCH 4/5] Fix sample packing --- FMControl.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FMControl.cpp b/FMControl.cpp index 5405a11..79dbdcb 100644 --- a/FMControl.cpp +++ b/FMControl.cpp @@ -167,9 +167,9 @@ unsigned int CFMControl::readModem(unsigned char* data, unsigned int space) pack = ((unsigned int)sample1) << 12; pack |= sample2; - data[j] = packPointer[1U]; - data[j + 1U] = packPointer[2U]; - data[j + 2U] = packPointer[3U]; + data[j] = packPointer[0U]; + data[j + 1U] = packPointer[1U]; + data[j + 2U] = packPointer[2U]; } return j;//return the number of bytes written From 8b31cb34fff4134f919919165e7b2c8bb7be58b8 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 24 May 2020 10:47:11 +0200 Subject: [PATCH 5/5] Use stdio instead of iostream, fix file not properly closed --- FMControl.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/FMControl.cpp b/FMControl.cpp index 79dbdcb..baa429c 100644 --- a/FMControl.cpp +++ b/FMControl.cpp @@ -21,8 +21,7 @@ #include #if defined(DUMP_RF_AUDIO) -#include -#include +#include #endif const float EMPHASIS_GAIN_DB = 0.0F; //Gain needs to be the same for pre an deeemphasis @@ -43,11 +42,6 @@ CFMControl::~CFMControl() bool CFMControl::writeModem(const unsigned char* data, unsigned int length) { -#if defined(DUMP_RF_AUDIO) - std::ofstream audiofile; - audiofile.open("audiodump.bin", std::ios::out | std::ios::app | std::ios::binary); -#endif - assert(data != NULL); assert(length > 0U); @@ -70,6 +64,10 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length) bufferLength = 255U; if (bufferLength >= 3U) { +#if defined(DUMP_RF_AUDIO) + FILE * audiofile = fopen("./audiodump.bin", "ab"); +#endif + bufferLength = bufferLength - bufferLength % 3U; //round down to nearest multiple of 3 unsigned char bufferData[255U]; m_incomingRFAudio.getData(bufferData, bufferLength); @@ -103,7 +101,8 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length) samples[i] = m_deemphasis.filter(samples[i]); #if defined(DUMP_RF_AUDIO) - audiofile.write((char*)(void*)samples, nSamples * sizeof(float)); + if(audiofile != NULL) + fwrite(samples, sizeof(float), nSamples, audiofile); #endif unsigned short out[170U]; // 85 * 2 @@ -116,13 +115,14 @@ bool CFMControl::writeModem(const unsigned char* data, unsigned int length) out[nOut++] = (sample >> 0) & 0xFFU; } +#if defined(DUMP_RF_AUDIO) + if(audiofile != NULL) + fclose(audiofile); +#endif + return m_network->writeData((unsigned char*)out, nOut); } -#if defined(DUMP_RF_AUDIO) - audiofile.close(); -#endif - return true; }