1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-14 12:22:00 -05:00
sdrangel/plugins/samplesource/sdrplayv3/sdrplayv3thread.cpp

406 lines
12 KiB
C++
Raw Normal View History

2021-04-11 16:21:36 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2016, 2019-2020, 2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
// Copyright (C) 2021-2022 Jon Beniston, M7RCE <jon@beniston.com> //
// Copyright (C) 2021 Franco Venturi <fventuri@comcast.net> //
2021-04-11 16:21:36 -04: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 //
// (at your option) any later version. //
// //
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <errno.h>
#include <chrono>
#include <thread>
2021-04-11 16:21:36 -04:00
#include "sdrplayv3thread.h"
#include "dsp/samplesinkfifo.h"
#include "util/poweroftwo.h"
2021-04-11 16:21:36 -04:00
#include <QDebug>
2023-11-22 09:28:35 -05:00
SDRPlayV3Thread::SDRPlayV3Thread(sdrplay_api_DeviceT* dev, SampleSinkFifo* sampleFifo, ReplayBuffer<qint16> *replayBuffer, QObject* parent) :
2021-04-11 16:21:36 -04:00
QThread(parent),
m_running(false),
m_dev(dev),
m_convertBuffer(SDRPLAYV3_INIT_NBSAMPLES),
m_sampleFifo(sampleFifo),
2023-11-22 09:28:35 -05:00
m_replayBuffer(replayBuffer),
2023-11-22 09:40:19 -05:00
m_samplerate(2000000),
2021-04-11 16:21:36 -04:00
m_log2Decim(0),
m_fcPos(0),
m_iqOrder(true),
m_iqCount(0)
2021-04-11 16:21:36 -04:00
{
}
SDRPlayV3Thread::~SDRPlayV3Thread()
{
stopWork();
}
void SDRPlayV3Thread::startWork()
{
sdrplay_api_ErrT err;
sdrplay_api_CallbackFnsT cbFns;
cbFns.StreamACbFn = &SDRPlayV3Thread::callbackHelper;
cbFns.StreamBCbFn = &SDRPlayV3Thread::callbackHelper;
cbFns.EventCbFn = &SDRPlayV3Thread::eventCallback;
if ((err = sdrplay_api_Init(m_dev->dev, &cbFns, this)) != sdrplay_api_Success)
{
qCritical() << "SDRPlayV3Thread::run: sdrplay_api_Init error: " << sdrplay_api_GetErrorString(err);
m_running = false;
return;
}
2021-04-11 16:21:36 -04:00
m_startWaitMutex.lock();
start();
while(!m_running) {
2021-04-11 16:21:36 -04:00
m_startWaiter.wait(&m_startWaitMutex, 100);
}
2021-04-11 16:21:36 -04:00
m_startWaitMutex.unlock();
}
void SDRPlayV3Thread::stopWork()
{
sdrplay_api_ErrT err;
if (m_running)
{
m_running = false;
if ((err = sdrplay_api_Uninit(m_dev->dev)) != sdrplay_api_Success) {
2021-04-11 16:21:36 -04:00
qWarning() << "SDRPlayV3Thread::callbackHelper: sdrplay_api_Uninit error: " << sdrplay_api_GetErrorString(err);
}
2021-04-11 16:21:36 -04:00
}
2021-04-11 16:21:36 -04:00
wait();
}
void SDRPlayV3Thread::setSamplerate(int samplerate)
{
m_samplerate = samplerate;
}
void SDRPlayV3Thread::setLog2Decimation(unsigned int log2_decim)
{
m_log2Decim = log2_decim;
}
void SDRPlayV3Thread::setFcPos(int fcPos)
{
m_fcPos = fcPos;
}
void SDRPlayV3Thread::resetRfChanged()
{
m_rfChanged = 0;
}
bool SDRPlayV3Thread::waitForRfChanged()
{
for (unsigned int i = 0; i < m_rfChangedTimeout && m_rfChanged == 0; i++)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
return m_rfChanged != 0;
}
2021-04-11 16:21:36 -04:00
// Don't really need a thread here - just using same structure as other plugins
void SDRPlayV3Thread::run()
{
m_running = true;
m_startWaiter.wakeAll();
}
void SDRPlayV3Thread::eventCallback(sdrplay_api_EventT eventId, sdrplay_api_TunerSelectT tuner, sdrplay_api_EventParamsT *params, void *cbContext)
{
// Could possibly report params->gainParams.currGain for eventId == sdrplay_api_GainChange
// or indicate ADC overload for eventId == sdrplay_api_PowerOverloadChange
(void) eventId;
(void) tuner;
(void) params;
(void) cbContext;
}
void SDRPlayV3Thread::callbackHelper(short *xi, short *xq, sdrplay_api_StreamCbParamsT *params, unsigned int numSamples, unsigned int reset, void *ctx)
{
(void) params;
(void) reset;
SDRPlayV3Thread* thread = (SDRPlayV3Thread*) ctx;
if (params->rfChanged)
thread->m_rfChanged = params->rfChanged;
2021-04-11 16:21:36 -04:00
if (thread->m_running)
{
// Interleave samples
2021-04-11 16:37:01 -04:00
for (int i = 0; i < (int)numSamples; i++)
2021-04-11 16:21:36 -04:00
{
thread->m_iq[thread->m_iqCount+i*2] = xi[i];
thread->m_iq[thread->m_iqCount+i*2+1] = xq[i];
2021-04-11 16:21:36 -04:00
}
thread->m_iqCount += numSamples * 2;
if (thread->m_iqCount > 8192) {
qCritical() << "SDRPlayV3Thread::callbackHelper: IQ buffer too small: " << numSamples;
}
// Decimators require length to be a power of 2
int iqLen = lowerPowerOfTwo(thread->m_iqCount);
2021-04-11 16:21:36 -04:00
if (thread->m_iqOrder) {
thread->callbackIQ(thread->m_iq, iqLen);
} else {
thread->callbackQI(thread->m_iq, iqLen);
}
// Shuffle buffer up
int iqRemaining = thread->m_iqCount - iqLen;
memmove(thread->m_iq, &thread->m_iq[iqLen], iqRemaining * sizeof(qint16));
thread->m_iqCount = iqRemaining;
2021-04-11 16:21:36 -04:00
}
}
2023-11-22 09:28:35 -05:00
void SDRPlayV3Thread::callbackIQ(const qint16* inBuf, qint32 len)
2021-04-11 16:21:36 -04:00
{
SampleVector::iterator it = m_convertBuffer.begin();
2023-11-22 09:28:35 -05:00
// Save data to replay buffer
2023-12-06 05:53:28 -05:00
m_replayBuffer->lock();
bool replayEnabled = m_replayBuffer->getSize() > 0;
if (replayEnabled) {
m_replayBuffer->write(inBuf, len);
}
2023-11-22 09:28:35 -05:00
2023-12-06 05:53:28 -05:00
const qint16* buf = inBuf;
qint32 remaining = len;
2023-11-22 09:28:35 -05:00
while (remaining > 0)
2023-12-06 05:53:28 -05:00
{
// Choose between live data or replayed data
if (replayEnabled && m_replayBuffer->useReplay()) {
len = m_replayBuffer->read(remaining, buf);
} else {
len = remaining;
}
remaining -= len;
2023-11-22 09:28:35 -05:00
if (m_log2Decim == 0)
2021-04-11 16:21:36 -04:00
{
2023-11-22 09:28:35 -05:00
m_decimatorsIQ.decimate1(&it, buf, len);
2021-04-11 16:21:36 -04:00
}
2023-11-22 09:28:35 -05:00
else
2021-04-11 16:21:36 -04:00
{
2023-11-22 09:28:35 -05:00
if (m_fcPos == 0) // Infradyne
2021-04-11 16:21:36 -04:00
{
2023-11-22 09:28:35 -05:00
switch (m_log2Decim)
{
case 1:
m_decimatorsIQ.decimate2_inf(&it, buf, len);
break;
case 2:
m_decimatorsIQ.decimate4_inf(&it, buf, len);
break;
case 3:
m_decimatorsIQ.decimate8_inf(&it, buf, len);
break;
case 4:
m_decimatorsIQ.decimate16_inf(&it, buf, len);
break;
case 5:
m_decimatorsIQ.decimate32_inf(&it, buf, len);
break;
case 6:
m_decimatorsIQ.decimate64_inf(&it, buf, len);
break;
default:
break;
}
2021-04-11 16:21:36 -04:00
}
2023-11-22 09:28:35 -05:00
else if (m_fcPos == 1) // Supradyne
2021-04-11 16:21:36 -04:00
{
2023-11-22 09:28:35 -05:00
switch (m_log2Decim)
{
case 1:
m_decimatorsIQ.decimate2_sup(&it, buf, len);
break;
case 2:
m_decimatorsIQ.decimate4_sup(&it, buf, len);
break;
case 3:
m_decimatorsIQ.decimate8_sup(&it, buf, len);
break;
case 4:
m_decimatorsIQ.decimate16_sup(&it, buf, len);
break;
case 5:
m_decimatorsIQ.decimate32_sup(&it, buf, len);
break;
case 6:
m_decimatorsIQ.decimate64_sup(&it, buf, len);
break;
default:
break;
}
}
else // Centered
{
switch (m_log2Decim)
{
case 1:
m_decimatorsIQ.decimate2_cen(&it, buf, len);
break;
case 2:
m_decimatorsIQ.decimate4_cen(&it, buf, len);
break;
case 3:
m_decimatorsIQ.decimate8_cen(&it, buf, len);
break;
case 4:
m_decimatorsIQ.decimate16_cen(&it, buf, len);
break;
case 5:
m_decimatorsIQ.decimate32_cen(&it, buf, len);
break;
case 6:
m_decimatorsIQ.decimate64_cen(&it, buf, len);
break;
default:
break;
}
2021-04-11 16:21:36 -04:00
}
}
}
2023-11-22 09:28:35 -05:00
m_replayBuffer->unlock();
2021-04-11 16:21:36 -04:00
m_sampleFifo->write(m_convertBuffer.begin(), it);
}
2023-11-22 09:28:35 -05:00
void SDRPlayV3Thread::callbackQI(const qint16* inBuf, qint32 len)
{
SampleVector::iterator it = m_convertBuffer.begin();
2023-11-22 09:28:35 -05:00
// Save data to replay buffer
2023-12-06 05:53:28 -05:00
m_replayBuffer->lock();
bool replayEnabled = m_replayBuffer->getSize() > 0;
if (replayEnabled) {
m_replayBuffer->write(inBuf, len);
}
2023-11-22 09:28:35 -05:00
2023-12-06 05:53:28 -05:00
const qint16* buf = inBuf;
qint32 remaining = len;
2023-11-22 09:28:35 -05:00
while (remaining > 0)
2023-12-06 05:53:28 -05:00
{
// Choose between live data or replayed data
if (replayEnabled && m_replayBuffer->useReplay()) {
len = m_replayBuffer->read(remaining, buf);
} else {
len = remaining;
}
remaining -= len;
2023-11-22 09:28:35 -05:00
if (m_log2Decim == 0)
{
2023-11-22 09:28:35 -05:00
m_decimatorsQI.decimate1(&it, buf, len);
}
2023-11-22 09:28:35 -05:00
else
{
2023-11-22 09:28:35 -05:00
if (m_fcPos == 0) // Infradyne
{
2023-11-22 09:28:35 -05:00
switch (m_log2Decim)
{
case 1:
m_decimatorsQI.decimate2_inf(&it, buf, len);
break;
case 2:
m_decimatorsQI.decimate4_inf(&it, buf, len);
break;
case 3:
m_decimatorsQI.decimate8_inf(&it, buf, len);
break;
case 4:
m_decimatorsQI.decimate16_inf(&it, buf, len);
break;
case 5:
m_decimatorsQI.decimate32_inf(&it, buf, len);
break;
case 6:
m_decimatorsQI.decimate64_inf(&it, buf, len);
break;
default:
break;
}
}
2023-11-22 09:28:35 -05:00
else if (m_fcPos == 1) // Supradyne
{
2023-11-22 09:28:35 -05:00
switch (m_log2Decim)
{
case 1:
m_decimatorsQI.decimate2_sup(&it, buf, len);
break;
case 2:
m_decimatorsQI.decimate4_sup(&it, buf, len);
break;
case 3:
m_decimatorsQI.decimate8_sup(&it, buf, len);
break;
case 4:
m_decimatorsQI.decimate16_sup(&it, buf, len);
break;
case 5:
m_decimatorsQI.decimate32_sup(&it, buf, len);
break;
case 6:
m_decimatorsQI.decimate64_sup(&it, buf, len);
break;
default:
break;
}
}
else // Centered
{
switch (m_log2Decim)
{
case 1:
m_decimatorsQI.decimate2_cen(&it, buf, len);
break;
case 2:
m_decimatorsQI.decimate4_cen(&it, buf, len);
break;
case 3:
m_decimatorsQI.decimate8_cen(&it, buf, len);
break;
case 4:
m_decimatorsQI.decimate16_cen(&it, buf, len);
break;
case 5:
m_decimatorsQI.decimate32_cen(&it, buf, len);
break;
case 6:
m_decimatorsQI.decimate64_cen(&it, buf, len);
break;
default:
break;
}
}
}
}
2023-11-22 09:28:35 -05:00
m_replayBuffer->unlock();
m_sampleFifo->write(m_convertBuffer.begin(), it);
}