From 85807ffadb2ab56a13cc9c9a0e98fe5717bcc792 Mon Sep 17 00:00:00 2001 From: f4exb Date: Wed, 16 Nov 2016 02:38:21 +0100 Subject: [PATCH] SDRPlay plugin: now working with open source libmirisdr --- plugins/samplesource/sdrplay/sdrplaygui.cpp | 59 ++-- plugins/samplesource/sdrplay/sdrplaygui.h | 2 + plugins/samplesource/sdrplay/sdrplaygui.ui | 24 +- plugins/samplesource/sdrplay/sdrplayinput.cpp | 303 +++++++++--------- plugins/samplesource/sdrplay/sdrplayinput.h | 3 +- .../samplesource/sdrplay/sdrplaysettings.cpp | 28 +- .../samplesource/sdrplay/sdrplaysettings.h | 4 +- .../samplesource/sdrplay/sdrplaythread.cpp | 112 ++++--- plugins/samplesource/sdrplay/sdrplaythread.h | 23 +- sdrbase/dsp/dspdevicesourceengine.cpp | 2 +- 10 files changed, 282 insertions(+), 278 deletions(-) diff --git a/plugins/samplesource/sdrplay/sdrplaygui.cpp b/plugins/samplesource/sdrplay/sdrplaygui.cpp index 8954aa55e..e8fc2da48 100644 --- a/plugins/samplesource/sdrplay/sdrplaygui.cpp +++ b/plugins/samplesource/sdrplay/sdrplaygui.cpp @@ -47,19 +47,19 @@ SDRPlayGui::SDRPlayGui(DeviceSourceAPI *deviceAPI, QWidget* parent) : ui->ifFrequency->clear(); for (int i = 0; i < SDRPlayIF::getNbIFs(); i++) { - ui->ifFrequency->addItem(QString::number(SDRPlayIF::getIF(i))); + ui->ifFrequency->addItem(QString::number(SDRPlayIF::getIF(i)/1000)); } ui->samplerate->clear(); for (int i = 0; i < SDRPlaySampleRates::getNbRates(); i++) { - ui->samplerate->addItem(QString::number(SDRPlaySampleRates::getRate(i))); + ui->samplerate->addItem(QString::number(SDRPlaySampleRates::getRate(i)/1000)); } ui->bandwidth->clear(); for (int i = 0; i < SDRPlayBandwidths::getNbBandwidths(); i++) { - ui->bandwidth->addItem(QString::number(SDRPlayBandwidths::getBandwidth(i))); + ui->bandwidth->addItem(QString::number(SDRPlayBandwidths::getBandwidth(i)/1000)); } connect(&m_updateTimer, SIGNAL(timeout()), this, SLOT(updateHardware())); @@ -205,8 +205,8 @@ void SDRPlayGui::displaySettings() { ui->centerFrequency->setValue(m_settings.m_centerFrequency / 1000); - ui->ppm->setValue(m_settings.m_LOppmCorrection); - ui->ppmText->setText(QString("%1").arg(QString::number(m_settings.m_LOppmCorrection/10.0, 'f', 1))); + ui->ppm->setValue(m_settings.m_LOppmTenths); + ui->ppmText->setText(QString("%1").arg(QString::number(m_settings.m_LOppmTenths/10.0, 'f', 1))); ui->samplerate->setCurrentIndex(m_settings.m_devSampleRateIndex); @@ -214,6 +214,7 @@ void SDRPlayGui::displaySettings() ui->iqImbalance->setChecked(m_settings.m_iqCorrection); ui->fBand->setCurrentIndex(m_settings.m_frequencyBandIndex); + ui->bandwidth->setCurrentIndex(m_settings.m_bandwidthIndex); ui->ifFrequency->setCurrentIndex(m_settings.m_ifFrequencyIndex); ui->samplerate->setCurrentIndex(m_settings.m_devSampleRateIndex); @@ -298,13 +299,14 @@ void SDRPlayGui::on_centerFrequency_changed(quint64 value) void SDRPlayGui::on_ppm_valueChanged(int value) { - m_settings.m_LOppmCorrection = value; - ui->ppmText->setText(QString("%1").arg(QString::number(m_settings.m_LOppmCorrection/10.0, 'f', 1))); + m_settings.m_LOppmTenths = value; + ui->ppmText->setText(QString("%1").arg(QString::number(m_settings.m_LOppmTenths/10.0, 'f', 1))); sendSettings(); } void SDRPlayGui::on_dcOffset_toggled(bool checked) { + qDebug("SDRPlayGui::on_dcOffset_toggled: %s", checked ? "on" : "off"); m_settings.m_dcBlock = checked; sendSettings(); } @@ -329,19 +331,6 @@ void SDRPlayGui::on_fBand_currentIndexChanged(int index) sendSettings(); } -void SDRPlayGui::on_mirDCCorr_currentIndexChanged(int index) -{ - m_settings.m_mirDcCorrIndex = index; - sendSettings(); -} - -void SDRPlayGui::on_mirDCCorrTrackTime_valueChanged(int value) -{ - m_settings.m_mirDcCorrTrackTimeIndex = value; - ui->mirDCCorrTrackTimeText->setText(tr("%1us").arg(m_settings.m_mirDcCorrTrackTimeIndex * 3)); - sendSettings(); -} - void SDRPlayGui::on_bandwidth_currentIndexChanged(int index) { m_settings.m_bandwidthIndex = index; @@ -354,6 +343,12 @@ void SDRPlayGui::on_samplerate_currentIndexChanged(int index) sendSettings(); } +void SDRPlayGui::on_ifFrequency_currentIndexChanged(int index) +{ + m_settings.m_ifFrequencyIndex = index; + sendSettings(); +} + void SDRPlayGui::on_decim_currentIndexChanged(int index) { m_settings.m_log2Decim = index; @@ -366,20 +361,24 @@ void SDRPlayGui::on_fcPos_currentIndexChanged(int index) sendSettings(); } -void SDRPlayGui::on_gr_valueChanged(int value) +void SDRPlayGui::on_gain_valueChanged(int value) { - m_settings.m_gainRedctionIndex = value; + if (value > (int)m_gains.size()) + { + return; + } - char grStrChr[20]; + int gain = m_gains[value]; + ui->gainText->setText(tr("%1.%2").arg(gain / 10).arg(abs(gain % 10))); + m_settings.m_gain = gain; - sprintf(grStrChr, "%03d:%02d-%02d-%02d", - m_settings.m_gainRedctionIndex, - 24 - m_lnaGr[m_settings.m_frequencyBandIndex][m_settings.m_gainRedctionIndex], - 19 - m_mixerGr[m_settings.m_frequencyBandIndex][m_settings.m_gainRedctionIndex], - 59 - m_bbGr[m_settings.m_frequencyBandIndex][m_settings.m_gainRedctionIndex]); - - ui->grText->setText(QString(grStrChr)); + sendSettings(); +} +void SDRPlayGui::on_gainAuto_toggled(bool checked) +{ + qDebug("SDRPlayGui::on_gainAuto_toggled: %s", checked ? "on" : "off"); + m_settings.m_autoGain = checked; sendSettings(); } diff --git a/plugins/samplesource/sdrplay/sdrplaygui.h b/plugins/samplesource/sdrplay/sdrplaygui.h index 28a63de39..9eade738d 100644 --- a/plugins/samplesource/sdrplay/sdrplaygui.h +++ b/plugins/samplesource/sdrplay/sdrplaygui.h @@ -80,9 +80,11 @@ private slots: void on_fBand_currentIndexChanged(int index); void on_bandwidth_currentIndexChanged(int index); void on_samplerate_currentIndexChanged(int index); + void on_ifFrequency_currentIndexChanged(int index); void on_decim_currentIndexChanged(int index); void on_fcPos_currentIndexChanged(int index); void on_gain_valueChanged(int value); + void on_gainAuto_toggled(bool checked); void on_startStop_toggled(bool checked); void on_record_toggled(bool checked); }; diff --git a/plugins/samplesource/sdrplay/sdrplaygui.ui b/plugins/samplesource/sdrplay/sdrplaygui.ui index bd00fc912..8ab9d8b3e 100644 --- a/plugins/samplesource/sdrplay/sdrplaygui.ui +++ b/plugins/samplesource/sdrplay/sdrplaygui.ui @@ -35,16 +35,7 @@ 3 - - 2 - - - 2 - - - 2 - - + 2 @@ -526,7 +517,7 @@ - + 3 @@ -561,10 +552,10 @@ - Tuner gain in dB + - --- + 000 Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -578,6 +569,13 @@ + + + + A + + + diff --git a/plugins/samplesource/sdrplay/sdrplayinput.cpp b/plugins/samplesource/sdrplay/sdrplayinput.cpp index 1c55a4609..ba8d4832e 100644 --- a/plugins/samplesource/sdrplay/sdrplayinput.cpp +++ b/plugins/samplesource/sdrplay/sdrplayinput.cpp @@ -50,7 +50,7 @@ bool SDRPlayInput::init(const Message& cmd) return false; } -bool SDRPlayInput::start(uint32_t device) +bool SDRPlayInput::start(int device) { QMutexLocker mutexLocker(&m_mutex); @@ -81,7 +81,7 @@ bool SDRPlayInput::start(uint32_t device) product[0] = '\0'; serial[0] = '\0'; - if ((res = mirisdr_get_device_usb_strings(m_dev, vendor, product, serial)) < 0) + if ((res = mirisdr_get_device_usb_strings(device, vendor, product, serial)) < 0) { qCritical("SDRPlayInput::start: error accessing USB device"); stop(); @@ -105,28 +105,32 @@ bool SDRPlayInput::start(uint32_t device) return false; } - if ((res = mirisdr_set_sample_format(m_dev, "336_S16"))) // sample format S12 + char s12FormatString[] = "336_S16"; + + if ((res = mirisdr_set_sample_format(m_dev, s12FormatString))) // sample format S12 { qCritical("SDRPlayInput::start: could not set sample format: rc: %d", res); stop(); return false; } - if ((res = mirisdr_set_transfer(m_dev, "BULK")) < 0) + char bulkFormatString[] = "BULK"; + + if ((res = mirisdr_set_transfer(m_dev, bulkFormatString)) < 0) { qCritical("SDRPlayInput::start: could not set USB Bulk mode: rc: %d", res); stop(); return false; } - if ((res = mirisdr_set_if_freq(m_dev, SDRPlayIF::m_if[m_settings.m_ifFrequencyIndex])) < 0) + if ((res = mirisdr_set_if_freq(m_dev, SDRPlayIF::getIF(m_settings.m_ifFrequencyIndex))) < 0) { qCritical("SDRPlayInput::start: could not set IF frequency at index %d: rc: %d", m_settings.m_ifFrequencyIndex, res); stop(); return false; } - if ((res = mirisdr_set_bandwidth(m_dev, SDRPlayBandwidths::m_bw[m_settings.m_bandwidthIndex])) < 0) + if ((res = mirisdr_set_bandwidth(m_dev, SDRPlayBandwidths::getBandwidth(m_settings.m_bandwidthIndex))) < 0) { qCritical("SDRPlayInput::start: could not set bandwidth at index %d: rc: %d", m_settings.m_bandwidthIndex, res); stop(); @@ -175,7 +179,7 @@ bool SDRPlayInput::start(uint32_t device) return false; } - if((m_sdrPlayThread = new SDRPlayThread(&m_sampleFifo)) == 0) + if((m_sdrPlayThread = new SDRPlayThread(m_dev, &m_sampleFifo)) == 0) { qFatal("SDRPlayInput::start: failed to create thread"); return false; @@ -186,28 +190,28 @@ bool SDRPlayInput::start(uint32_t device) mutexLocker.unlock(); applySettings(m_settings, true); + + return true; } void SDRPlayInput::stop() { - mir_sdr_ErrT r; QMutexLocker mutexLocker(&m_mutex); - r = mir_sdr_StreamUninit(); - - if (r != mir_sdr_Success) - { - qCritical("SDRPlayInput::stop: stream uninit failed with code %d", (int) r); - } - - m_mirStreamRunning = false; - if(m_sdrPlayThread != 0) { m_sdrPlayThread->stopWork(); delete m_sdrPlayThread; m_sdrPlayThread = 0; } + + if (m_dev != 0) + { + mirisdr_close(m_dev); + m_dev = 0; + } + + m_deviceDescription.clear(); } const QString& SDRPlayInput::getDeviceDescription() const @@ -218,7 +222,7 @@ const QString& SDRPlayInput::getDeviceDescription() const int SDRPlayInput::getSampleRate() const { int rate = SDRPlaySampleRates::getRate(m_settings.m_devSampleRateIndex); - return (rate * 1000) / (1<configureCorrections(m_settings.m_dcBlock, m_settings.m_iqCorrection); } - if (m_settings.m_iqCorrection != settings.m_iqCorrection) + if ((m_settings.m_iqCorrection != settings.m_iqCorrection) || force) { m_settings.m_iqCorrection = settings.m_iqCorrection; m_deviceAPI->configureCorrections(m_settings.m_dcBlock, m_settings.m_iqCorrection); } + if ((m_settings.m_autoGain != settings.m_autoGain) || force) + { + m_settings.m_autoGain = settings.m_autoGain; + + if(m_dev != 0) + { + int r = mirisdr_set_tuner_gain_mode(m_dev, m_settings.m_autoGain ? 0 : 1); + + if (r < 0) + { + qDebug("SDRPlayInput::applySettings: could not set auto gain %s: rc: %d", m_settings.m_autoGain ? "on" : "off", r); + } + else + { + qDebug("SDRPlayInput::applySettings: auto gain set to %s", m_settings.m_autoGain ? "on" : "off"); + forceManualGain = !m_settings.m_autoGain; + } + } + } + + if (!m_settings.m_autoGain) + { + if ((m_settings.m_gain != settings.m_gain) || force || forceManualGain) + { + m_settings.m_gain = settings.m_gain; + + if(m_dev != 0) + { + int r = mirisdr_set_tuner_gain(m_dev, m_settings.m_gain); + + if (r < 0) + { + qDebug("SDRPlayInput::applySettings: could not set tuner gain()"); + } + } + } + } + + if ((m_settings.m_devSampleRateIndex != settings.m_devSampleRateIndex) || force) + { + forwardChange = true; + + if(m_dev != 0) + { + int sampleRate = SDRPlaySampleRates::getRate(m_settings.m_devSampleRateIndex); + int r = mirisdr_set_sample_rate(m_dev, sampleRate); + + if(r < 0) + { + qCritical("SDRPlayInput::applySettings: could not set sample rate: %d rc: %d", sampleRate, r); + } + else + { + qDebug("SDRPlayInput::applySettings: sample rate set to %d", sampleRate); + m_settings.m_devSampleRateIndex = settings.m_devSampleRateIndex; + m_sdrPlayThread->setSamplerate(sampleRate); + } + } + } + if ((m_settings.m_log2Decim != settings.m_log2Decim) || force) { m_settings.m_log2Decim = settings.m_log2Decim; @@ -273,7 +336,55 @@ bool SDRPlayInput::applySettings(const SDRPlaySettings& settings, bool force) if (m_sdrPlayThread != 0) { m_sdrPlayThread->setLog2Decimation(m_settings.m_log2Decim); - qDebug() << "SDRPlayInput: set decimation to " << (1< 85) - { - if (m_settings.m_gainRedctionIndex < 85) - { - m_settings.m_gainRedctionIndex = 85; - - if (m_mirStreamRunning) - { - reinitMirSDR(mir_sdr_CHANGE_GR); - } - } - } - else - { - m_settings.m_gainRedctionIndex = settings.m_gainRedctionIndex; - - if (m_mirStreamRunning) - { - reinitMirSDR(mir_sdr_CHANGE_GR); - } - } + qDebug("SDRPlayInput::applySettings: bandwidth set to %d", bandwidth); + m_settings.m_bandwidthIndex = settings.m_bandwidthIndex; } } - if ((m_settings.m_LOppmCorrection != settings.m_LOppmCorrection) || force) + if (m_settings.m_ifFrequencyIndex != settings.m_ifFrequencyIndex) { - m_settings.m_LOppmCorrection = settings.m_LOppmCorrection; + int iFFrequency = SDRPlayIF::getIF(settings.m_ifFrequencyIndex); + int r = mirisdr_set_if_freq(m_dev, iFFrequency); - mir_sdr_ErrT r = mir_sdr_SetPpm(m_settings.m_LOppmCorrection / 10.0); - - if (r != mir_sdr_Success) + if (r < 0) { - qDebug("SDRPlayInput::applySettings: mir_sdr_SetPpm failed with code %d", (int) r); + qCritical("SDRPlayInput::applySettings: set IF frequency to %d failed: rc: %d", iFFrequency, r); } - } - - if ((m_settings.m_mirDcCorrIndex != settings.m_mirDcCorrIndex) || force) - { - m_settings.m_mirDcCorrIndex = settings.m_mirDcCorrIndex; - - if (m_mirStreamRunning) + else { - mir_sdr_ErrT r = mir_sdr_SetDcMode(m_settings.m_mirDcCorrIndex, 0); - - if (r != mir_sdr_Success) - { - qDebug("SDRPlayInput::applySettings: mir_sdr_SetDcMode failed with code %d", (int) r); - } - } - } - - if ((m_settings.m_mirDcCorrTrackTimeIndex != settings.m_mirDcCorrTrackTimeIndex) || force) - { - m_settings.m_mirDcCorrTrackTimeIndex = settings.m_mirDcCorrTrackTimeIndex; - - if (m_mirStreamRunning) - { - mir_sdr_ErrT r = mir_sdr_SetDcTrackTime(m_settings.m_mirDcCorrTrackTimeIndex); - - if (r != mir_sdr_Success) - { - qDebug("SDRPlayInput::applySettings: mir_sdr_SetDcTrackTime failed with code %d", (int) r); - } + qDebug("SDRPlayInput::applySettings: IF frequency set to %d", iFFrequency); + m_settings.m_ifFrequencyIndex = settings.m_ifFrequencyIndex; } } @@ -418,37 +447,23 @@ bool SDRPlayInput::applySettings(const SDRPlaySettings& settings, bool force) return true; } -void SDRPlayInput::reinitMirSDR(mir_sdr_ReasonForReinitT reasonForReinit) +bool SDRPlayInput::setCenterFrequency(quint64 freq_hz) { - int grdB = -m_settings.m_gainRedctionIndex; - int rate = SDRPlaySampleRates::getRate(m_settings.m_devSampleRateIndex); - int gRdBsystem; + qint64 df = ((qint64)freq_hz * m_settings.m_LOppmTenths) / 10000000LL; + freq_hz += df; - mir_sdr_ErrT r = mir_sdr_Reinit( - &grdB, - rate / 1e3, - m_settings.m_centerFrequency / 1e6, - (mir_sdr_Bw_MHzT) m_settings.m_bandwidthIndex, - mir_sdr_IF_Zero, - mir_sdr_LO_Auto, - 1, // LNA - &gRdBsystem, - 0, // use mir_sdr_SetGr() to set initial gain reduction - &m_samplesPerPacket, - reasonForReinit); + int r = mirisdr_set_center_freq(m_dev, static_cast(freq_hz)); - if (r != mir_sdr_Success) + if (r != 0) { - qCritical("SDRPlayInput::reinitMirSDR (%d): MirSDR stream reinit failed with code %d", (int) reasonForReinit, (int) r); - m_mirStreamRunning = false; + qWarning("SDRPlayInput::setCenterFrequency: could not frequency to %llu Hz", freq_hz); + return false; } else { - qDebug("SDRPlayInput::reinitMirSDR (%d): MirSDR stream restarted: samplesPerPacket: %d", (int) reasonForReinit, m_samplesPerPacket); + qWarning("SDRPlayInput::setCenterFrequency: frequency set to %llu Hz", freq_hz); + return true; } } -void SDRPlayInput::callbackGC(unsigned int gRdB, unsigned int lnaGRdB, void *cbContext) -{ - return; -} + diff --git a/plugins/samplesource/sdrplay/sdrplayinput.h b/plugins/samplesource/sdrplay/sdrplayinput.h index fd536c897..26c04e95f 100644 --- a/plugins/samplesource/sdrplay/sdrplayinput.h +++ b/plugins/samplesource/sdrplay/sdrplayinput.h @@ -73,7 +73,7 @@ public: virtual ~SDRPlayInput(); virtual bool init(const Message& message); - virtual bool start(uint32_t device); + virtual bool start(int device); virtual void stop(); virtual const QString& getDeviceDescription() const; @@ -84,6 +84,7 @@ public: private: bool applySettings(const SDRPlaySettings& settings, bool force); + bool setCenterFrequency(quint64 freq); DeviceSourceAPI *m_deviceAPI; QMutex m_mutex; diff --git a/plugins/samplesource/sdrplay/sdrplaysettings.cpp b/plugins/samplesource/sdrplay/sdrplaysettings.cpp index b02ad139f..b04e5cfad 100644 --- a/plugins/samplesource/sdrplay/sdrplaysettings.cpp +++ b/plugins/samplesource/sdrplay/sdrplaysettings.cpp @@ -28,33 +28,33 @@ void SDRPlaySettings::resetToDefaults() { m_centerFrequency = 7040*1000; m_gain = 0; - m_LOppmCorrection = 0; + m_LOppmTenths = 0; m_frequencyBandIndex = 0; m_ifFrequencyIndex = 0; m_bandwidthIndex = 0; m_devSampleRateIndex = 0; - m_gainRedctionIndex = 35; m_log2Decim = 0; m_fcPos = FC_POS_CENTER; m_dcBlock = false; m_iqCorrection = false; + m_autoGain = false; } QByteArray SDRPlaySettings::serialize() const { SimpleSerializer s(1); - s.writeS32(1, m_LOppmCorrection); + s.writeS32(1, m_LOppmTenths); s.writeU32(2, m_frequencyBandIndex); s.writeU32(3, m_ifFrequencyIndex); s.writeS32(4, m_gain); s.writeU32(5, m_bandwidthIndex); s.writeU32(6, m_devSampleRateIndex); - s.writeU32(7, m_gainRedctionIndex); - s.writeU32(8, m_log2Decim); - s.writeS32(9, (int) m_fcPos); - s.writeBool(10, m_dcBlock); - s.writeBool(11, m_iqCorrection); + s.writeU32(7, m_log2Decim); + s.writeS32(8, (int) m_fcPos); + s.writeBool(9, m_dcBlock); + s.writeBool(10, m_iqCorrection); + s.writeBool(11, m_autoGain); return s.final(); } @@ -73,18 +73,18 @@ bool SDRPlaySettings::deserialize(const QByteArray& data) { int intval; - d.readS32(1, &m_LOppmCorrection, 0); + d.readS32(1, &m_LOppmTenths, 0); d.readU32(2, &m_frequencyBandIndex, 0); d.readU32(3, &m_ifFrequencyIndex, 0); d.readS32(4, &m_gain, 0); d.readU32(5, &m_bandwidthIndex, 0); d.readU32(6, &m_devSampleRateIndex, 0); - d.readU32(7, &m_gainRedctionIndex, 35); - d.readU32(8, &m_log2Decim, 0); - d.readS32(9, &intval, 0); + d.readU32(7, &m_log2Decim, 0); + d.readS32(8, &intval, 0); m_fcPos = (fcPos_t) intval; - d.readBool(10, &m_dcBlock, false); - d.readBool(11, &m_iqCorrection, false); + d.readBool(9, &m_dcBlock, false); + d.readBool(10, &m_iqCorrection, false); + d.readBool(11, &m_autoGain, false); return true; } diff --git a/plugins/samplesource/sdrplay/sdrplaysettings.h b/plugins/samplesource/sdrplay/sdrplaysettings.h index 86080ded0..93f00b9c8 100644 --- a/plugins/samplesource/sdrplay/sdrplaysettings.h +++ b/plugins/samplesource/sdrplay/sdrplaysettings.h @@ -29,16 +29,16 @@ struct SDRPlaySettings { uint64_t m_centerFrequency; qint32 m_gain; - int32_t m_LOppmCorrection; + int32_t m_LOppmTenths; uint32_t m_frequencyBandIndex; uint32_t m_ifFrequencyIndex; uint32_t m_bandwidthIndex; uint32_t m_devSampleRateIndex; - uint32_t m_gainRedctionIndex; uint32_t m_log2Decim; fcPos_t m_fcPos; bool m_dcBlock; bool m_iqCorrection; + bool m_autoGain; SDRPlaySettings(); void resetToDefaults(); diff --git a/plugins/samplesource/sdrplay/sdrplaythread.cpp b/plugins/samplesource/sdrplay/sdrplaythread.cpp index 2bba5072b..78ca6a520 100644 --- a/plugins/samplesource/sdrplay/sdrplaythread.cpp +++ b/plugins/samplesource/sdrplay/sdrplaythread.cpp @@ -19,23 +19,21 @@ #include "sdrplaythread.h" #include "dsp/samplesinkfifo.h" -SDRPlayThread *SDRPlayThread::m_this = 0; - -SDRPlayThread::SDRPlayThread(SampleSinkFifo* sampleFifo, QObject* parent) : +SDRPlayThread::SDRPlayThread(mirisdr_dev_t* dev, SampleSinkFifo* sampleFifo, QObject* parent) : QThread(parent), m_running(false), + m_dev(dev), m_convertBuffer(SDRPLAY_INIT_NBSAMPLES), m_sampleFifo(sampleFifo), + m_samplerate(288000), m_log2Decim(0), m_fcPos(0) { - m_this = this; } SDRPlayThread::~SDRPlayThread() { stopWork(); - m_this = 0; } void SDRPlayThread::startWork() @@ -53,6 +51,11 @@ void SDRPlayThread::stopWork() wait(); } +void SDRPlayThread::setSamplerate(int samplerate) +{ + m_samplerate = samplerate; +} + void SDRPlayThread::setLog2Decimation(unsigned int log2_decim) { m_log2Decim = log2_decim; @@ -63,110 +66,114 @@ void SDRPlayThread::setFcPos(int fcPos) m_fcPos = fcPos; } -void SDRPlayThread::streamCallback( - short *xi, - short *xq, - unsigned int firstSampleNum, - int grChanged, - int rfChanged, - int fsChanged, - unsigned int numSamples, - unsigned int reset, - void *cbContext) +void SDRPlayThread::run() { - qDebug("SDRPlayThread::streamCallback"); - m_this->callback(xi, xq, numSamples); -} + int res; -void SDRPlayThread::callback(short *xi, short *xq, unsigned int numSamples) -{ - qDebug("SDRPlayThread::callback"); - if (m_convertBuffer.size() < numSamples) + m_running = true; + m_startWaiter.wakeAll(); + + while (m_running) { - m_convertBuffer.resize(numSamples); + if ((res = mirisdr_read_async(m_dev, &SDRPlayThread::callbackHelper, this, 32, SDRPLAY_INIT_NBSAMPLES)) < 0) + { + qCritical("SDRPlayThread::run: async read error: rc %d: %s", res, strerror(errno)); + break; + } } + m_running = false; +} + +void SDRPlayThread::callbackHelper(unsigned char* buf, uint32_t len, void* ctx) +{ + SDRPlayThread* thread = (SDRPlayThread*) ctx; + thread->callback((const qint16*) buf, len/2); +} + +void SDRPlayThread::callback(const qint16* buf, qint32 len) +{ SampleVector::iterator it = m_convertBuffer.begin(); if (m_log2Decim == 0) { - m_decimators.decimate1(&it, xi, xq, numSamples); + m_decimators.decimate1(&it, buf, len); } else { - if (m_fcPos == 0) // Infra + if (m_fcPos == 0) // Infradyne { switch (m_log2Decim) { case 1: - m_decimators.decimate2_inf(&it, xi, xq, numSamples); + m_decimators.decimate2_inf(&it, buf, len); break; case 2: - m_decimators.decimate4_inf(&it, xi, xq, numSamples); + m_decimators.decimate4_inf(&it, buf, len); break; case 3: - m_decimators.decimate8_inf(&it, xi, xq, numSamples); + m_decimators.decimate8_inf(&it, buf, len); break; case 4: - m_decimators.decimate16_inf(&it, xi, xq, numSamples); + m_decimators.decimate16_inf(&it, buf, len); break; case 5: - m_decimators.decimate32_inf(&it, xi, xq, numSamples); + m_decimators.decimate32_inf(&it, buf, len); break; case 6: - m_decimators.decimate64_inf(&it, xi, xq, numSamples); + m_decimators.decimate64_inf(&it, buf, len); break; default: break; } } - else if (m_fcPos == 1) // Supra + else if (m_fcPos == 1) // Supradyne { switch (m_log2Decim) { case 1: - m_decimators.decimate2_sup(&it, xi, xq, numSamples); + m_decimators.decimate2_sup(&it, buf, len); break; case 2: - m_decimators.decimate4_sup(&it, xi, xq, numSamples); + m_decimators.decimate4_sup(&it, buf, len); break; case 3: - m_decimators.decimate8_sup(&it, xi, xq, numSamples); + m_decimators.decimate8_sup(&it, buf, len); break; case 4: - m_decimators.decimate16_sup(&it, xi, xq, numSamples); + m_decimators.decimate16_sup(&it, buf, len); break; case 5: - m_decimators.decimate32_sup(&it, xi, xq, numSamples); + m_decimators.decimate32_sup(&it, buf, len); break; case 6: - m_decimators.decimate64_sup(&it, xi, xq, numSamples); + m_decimators.decimate64_sup(&it, buf, len); break; default: break; } } - else if (m_fcPos == 2) // Center + else // Centered { switch (m_log2Decim) { case 1: - m_decimators.decimate2_cen(&it, xi, xq, numSamples); + m_decimators.decimate2_cen(&it, buf, len); break; case 2: - m_decimators.decimate4_cen(&it, xi, xq, numSamples); + m_decimators.decimate4_cen(&it, buf, len); break; case 3: - m_decimators.decimate8_cen(&it, xi, xq, numSamples); + m_decimators.decimate8_cen(&it, buf, len); break; case 4: - m_decimators.decimate16_cen(&it, xi, xq, numSamples); + m_decimators.decimate16_cen(&it, buf, len); break; case 5: - m_decimators.decimate32_cen(&it, xi, xq, numSamples); + m_decimators.decimate32_cen(&it, buf, len); break; case 6: - m_decimators.decimate64_cen(&it, xi, xq, numSamples); + m_decimators.decimate64_cen(&it, buf, len); break; default: break; @@ -175,21 +182,10 @@ void SDRPlayThread::callback(short *xi, short *xq, unsigned int numSamples) } m_sampleFifo->write(m_convertBuffer.begin(), it); -} -void SDRPlayThread::run() -{ - int res; - - m_running = true; - m_startWaiter.wakeAll(); - - while(m_running) + if(!m_running) { - sleep(1); + mirisdr_cancel_async(m_dev); } - - m_running = false; } - diff --git a/plugins/samplesource/sdrplay/sdrplaythread.h b/plugins/samplesource/sdrplay/sdrplaythread.h index 4de99f303..2882f4a44 100644 --- a/plugins/samplesource/sdrplay/sdrplaythread.h +++ b/plugins/samplesource/sdrplay/sdrplaythread.h @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include "dsp/samplesinkfifo.h" #include "dsp/decimators.h" @@ -30,41 +30,34 @@ class SDRPlayThread : public QThread { Q_OBJECT public: - SDRPlayThread(SampleSinkFifo* sampleFifo, QObject* parent = NULL); + SDRPlayThread(mirisdr_dev_t* dev, SampleSinkFifo* sampleFifo, QObject* parent = NULL); ~SDRPlayThread(); void startWork(); void stopWork(); + void setSamplerate(int samplerate); void setLog2Decimation(unsigned int log2_decim); void setFcPos(int fcPos); - static void streamCallback ( - short *xi, - short *xq, - unsigned int firstSampleNum, - int grChanged, - int rfChanged, - int fsChanged, - unsigned int numSamples, - unsigned int reset, - void *cbContext); - private: QMutex m_startWaitMutex; QWaitCondition m_startWaiter; bool m_running; + mirisdr_dev_t *m_dev; SampleVector m_convertBuffer; SampleSinkFifo* m_sampleFifo; + int m_samplerate; unsigned int m_log2Decim; int m_fcPos; - static SDRPlayThread *m_this; Decimators m_decimators; void run(); - void callback(short *xi, short *xq, unsigned int numSamples); + void callback(const qint16* buf, qint32 len); + + static void callbackHelper(unsigned char* buf, uint32_t len, void* ctx); }; #endif /* PLUGINS_SAMPLESOURCE_SDRPLAY_SDRPLAYTHREAD_H_ */ diff --git a/sdrbase/dsp/dspdevicesourceengine.cpp b/sdrbase/dsp/dspdevicesourceengine.cpp index 58197414f..2c6b45cf2 100644 --- a/sdrbase/dsp/dspdevicesourceengine.cpp +++ b/sdrbase/dsp/dspdevicesourceengine.cpp @@ -473,7 +473,7 @@ DSPDeviceSourceEngine::State DSPDeviceSourceEngine::gotoRunning() DSPDeviceSourceEngine::State DSPDeviceSourceEngine::gotoError(const QString& errorMessage) { - qDebug() << "DSPDeviceSourceEngine::gotoError"; + qDebug() << "DSPDeviceSourceEngine::gotoError: " << errorMessage; m_errorMessage = errorMessage; m_deviceDescription.clear();