Skip to content

Commit

Permalink
Remove the FM networking sample rate conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Apr 11, 2021
1 parent a5ee428 commit cfe9e0f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 184 deletions.
10 changes: 1 addition & 9 deletions Conf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,11 @@ m_pocsagLocalPort(0U),
m_pocsagNetworkModeHang(3U),
m_pocsagNetworkDebug(false),
m_fmNetworkEnabled(false),
m_fmNetworkProtocol("MMDVM"),
m_fmNetworkProtocol("USRP"),
m_fmGatewayAddress(),
m_fmGatewayPort(0U),
m_fmLocalAddress(),
m_fmLocalPort(0U),
m_fmSampleRate(8000U),
m_fmPreEmphasis(true),
m_fmDeEmphasis(true),
m_fmTXAudioGain(1.0F),
Expand Down Expand Up @@ -1013,8 +1012,6 @@ bool CConf::read()
m_fmGatewayAddress = value;
else if (::strcmp(key, "GatewayPort") == 0)
m_fmGatewayPort = (unsigned int)::atoi(value);
else if (::strcmp(key, "SampleRate") == 0)
m_fmSampleRate = (unsigned int)::atoi(value);
else if (::strcmp(key, "PreEmphasis") == 0)
m_fmPreEmphasis = ::atoi(value) == 1;
else if (::strcmp(key, "DeEmphasis") == 0)
Expand Down Expand Up @@ -2237,11 +2234,6 @@ unsigned int CConf::getFMLocalPort() const
return m_fmLocalPort;
}

unsigned int CConf::getFMSampleRate() const
{
return m_fmSampleRate;
}

bool CConf::getFMPreEmphasis() const
{
return m_fmPreEmphasis;
Expand Down
2 changes: 0 additions & 2 deletions Conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ class CConf
unsigned int getFMGatewayPort() const;
std::string getFMLocalAddress() const;
unsigned int getFMLocalPort() const;
unsigned int getFMSampleRate() const;
bool getFMPreEmphasis() const;
bool getFMDeEmphasis() const;
float getFMTXAudioGain() const;
Expand Down Expand Up @@ -613,7 +612,6 @@ class CConf
unsigned int m_fmGatewayPort;
std::string m_fmLocalAddress;
unsigned int m_fmLocalPort;
unsigned int m_fmSampleRate;
bool m_fmPreEmphasis;
bool m_fmDeEmphasis;
float m_fmTXAudioGain;
Expand Down
161 changes: 9 additions & 152 deletions FMNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,28 @@

const unsigned int BUFFER_LENGTH = 1500U;

CFMNetwork::CFMNetwork(const std::string& protocol, const std::string& localAddress, unsigned int localPort, const std::string& gatewayAddress, unsigned int gatewayPort, unsigned int sampleRate, bool debug) :
m_protocol(FMNP_MMDVM),
CFMNetwork::CFMNetwork(const std::string& protocol, const std::string& localAddress, unsigned int localPort, const std::string& gatewayAddress, unsigned int gatewayPort, bool debug) :
m_protocol(FMNP_USRP),
m_socket(localAddress, localPort),
m_addr(),
m_addrLen(0U),
m_sampleRate(sampleRate),
m_debug(debug),
m_enabled(false),
m_buffer(2000U, "FM Network"),
m_pollTimer(1000U, 5U),
m_seqNo(0U)
{
assert(gatewayPort > 0U);
assert(!gatewayAddress.empty());
assert(sampleRate > 0U);

if (CUDPSocket::lookup(gatewayAddress, gatewayPort, m_addr, m_addrLen) != 0)
m_addrLen = 0U;

if (protocol == "USRP")
m_protocol = FMNP_USRP;

#if !defined(_WIN32) && !defined(_WIN64)
int error;
m_incoming = ::src_new(SRC_SINC_FASTEST, 1, &error);
m_outgoing = ::src_new(SRC_SINC_FASTEST, 1, &error);

assert(m_incoming != NULL);
assert(m_outgoing != NULL);
#endif
// if (protocol == "USRP")
// m_protocol = FMNP_USRP;
}

CFMNetwork::~CFMNetwork()
{
#if !defined(_WIN32) && !defined(_WIN64)
::src_delete(m_incoming);
::src_delete(m_outgoing);
#endif
}

bool CFMNetwork::open()
Expand All @@ -76,8 +60,6 @@ bool CFMNetwork::open()

LogMessage("Opening FM network connection");

m_pollTimer.start();

return m_socket.open(m_addr);
}

Expand All @@ -86,31 +68,6 @@ bool CFMNetwork::writeData(float* data, unsigned int nSamples)
assert(data != NULL);
assert(nSamples > 0U);

#if !defined(_WIN32) && !defined(_WIN64)
assert(m_outgoing != NULL);

float out[1000U];
SRC_DATA src;

if (m_sampleRate != 8000U) {
src.data_in = data;
src.data_out = out;
src.input_frames = nSamples;
src.output_frames = 1000;
src.end_of_input = 0;
src.src_ratio = double(m_sampleRate) / 8000.0;

int ret = ::src_process(m_outgoing, &src);
if (ret != 0) {
LogError("Error up/downsampling of the output audio has an error - %s", src_strerror(ret));
return false;
}
} else {
src.data_out = data;
src.output_frames_gen = nSamples;
}
#endif

unsigned char buffer[1500U];
::memset(buffer, 0x00U, 1500U);

Expand Down Expand Up @@ -159,19 +116,10 @@ bool CFMNetwork::writeData(float* data, unsigned int nSamples)
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
buffer[length++] = 0x00U;
} else {
buffer[length++] = 'F';
buffer[length++] = 'M';
buffer[length++] = 'D';
}

#if defined(_WIN32) || defined(_WIN64)
for (unsigned int i = 0U; i < nSamples; i++) {
short val = short(data[i] * 32767.0F + 0.5F); // Changing audio format from float to S16LE
#else
for (long i = 0L; i < src.output_frames_gen; i++) {
short val = short(src.data_out[i] * 32767.0F + 0.5F); // Changing audio format from float to S16LE
#endif

buffer[length++] = (val >> 0) & 0xFFU;
buffer[length++] = (val >> 8) & 0xFFU;
Expand All @@ -187,31 +135,11 @@ bool CFMNetwork::writeData(float* data, unsigned int nSamples)

bool CFMNetwork::writeEOT()
{
if (m_protocol == FMNP_MMDVM) {
unsigned char buffer[10U];
::memset(buffer, 0x00U, 10U);

buffer[0U] = 'F';
buffer[1U] = 'M';
buffer[2U] = 'E';

if (m_debug)
CUtils::dump(1U, "FM Network End of Transmission Sent", buffer, 3U);

return m_socket.write(buffer, 3U, m_addr, m_addrLen);
}

return true;
}

void CFMNetwork::clock(unsigned int ms)
{
m_pollTimer.clock(ms);
if (m_pollTimer.hasExpired()) {
writePoll();
m_pollTimer.start();
}

unsigned char buffer[BUFFER_LENGTH];

sockaddr_storage addr;
Expand Down Expand Up @@ -248,19 +176,6 @@ void CFMNetwork::clock(unsigned int ms)

if (type == 0U)
m_buffer.addData(buffer + 32U, length - 32U);
} else {
// Ignore incoming polls
if (::memcmp(buffer, "FMP", 3U) == 0)
return;

// Invalid packet type?
if (::memcmp(buffer, "FMD", 3U) != 0)
return;

if (length < 3)
return;

m_buffer.addData(buffer + 3U, length - 3U);
}
}

Expand All @@ -279,58 +194,16 @@ unsigned int CFMNetwork::read(float* data, unsigned int nSamples)
unsigned char buffer[1500U];
m_buffer.getData(buffer, nSamples * sizeof(unsigned short));

#if !defined(_WIN32) && !defined(_WIN64)
assert(m_incoming != NULL);

SRC_DATA src;

if (m_sampleRate != 8000U) {
float in[750U];

for (unsigned int i = 0U; i < nSamples; i++) {
short val = ((buffer[i * 2U + 0U] & 0xFFU) << 0) + // Changing audio format from U16BE to S16LE
((buffer[i * 2U + 1U] & 0xFFU) << 8); // Changing audio format from U16BE to S16LE
in[i] = (float(val) / 65536.0F); // Changing audio format from U16BE to S16LE
}

src.data_in = in;
src.data_out = data;
src.input_frames = nSamples;
src.output_frames = 750;
src.end_of_input = 0;
src.src_ratio = 8000.0 / double(m_sampleRate);

int ret = ::src_process(m_incoming, &src);
if (ret != 0) {
LogError("Error up/downsampling of the input audio has an error - %s", src_strerror(ret));
return 0U;
}

return src.output_frames_gen;
} else {
#endif
for (unsigned int i = 0U; i < nSamples; i++) {
short val = ((buffer[i * 2U + 0U] & 0xFFU) << 0) +
((buffer[i * 2U + 1U] & 0xFFU) << 8);
data[i] = float(val) / 65536.0F;
}

return nSamples;
#if !defined(_WIN32) && !defined(_WIN64)
for (unsigned int i = 0U; i < nSamples; i++) {
short val = ((buffer[i * 2U + 0U] & 0xFFU) << 0) + ((buffer[i * 2U + 1U] & 0xFFU) << 8);
data[i] = float(val) / 65536.0F;
}
#endif

return nSamples;
}

void CFMNetwork::reset()
{
#if !defined(_WIN32) && !defined(_WIN64)
assert(m_incoming != NULL);
assert(m_outgoing != NULL);

::src_reset(m_incoming);
::src_reset(m_outgoing);
#endif

m_buffer.clear();
}

Expand All @@ -350,19 +223,3 @@ void CFMNetwork::enable(bool enabled)

m_enabled = enabled;
}

void CFMNetwork::writePoll()
{
if (m_protocol == FMNP_MMDVM) {
unsigned char buffer[3U];

buffer[0U] = 'F';
buffer[1U] = 'M';
buffer[2U] = 'P';

if (m_debug)
CUtils::dump(1U, "FM Network Poll Sent", buffer, 3U);

m_socket.write(buffer, 3U, m_addr, m_addrLen);
}
}
16 changes: 1 addition & 15 deletions FMNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,17 @@

#include "RingBuffer.h"
#include "UDPSocket.h"
#include "Timer.h"

#if !defined(_WIN32) && !defined(_WIN64)
#include <samplerate.h>
#endif

#include <cstdint>
#include <string>

enum FM_NETWORK_PROTOCOL {
FMNP_MMDVM,
FMNP_USRP
};

class CFMNetwork {
public:
CFMNetwork(const std::string& protocol, const std::string& myAddress, unsigned int myPort, const std::string& gatewayAddress, unsigned int gatewayPort, unsigned int sampleRate, bool debug);
CFMNetwork(const std::string& protocol, const std::string& myAddress, unsigned int myPort, const std::string& gatewayAddress, unsigned int gatewayPort, bool debug);
~CFMNetwork();

bool open();
Expand All @@ -61,18 +55,10 @@ class CFMNetwork {
CUDPSocket m_socket;
sockaddr_storage m_addr;
unsigned int m_addrLen;
unsigned int m_sampleRate;
bool m_debug;
bool m_enabled;
CRingBuffer<unsigned char> m_buffer;
CTimer m_pollTimer;
unsigned int m_seqNo;
#if !defined(_WIN32) && !defined(_WIN64)
SRC_STATE* m_incoming;
SRC_STATE* m_outgoing;
#endif

void writePoll();
};

#endif
4 changes: 1 addition & 3 deletions MMDVM.ini
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,11 @@ Debug=0

[FM Network]
Enable=1
# Values are MMDVM and USRP
Protocol=USRP
# Protocol=USRP
LocalAddress=127.0.0.1
LocalPort=3810
GatewayAddress=127.0.0.1
GatewayPort=4810
SampleRate=8000
PreEmphasis=1
DeEmphasis=1
TXAudioGain=1.0
Expand Down
4 changes: 1 addition & 3 deletions MMDVMHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,6 @@ bool CMMDVMHost::createFMNetwork()
unsigned int gatewayPort = m_conf.getFMGatewayPort();
std::string localAddress = m_conf.getFMLocalAddress();
unsigned int localPort = m_conf.getFMLocalPort();
unsigned int sampleRate = m_conf.getFMSampleRate();
bool preEmphasis = m_conf.getFMPreEmphasis();
bool deEmphasis = m_conf.getFMDeEmphasis();
float txAudioGain = m_conf.getFMTXAudioGain();
Expand All @@ -1819,14 +1818,13 @@ bool CMMDVMHost::createFMNetwork()
LogInfo(" Gateway Port: %u", gatewayPort);
LogInfo(" Local Address: %s", localAddress.c_str());
LogInfo(" Local Port: %u", localPort);
LogInfo(" Sample Rate: %u", sampleRate);
LogInfo(" Pre-Emphasis: %s", preEmphasis ? "yes" : "no");
LogInfo(" De-Emphasis: %s", deEmphasis ? "yes" : "no");
LogInfo(" TX Audio Gain: %.2f", txAudioGain);
LogInfo(" RX Audio Gain: %.2f", rxAudioGain);
LogInfo(" Mode Hang: %us", m_fmNetModeHang);

m_fmNetwork = new CFMNetwork(protocol, localAddress, localPort, gatewayAddress, gatewayPort, sampleRate, debug);
m_fmNetwork = new CFMNetwork(protocol, localAddress, localPort, gatewayAddress, gatewayPort, debug);

bool ret = m_fmNetwork->open();
if (!ret) {
Expand Down

0 comments on commit cfe9e0f

Please sign in to comment.