2015-12-06 13:47:55 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2015 F4EXB //
|
|
|
|
// written by Edouard Griffiths //
|
2015-11-18 21:27:37 -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 as version 3 of the License, or //
|
|
|
|
// //
|
|
|
|
// 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 V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-12-01 20:28:31 -05:00
|
|
|
#include <QUdpSocket>
|
2015-12-02 22:02:21 -05:00
|
|
|
#include <QHostAddress>
|
2017-08-17 18:10:15 -04:00
|
|
|
|
|
|
|
#include "dsp/downchannelizer.h"
|
2015-12-02 22:02:21 -05:00
|
|
|
#include "dsp/dspengine.h"
|
2017-08-17 18:10:15 -04:00
|
|
|
#include "util/db.h"
|
2016-10-02 07:18:07 -04:00
|
|
|
|
2017-05-19 23:48:39 -04:00
|
|
|
#include "udpsrcgui.h"
|
2017-08-17 18:10:15 -04:00
|
|
|
#include "udpsrc.h"
|
2015-11-18 21:27:37 -05:00
|
|
|
|
|
|
|
MESSAGE_CLASS_DEFINITION(UDPSrc::MsgUDPSrcConfigure, Message)
|
2015-12-03 21:46:51 -05:00
|
|
|
MESSAGE_CLASS_DEFINITION(UDPSrc::MsgUDPSrcConfigureImmediate, Message)
|
2015-11-18 21:27:37 -05:00
|
|
|
MESSAGE_CLASS_DEFINITION(UDPSrc::MsgUDPSrcSpectrum, Message)
|
|
|
|
|
2016-10-02 16:29:04 -04:00
|
|
|
UDPSrc::UDPSrc(MessageQueue* uiMessageQueue, UDPSrcGUI* udpSrcGUI, BasebandSampleSink* spectrum) :
|
2017-08-17 14:23:17 -04:00
|
|
|
m_outMovingAverage(480, 1e-10),
|
|
|
|
m_inMovingAverage(480, 1e-10),
|
2017-08-19 19:56:23 -04:00
|
|
|
m_amMovingAverage(1200, 1e-10),
|
2017-05-25 14:13:34 -04:00
|
|
|
m_audioFifo(4, 24000),
|
2017-08-17 18:10:15 -04:00
|
|
|
m_squelchOpen(false),
|
|
|
|
m_squelchOpenCount(0),
|
|
|
|
m_squelchCloseCount(0),
|
|
|
|
m_squelchThreshold(4800),
|
2017-08-19 19:56:23 -04:00
|
|
|
m_agc(9600, m_agcTarget, 1e-6),
|
2017-05-25 14:13:34 -04:00
|
|
|
m_settingsMutex(QMutex::Recursive)
|
2015-11-18 21:27:37 -05:00
|
|
|
{
|
2015-11-19 02:55:58 -05:00
|
|
|
setObjectName("UDPSrc");
|
2015-11-18 21:27:37 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
m_udpBuffer = new UDPSink<Sample>(this, udpBlockSize, m_config.m_udpPort);
|
|
|
|
m_udpBufferMono = new UDPSink<FixReal>(this, udpBlockSize, m_config.m_udpPort);
|
2015-12-02 22:02:21 -05:00
|
|
|
m_audioSocket = new QUdpSocket(this);
|
2016-04-06 03:33:29 -04:00
|
|
|
m_udpAudioBuf = new char[m_udpAudioPayloadSize];
|
2015-12-02 22:02:21 -05:00
|
|
|
|
|
|
|
m_audioBuffer.resize(1<<9);
|
|
|
|
m_audioBufferFill = 0;
|
2015-12-01 20:28:31 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
m_nco.setFreq(0, m_config.m_inputSampleRate);
|
|
|
|
m_interpolator.create(16, m_config.m_inputSampleRate, m_config.m_rfBandwidth / 2.0);
|
|
|
|
m_sampleDistanceRemain = m_config.m_inputSampleRate / m_config.m_outputSampleRate;
|
2015-11-18 21:27:37 -05:00
|
|
|
m_uiMessageQueue = uiMessageQueue;
|
2015-11-19 02:55:58 -05:00
|
|
|
m_udpSrcGUI = udpSrcGUI;
|
2015-11-18 21:27:37 -05:00
|
|
|
m_spectrum = spectrum;
|
|
|
|
m_spectrumEnabled = false;
|
|
|
|
m_nextSSBId = 0;
|
|
|
|
m_nextS16leId = 0;
|
|
|
|
|
|
|
|
m_last = 0;
|
|
|
|
m_this = 0;
|
|
|
|
m_scale = 0;
|
|
|
|
m_magsq = 0;
|
2017-08-17 14:23:17 -04:00
|
|
|
m_inMagsq = 0;
|
2017-08-18 18:12:56 -04:00
|
|
|
UDPFilter = new fftfilt(0.0, (m_config.m_rfBandwidth / 2.0) / m_config.m_outputSampleRate, udpBlockSize);
|
2015-12-02 22:02:21 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
m_phaseDiscri.setFMScaling((float) m_config. m_outputSampleRate / (2.0f * m_config.m_fmDeviation));
|
2016-04-03 21:44:06 -04:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
if (m_audioSocket->bind(QHostAddress::LocalHost, m_config.m_audioPort))
|
2015-12-02 22:02:21 -05:00
|
|
|
{
|
2017-08-18 18:12:56 -04:00
|
|
|
qDebug("UDPSrc::UDPSrc: bind audio socket to port %d", m_config.m_audioPort);
|
2016-04-06 03:33:29 -04:00
|
|
|
connect(m_audioSocket, SIGNAL(readyRead()), this, SLOT(audioReadyRead()), Qt::QueuedConnection);
|
2015-12-02 22:02:21 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("UDPSrc::UDPSrc: cannot bind audio port");
|
|
|
|
}
|
|
|
|
|
2017-08-19 18:32:40 -04:00
|
|
|
m_agc.setClampMax(32768.0*32768.0);
|
|
|
|
m_agc.setClamping(true);
|
|
|
|
|
2015-12-03 21:46:51 -05:00
|
|
|
//DSPEngine::instance()->addAudioSink(&m_audioFifo);
|
2015-11-18 21:27:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
UDPSrc::~UDPSrc()
|
|
|
|
{
|
2015-12-02 22:02:21 -05:00
|
|
|
delete m_audioSocket;
|
2016-04-03 05:29:11 -04:00
|
|
|
delete m_udpBuffer;
|
2016-04-05 11:02:24 -04:00
|
|
|
delete m_udpBufferMono;
|
2016-04-06 03:33:29 -04:00
|
|
|
delete[] m_udpAudioBuf;
|
2015-11-19 02:55:58 -05:00
|
|
|
if (UDPFilter) delete UDPFilter;
|
2017-08-18 18:12:56 -04:00
|
|
|
if (m_running.m_audioActive) DSPEngine::instance()->removeAudioSink(&m_audioFifo);
|
2015-11-18 21:27:37 -05:00
|
|
|
}
|
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
/** what needs the "apply" button validation */
|
2015-12-03 21:46:51 -05:00
|
|
|
void UDPSrc::configure(MessageQueue* messageQueue,
|
|
|
|
SampleFormat sampleFormat,
|
|
|
|
Real outputSampleRate,
|
|
|
|
Real rfBandwidth,
|
2016-04-03 21:44:06 -04:00
|
|
|
int fmDeviation,
|
2015-12-03 21:46:51 -05:00
|
|
|
QString& udpAddress,
|
|
|
|
int udpPort,
|
2017-08-18 18:12:56 -04:00
|
|
|
int audioPort,
|
|
|
|
bool force)
|
2015-11-18 21:27:37 -05:00
|
|
|
{
|
2015-12-03 21:46:51 -05:00
|
|
|
Message* cmd = MsgUDPSrcConfigure::create(sampleFormat,
|
|
|
|
outputSampleRate,
|
|
|
|
rfBandwidth,
|
2016-04-03 21:44:06 -04:00
|
|
|
fmDeviation,
|
2015-12-03 21:46:51 -05:00
|
|
|
udpAddress,
|
|
|
|
udpPort,
|
2017-08-18 18:12:56 -04:00
|
|
|
audioPort,
|
|
|
|
force);
|
2015-12-03 21:46:51 -05:00
|
|
|
messageQueue->push(cmd);
|
|
|
|
}
|
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
/** changes applied immediately */
|
2015-12-03 21:46:51 -05:00
|
|
|
void UDPSrc::configureImmediate(MessageQueue* messageQueue,
|
2015-12-05 06:03:26 -05:00
|
|
|
bool audioActive,
|
2015-12-05 06:23:55 -05:00
|
|
|
bool audioStereo,
|
2017-08-17 06:57:45 -04:00
|
|
|
Real boost,
|
2017-08-17 14:23:17 -04:00
|
|
|
int volume,
|
|
|
|
Real squelchDB,
|
2017-08-18 18:12:56 -04:00
|
|
|
Real squelchGate,
|
|
|
|
bool squelchEnabled,
|
2017-08-19 18:32:40 -04:00
|
|
|
bool agc,
|
2017-08-18 18:12:56 -04:00
|
|
|
bool force)
|
2015-12-03 21:46:51 -05:00
|
|
|
{
|
|
|
|
Message* cmd = MsgUDPSrcConfigureImmediate::create(
|
2015-12-05 06:03:26 -05:00
|
|
|
audioActive,
|
2015-12-05 06:23:55 -05:00
|
|
|
audioStereo,
|
2015-12-03 21:46:51 -05:00
|
|
|
boost,
|
2017-08-17 14:23:17 -04:00
|
|
|
volume,
|
|
|
|
squelchDB,
|
2017-08-18 18:12:56 -04:00
|
|
|
squelchGate,
|
|
|
|
squelchEnabled,
|
2017-08-19 18:32:40 -04:00
|
|
|
agc,
|
2017-08-18 18:12:56 -04:00
|
|
|
force);
|
2015-11-18 21:27:37 -05:00
|
|
|
messageQueue->push(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSrc::setSpectrum(MessageQueue* messageQueue, bool enabled)
|
|
|
|
{
|
|
|
|
Message* cmd = MsgUDPSrcSpectrum::create(enabled);
|
|
|
|
messageQueue->push(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSrc::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly)
|
|
|
|
{
|
|
|
|
Complex ci;
|
|
|
|
fftfilt::cmplx* sideband;
|
2017-08-17 18:10:15 -04:00
|
|
|
double l, r;
|
2015-11-18 21:27:37 -05:00
|
|
|
|
|
|
|
m_sampleBuffer.clear();
|
|
|
|
m_settingsMutex.lock();
|
|
|
|
|
2016-04-03 05:29:11 -04:00
|
|
|
for(SampleVector::const_iterator it = begin; it < end; ++it)
|
|
|
|
{
|
2015-11-18 21:27:37 -05:00
|
|
|
Complex c(it->real(), it->imag());
|
|
|
|
c *= m_nco.nextIQ();
|
|
|
|
|
2016-10-09 19:53:32 -04:00
|
|
|
if(m_interpolator.decimate(&m_sampleDistanceRemain, c, &ci))
|
2015-11-18 21:27:37 -05:00
|
|
|
{
|
2017-08-19 18:32:40 -04:00
|
|
|
double inMagSq;
|
|
|
|
double agcFactor = 1.0;
|
|
|
|
|
|
|
|
if ((m_running.m_agc) &&
|
|
|
|
(m_running.m_sampleFormat != FormatNFM) &&
|
|
|
|
(m_running.m_sampleFormat != FormatNFMMono) &&
|
|
|
|
(m_running.m_sampleFormat != FormatS16LE))
|
|
|
|
{
|
|
|
|
agcFactor = m_agc.feedAndGetValue(ci);
|
|
|
|
inMagSq = m_agc.getMagSq();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inMagSq = ci.real()*ci.real() + ci.imag()*ci.imag();
|
|
|
|
}
|
2017-08-17 18:10:15 -04:00
|
|
|
|
2017-08-17 14:23:17 -04:00
|
|
|
m_inMovingAverage.feed(inMagSq / (1<<30));
|
|
|
|
m_inMagsq = m_inMovingAverage.average();
|
|
|
|
|
2017-08-17 18:10:15 -04:00
|
|
|
Sample ss(ci.real(), ci.imag());
|
|
|
|
m_sampleBuffer.push_back(ss);
|
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
m_sampleDistanceRemain += m_running.m_inputSampleRate / m_running.m_outputSampleRate;
|
2015-11-18 21:27:37 -05:00
|
|
|
|
2017-08-17 18:10:15 -04:00
|
|
|
calculateSquelch(m_inMagsq);
|
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
if (m_running.m_sampleFormat == FormatLSB) // binaural LSB
|
2016-04-05 11:02:24 -04:00
|
|
|
{
|
2017-08-19 18:32:40 -04:00
|
|
|
ci *= agcFactor;
|
2016-04-05 11:02:24 -04:00
|
|
|
int n_out = UDPFilter->runSSB(ci, &sideband, false);
|
|
|
|
|
|
|
|
if (n_out)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < n_out; i++)
|
|
|
|
{
|
2017-08-18 18:12:56 -04:00
|
|
|
l = m_squelchOpen ? sideband[i].real() * m_running.m_gain : 0;
|
|
|
|
r = m_squelchOpen ? sideband[i].imag() * m_running.m_gain : 0;
|
2017-08-17 18:10:15 -04:00
|
|
|
m_udpBuffer->write(Sample(l, r));
|
|
|
|
m_outMovingAverage.feed((l*l + r*r) / (1<<30));
|
2016-04-05 11:02:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 18:12:56 -04:00
|
|
|
if (m_running.m_sampleFormat == FormatUSB) // binaural USB
|
2015-12-01 20:28:31 -05:00
|
|
|
{
|
2017-08-19 18:32:40 -04:00
|
|
|
ci *= agcFactor;
|
2016-04-03 05:29:11 -04:00
|
|
|
int n_out = UDPFilter->runSSB(ci, &sideband, true);
|
|
|
|
|
|
|
|
if (n_out)
|
2015-12-01 20:28:31 -05:00
|
|
|
{
|
2016-04-05 11:02:24 -04:00
|
|
|
for (int i = 0; i < n_out; i++)
|
2016-04-03 05:29:11 -04:00
|
|
|
{
|
2017-08-18 18:12:56 -04:00
|
|
|
l = m_squelchOpen ? sideband[i].real() * m_running.m_gain : 0;
|
|
|
|
r = m_squelchOpen ? sideband[i].imag() * m_running.m_gain : 0;
|
2016-04-03 05:29:11 -04:00
|
|
|
m_udpBuffer->write(Sample(l, r));
|
2017-08-17 18:10:15 -04:00
|
|
|
m_outMovingAverage.feed((l*l + r*r) / (1<<30));
|
2016-04-03 05:29:11 -04:00
|
|
|
}
|
2015-11-18 21:27:37 -05:00
|
|
|
}
|
|
|
|
}
|
2017-08-18 18:12:56 -04:00
|
|
|
else if (m_running.m_sampleFormat == FormatNFM)
|
2015-12-01 20:28:31 -05:00
|
|
|
{
|
2017-08-18 18:12:56 -04:00
|
|
|
double demod = m_squelchOpen ? 32768.0 * m_phaseDiscri.phaseDiscriminator(ci) * m_running.m_gain : 0;
|
2016-04-04 16:28:56 -04:00
|
|
|
m_udpBuffer->write(Sample(demod, demod));
|
2017-08-17 18:10:15 -04:00
|
|
|
m_outMovingAverage.feed((demod * demod) / (1<<30));
|
2016-04-03 05:29:11 -04:00
|
|
|
}
|
2017-08-18 18:12:56 -04:00
|
|
|
else if (m_running.m_sampleFormat == FormatNFMMono)
|
2016-04-05 11:02:24 -04:00
|
|
|
{
|
2017-08-18 18:12:56 -04:00
|
|
|
FixReal demod = m_squelchOpen ? (FixReal) (32768.0f * m_phaseDiscri.phaseDiscriminator(ci) * m_running.m_gain) : 0;
|
2016-04-05 11:02:24 -04:00
|
|
|
m_udpBufferMono->write(demod);
|
2017-08-17 18:10:15 -04:00
|
|
|
m_outMovingAverage.feed((demod * demod) / 1073741824.0);
|
2016-04-05 11:02:24 -04:00
|
|
|
}
|
2017-08-18 18:12:56 -04:00
|
|
|
else if (m_running.m_sampleFormat == FormatLSBMono) // Monaural LSB
|
2016-04-05 11:02:24 -04:00
|
|
|
{
|
2017-08-19 18:32:40 -04:00
|
|
|
ci *= agcFactor;
|
2016-04-05 11:02:24 -04:00
|
|
|
int n_out = UDPFilter->runSSB(ci, &sideband, false);
|
|
|
|
|
|
|
|
if (n_out)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < n_out; i++)
|
|
|
|
{
|
2017-08-18 18:12:56 -04:00
|
|
|
l = m_squelchOpen ? (sideband[i].real() + sideband[i].imag()) * 0.7 * m_running.m_gain : 0;
|
2016-04-05 11:02:24 -04:00
|
|
|
m_udpBufferMono->write(l);
|
2017-08-17 18:10:15 -04:00
|
|
|
m_outMovingAverage.feed((l * l) / (1<<30));
|
2016-04-05 11:02:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 18:12:56 -04:00
|
|
|
else if (m_running.m_sampleFormat == FormatUSBMono) // Monaural USB
|
2016-04-05 11:02:24 -04:00
|
|
|
{
|
2017-08-19 18:32:40 -04:00
|
|
|
ci *= agcFactor;
|
2016-04-05 11:02:24 -04:00
|
|
|
int n_out = UDPFilter->runSSB(ci, &sideband, true);
|
|
|
|
|
|
|
|
if (n_out)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < n_out; i++)
|
|
|
|
{
|
2017-08-18 18:12:56 -04:00
|
|
|
l = m_squelchOpen ? (sideband[i].real() + sideband[i].imag()) * 0.7 * m_running.m_gain : 0;
|
2016-04-05 11:02:24 -04:00
|
|
|
m_udpBufferMono->write(l);
|
2017-08-17 18:10:15 -04:00
|
|
|
m_outMovingAverage.feed((l * l) / (1<<30));
|
2016-04-05 11:02:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 18:12:56 -04:00
|
|
|
else if (m_running.m_sampleFormat == FormatAMMono)
|
2016-04-05 11:02:24 -04:00
|
|
|
{
|
2017-08-19 18:32:40 -04:00
|
|
|
FixReal demod = m_squelchOpen ? (FixReal) (sqrt(inMagSq) * agcFactor * m_running.m_gain) : 0;
|
2016-04-05 11:02:24 -04:00
|
|
|
m_udpBufferMono->write(demod);
|
2017-08-19 18:32:40 -04:00
|
|
|
m_outMovingAverage.feed((demod * demod) / 1073741824.0);
|
2016-04-05 11:02:24 -04:00
|
|
|
}
|
2017-08-19 18:32:40 -04:00
|
|
|
else if (m_running.m_sampleFormat == FormatAMNoDCMono)
|
|
|
|
{
|
|
|
|
m_amMovingAverage.feed(inMagSq);
|
|
|
|
|
|
|
|
if (m_squelchOpen)
|
|
|
|
{
|
|
|
|
FixReal demod = (FixReal) ((sqrt(inMagSq) - sqrt(m_amMovingAverage.average())) * agcFactor * m_running.m_gain);
|
|
|
|
m_udpBufferMono->write(demod);
|
|
|
|
m_outMovingAverage.feed((demod * demod) / 1073741824.0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_udpBufferMono->write(0);
|
|
|
|
m_outMovingAverage.feed(0);
|
|
|
|
}
|
2017-08-20 16:18:33 -04:00
|
|
|
}
|
|
|
|
else if (m_running.m_sampleFormat == FormatAMBPFMono)
|
|
|
|
{
|
|
|
|
if (m_squelchOpen)
|
|
|
|
{
|
|
|
|
double demodf = sqrt(inMagSq);
|
|
|
|
demodf = m_bandpass.filter(demodf);
|
|
|
|
demodf /= 301.0;
|
|
|
|
FixReal demod = (FixReal) (demodf * agcFactor * m_running.m_gain);
|
|
|
|
m_udpBufferMono->write(demod);
|
|
|
|
m_outMovingAverage.feed((demod * demod) / 1073741824.0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_udpBufferMono->write(0);
|
|
|
|
m_outMovingAverage.feed(0);
|
|
|
|
}
|
2017-08-19 18:32:40 -04:00
|
|
|
}
|
2016-04-03 05:29:11 -04:00
|
|
|
else // Raw I/Q samples
|
|
|
|
{
|
2017-08-17 18:10:15 -04:00
|
|
|
if (m_squelchOpen)
|
|
|
|
{
|
2017-08-18 18:12:56 -04:00
|
|
|
Sample s(ci.real() * m_running.m_gain, ci.imag() * m_running.m_gain);
|
2017-08-17 18:10:15 -04:00
|
|
|
m_udpBuffer->write(s);
|
2017-08-18 18:12:56 -04:00
|
|
|
m_outMovingAverage.feed((inMagSq*m_running.m_gain*m_running.m_gain) / (1<<30));
|
2017-08-17 18:10:15 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Sample s(0, 0);
|
|
|
|
m_udpBuffer->write(s);
|
|
|
|
m_outMovingAverage.feed(0);
|
|
|
|
}
|
2015-11-18 21:27:37 -05:00
|
|
|
}
|
2017-08-17 18:10:15 -04:00
|
|
|
|
|
|
|
m_magsq = m_outMovingAverage.average();
|
2015-11-18 21:27:37 -05:00
|
|
|
}
|
|
|
|
}
|
2016-04-03 05:29:11 -04:00
|
|
|
|
2016-04-03 21:44:06 -04:00
|
|
|
//qDebug() << "UDPSrc::feed: " << m_sampleBuffer.size() * 4;
|
2016-04-03 05:29:11 -04:00
|
|
|
|
|
|
|
if((m_spectrum != 0) && (m_spectrumEnabled))
|
2015-12-01 20:28:31 -05:00
|
|
|
{
|
2016-04-03 05:29:11 -04:00
|
|
|
m_spectrum->feed(m_sampleBuffer.begin(), m_sampleBuffer.end(), positiveOnly);
|
2015-12-01 20:28:31 -05:00
|
|
|
}
|
2015-11-18 21:27:37 -05:00
|
|
|
|
|
|
|
m_settingsMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSrc::start()
|
|
|
|
{
|
2016-04-03 21:44:06 -04:00
|
|
|
m_phaseDiscri.reset();
|
2015-11-18 21:27:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void UDPSrc::stop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UDPSrc::handleMessage(const Message& cmd)
|
|
|
|
{
|
|
|
|
qDebug() << "UDPSrc::handleMessage";
|
|
|
|
|
2016-10-02 15:52:39 -04:00
|
|
|
if (DownChannelizer::MsgChannelizerNotification::match(cmd))
|
2015-11-18 21:27:37 -05:00
|
|
|
{
|
2016-10-02 15:52:39 -04:00
|
|
|
DownChannelizer::MsgChannelizerNotification& notif = (DownChannelizer::MsgChannelizerNotification&) cmd;
|
2015-11-18 21:27:37 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
m_config.m_inputSampleRate = notif.getSampleRate();
|
|
|
|
m_config.m_inputFrequencyOffset = notif.getFrequencyOffset();
|
2015-11-18 21:27:37 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
apply(false);
|
2015-11-18 21:27:37 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
qDebug() << "UDPSrc::handleMessage: MsgChannelizerNotification: m_inputSampleRate: " << m_config.m_inputSampleRate
|
|
|
|
<< " frequencyOffset: " << notif.getFrequencyOffset();
|
2015-11-18 21:27:37 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
return true;
|
2015-11-18 21:27:37 -05:00
|
|
|
}
|
2015-12-03 21:46:51 -05:00
|
|
|
else if (MsgUDPSrcConfigureImmediate::match(cmd))
|
|
|
|
{
|
|
|
|
MsgUDPSrcConfigureImmediate& cfg = (MsgUDPSrcConfigureImmediate&) cmd;
|
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
m_config.m_audioActive = cfg.getAudioActive();
|
|
|
|
m_config.m_audioStereo = cfg.getAudioStereo();
|
|
|
|
m_config.m_gain = cfg.getGain();
|
|
|
|
m_config.m_volume = cfg.getVolume();
|
|
|
|
m_config.m_squelch = CalcDb::powerFromdB(cfg.getSquelchDB());
|
|
|
|
m_config.m_squelchGate = cfg.getSquelchGate();
|
|
|
|
m_config.m_squelchEnabled = cfg.getSquelchEnabled();
|
2017-08-19 18:32:40 -04:00
|
|
|
m_config.m_agc = cfg.getAGC();
|
2015-12-05 06:03:26 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
apply(cfg.getForce());
|
2015-12-03 21:46:51 -05:00
|
|
|
|
|
|
|
qDebug() << "UDPSrc::handleMessage: MsgUDPSrcConfigureImmediate: "
|
2017-08-18 18:12:56 -04:00
|
|
|
<< " m_audioActive: " << m_config.m_audioActive
|
|
|
|
<< " m_audioStereo: " << m_config.m_audioStereo
|
|
|
|
<< " m_gain: " << m_config.m_gain
|
|
|
|
<< " m_squelchEnabled: " << m_config.m_squelchEnabled
|
|
|
|
<< " m_squelch: " << m_config.m_squelch
|
|
|
|
<< " getSquelchDB: " << cfg.getSquelchDB()
|
2017-08-19 18:32:40 -04:00
|
|
|
<< " m_squelchGate" << m_config.m_squelchGate
|
|
|
|
<< " m_agc" << m_config.m_agc;
|
2015-12-03 21:46:51 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
2015-11-18 21:27:37 -05:00
|
|
|
else if (MsgUDPSrcConfigure::match(cmd))
|
|
|
|
{
|
|
|
|
MsgUDPSrcConfigure& cfg = (MsgUDPSrcConfigure&) cmd;
|
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
m_config.m_sampleFormat = cfg.getSampleFormat();
|
|
|
|
m_config.m_outputSampleRate = cfg.getOutputSampleRate();
|
|
|
|
m_config.m_rfBandwidth = cfg.getRFBandwidth();
|
|
|
|
m_config.m_udpAddressStr = cfg.getUDPAddress();
|
|
|
|
m_config.m_udpPort = cfg.getUDPPort();
|
|
|
|
m_config.m_audioPort = cfg.getAudioPort();
|
|
|
|
m_config.m_fmDeviation = cfg.getFMDeviation();
|
2015-12-02 22:02:21 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
apply(cfg.getForce());
|
2015-12-04 21:36:31 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
qDebug() << "UDPSrc::handleMessage: MsgUDPSrcConfigure: m_sampleFormat: " << m_config.m_sampleFormat
|
|
|
|
<< " m_outputSampleRate: " << m_config.m_outputSampleRate
|
|
|
|
<< " m_rfBandwidth: " << m_config.m_rfBandwidth
|
|
|
|
<< " m_udpAddressStr: " << m_config.m_udpAddressStr
|
|
|
|
<< " m_udpPort: " << m_config.m_udpPort
|
|
|
|
<< " m_audioPort: " << m_config.m_audioPort;
|
2015-11-18 21:27:37 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (MsgUDPSrcSpectrum::match(cmd))
|
|
|
|
{
|
|
|
|
MsgUDPSrcSpectrum& spc = (MsgUDPSrcSpectrum&) cmd;
|
|
|
|
|
|
|
|
m_spectrumEnabled = spc.getEnabled();
|
|
|
|
|
2015-12-02 22:02:21 -05:00
|
|
|
qDebug() << "UDPSrc::handleMessage: MsgUDPSrcSpectrum: m_spectrumEnabled: " << m_spectrumEnabled;
|
2015-11-18 21:27:37 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(m_spectrum != 0)
|
|
|
|
{
|
|
|
|
return m_spectrum->handleMessage(cmd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-02 22:02:21 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
void UDPSrc::apply(bool force)
|
|
|
|
{
|
|
|
|
m_settingsMutex.lock();
|
|
|
|
|
|
|
|
if ((m_config.m_inputSampleRate != m_running.m_inputSampleRate) ||
|
|
|
|
(m_config.m_inputFrequencyOffset != m_running.m_inputFrequencyOffset) ||
|
|
|
|
(m_config.m_rfBandwidth != m_running.m_rfBandwidth) ||
|
|
|
|
(m_config.m_outputSampleRate != m_running.m_outputSampleRate) || force)
|
|
|
|
{
|
|
|
|
m_nco.setFreq(-m_config.m_inputFrequencyOffset, m_config.m_inputSampleRate);
|
|
|
|
m_interpolator.create(16, m_config.m_inputSampleRate, m_config.m_rfBandwidth / 2.0);
|
|
|
|
m_sampleDistanceRemain = m_config.m_inputSampleRate / m_config.m_outputSampleRate;
|
|
|
|
|
2017-08-19 21:47:21 -04:00
|
|
|
m_squelchThreshold = m_config.m_outputSampleRate * m_config.m_squelchGate;
|
2017-08-18 18:12:56 -04:00
|
|
|
initSquelch(m_squelchOpen);
|
2017-08-19 21:47:21 -04:00
|
|
|
m_agc.resize(m_config.m_outputSampleRate * 0.2, m_agcTarget); // Fixed 200 ms
|
|
|
|
m_agc.setStepDownDelay( m_config.m_outputSampleRate * (m_config.m_squelchGate == 0 ? 0.01 : m_config.m_squelchGate));
|
|
|
|
m_agc.setGate(m_config.m_outputSampleRate * 0.02);
|
2017-08-18 18:12:56 -04:00
|
|
|
|
2017-08-20 16:18:33 -04:00
|
|
|
m_bandpass.create(301, m_config.m_outputSampleRate, 300.0, m_config.m_rfBandwidth / 2.0f);
|
|
|
|
|
2017-08-19 21:47:21 -04:00
|
|
|
m_inMovingAverage.resize(m_config.m_outputSampleRate * 0.01, 1e-10); // 10 ms
|
2017-08-20 03:05:09 -04:00
|
|
|
m_amMovingAverage.resize(m_config.m_outputSampleRate * 0.005, 1e-10); // 5 ms
|
2017-08-18 18:12:56 -04:00
|
|
|
m_outMovingAverage.resize(m_config.m_outputSampleRate * 0.01, 1e-10); // 10 ms
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((m_config.m_audioActive != m_config.m_audioActive) || force)
|
|
|
|
{
|
|
|
|
if (m_config.m_audioActive)
|
|
|
|
{
|
|
|
|
m_audioBufferFill = 0;
|
|
|
|
DSPEngine::instance()->addAudioSink(&m_audioFifo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DSPEngine::instance()->removeAudioSink(&m_audioFifo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((m_config.m_squelchGate != m_running.m_squelchGate) || force)
|
|
|
|
{
|
2017-08-19 21:47:21 -04:00
|
|
|
m_squelchThreshold = m_config.m_outputSampleRate * m_config.m_squelchGate;
|
2017-08-18 18:52:10 -04:00
|
|
|
initSquelch(m_squelchOpen);
|
2017-08-19 21:47:21 -04:00
|
|
|
m_agc.setStepDownDelay(m_config.m_outputSampleRate * (m_config.m_squelchGate == 0 ? 0.01 : m_config.m_squelchGate)); // same delay for up and down
|
|
|
|
m_agc.setGate(m_config.m_outputSampleRate * 0.02);
|
2017-08-19 18:32:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((m_config.m_squelch != m_running.m_squelch) || force)
|
|
|
|
{
|
2017-08-19 21:47:21 -04:00
|
|
|
m_agc.setThreshold(m_config.m_squelch*(1<<23));
|
2017-08-18 18:12:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((m_config.m_udpAddressStr != m_running.m_udpAddressStr) || force)
|
|
|
|
{
|
|
|
|
m_udpBuffer->setAddress(m_config.m_udpAddressStr);
|
|
|
|
m_udpBufferMono->setAddress(m_config.m_udpAddressStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((m_config.m_udpPort != m_running.m_udpPort) || force)
|
|
|
|
{
|
|
|
|
m_udpBuffer->setPort(m_config.m_udpPort);
|
|
|
|
m_udpBufferMono->setPort(m_config.m_udpPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((m_config.m_audioPort != m_running.m_audioPort) || force)
|
|
|
|
{
|
|
|
|
disconnect(m_audioSocket, SIGNAL(readyRead()), this, SLOT(audioReadyRead()));
|
|
|
|
delete m_audioSocket;
|
|
|
|
m_audioSocket = new QUdpSocket(this);
|
|
|
|
|
|
|
|
if (m_audioSocket->bind(QHostAddress::LocalHost, m_config.m_audioPort))
|
|
|
|
{
|
|
|
|
connect(m_audioSocket, SIGNAL(readyRead()), this, SLOT(audioReadyRead()), Qt::QueuedConnection);
|
|
|
|
qDebug("UDPSrc::handleMessage: audio socket bound to port %d", m_config.m_audioPort);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("UDPSrc::handleMessage: cannot bind audio socket");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((m_config.m_fmDeviation != m_running.m_fmDeviation) || force)
|
|
|
|
{
|
|
|
|
m_phaseDiscri.setFMScaling((float) m_config.m_outputSampleRate / (2.0f * m_config.m_fmDeviation));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_settingsMutex.unlock();
|
|
|
|
|
|
|
|
m_running = m_config;
|
|
|
|
}
|
|
|
|
|
2015-12-02 22:02:21 -05:00
|
|
|
void UDPSrc::audioReadyRead()
|
|
|
|
{
|
|
|
|
while (m_audioSocket->hasPendingDatagrams())
|
|
|
|
{
|
2016-04-06 03:33:29 -04:00
|
|
|
qint64 pendingDataSize = m_audioSocket->pendingDatagramSize();
|
|
|
|
qint64 udpReadBytes = m_audioSocket->readDatagram(m_udpAudioBuf, pendingDataSize, 0, 0);
|
|
|
|
//qDebug("UDPSrc::audioReadyRead: %lld", udpReadBytes);
|
2015-12-02 22:02:21 -05:00
|
|
|
|
2017-08-18 18:12:56 -04:00
|
|
|
if (m_running.m_audioActive)
|
2015-12-03 21:46:51 -05:00
|
|
|
{
|
2017-08-18 18:12:56 -04:00
|
|
|
if (m_running.m_audioStereo)
|
2015-12-02 22:02:21 -05:00
|
|
|
{
|
2016-04-06 03:33:29 -04:00
|
|
|
for (int i = 0; i < udpReadBytes - 3; i += 4)
|
2015-12-02 22:02:21 -05:00
|
|
|
{
|
2016-04-06 03:33:29 -04:00
|
|
|
qint16 l_sample = (qint16) *(&m_udpAudioBuf[i]);
|
|
|
|
qint16 r_sample = (qint16) *(&m_udpAudioBuf[i+2]);
|
2017-08-18 18:12:56 -04:00
|
|
|
m_audioBuffer[m_audioBufferFill].l = l_sample * m_running.m_volume;
|
|
|
|
m_audioBuffer[m_audioBufferFill].r = r_sample * m_running.m_volume;
|
2015-12-05 05:49:24 -05:00
|
|
|
++m_audioBufferFill;
|
2015-12-03 21:46:51 -05:00
|
|
|
|
2015-12-05 05:49:24 -05:00
|
|
|
if (m_audioBufferFill >= m_audioBuffer.size())
|
2015-12-03 21:46:51 -05:00
|
|
|
{
|
2015-12-05 05:49:24 -05:00
|
|
|
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill, 1);
|
|
|
|
|
|
|
|
if (res != m_audioBufferFill)
|
|
|
|
{
|
|
|
|
qDebug("UDPSrc::audioReadyRead: (stereo) lost %u samples", m_audioBufferFill - res);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_audioBufferFill = 0;
|
2015-12-03 21:46:51 -05:00
|
|
|
}
|
2015-12-05 05:49:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-06 03:33:29 -04:00
|
|
|
for (int i = 0; i < udpReadBytes - 1; i += 2)
|
2015-12-05 05:49:24 -05:00
|
|
|
{
|
2016-04-06 03:33:29 -04:00
|
|
|
qint16 sample = (qint16) *(&m_udpAudioBuf[i]);
|
2017-08-18 18:12:56 -04:00
|
|
|
m_audioBuffer[m_audioBufferFill].l = sample * m_running.m_volume;
|
|
|
|
m_audioBuffer[m_audioBufferFill].r = sample * m_running.m_volume;
|
2015-12-05 05:49:24 -05:00
|
|
|
++m_audioBufferFill;
|
|
|
|
|
|
|
|
if (m_audioBufferFill >= m_audioBuffer.size())
|
|
|
|
{
|
|
|
|
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill, 1);
|
|
|
|
|
|
|
|
if (res != m_audioBufferFill)
|
|
|
|
{
|
|
|
|
qDebug("UDPSrc::audioReadyRead: (mono) lost %u samples", m_audioBufferFill - res);
|
|
|
|
}
|
2015-12-03 21:46:51 -05:00
|
|
|
|
2015-12-05 05:49:24 -05:00
|
|
|
m_audioBufferFill = 0;
|
|
|
|
}
|
2015-12-02 22:02:21 -05:00
|
|
|
}
|
2015-12-03 21:46:51 -05:00
|
|
|
}
|
2015-12-02 22:02:21 -05:00
|
|
|
|
2015-12-03 21:46:51 -05:00
|
|
|
if (m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill, 0) != m_audioBufferFill)
|
|
|
|
{
|
|
|
|
qDebug("UDPSrc::audioReadyRead: lost samples");
|
2015-12-02 22:02:21 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 21:46:51 -05:00
|
|
|
m_audioBufferFill = 0;
|
2015-12-02 22:02:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//qDebug("UDPSrc::audioReadyRead: done");
|
|
|
|
}
|