diff --git a/FMControl.cpp b/FMControl.cpp index 18d90c5..baa429c 100644 --- a/FMControl.cpp +++ b/FMControl.cpp @@ -20,6 +20,10 @@ #include +#if defined(DUMP_RF_AUDIO) +#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; @@ -41,6 +45,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 +57,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(); @@ -59,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); @@ -68,28 +77,34 @@ 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) + if(audiofile != NULL) + fwrite(samples, sizeof(float), nSamples, audiofile); +#endif + unsigned short out[170U]; // 85 * 2 unsigned int nOut = 0U; @@ -100,6 +115,11 @@ 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); } @@ -147,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 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);