Skip to content

Commit

Permalink
KiwiSDR: implement variable sample rate. Fixes #1523
Browse files Browse the repository at this point in the history
  • Loading branch information
f4exb committed Nov 26, 2022
1 parent ba1dfa9 commit 6bba016
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 12 deletions.
20 changes: 19 additions & 1 deletion plugins/samplesource/kiwisdr/kiwisdrinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ MESSAGE_CLASS_DEFINITION(KiwiSDRInput::MsgSetStatus, Message)

KiwiSDRInput::KiwiSDRInput(DeviceAPI *deviceAPI) :
m_deviceAPI(deviceAPI),
m_sampleRate(12000),
m_settings(),
m_kiwiSDRWorker(nullptr),
m_kiwiSDRWorkerThread(nullptr),
Expand Down Expand Up @@ -101,6 +102,7 @@ bool KiwiSDRInput::start()

m_kiwiSDRWorkerThread = new QThread();
m_kiwiSDRWorker = new KiwiSDRWorker(&m_sampleFifo);
m_kiwiSDRWorker->setInputMessageQueue(getInputMessageQueue());
m_kiwiSDRWorker->moveToThread(m_kiwiSDRWorkerThread);

QObject::connect(m_kiwiSDRWorkerThread, &QThread::finished, m_kiwiSDRWorker, &QObject::deleteLater);
Expand Down Expand Up @@ -174,7 +176,7 @@ const QString& KiwiSDRInput::getDeviceDescription() const

int KiwiSDRInput::getSampleRate() const
{
return 12000;
return m_sampleRate;
}

quint64 KiwiSDRInput::getCenterFrequency() const
Expand Down Expand Up @@ -219,6 +221,22 @@ bool KiwiSDRInput::handleMessage(const Message& message)

return true;
}
else if (KiwiSDRWorker::MsgReportSampleRate::match(message))
{
KiwiSDRWorker::MsgReportSampleRate& report = (KiwiSDRWorker::MsgReportSampleRate&) message;
m_sampleRate = report.getSampleRate();
qDebug() << "KiwiSDRInput::handleMessage: KiwiSDRWorker::MsgReportSampleRate: m_sampleRate: " << m_sampleRate;

if (!m_sampleFifo.setSize(m_sampleRate * 2)) {
qCritical("KiwiSDRInput::KiwiSDRInput: Could not allocate SampleFifo");
}

DSPSignalNotification *notif = new DSPSignalNotification(
m_sampleRate, m_settings.m_centerFrequency);
m_deviceAPI->getDeviceEngineInputMessageQueue()->push(notif);

return true;
}
else if (MsgStartStop::match(message))
{
MsgStartStop& cmd = (MsgStartStop&) message;
Expand Down
1 change: 1 addition & 0 deletions plugins/samplesource/kiwisdr/kiwisdrinput.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class KiwiSDRInput : public DeviceSampleSource {
private:
DeviceAPI *m_deviceAPI;
QMutex m_mutex;
int m_sampleRate;
KiwiSDRSettings m_settings;
KiwiSDRWorker* m_kiwiSDRWorker;
QThread *m_kiwiSDRWorkerThread;
Expand Down
48 changes: 37 additions & 11 deletions plugins/samplesource/kiwisdr/kiwisdrworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
///////////////////////////////////////////////////////////////////////////////////

#include <boost/endian/conversion.hpp>
#include "util/messagequeue.h"
#include "kiwisdrworker.h"

MESSAGE_CLASS_DEFINITION(KiwiSDRWorker::MsgReportSampleRate, Message)

KiwiSDRWorker::KiwiSDRWorker(SampleSinkFifo* sampleFifo) :
QObject(),
m_timer(this),
m_samplesBuf(),
m_sampleFifo(sampleFifo),
m_centerFrequency(1450000),
m_sampleRate(12000),
m_inputMessageQueue(nullptr),
m_gain(20),
m_useAGC(true),
m_status(0)
Expand Down Expand Up @@ -66,7 +71,8 @@ void KiwiSDRWorker::sendCenterFrequency()
return;

QString freq = QString::number(m_centerFrequency / 1000.0, 'f', 3);
QString msg = "SET mod=iq low_cut=-5980 high_cut=5980 freq=" + freq;
int bw = (m_sampleRate/2) - 20;
QString msg = QString("SET mod=iq low_cut=-%1 high_cut=%2 freq=%3").arg(bw).arg(bw).arg(freq);
m_webSocket.sendTextMessage(msg);
}

Expand All @@ -87,16 +93,36 @@ void KiwiSDRWorker::onBinaryMessageReceived(const QByteArray &message)
if (message[0] == 'M' && message[1] == 'S' && message[2] == 'G')
{
QStringList al = QString::fromUtf8(message).split(' ');
if (al.size() > 2 && al[2] == "audio_rate=12000")
{
m_webSocket.sendTextMessage("SET AR OK in=12000 out=48000");
m_webSocket.sendTextMessage("SERVER DE CLIENT KiwiAngel SND");
sendGain();
sendCenterFrequency();
m_timer.start(5000);
m_status = 2;
emit updateStatus(2);
}

if ((al.size() > 2) && al[2].startsWith("audio_rate="))
{
QStringList rateKeyVal = al[2].split('=');

if (rateKeyVal.size() > 1)
{
bool ok;
int sampleRate = rateKeyVal[1].toInt(&ok);

if (ok) {
m_sampleRate = sampleRate;
}

qDebug("KiwiSDRWorker::onBinaryMessageReceived: sample rate: %d", m_sampleRate);

if (m_inputMessageQueue) {
m_inputMessageQueue->push(MsgReportSampleRate::create(m_sampleRate));
}

QString msg = QString("SET AR OK in=%1 out=48000").arg(m_sampleRate);
m_webSocket.sendTextMessage(msg);
m_webSocket.sendTextMessage("SERVER DE CLIENT KiwiAngel SND");
sendGain();
sendCenterFrequency();
m_timer.start(5000);
m_status = 2;
emit updateStatus(2);
}
}
}
else if (message[0] == 'S' && message[1] == 'N' && message[2] == 'D')
{
Expand Down
25 changes: 25 additions & 0 deletions plugins/samplesource/kiwisdr/kiwisdrworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,36 @@
#include <QtWebSockets/QtWebSockets>

#include "dsp/samplesinkfifo.h"
#include "util/message.h"

class MessageQueue;

class KiwiSDRWorker : public QObject {
Q_OBJECT

public:
class MsgReportSampleRate : public Message {
MESSAGE_CLASS_DECLARATION

public:
int getSampleRate() const { return m_sampleRate; }

static MsgReportSampleRate* create(int sampleRate) {
return new MsgReportSampleRate(sampleRate);
}

private:
int m_sampleRate;

MsgReportSampleRate(int sampleRate) :
Message(),
m_sampleRate(sampleRate)
{ }
};

KiwiSDRWorker(SampleSinkFifo* sampleFifo);
int getStatus() const { return m_status; }
void setInputMessageQueue(MessageQueue *messageQueue) { m_inputMessageQueue = messageQueue; }

private:
QTimer m_timer;
Expand All @@ -39,6 +62,8 @@ class KiwiSDRWorker : public QObject {

QString m_serverAddress;
uint64_t m_centerFrequency;
int m_sampleRate;
MessageQueue *m_inputMessageQueue;

uint32_t m_gain;
bool m_useAGC;
Expand Down

0 comments on commit 6bba016

Please sign in to comment.