1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2025-02-03 09:44:01 -05:00

Create own class for logical device DSP engine. Use this class in main DSP engine

This commit is contained in:
f4exb 2015-10-15 04:05:19 +02:00
parent 9cba7515d8
commit 9c9776e569
8 changed files with 896 additions and 714 deletions

View File

@ -33,8 +33,8 @@ ENDIF()
##############################################################################
#include(${QT_USE_FILE})
#set( QT_DEFINITIONS "${QT_DEFINITIONS} -DQT_NO_DEBUG_OUTPUT" )
set( QT_DEFINITIONS "${QT_DEFINITIONS}" )
set( QT_DEFINITIONS "${QT_DEFINITIONS} -DQT_NO_DEBUG_OUTPUT" )
#set( QT_DEFINITIONS "${QT_DEFINITIONS}" )
add_definitions(${QT_DEFINITIONS})
if(MSVC)
@ -62,6 +62,7 @@ set(sdrbase_SOURCES
sdrbase/dsp/ctcssdetector.cpp
sdrbase/dsp/dspcommands.cpp
sdrbase/dsp/dspengine.cpp
sdrbase/dsp/dspdeviceengine.cpp
sdrbase/dsp/fftengine.cpp
sdrbase/dsp/fftfilt.cxx
sdrbase/dsp/fftwindow.cpp
@ -134,6 +135,7 @@ set(sdrbase_HEADERS
include/dsp/decimators.h
include/dsp/dspcommands.h
include/dsp/dspengine.h
include/dsp/dspdeviceengine.h
include/dsp/dsptypes.h
include/dsp/fftengine.h
include/dsp/fftfilt.h

View File

@ -165,7 +165,7 @@ Assuming Debian Jessie is used:
- Trigger line display for all trigger modes
- Coarse and fine trigger level sliders
- Minimalist recording (no file choice)
- File sample source plugin (recording reader) not working
- File sample source plugin (recording reader)
<h2>Major redesign</h2>
@ -184,11 +184,14 @@ Assuming Debian Jessie is used:
- Many other little things...
<h1>To Do</h1>
- Allow the handling of more than one device at the same time. For Rx/Tx devices like the BladeRF Rx and Tx appear as two devices although the same plugin handles both. This effectively opens Tx support.
- Allow the handling of more than one device at the same time. For Rx/Tx devices like the BladeRF Rx and Tx appear as two logical devices with two plugin instances and a common handler for the physical device services both plugins. This effectively opens Tx support.
- Tx channels
- Possibility to connect channels for example Rx to Tx or single Rx channel to dual Rx channel supporting MI(MO) features like 360 degree polarization detection.
- Specialize plugins into channel and sample source plugins since both have almost complete different requirements and only little in common
- Enhance presets management (Edit, Move, Import/Export from/to human readable format like JSON).
- 32 bit samples for the Channel Analyzer
- Trace history in the Channel Analyzer
- Enhance presets management (Edit, Move, Import/Export from/to human readable format like JSON).
- Headless mode based on a saved configuration in above human readable form
- Allow arbitrary sample rate for channelizers and demodulators (not multiple of 48 kHz). Prerequisite for polyphase channelizer
- Implement polyphase channelizer
- Level calibration

View File

@ -0,0 +1,125 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 F4EXB //
// written by Edouard Griffiths //
// //
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_DSPDEVICEENGINE_H
#define INCLUDE_DSPDEVICEENGINE_H
#include <QThread>
#include <QTimer>
#include <QMutex>
#include <QWaitCondition>
#include "dsp/dsptypes.h"
#include "dsp/fftwindow.h"
#include "dsp/samplefifo.h"
#include "util/messagequeue.h"
#include "util/syncmessenger.h"
#include "util/export.h"
class SampleSource;
class SampleSink;
class ThreadedSampleSink;
class SDRANGEL_API DSPDeviceEngine : public QThread {
Q_OBJECT
public:
enum State {
StNotStarted, //!< engine is before initialization
StIdle, //!< engine is idle
StReady, //!< engine is ready to run
StRunning, //!< engine is running
StError //!< engine is in error
};
DSPDeviceEngine(QObject* parent = NULL);
~DSPDeviceEngine();
MessageQueue* getInputMessageQueue() { return &m_inputMessageQueue; }
MessageQueue* getOutputMessageQueue() { return &m_outputMessageQueue; }
void start(); //!< This thread start
void stop(); //!< This thread stop
bool initAcquisition(); //!< Initialize acquisition sequence
bool startAcquisition(); //!< Start acquisition sequence
void stopAcquistion(); //!< Stop acquisition sequence
void setSource(SampleSource* source); //!< Set the sample source type
void setSourceSequence(int sequence); //!< Set the sample source sequence in type
void addSink(SampleSink* sink); //!< Add a sample sink
void removeSink(SampleSink* sink); //!< Remove a sample sink
void addThreadedSink(ThreadedSampleSink* sink); //!< Add a sample sink that will run on its own thread
void removeThreadedSink(ThreadedSampleSink* sink); //!< Remove a sample sink that runs on its own thread
void configureCorrections(bool dcOffsetCorrection, bool iqImbalanceCorrection); //!< Configure DSP corrections
State state() const { return m_state; } //!< Return DSP engine current state
QString errorMessage(); //!< Return the current error message
QString sourceDeviceDescription(); //!< Return the source device description
private:
MessageQueue m_inputMessageQueue; //<! Input message queue. Post here.
MessageQueue m_outputMessageQueue; //<! Output message queue. Listen here.
SyncMessenger m_syncMessenger; //!< Used to process messages synchronously with the thread
State m_state;
QString m_errorMessage;
QString m_deviceDescription;
SampleSource* m_sampleSource;
int m_sampleSourceSequence;
typedef std::list<SampleSink*> SampleSinks;
SampleSinks m_sampleSinks; //!< sample sinks within main thread (usually spectrum, file output)
typedef std::list<ThreadedSampleSink*> ThreadedSampleSinks;
ThreadedSampleSinks m_threadedSampleSinks; //!< sample sinks on their own threads (usually channels)
uint m_sampleRate;
quint64 m_centerFrequency;
bool m_dcOffsetCorrection;
bool m_iqImbalanceCorrection;
double m_iOffset, m_qOffset;
qint32 m_iRange;
qint32 m_qRange;
qint32 m_imbalance;
void run();
void dcOffset(SampleVector::iterator begin, SampleVector::iterator end);
void imbalance(SampleVector::iterator begin, SampleVector::iterator end);
void work(); //!< transfer samples from source to sinks if in running state
State gotoIdle(); //!< Go to the idle state
State gotoInit(); //!< Go to the acquisition init state from idle
State gotoRunning(); //!< Go to the running state from ready state
State gotoError(const QString& errorMsg); //!< Go to an error state
void handleSetSource(SampleSource* source); //!< Manage source setting
private slots:
void handleData(); //!< Handle data when samples from source FIFO are ready to be processed
void handleInputMessages(); //!< Handle input message queue
void handleSynchronousMessages(); //!< Handle synchronous messages with the thread
};
#endif // INCLUDE_DSPDEVICEENGINE_H

View File

@ -18,48 +18,29 @@
#ifndef INCLUDE_DSPENGINE_H
#define INCLUDE_DSPENGINE_H
#include <QThread>
#include <QTimer>
#include <QMutex>
#include <QWaitCondition>
#include "dsp/dsptypes.h"
#include "dsp/fftwindow.h"
#include "dsp/samplefifo.h"
#include <QObject>
#include "dsp/dspdeviceengine.h"
#include "audio/audiooutput.h"
#include "util/messagequeue.h"
#include "util/syncmessenger.h"
#include "util/export.h"
class SampleSource;
class SampleSink;
class DSPDeviceEngine;
class ThreadedSampleSink;
class AudioFifo;
class SDRANGEL_API DSPEngine : public QThread {
class SDRANGEL_API DSPEngine : public QObject {
Q_OBJECT
public:
enum State {
StNotStarted, //!< engine is before initialization
StIdle, //!< engine is idle
StReady, //!< engine is ready to run
StRunning, //!< engine is running
StError //!< engine is in error
};
DSPEngine(QObject* parent = NULL);
DSPEngine();
~DSPEngine();
static DSPEngine *instance();
MessageQueue* getInputMessageQueue() { return &m_inputMessageQueue; }
MessageQueue* getOutputMessageQueue() { return &m_outputMessageQueue; }
MessageQueue* getInputMessageQueue();
MessageQueue* getOutputMessageQueue();
uint getAudioSampleRate() const { return m_audioSampleRate; }
void setAudioSampleRate(uint rate);
void start(); //!< This thread start
void stop(); //!< This thread stop
void start(); //!< Device engine(s) start
void stop(); //!< Device engine(s) stop
bool initAcquisition(); //!< Initialize acquisition sequence
bool startAcquisition(); //!< Start acquisition sequence
@ -77,62 +58,17 @@ public:
void addAudioSink(AudioFifo* audioFifo); //!< Add the audio sink
void removeAudioSink(AudioFifo* audioFifo); //!< Remove the audio sink
void configureCorrections(bool dcOffsetCorrection, bool iqImbalanceCorrection); //!< Configure DSP corrections
void configureCorrections(bool dcOffsetCorrection, bool iqImbalanceCorrection); //!< Configure DSP corrections
State state() const { return m_state; } //!< Return DSP engine current state
DSPDeviceEngine::State state() const;
QString errorMessage(); //!< Return the current error message
QString errorMessage(); //!< Return the current error message
QString sourceDeviceDescription(); //!< Return the source device description
private:
MessageQueue m_inputMessageQueue; //<! Input message queue. Post here.
MessageQueue m_outputMessageQueue; //<! Output message queue. Listen here.
SyncMessenger m_syncMessenger; //!< Used to process messages synchronously with the thread
State m_state;
QString m_errorMessage;
QString m_deviceDescription;
SampleSource* m_sampleSource;
int m_sampleSourceSequence;
typedef std::list<SampleSink*> SampleSinks;
SampleSinks m_sampleSinks; //!< sample sinks within main thread (usually spectrum, file output)
typedef std::list<ThreadedSampleSink*> ThreadedSampleSinks;
ThreadedSampleSinks m_threadedSampleSinks; //!< sample sinks on their own threads (usually channels)
DSPDeviceEngine *m_deviceEngine;
AudioOutput m_audioOutput;
uint m_sampleRate;
quint64 m_centerFrequency;
uint m_audioSampleRate;
bool m_dcOffsetCorrection;
bool m_iqImbalanceCorrection;
double m_iOffset, m_qOffset;
qint32 m_iRange;
qint32 m_qRange;
qint32 m_imbalance;
void run();
void dcOffset(SampleVector::iterator begin, SampleVector::iterator end);
void imbalance(SampleVector::iterator begin, SampleVector::iterator end);
void work(); //!< transfer samples from source to sinks if in running state
State gotoIdle(); //!< Go to the idle state
State gotoInit(); //!< Go to the acquisition init state from idle
State gotoRunning(); //!< Go to the running state from ready state
State gotoError(const QString& errorMsg); //!< Go to an error state
void handleSetSource(SampleSource* source); //!< Manage source setting
private slots:
void handleData(); //!< Handle data when samples from source FIFO are ready to be processed
void handleInputMessages(); //!< Handle input message queue
void handleSynchronousMessages(); //!< Handle synchronous messages with the thread
};
#endif // INCLUDE_DSPENGINE_H

View File

@ -0,0 +1,646 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 F4EXB //
// written by Edouard Griffiths //
// //
// 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/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <QDebug>
#include "dsp/dspdeviceengine.h"
#include "dsp/channelizer.h"
#include "dsp/samplefifo.h"
#include "dsp/samplesink.h"
#include "dsp/threadedsamplesink.h"
#include "dsp/dspcommands.h"
#include "dsp/samplesource.h"
DSPDeviceEngine::DSPDeviceEngine(QObject* parent) :
QThread(parent),
m_state(StNotStarted),
m_sampleSource(0),
m_sampleSourceSequence(0),
m_sampleSinks(),
m_sampleRate(0),
m_centerFrequency(0),
m_dcOffsetCorrection(false),
m_iqImbalanceCorrection(false),
m_iOffset(0),
m_qOffset(0),
m_iRange(1 << 16),
m_qRange(1 << 16),
m_imbalance(65536)
{
moveToThread(this);
}
DSPDeviceEngine::~DSPDeviceEngine()
{
wait();
}
void DSPDeviceEngine::run()
{
qDebug() << "DSPDeviceEngine::run";
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection);
connect(&m_syncMessenger, SIGNAL(messageSent()), this, SLOT(handleSynchronousMessages()), Qt::QueuedConnection);
m_state = StIdle;
m_syncMessenger.done(); // Release start() that is waiting in main trhead
exec();
}
void DSPDeviceEngine::start()
{
qDebug() << "DSPDeviceEngine::start";
DSPPing cmd;
QThread::start();
m_syncMessenger.sendWait(cmd);
}
void DSPDeviceEngine::stop()
{
qDebug() << "DSPDeviceEngine::stop";
DSPExit cmd;
m_syncMessenger.sendWait(cmd);
}
bool DSPDeviceEngine::initAcquisition()
{
qDebug() << "DSPDeviceEngine::initAcquisition";
DSPAcquisitionInit cmd;
return m_syncMessenger.sendWait(cmd) == StReady;
}
bool DSPDeviceEngine::startAcquisition()
{
qDebug() << "DSPDeviceEngine::startAcquisition";
DSPAcquisitionStart cmd;
return m_syncMessenger.sendWait(cmd) == StRunning;
}
void DSPDeviceEngine::stopAcquistion()
{
qDebug() << "DSPDeviceEngine::stopAcquistion";
DSPAcquisitionStop cmd;
m_syncMessenger.storeMessage(cmd);
handleSynchronousMessages();
if(m_dcOffsetCorrection)
{
qDebug("DC offset:%f,%f", m_iOffset, m_qOffset);
}
}
void DSPDeviceEngine::setSource(SampleSource* source)
{
qDebug() << "DSPDeviceEngine::setSource";
DSPSetSource cmd(source);
m_syncMessenger.sendWait(cmd);
}
void DSPDeviceEngine::setSourceSequence(int sequence)
{
qDebug("DSPDeviceEngine::setSourceSequence: seq: %d", sequence);
m_sampleSourceSequence = sequence;
}
void DSPDeviceEngine::addSink(SampleSink* sink)
{
qDebug() << "DSPDeviceEngine::addSink: " << sink->objectName().toStdString().c_str();
DSPAddSink cmd(sink);
m_syncMessenger.sendWait(cmd);
}
void DSPDeviceEngine::removeSink(SampleSink* sink)
{
qDebug() << "DSPDeviceEngine::removeSink: " << sink->objectName().toStdString().c_str();
DSPRemoveSink cmd(sink);
m_syncMessenger.sendWait(cmd);
}
void DSPDeviceEngine::addThreadedSink(ThreadedSampleSink* sink)
{
qDebug() << "DSPDeviceEngine::addThreadedSink: " << sink->objectName().toStdString().c_str();
DSPAddThreadedSampleSink cmd(sink);
m_syncMessenger.sendWait(cmd);
}
void DSPDeviceEngine::removeThreadedSink(ThreadedSampleSink* sink)
{
qDebug() << "DSPDeviceEngine::removeThreadedSink: " << sink->objectName().toStdString().c_str();
DSPRemoveThreadedSampleSink cmd(sink);
m_syncMessenger.sendWait(cmd);
}
void DSPDeviceEngine::configureCorrections(bool dcOffsetCorrection, bool iqImbalanceCorrection)
{
qDebug() << "DSPDeviceEngine::configureCorrections";
DSPConfigureCorrection* cmd = new DSPConfigureCorrection(dcOffsetCorrection, iqImbalanceCorrection);
m_inputMessageQueue.push(cmd);
}
QString DSPDeviceEngine::errorMessage()
{
qDebug() << "DSPDeviceEngine::errorMessage";
DSPGetErrorMessage cmd;
m_syncMessenger.sendWait(cmd);
return cmd.getErrorMessage();
}
QString DSPDeviceEngine::sourceDeviceDescription()
{
qDebug() << "DSPDeviceEngine::sourceDeviceDescription";
DSPGetSourceDeviceDescription cmd;
m_syncMessenger.sendWait(cmd);
return cmd.getDeviceDescription();
}
void DSPDeviceEngine::dcOffset(SampleVector::iterator begin, SampleVector::iterator end)
{
double count;
int io = 0;
int qo = 0;
Sample corr((qint16)m_iOffset, (qint16)m_qOffset);
// sum and correct in one pass
for(SampleVector::iterator it = begin; it < end; it++)
{
io += it->real();
qo += it->imag();
*it -= corr;
}
// moving average
count = end - begin;
m_iOffset = (15.0 * m_iOffset + (double)io / count) / 16.0;
m_qOffset = (15.0 * m_qOffset + (double)qo / count) / 16.0;
}
void DSPDeviceEngine::imbalance(SampleVector::iterator begin, SampleVector::iterator end)
{
int iMin = 0;
int iMax = 0;
int qMin = 0;
int qMax = 0;
// find value ranges for both I and Q
// both intervals should be same same size (for a perfect circle)
for (SampleVector::iterator it = begin; it < end; it++)
{
if (it != begin)
{
if (it->real() < iMin) {
iMin = it->real();
} else if (it->real() > iMax) {
iMax = it->real();
}
if (it->imag() < qMin) {
qMin = it->imag();
} else if (it->imag() > qMax) {
qMax = it->imag();
}
}
else
{
iMin = it->real();
iMax = it->real();
qMin = it->imag();
qMax = it->imag();
}
}
// sliding average (el cheapo again)
m_iRange = (m_iRange * 15 + (iMax - iMin)) >> 4;
m_qRange = (m_qRange * 15 + (qMax - qMin)) >> 4;
// calculate imbalance as Q15.16
if(m_qRange != 0) {
m_imbalance = ((uint)m_iRange << 16) / (uint)m_qRange;
}
// correct imbalance and convert back to signed int 16
for(SampleVector::iterator it = begin; it < end; it++) {
it->m_imag = (it->m_imag * m_imbalance) >> 16;
}
}
void DSPDeviceEngine::work()
{
SampleFifo* sampleFifo = m_sampleSource->getSampleFifo();
std::size_t samplesDone = 0;
bool positiveOnly = false;
while ((sampleFifo->fill() > 0) && (m_inputMessageQueue.size() == 0) && (samplesDone < m_sampleRate))
{
SampleVector::iterator part1begin;
SampleVector::iterator part1end;
SampleVector::iterator part2begin;
SampleVector::iterator part2end;
std::size_t count = sampleFifo->readBegin(sampleFifo->fill(), &part1begin, &part1end, &part2begin, &part2end);
// first part of FIFO data
if (part1begin != part1end)
{
// correct stuff
if (m_dcOffsetCorrection)
{
dcOffset(part1begin, part1end);
}
if (m_iqImbalanceCorrection)
{
imbalance(part1begin, part1end);
}
// feed data to direct sinks
for (SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); ++it)
{
(*it)->feed(part1begin, part1end, positiveOnly);
}
// feed data to threaded sinks
for (ThreadedSampleSinks::const_iterator it = m_threadedSampleSinks.begin(); it != m_threadedSampleSinks.end(); ++it)
{
(*it)->feed(part1begin, part1end, positiveOnly);
}
}
// second part of FIFO data (used when block wraps around)
if(part2begin != part2end)
{
// correct stuff
if (m_dcOffsetCorrection)
{
dcOffset(part2begin, part2end);
}
if (m_iqImbalanceCorrection)
{
imbalance(part2begin, part2end);
}
// feed data to direct sinks
for (SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); it++)
{
(*it)->feed(part2begin, part2end, positiveOnly);
}
// feed data to threaded sinks
for (ThreadedSampleSinks::const_iterator it = m_threadedSampleSinks.begin(); it != m_threadedSampleSinks.end(); ++it)
{
(*it)->feed(part2begin, part2end, positiveOnly);
}
}
// adjust FIFO pointers
sampleFifo->readCommit((unsigned int) count);
samplesDone += count;
}
}
// notStarted -> idle -> init -> running -+
// ^ |
// +-----------------------+
DSPDeviceEngine::State DSPDeviceEngine::gotoIdle()
{
qDebug() << "DSPDeviceEngine::gotoIdle";
switch(m_state) {
case StNotStarted:
return StNotStarted;
case StIdle:
case StError:
return StIdle;
case StReady:
case StRunning:
break;
}
if(m_sampleSource == 0)
{
return StIdle;
}
// stop everything
for(SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); it++)
{
(*it)->stop();
}
m_sampleSource->stop();
m_deviceDescription.clear();
m_sampleRate = 0;
return StIdle;
}
DSPDeviceEngine::State DSPDeviceEngine::gotoInit()
{
qDebug() << "DSPDeviceEngine::gotoInit";
switch(m_state) {
case StNotStarted:
return StNotStarted;
case StRunning: // FIXME: assumes it goes first through idle state. Could we get back to init from running directly?
return StRunning;
case StReady:
return StReady;
case StIdle:
case StError:
break;
}
if (m_sampleSource == 0)
{
return gotoError("No sample source configured");
}
// init: pass sample rate and center frequency to all sample rate and/or center frequency dependent sinks and wait for completion
m_iOffset = 0;
m_qOffset = 0;
m_iRange = 1 << 16;
m_qRange = 1 << 16;
m_deviceDescription = m_sampleSource->getDeviceDescription();
m_centerFrequency = m_sampleSource->getCenterFrequency();
m_sampleRate = m_sampleSource->getSampleRate();
qDebug() << "DSPDeviceEngine::gotoInit: " << m_deviceDescription.toStdString().c_str() << ": "
<< " sampleRate: " << m_sampleRate
<< " centerFrequency: " << m_centerFrequency;
DSPSignalNotification notif(m_sampleRate, m_centerFrequency);
for (SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); ++it)
{
qDebug() << "DSPDeviceEngine::gotoInit: initializing " << (*it)->objectName().toStdString().c_str();
(*it)->handleMessage(notif);
}
for (ThreadedSampleSinks::const_iterator it = m_threadedSampleSinks.begin(); it != m_threadedSampleSinks.end(); ++it)
{
qDebug() << "DSPDeviceEngine::gotoInit: initializing ThreadedSampleSink(" << (*it)->getSampleSinkObjectName().toStdString().c_str() << ")";
(*it)->handleSinkMessage(notif);
}
// pass data to listeners
DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy for the output queue
m_outputMessageQueue.push(rep);
return StReady;
}
DSPDeviceEngine::State DSPDeviceEngine::gotoRunning()
{
qDebug() << "DSPDeviceEngine::gotoRunning";
switch(m_state)
{
case StNotStarted:
return StNotStarted;
case StIdle:
return StIdle;
case StRunning:
return StRunning;
case StReady:
case StError:
break;
}
if(m_sampleSource == NULL) {
return gotoError("DSPDeviceEngine::gotoRunning: No sample source configured");
}
qDebug() << "DSPDeviceEngine::gotoRunning: " << m_deviceDescription.toStdString().c_str() << " started";
// Start everything
if(!m_sampleSource->start(m_sampleSourceSequence))
{
return gotoError("Could not start sample source");
}
for(SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); it++)
{
qDebug() << "DSPDeviceEngine::gotoRunning: starting " << (*it)->objectName().toStdString().c_str();
(*it)->start();
}
for (ThreadedSampleSinks::const_iterator it = m_threadedSampleSinks.begin(); it != m_threadedSampleSinks.end(); ++it)
{
qDebug() << "DSPDeviceEngine::gotoRunning: starting ThreadedSampleSink(" << (*it)->getSampleSinkObjectName().toStdString().c_str() << ")";
(*it)->start();
}
qDebug() << "DSPDeviceEngine::gotoRunning:input message queue pending: " << m_inputMessageQueue.size();
return StRunning;
}
DSPDeviceEngine::State DSPDeviceEngine::gotoError(const QString& errorMessage)
{
qDebug() << "DSPDeviceEngine::gotoError";
m_errorMessage = errorMessage;
m_deviceDescription.clear();
m_state = StError;
return StError;
}
void DSPDeviceEngine::handleSetSource(SampleSource* source)
{
gotoIdle();
if(m_sampleSource != 0)
{
disconnect(m_sampleSource->getSampleFifo(), SIGNAL(dataReady()), this, SLOT(handleData()));
}
m_sampleSource = source;
if(m_sampleSource != 0)
{
qDebug("DSPDeviceEngine::handleSetSource: set %s", qPrintable(source->getDeviceDescription()));
connect(m_sampleSource->getSampleFifo(), SIGNAL(dataReady()), this, SLOT(handleData()), Qt::QueuedConnection);
}
else
{
qDebug("DSPDeviceEngine::handleSetSource: set none");
}
}
void DSPDeviceEngine::handleData()
{
if(m_state == StRunning)
{
work();
}
}
void DSPDeviceEngine::handleSynchronousMessages()
{
Message *message = m_syncMessenger.getMessage();
qDebug() << "DSPDeviceEngine::handleSynchronousMessages: " << message->getIdentifier();
if (DSPExit::match(*message))
{
gotoIdle();
m_state = StNotStarted;
exit();
}
else if (DSPAcquisitionInit::match(*message))
{
m_state = gotoIdle();
if(m_state == StIdle) {
m_state = gotoInit(); // State goes ready if init is performed
}
}
else if (DSPAcquisitionStart::match(*message))
{
if(m_state == StReady) {
m_state = gotoRunning();
}
}
else if (DSPAcquisitionStop::match(*message))
{
m_state = gotoIdle();
}
else if (DSPGetSourceDeviceDescription::match(*message))
{
((DSPGetSourceDeviceDescription*) message)->setDeviceDescription(m_deviceDescription);
}
else if (DSPGetErrorMessage::match(*message))
{
((DSPGetErrorMessage*) message)->setErrorMessage(m_errorMessage);
}
else if (DSPSetSource::match(*message)) {
handleSetSource(((DSPSetSource*) message)->getSampleSource());
}
else if (DSPAddSink::match(*message))
{
SampleSink* sink = ((DSPAddSink*) message)->getSampleSink();
m_sampleSinks.push_back(sink);
}
else if (DSPRemoveSink::match(*message))
{
SampleSink* sink = ((DSPRemoveSink*) message)->getSampleSink();
if(m_state == StRunning) {
sink->stop();
}
m_sampleSinks.remove(sink);
}
else if (DSPAddThreadedSampleSink::match(*message))
{
ThreadedSampleSink *threadedSink = ((DSPAddThreadedSampleSink*) message)->getThreadedSampleSink();
m_threadedSampleSinks.push_back(threadedSink);
threadedSink->start();
}
else if (DSPRemoveThreadedSampleSink::match(*message))
{
ThreadedSampleSink* threadedSink = ((DSPRemoveThreadedSampleSink*) message)->getThreadedSampleSink();
threadedSink->stop();
m_threadedSampleSinks.remove(threadedSink);
}
m_syncMessenger.done(m_state);
}
void DSPDeviceEngine::handleInputMessages()
{
qDebug() << "DSPDeviceEngine::handleInputMessages";
Message* message;
while ((message = m_inputMessageQueue.pop()) != 0)
{
qDebug("DSPDeviceEngine::handleInputMessages: message: %s", message->getIdentifier());
if (DSPConfigureCorrection::match(*message))
{
DSPConfigureCorrection* conf = (DSPConfigureCorrection*) message;
m_iqImbalanceCorrection = conf->getIQImbalanceCorrection();
if(m_dcOffsetCorrection != conf->getDCOffsetCorrection())
{
m_dcOffsetCorrection = conf->getDCOffsetCorrection();
m_iOffset = 0;
m_qOffset = 0;
}
if(m_iqImbalanceCorrection != conf->getIQImbalanceCorrection())
{
m_iqImbalanceCorrection = conf->getIQImbalanceCorrection();
m_iRange = 1 << 16;
m_qRange = 1 << 16;
m_imbalance = 65536;
}
delete message;
}
else if (DSPSignalNotification::match(*message))
{
DSPSignalNotification *notif = (DSPSignalNotification *) message;
// update DSP values
m_sampleRate = notif->getSampleRate();
m_centerFrequency = notif->getCenterFrequency();
qDebug() << "DSPDeviceEngine::handleInputMessages: DSPSignalNotification(" << m_sampleRate << "," << m_centerFrequency << ")";
// forward source changes to sinks with immediate execution
for(SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); it++)
{
qDebug() << "DSPDeviceEngine::handleInputMessages: forward message to " << (*it)->objectName().toStdString().c_str();
(*it)->handleMessage(*message);
}
for (ThreadedSampleSinks::const_iterator it = m_threadedSampleSinks.begin(); it != m_threadedSampleSinks.end(); ++it)
{
qDebug() << "DSPDeviceEngine::handleSourceMessages: forward message to ThreadedSampleSink(" << (*it)->getSampleSinkObjectName().toStdString().c_str() << ")";
(*it)->handleSinkMessage(*message);
}
// forward changes to listeners on DSP output queue
DSPSignalNotification* rep = new DSPSignalNotification(*notif); // make a copy for the output queue
m_outputMessageQueue.push(rep);
delete message;
}
}
}

View File

@ -15,39 +15,19 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <QDebug>
#include <QGlobalStatic>
#include <QThread>
#include "dsp/dspengine.h"
#include "dsp/channelizer.h"
#include "dsp/samplefifo.h"
#include "dsp/samplesink.h"
#include "dsp/threadedsamplesink.h"
#include "dsp/dspcommands.h"
#include "dsp/samplesource.h"
DSPEngine::DSPEngine(QObject* parent) :
QThread(parent),
m_state(StNotStarted),
m_sampleSource(0),
m_sampleSourceSequence(0),
m_sampleSinks(),
m_sampleRate(0),
m_centerFrequency(0),
m_audioSampleRate(48000),
m_dcOffsetCorrection(false),
m_iqImbalanceCorrection(false),
m_iOffset(0),
m_qOffset(0),
m_iRange(1 << 16),
m_qRange(1 << 16),
m_imbalance(65536)
DSPEngine::DSPEngine() :
m_audioSampleRate(48000) // Use default output device at 48 kHz
{
moveToThread(this);
m_deviceEngine = new DSPDeviceEngine();
}
DSPEngine::~DSPEngine()
{
wait();
delete m_deviceEngine;
}
Q_GLOBAL_STATIC(DSPEngine, dspEngine)
@ -56,629 +36,121 @@ DSPEngine *DSPEngine::instance()
return dspEngine;
}
void DSPEngine::run()
MessageQueue* DSPEngine::getInputMessageQueue()
{
qDebug() << "DSPEngine::run";
return m_deviceEngine->getInputMessageQueue();
}
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()), Qt::QueuedConnection);
connect(&m_syncMessenger, SIGNAL(messageSent()), this, SLOT(handleSynchronousMessages()), Qt::QueuedConnection);
m_state = StIdle;
m_syncMessenger.done(); // Release start() that is waiting in main trhead
exec();
MessageQueue* DSPEngine::getOutputMessageQueue()
{
return m_deviceEngine->getOutputMessageQueue();
}
void DSPEngine::start()
{
qDebug() << "DSPEngine::start";
DSPPing cmd;
QThread::start();
m_syncMessenger.sendWait(cmd);
qDebug("DSPEngine::start");
m_deviceEngine->start();
}
void DSPEngine::stop()
{
qDebug() << "DSPEngine::stop";
DSPExit cmd;
m_syncMessenger.sendWait(cmd);
qDebug("DSPEngine::stop");
m_audioOutput.stop();
m_deviceEngine->stop();
}
bool DSPEngine::initAcquisition()
{
qDebug() << "DSPEngine::initAcquisition";
DSPAcquisitionInit cmd;
return m_syncMessenger.sendWait(cmd) == StReady;
qDebug("DSPEngine::initAcquisition");
return m_deviceEngine->initAcquisition();
}
bool DSPEngine::startAcquisition()
{
qDebug() << "DSPEngine::startAcquisition";
DSPAcquisitionStart cmd;
qDebug("DSPEngine::startAcquisition");
bool started = m_deviceEngine->startAcquisition();
return m_syncMessenger.sendWait(cmd) == StRunning;
if (started)
{
m_audioOutput.start(-1, m_audioSampleRate);
m_audioSampleRate = m_audioOutput.getRate(); // update with actual rate
}
return started;
}
void DSPEngine::stopAcquistion()
{
qDebug() << "DSPEngine::stopAcquistion";
DSPAcquisitionStop cmd;
m_syncMessenger.storeMessage(cmd);
handleSynchronousMessages();
if(m_dcOffsetCorrection)
{
qDebug("DC offset:%f,%f", m_iOffset, m_qOffset);
}
qDebug("DSPEngine::stopAcquistion");
m_audioOutput.stop();
m_deviceEngine->stopAcquistion();
}
void DSPEngine::setSource(SampleSource* source)
{
qDebug() << "DSPEngine::setSource";
DSPSetSource cmd(source);
m_syncMessenger.sendWait(cmd);
qDebug("DSPEngine::setSource");
m_deviceEngine->setSource(source);
}
void DSPEngine::setSourceSequence(int sequence)
{
qDebug("DSPEngine::setSourceSequence: seq: %d", sequence);
m_sampleSourceSequence = sequence;
qDebug("DSPEngine::setSource");
m_deviceEngine->setSourceSequence(sequence);
}
void DSPEngine::addSink(SampleSink* sink)
{
qDebug() << "DSPEngine::addSink: " << sink->objectName().toStdString().c_str();
DSPAddSink cmd(sink);
m_syncMessenger.sendWait(cmd);
qDebug("DSPEngine::setSource");
m_deviceEngine->addSink(sink);
}
void DSPEngine::removeSink(SampleSink* sink)
{
qDebug() << "DSPEngine::removeSink: " << sink->objectName().toStdString().c_str();
DSPRemoveSink cmd(sink);
m_syncMessenger.sendWait(cmd);
qDebug("DSPEngine::removeSink");
m_deviceEngine->removeSink(sink);
}
void DSPEngine::addThreadedSink(ThreadedSampleSink* sink)
{
qDebug() << "DSPEngine::addThreadedSink: " << sink->objectName().toStdString().c_str();
DSPAddThreadedSampleSink cmd(sink);
m_syncMessenger.sendWait(cmd);
qDebug("DSPEngine::addThreadedSink");
m_deviceEngine->addThreadedSink(sink);
}
void DSPEngine::removeThreadedSink(ThreadedSampleSink* sink)
{
qDebug() << "DSPEngine::removeThreadedSink: " << sink->objectName().toStdString().c_str();
DSPRemoveThreadedSampleSink cmd(sink);
m_syncMessenger.sendWait(cmd);
qDebug("DSPEngine::addThreadedSink");
m_deviceEngine->removeThreadedSink(sink);
}
void DSPEngine::addAudioSink(AudioFifo* audioFifo)
{
qDebug() << "DSPEngine::addAudioSink";
DSPAddAudioSink cmd(audioFifo);
m_syncMessenger.sendWait(cmd);
qDebug("DSPEngine::addAudioSink");
m_audioOutput.addFifo(audioFifo);
}
void DSPEngine::removeAudioSink(AudioFifo* audioFifo)
{
qDebug() << "DSPEngine::removeAudioSink";
DSPRemoveAudioSink cmd(audioFifo);
m_syncMessenger.sendWait(cmd);
qDebug("DSPEngine::removeAudioSink");
m_audioOutput.removeFifo(audioFifo);
}
void DSPEngine::configureCorrections(bool dcOffsetCorrection, bool iqImbalanceCorrection)
{
qDebug() << "DSPEngine::configureCorrections";
DSPConfigureCorrection* cmd = new DSPConfigureCorrection(dcOffsetCorrection, iqImbalanceCorrection);
m_inputMessageQueue.push(cmd);
qDebug("DSPEngine::configureCorrections");
m_deviceEngine->configureCorrections(dcOffsetCorrection, iqImbalanceCorrection);
}
DSPDeviceEngine::State DSPEngine::state() const
{
return m_deviceEngine->state();
}
QString DSPEngine::errorMessage()
{
qDebug() << "DSPEngine::errorMessage";
DSPGetErrorMessage cmd;
m_syncMessenger.sendWait(cmd);
return cmd.getErrorMessage();
return m_deviceEngine->errorMessage();
}
QString DSPEngine::sourceDeviceDescription()
{
qDebug() << "DSPEngine::sourceDeviceDescription";
DSPGetSourceDeviceDescription cmd;
m_syncMessenger.sendWait(cmd);
return cmd.getDeviceDescription();
}
void DSPEngine::dcOffset(SampleVector::iterator begin, SampleVector::iterator end)
{
double count;
int io = 0;
int qo = 0;
Sample corr((qint16)m_iOffset, (qint16)m_qOffset);
// sum and correct in one pass
for(SampleVector::iterator it = begin; it < end; it++)
{
io += it->real();
qo += it->imag();
*it -= corr;
}
// moving average
count = end - begin;
m_iOffset = (15.0 * m_iOffset + (double)io / count) / 16.0;
m_qOffset = (15.0 * m_qOffset + (double)qo / count) / 16.0;
}
void DSPEngine::imbalance(SampleVector::iterator begin, SampleVector::iterator end)
{
int iMin = 0;
int iMax = 0;
int qMin = 0;
int qMax = 0;
// find value ranges for both I and Q
// both intervals should be same same size (for a perfect circle)
for (SampleVector::iterator it = begin; it < end; it++)
{
if (it != begin)
{
if (it->real() < iMin) {
iMin = it->real();
} else if (it->real() > iMax) {
iMax = it->real();
}
if (it->imag() < qMin) {
qMin = it->imag();
} else if (it->imag() > qMax) {
qMax = it->imag();
}
}
else
{
iMin = it->real();
iMax = it->real();
qMin = it->imag();
qMax = it->imag();
}
}
// sliding average (el cheapo again)
m_iRange = (m_iRange * 15 + (iMax - iMin)) >> 4;
m_qRange = (m_qRange * 15 + (qMax - qMin)) >> 4;
// calculate imbalance as Q15.16
if(m_qRange != 0) {
m_imbalance = ((uint)m_iRange << 16) / (uint)m_qRange;
}
// correct imbalance and convert back to signed int 16
for(SampleVector::iterator it = begin; it < end; it++) {
it->m_imag = (it->m_imag * m_imbalance) >> 16;
}
}
void DSPEngine::work()
{
SampleFifo* sampleFifo = m_sampleSource->getSampleFifo();
std::size_t samplesDone = 0;
bool positiveOnly = false;
while ((sampleFifo->fill() > 0) && (m_inputMessageQueue.size() == 0) && (samplesDone < m_sampleRate))
{
SampleVector::iterator part1begin;
SampleVector::iterator part1end;
SampleVector::iterator part2begin;
SampleVector::iterator part2end;
std::size_t count = sampleFifo->readBegin(sampleFifo->fill(), &part1begin, &part1end, &part2begin, &part2end);
// first part of FIFO data
if (part1begin != part1end)
{
// correct stuff
if (m_dcOffsetCorrection)
{
dcOffset(part1begin, part1end);
}
if (m_iqImbalanceCorrection)
{
imbalance(part1begin, part1end);
}
// feed data to direct sinks
for (SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); ++it)
{
(*it)->feed(part1begin, part1end, positiveOnly);
}
// feed data to threaded sinks
for (ThreadedSampleSinks::const_iterator it = m_threadedSampleSinks.begin(); it != m_threadedSampleSinks.end(); ++it)
{
(*it)->feed(part1begin, part1end, positiveOnly);
}
}
// second part of FIFO data (used when block wraps around)
if(part2begin != part2end)
{
// correct stuff
if (m_dcOffsetCorrection)
{
dcOffset(part2begin, part2end);
}
if (m_iqImbalanceCorrection)
{
imbalance(part2begin, part2end);
}
// feed data to direct sinks
for (SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); it++)
{
(*it)->feed(part2begin, part2end, positiveOnly);
}
// feed data to threaded sinks
for (ThreadedSampleSinks::const_iterator it = m_threadedSampleSinks.begin(); it != m_threadedSampleSinks.end(); ++it)
{
(*it)->feed(part2begin, part2end, positiveOnly);
}
}
// adjust FIFO pointers
sampleFifo->readCommit((unsigned int) count);
samplesDone += count;
}
}
// notStarted -> idle -> init -> running -+
// ^ |
// +-----------------------+
DSPEngine::State DSPEngine::gotoIdle()
{
qDebug() << "DSPEngine::gotoIdle";
switch(m_state) {
case StNotStarted:
return StNotStarted;
case StIdle:
case StError:
return StIdle;
case StReady:
case StRunning:
break;
}
if(m_sampleSource == 0)
{
return StIdle;
}
// stop everything
for(SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); it++)
{
(*it)->stop();
}
m_sampleSource->stop();
m_deviceDescription.clear();
m_audioOutput.stop();
m_sampleRate = 0;
return StIdle;
}
DSPEngine::State DSPEngine::gotoInit()
{
qDebug() << "DSPEngine::gotoInit";
switch(m_state) {
case StNotStarted:
return StNotStarted;
case StRunning: // FIXME: assumes it goes first through idle state. Could we get back to init from running directly?
return StRunning;
case StReady:
return StReady;
case StIdle:
case StError:
break;
}
if (m_sampleSource == 0)
{
return gotoError("No sample source configured");
}
// init: pass sample rate and center frequency to all sample rate and/or center frequency dependent sinks and wait for completion
m_iOffset = 0;
m_qOffset = 0;
m_iRange = 1 << 16;
m_qRange = 1 << 16;
m_deviceDescription = m_sampleSource->getDeviceDescription();
m_centerFrequency = m_sampleSource->getCenterFrequency();
m_sampleRate = m_sampleSource->getSampleRate();
qDebug() << "DSPEngine::gotoInit: " << m_deviceDescription.toStdString().c_str() << ": "
<< " sampleRate: " << m_sampleRate
<< " centerFrequency: " << m_centerFrequency;
DSPSignalNotification notif(m_sampleRate, m_centerFrequency);
for (SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); ++it)
{
qDebug() << "DSPEngine::gotoInit: initializing " << (*it)->objectName().toStdString().c_str();
(*it)->handleMessage(notif);
}
for (ThreadedSampleSinks::const_iterator it = m_threadedSampleSinks.begin(); it != m_threadedSampleSinks.end(); ++it)
{
qDebug() << "DSPEngine::gotoInit: initializing ThreadedSampleSink(" << (*it)->getSampleSinkObjectName().toStdString().c_str() << ")";
(*it)->handleSinkMessage(notif);
}
// pass data to listeners
DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy for the output queue
m_outputMessageQueue.push(rep);
return StReady;
}
DSPEngine::State DSPEngine::gotoRunning()
{
qDebug() << "DSPEngine::gotoRunning";
switch(m_state)
{
case StNotStarted:
return StNotStarted;
case StIdle:
return StIdle;
case StRunning:
return StRunning;
case StReady:
case StError:
break;
}
if(m_sampleSource == NULL) {
return gotoError("DSPEngine::gotoRunning: No sample source configured");
}
qDebug() << "DSPEngine::gotoRunning: " << m_deviceDescription.toStdString().c_str() << " started";
// Start everything
if(!m_sampleSource->start(m_sampleSourceSequence))
{
return gotoError("Could not start sample source");
}
m_audioOutput.start(-1, 48000); // Use default output device at 48 kHz
m_audioSampleRate = m_audioOutput.getRate();
for(SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); it++)
{
qDebug() << "DSPEngine::gotoRunning: starting " << (*it)->objectName().toStdString().c_str();
(*it)->start();
}
for (ThreadedSampleSinks::const_iterator it = m_threadedSampleSinks.begin(); it != m_threadedSampleSinks.end(); ++it)
{
qDebug() << "DSPEngine::gotoRunning: starting ThreadedSampleSink(" << (*it)->getSampleSinkObjectName().toStdString().c_str() << ")";
(*it)->start();
}
qDebug() << "DSPEngine::gotoRunning:input message queue pending: " << m_inputMessageQueue.size();
return StRunning;
}
DSPEngine::State DSPEngine::gotoError(const QString& errorMessage)
{
qDebug() << "DSPEngine::gotoError";
m_errorMessage = errorMessage;
m_deviceDescription.clear();
m_state = StError;
return StError;
}
void DSPEngine::handleSetSource(SampleSource* source)
{
gotoIdle();
if(m_sampleSource != 0)
{
disconnect(m_sampleSource->getSampleFifo(), SIGNAL(dataReady()), this, SLOT(handleData()));
}
m_sampleSource = source;
if(m_sampleSource != 0)
{
qDebug("DSPEngine::handleSetSource: set %s", qPrintable(source->getDeviceDescription()));
connect(m_sampleSource->getSampleFifo(), SIGNAL(dataReady()), this, SLOT(handleData()), Qt::QueuedConnection);
}
else
{
qDebug("DSPEngine::handleSetSource: set none");
}
}
void DSPEngine::handleData()
{
if(m_state == StRunning)
{
work();
}
}
void DSPEngine::handleSynchronousMessages()
{
Message *message = m_syncMessenger.getMessage();
qDebug() << "DSPEngine::handleSynchronousMessages: " << message->getIdentifier();
if (DSPExit::match(*message))
{
gotoIdle();
m_state = StNotStarted;
exit();
}
else if (DSPAcquisitionInit::match(*message))
{
m_state = gotoIdle();
if(m_state == StIdle) {
m_state = gotoInit(); // State goes ready if init is performed
}
}
else if (DSPAcquisitionStart::match(*message))
{
if(m_state == StReady) {
m_state = gotoRunning();
}
}
else if (DSPAcquisitionStop::match(*message))
{
m_state = gotoIdle();
}
else if (DSPGetSourceDeviceDescription::match(*message))
{
((DSPGetSourceDeviceDescription*) message)->setDeviceDescription(m_deviceDescription);
}
else if (DSPGetErrorMessage::match(*message))
{
((DSPGetErrorMessage*) message)->setErrorMessage(m_errorMessage);
}
else if (DSPSetSource::match(*message)) {
handleSetSource(((DSPSetSource*) message)->getSampleSource());
}
else if (DSPAddSink::match(*message))
{
SampleSink* sink = ((DSPAddSink*) message)->getSampleSink();
m_sampleSinks.push_back(sink);
}
else if (DSPRemoveSink::match(*message))
{
SampleSink* sink = ((DSPRemoveSink*) message)->getSampleSink();
if(m_state == StRunning) {
sink->stop();
}
m_sampleSinks.remove(sink);
}
else if (DSPAddThreadedSampleSink::match(*message))
{
ThreadedSampleSink *threadedSink = ((DSPAddThreadedSampleSink*) message)->getThreadedSampleSink();
m_threadedSampleSinks.push_back(threadedSink);
threadedSink->start();
}
else if (DSPRemoveThreadedSampleSink::match(*message))
{
ThreadedSampleSink* threadedSink = ((DSPRemoveThreadedSampleSink*) message)->getThreadedSampleSink();
threadedSink->stop();
m_threadedSampleSinks.remove(threadedSink);
}
else if (DSPAddAudioSink::match(*message))
{
m_audioOutput.addFifo(((DSPAddAudioSink*) message)->getAudioFifo());
}
else if (DSPRemoveAudioSink::match(*message))
{
m_audioOutput.removeFifo(((DSPRemoveAudioSink*) message)->getAudioFifo());
}
m_syncMessenger.done(m_state);
}
void DSPEngine::handleInputMessages()
{
qDebug() << "DSPEngine::handleInputMessages";
Message* message;
while ((message = m_inputMessageQueue.pop()) != 0)
{
qDebug("DSPEngine::handleInputMessages: message: %s", message->getIdentifier());
if (DSPConfigureCorrection::match(*message))
{
DSPConfigureCorrection* conf = (DSPConfigureCorrection*) message;
m_iqImbalanceCorrection = conf->getIQImbalanceCorrection();
if(m_dcOffsetCorrection != conf->getDCOffsetCorrection())
{
m_dcOffsetCorrection = conf->getDCOffsetCorrection();
m_iOffset = 0;
m_qOffset = 0;
}
if(m_iqImbalanceCorrection != conf->getIQImbalanceCorrection())
{
m_iqImbalanceCorrection = conf->getIQImbalanceCorrection();
m_iRange = 1 << 16;
m_qRange = 1 << 16;
m_imbalance = 65536;
}
delete message;
}
else if (DSPSignalNotification::match(*message))
{
DSPSignalNotification *notif = (DSPSignalNotification *) message;
// update DSP values
m_sampleRate = notif->getSampleRate();
m_centerFrequency = notif->getCenterFrequency();
qDebug() << "DSPEngine::handleInputMessages: DSPSignalNotification(" << m_sampleRate << "," << m_centerFrequency << ")";
// forward source changes to sinks with immediate execution
for(SampleSinks::const_iterator it = m_sampleSinks.begin(); it != m_sampleSinks.end(); it++)
{
qDebug() << "DSPEngine::handleInputMessages: forward message to " << (*it)->objectName().toStdString().c_str();
(*it)->handleMessage(*message);
}
for (ThreadedSampleSinks::const_iterator it = m_threadedSampleSinks.begin(); it != m_threadedSampleSinks.end(); ++it)
{
qDebug() << "DSPEngine::handleSourceMessages: forward message to ThreadedSampleSink(" << (*it)->getSampleSinkObjectName().toStdString().c_str() << ")";
(*it)->handleSinkMessage(*message);
}
// forward changes to listeners on DSP output queue
DSPSignalNotification* rep = new DSPSignalNotification(*notif); // make a copy for the output queue
m_outputMessageQueue.push(rep);
delete message;
}
}
}
void DSPEngine::setAudioSampleRate(uint rate)
{
m_audioSampleRate = rate;
return m_deviceEngine->sourceDeviceDescription();
}

View File

@ -21,6 +21,47 @@ void ThreadedSampleFifo::writeToFifo(SampleVector::const_iterator& begin, Sample
m_sampleFifo.write(begin, end);
}
void ThreadedSampleFifo::handleFifoData() // FIXME: Fixed? Move it to the new threadable sink class
{
bool positiveOnly = false;
while ((m_sampleFifo.fill() > 0) && (m_sampleSink->getInputMessageQueue()->size() == 0))
{
SampleVector::iterator part1begin;
SampleVector::iterator part1end;
SampleVector::iterator part2begin;
SampleVector::iterator part2end;
std::size_t count = m_sampleFifo.readBegin(m_sampleFifo.fill(), &part1begin, &part1end, &part2begin, &part2end);
// first part of FIFO data
if (count > 0)
{
// handle data
if(m_sampleSink != NULL)
{
m_sampleSink->feed(part1begin, part1end, positiveOnly);
}
m_sampleFifo.readCommit(part1end - part1begin);
}
// second part of FIFO data (used when block wraps around)
if(part2begin != part2end)
{
// handle data
if(m_sampleSink != NULL)
{
m_sampleSink->feed(part2begin, part2end, positiveOnly);
}
m_sampleFifo.readCommit(part2end - part2begin);
}
}
}
ThreadedSampleSink::ThreadedSampleSink(SampleSink* sampleSink, QObject *parent) :
m_sampleSink(sampleSink)
{
@ -78,46 +119,3 @@ QString ThreadedSampleSink::getSampleSinkObjectName() const
{
return m_sampleSink->objectName();
}
void ThreadedSampleFifo::handleFifoData() // FIXME: Fixed? Move it to the new threadable sink class
{
bool positiveOnly = false;
while ((m_sampleFifo.fill() > 0) && (m_sampleSink->getInputMessageQueue()->size() == 0))
{
SampleVector::iterator part1begin;
SampleVector::iterator part1end;
SampleVector::iterator part2begin;
SampleVector::iterator part2end;
std::size_t count = m_sampleFifo.readBegin(m_sampleFifo.fill(), &part1begin, &part1end, &part2begin, &part2end);
// first part of FIFO data
if (count > 0)
{
// handle data
if(m_sampleSink != NULL)
{
m_sampleSink->feed(part1begin, part1end, positiveOnly);
}
m_sampleFifo.readCommit(part1end - part1begin);
}
// second part of FIFO data (used when block wraps around)
if(part2begin != part2end)
{
// handle data
if(m_sampleSink != NULL)
{
m_sampleSink->feed(part2begin, part2end, positiveOnly);
}
m_sampleFifo.readCommit(part2end - part2begin);
}
}
}

View File

@ -50,7 +50,7 @@ MainWindow::MainWindow(QWidget* parent) :
m_audioDeviceInfo(new AudioDeviceInfo),
m_settings(),
m_dspEngine(DSPEngine::instance()),
m_lastEngineState((DSPEngine::State)-1),
m_lastEngineState((DSPDeviceEngine::State)-1),
m_startOsmoSDRUpdateAfterStop(false),
m_inputGUI(0),
m_sampleRate(0),
@ -418,28 +418,28 @@ void MainWindow::updateStatus()
int state = m_dspEngine->state();
if(m_lastEngineState != state) {
switch(state) {
case DSPEngine::StNotStarted:
case DSPDeviceEngine::StNotStarted:
m_engineIdle->setColor(Qt::gray);
m_engineRunning->setColor(Qt::gray);
m_engineError->setColor(Qt::gray);
statusBar()->clearMessage();
break;
case DSPEngine::StIdle:
case DSPDeviceEngine::StIdle:
m_engineIdle->setColor(Qt::cyan);
m_engineRunning->setColor(Qt::gray);
m_engineError->setColor(Qt::gray);
statusBar()->clearMessage();
break;
case DSPEngine::StRunning:
case DSPDeviceEngine::StRunning:
m_engineIdle->setColor(Qt::gray);
m_engineRunning->setColor(Qt::green);
m_engineError->setColor(Qt::gray);
statusBar()->showMessage(tr("Sampling from %1").arg(m_dspEngine->sourceDeviceDescription()));
break;
case DSPEngine::StError:
case DSPDeviceEngine::StError:
m_engineIdle->setColor(Qt::gray);
m_engineRunning->setColor(Qt::gray);
m_engineError->setColor(Qt::red);