From 289c1a203f978680b6847b8c989f6ca6fd172201 Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 24 Oct 2016 18:06:44 +0200 Subject: [PATCH] Tx ph.1: Sample source FIFO read with signal. Use a specific spectrum sink (vis) reference in Device sink engine for main spectrum rendering --- .../samplesink/filesink/filesinkthread.cpp | 2 +- sdrbase/device/devicesinkapi.cpp | 8 +-- sdrbase/device/devicesinkapi.h | 4 +- sdrbase/dsp/dspcommands.cpp | 2 + sdrbase/dsp/dspcommands.h | 24 +++++++++ sdrbase/dsp/dspdevicesinkengine.cpp | 50 +++++++------------ sdrbase/dsp/dspdevicesinkengine.h | 7 ++- sdrbase/dsp/samplesourcefifo.cpp | 9 +++- sdrbase/dsp/samplesourcefifo.h | 8 +-- sdrbase/mainwindow.cpp | 4 +- 10 files changed, 70 insertions(+), 48 deletions(-) diff --git a/plugins/samplesink/filesink/filesinkthread.cpp b/plugins/samplesink/filesink/filesinkthread.cpp index 4742f83e7..6c15a0193 100644 --- a/plugins/samplesink/filesink/filesinkthread.cpp +++ b/plugins/samplesink/filesink/filesinkthread.cpp @@ -129,7 +129,7 @@ void FileSinkThread::tick() SampleVector::iterator beginRead; - m_sampleFifo->read(beginRead, m_samplesChunkSize); + m_sampleFifo->readAndSignal(beginRead, m_samplesChunkSize); m_ofstream->write(reinterpret_cast(&(*beginRead)), m_samplesChunkSize*4); m_samplesCount += m_samplesChunkSize; } diff --git a/sdrbase/device/devicesinkapi.cpp b/sdrbase/device/devicesinkapi.cpp index 7f4fe2878..e4172cd3c 100644 --- a/sdrbase/device/devicesinkapi.cpp +++ b/sdrbase/device/devicesinkapi.cpp @@ -42,14 +42,14 @@ DeviceSinkAPI::~DeviceSinkAPI() { } -void DeviceSinkAPI::addSink(BasebandSampleSink *sink) +void DeviceSinkAPI::addSpectrumSink(BasebandSampleSink *spectrumSink) { - m_deviceSinkEngine->addSink(sink); + m_deviceSinkEngine->addSpectrumSink(spectrumSink); } -void DeviceSinkAPI::removeSink(BasebandSampleSink* sink) +void DeviceSinkAPI::removeSpectrumSink(BasebandSampleSink* spectrumSink) { - m_deviceSinkEngine->removeSink(sink); + m_deviceSinkEngine->removeSpectrumSink(spectrumSink); } void DeviceSinkAPI::addSource(BasebandSampleSource *source) diff --git a/sdrbase/device/devicesinkapi.h b/sdrbase/device/devicesinkapi.h index 0e6c97733..10c492260 100644 --- a/sdrbase/device/devicesinkapi.h +++ b/sdrbase/device/devicesinkapi.h @@ -41,8 +41,8 @@ class SDRANGEL_API DeviceSinkAPI : public QObject { public: // Device engine stuff - void addSink(BasebandSampleSink* sink); //!< Add a sample sink to device engine (spectrum vis) - void removeSink(BasebandSampleSink* sink); //!< Remove a sample sink from device engine (spectrum vis) + void addSpectrumSink(BasebandSampleSink* sink); //!< Add the spectrum sample sink to device engine (spectrum vis) + void removeSpectrumSink(BasebandSampleSink* sink); //!< Remove the spectrum sample sink from device engine (spectrum vis) void addSource(BasebandSampleSource* source); //!< Add a baseband sample source to device engine void removeSource(BasebandSampleSource* sink); //!< Remove a baseband sample source from device engine void addThreadedSource(ThreadedBasebandSampleSource* sink); //!< Add a baseband sample source that will run on its own thread to device engine diff --git a/sdrbase/dsp/dspcommands.cpp b/sdrbase/dsp/dspcommands.cpp index 3ce364370..b5d1fb969 100644 --- a/sdrbase/dsp/dspcommands.cpp +++ b/sdrbase/dsp/dspcommands.cpp @@ -30,8 +30,10 @@ MESSAGE_CLASS_DEFINITION(DSPGetErrorMessage, Message) MESSAGE_CLASS_DEFINITION(DSPSetSource, Message) MESSAGE_CLASS_DEFINITION(DSPSetSink, Message) MESSAGE_CLASS_DEFINITION(DSPAddSink, Message) +MESSAGE_CLASS_DEFINITION(DSPAddSpectrumSink, Message) MESSAGE_CLASS_DEFINITION(DSPAddSource, Message) MESSAGE_CLASS_DEFINITION(DSPRemoveSink, Message) +MESSAGE_CLASS_DEFINITION(DSPRemoveSpectrumSink, Message) MESSAGE_CLASS_DEFINITION(DSPRemoveSource, Message) MESSAGE_CLASS_DEFINITION(DSPAddThreadedSampleSink, Message) MESSAGE_CLASS_DEFINITION(DSPAddThreadedSampleSource, Message) diff --git a/sdrbase/dsp/dspcommands.h b/sdrbase/dsp/dspcommands.h index 2e4f050a9..8ba938381 100644 --- a/sdrbase/dsp/dspcommands.h +++ b/sdrbase/dsp/dspcommands.h @@ -128,6 +128,18 @@ private: BasebandSampleSink* m_sampleSink; }; +class SDRANGEL_API DSPAddSpectrumSink : public Message { + MESSAGE_CLASS_DECLARATION + +public: + DSPAddSpectrumSink(BasebandSampleSink* sampleSink) : Message(), m_sampleSink(sampleSink) { } + + BasebandSampleSink* getSampleSink() const { return m_sampleSink; } + +private: + BasebandSampleSink* m_sampleSink; +}; + class SDRANGEL_API DSPAddSource : public Message { MESSAGE_CLASS_DECLARATION @@ -152,6 +164,18 @@ private: BasebandSampleSink* m_sampleSink; }; +class SDRANGEL_API DSPRemoveSpectrumSink : public Message { + MESSAGE_CLASS_DECLARATION + +public: + DSPRemoveSpectrumSink(BasebandSampleSink* sampleSink) : Message(), m_sampleSink(sampleSink) { } + + BasebandSampleSink* getSampleSink() const { return m_sampleSink; } + +private: + BasebandSampleSink* m_sampleSink; +}; + class SDRANGEL_API DSPRemoveSource : public Message { MESSAGE_CLASS_DECLARATION diff --git a/sdrbase/dsp/dspdevicesinkengine.cpp b/sdrbase/dsp/dspdevicesinkengine.cpp index cadec73bc..65c696e95 100644 --- a/sdrbase/dsp/dspdevicesinkengine.cpp +++ b/sdrbase/dsp/dspdevicesinkengine.cpp @@ -35,6 +35,7 @@ DSPDeviceSinkEngine::DSPDeviceSinkEngine(uint uid, QObject* parent) : m_deviceSampleSink(0), m_sampleSinkSequence(0), m_basebandSampleSources(), + m_spectrumSink(0), m_sampleRate(0), m_centerFrequency(0) { @@ -137,17 +138,17 @@ void DSPDeviceSinkEngine::removeThreadedSource(ThreadedBasebandSampleSource* sou m_syncMessenger.sendWait(cmd); } -void DSPDeviceSinkEngine::addSink(BasebandSampleSink* sink) +void DSPDeviceSinkEngine::addSpectrumSink(BasebandSampleSink* spectrumSink) { - qDebug() << "DSPDeviceSinkEngine::addSink: " << sink->objectName().toStdString().c_str(); - DSPAddSink cmd(sink); + qDebug() << "DSPDeviceSinkEngine::addSpectrumSink: " << spectrumSink->objectName().toStdString().c_str(); + DSPAddSpectrumSink cmd(spectrumSink); m_syncMessenger.sendWait(cmd); } -void DSPDeviceSinkEngine::removeSink(BasebandSampleSink* sink) +void DSPDeviceSinkEngine::removeSpectrumSink(BasebandSampleSink* spectrumSink) { - qDebug() << "DSPDeviceSinkEngine::removeSink: " << sink->objectName().toStdString().c_str(); - DSPRemoveSink cmd(sink); + qDebug() << "DSPDeviceSinkEngine::removeSpectrumSink: " << spectrumSink->objectName().toStdString().c_str(); + DSPRemoveSpectrumSink cmd(spectrumSink); m_syncMessenger.sendWait(cmd); } @@ -216,10 +217,10 @@ void DSPDeviceSinkEngine::work() sourceOccurence = 0; } - // feed the mix to the sinks normally just the main spectrum vis - for (BasebandSampleSinks::iterator it = m_basebandSampleSinks.begin(); it != m_basebandSampleSinks.end(); ++it) + // feed the mix to the main spectrum sink + if (m_spectrumSink) { - (*it)->feed(writeBegin, writeBegin + nbWriteSamples, false); + m_spectrumSink->feed(writeBegin, writeBegin + nbWriteSamples, false); } } } @@ -262,11 +263,7 @@ DSPDeviceSinkEngine::State DSPDeviceSinkEngine::gotoIdle() (*it)->stop(); } - for(BasebandSampleSinks::const_iterator it = m_basebandSampleSinks.begin(); it != m_basebandSampleSinks.end(); it++) - { - (*it)->stop(); - } - + m_spectrumSink->stop(); m_deviceSampleSink->stop(); m_deviceDescription.clear(); m_sampleRate = 0; @@ -322,11 +319,7 @@ DSPDeviceSinkEngine::State DSPDeviceSinkEngine::gotoInit() (*it)->handleSourceMessage(notif); } - for (BasebandSampleSinks::const_iterator it = m_basebandSampleSinks.begin(); it != m_basebandSampleSinks.end(); ++it) - { - qDebug() << "DSPDeviceSinkEngine::gotoInit: initializing " << (*it)->objectName().toStdString().c_str(); - (*it)->handleMessage(notif); - } + m_spectrumSink->handleMessage(notif); // pass data to listeners @@ -381,11 +374,7 @@ DSPDeviceSinkEngine::State DSPDeviceSinkEngine::gotoRunning() (*it)->start(); } - for(BasebandSampleSinks::const_iterator it = m_basebandSampleSinks.begin(); it != m_basebandSampleSinks.end(); it++) - { - qDebug() << "DSPDeviceSinkEngine::gotoRunning: starting " << (*it)->objectName().toStdString().c_str(); - (*it)->start(); - } + m_spectrumSink->start(); qDebug() << "DSPDeviceSinkEngine::gotoRunning: input message queue pending: " << m_inputMessageQueue.size(); @@ -472,20 +461,19 @@ void DSPDeviceSinkEngine::handleSynchronousMessages() else if (DSPSetSink::match(*message)) { handleSetSink(((DSPSetSink*) message)->getSampleSink()); } - else if (DSPAddSink::match(*message)) + else if (DSPAddSpectrumSink::match(*message)) { - BasebandSampleSink* sink = ((DSPAddSink*) message)->getSampleSink(); - m_basebandSampleSinks.push_back(sink); + m_spectrumSink = ((DSPAddSpectrumSink*) message)->getSampleSink(); } - else if (DSPRemoveSink::match(*message)) + else if (DSPRemoveSpectrumSink::match(*message)) { - BasebandSampleSink* sink = ((DSPRemoveSink*) message)->getSampleSink(); + BasebandSampleSink* spectrumSink = ((DSPRemoveSpectrumSink*) message)->getSampleSink(); if(m_state == StRunning) { - sink->stop(); + spectrumSink->stop(); } - m_basebandSampleSinks.remove(sink); + m_spectrumSink = 0; } else if (DSPAddSource::match(*message)) { diff --git a/sdrbase/dsp/dspdevicesinkengine.h b/sdrbase/dsp/dspdevicesinkengine.h index f8f92a6e7..9d8c2cbba 100644 --- a/sdrbase/dsp/dspdevicesinkengine.h +++ b/sdrbase/dsp/dspdevicesinkengine.h @@ -70,8 +70,8 @@ public: void addThreadedSource(ThreadedBasebandSampleSource* source); //!< Add a baseband sample source that will run on its own thread void removeThreadedSource(ThreadedBasebandSampleSource* source); //!< Remove a baseband sample source that runs on its own thread - void addSink(BasebandSampleSink* sink); //!< Add a baseband sample sink - void removeSink(BasebandSampleSink* sink); //!< Remove a baseband sample sink + void addSpectrumSink(BasebandSampleSink* spectrumSink); //!< Add a spectrum vis baseband sample sink + void removeSpectrumSink(BasebandSampleSink* spectrumSink); //!< Add a spectrum vis baseband sample sink State state() const { return m_state; } //!< Return DSP engine current state @@ -99,8 +99,7 @@ private: typedef std::list ThreadedBasebandSampleSources; ThreadedBasebandSampleSources m_threadedBasebandSampleSources; //!< baseband sample sources on their own threads (usually channels) - typedef std::list BasebandSampleSinks; - BasebandSampleSinks m_basebandSampleSinks; //!< baseband sample sinks within main thread (this is only the spectrum vis normally) + BasebandSampleSink *m_spectrumSink; uint m_sampleRate; quint64 m_centerFrequency; diff --git a/sdrbase/dsp/samplesourcefifo.cpp b/sdrbase/dsp/samplesourcefifo.cpp index 28c2cb2d7..898f30bb3 100644 --- a/sdrbase/dsp/samplesourcefifo.cpp +++ b/sdrbase/dsp/samplesourcefifo.cpp @@ -39,7 +39,7 @@ void SampleSourceFifo::init() m_init = true; } -void SampleSourceFifo::read(SampleVector::iterator& beginRead, unsigned int nbSamples) +void SampleSourceFifo::readAndSignal(SampleVector::iterator& beginRead, unsigned int nbSamples) { QMutexLocker mutexLocker(&m_mutex); @@ -48,6 +48,8 @@ void SampleSourceFifo::read(SampleVector::iterator& beginRead, unsigned int nbSa beginRead = m_data.begin() + m_size + m_ir; m_ir = (m_ir + nbSamples) % m_size; + emit dataRead(nbSamples); + int i_delta = m_iw - m_ir; if (m_init) @@ -82,6 +84,11 @@ void SampleSourceFifo::write(const Sample& sample) } } +void SampleSourceFifo::getReadIterator(SampleVector::iterator& writeUntil) +{ + writeUntil = m_data.begin() + m_size + m_ir; +} + void SampleSourceFifo::getWriteIterator(SampleVector::iterator& writeAt) { writeAt = m_data.begin() + m_iw; diff --git a/sdrbase/dsp/samplesourcefifo.h b/sdrbase/dsp/samplesourcefifo.h index 31645d721..aa110e2f6 100644 --- a/sdrbase/dsp/samplesourcefifo.h +++ b/sdrbase/dsp/samplesourcefifo.h @@ -34,9 +34,10 @@ public: unsigned int getChunkSize() const { return m_samplesChunkSize; } void init(); - /** begin read at current read point for the given length */ - void read(SampleVector::iterator& beginRead, unsigned int nbSamples); + /** begin read at current read point for the given length and activate R/W signals */ + void readAndSignal(SampleVector::iterator& beginRead, unsigned int nbSamples); + void getReadIterator(SampleVector::iterator& readUntil); //!< get iterator past the last sample that was read by a read with signal (i.e. current read iterator) void getWriteIterator(SampleVector::iterator& writeAt); //!< get iterator to current item for update - write phase 1 void bumpIndex(SampleVector::iterator& writeAt); //!< copy current item to second buffer and bump write index - write phase 2 @@ -52,7 +53,8 @@ private: QMutex m_mutex; signals: - void dataWrite(); // signal data is read past a read chunk of samples and write is needed + void dataWrite(); // signal data is read past a read chunk of samples and write is needed + void dataRead(int nbSamples); // signal a read has been done for a number of samples }; #endif /* SDRBASE_DSP_SAMPLESOURCEFIFO_H_ */ diff --git a/sdrbase/mainwindow.cpp b/sdrbase/mainwindow.cpp index beb1584d3..086e4cb44 100644 --- a/sdrbase/mainwindow.cpp +++ b/sdrbase/mainwindow.cpp @@ -241,7 +241,7 @@ void MainWindow::addSinkDevice() connect(m_deviceUIs.back()->m_samplingDeviceControl->getAddChannelButton(), SIGNAL(clicked(bool)), this, SLOT(on_channel_addClicked(bool))); - dspDeviceSinkEngine->addSink(m_deviceUIs.back()->m_spectrumVis); + dspDeviceSinkEngine->addSpectrumSink(m_deviceUIs.back()->m_spectrumVis); ui->tabSpectra->addTab(m_deviceUIs.back()->m_spectrum, tabNameCStr); ui->tabSpectraGUI->addTab(m_deviceUIs.back()->m_spectrumGUI, tabNameCStr); ui->tabChannels->addTab(m_deviceUIs.back()->m_channelWindow, tabNameCStr); @@ -295,7 +295,7 @@ void MainWindow::removeLastDevice() { DSPDeviceSinkEngine *lastDeviceEngine = m_deviceUIs.back()->m_deviceSinkEngine; lastDeviceEngine->stopGeneration(); - lastDeviceEngine->removeSink(m_deviceUIs.back()->m_spectrumVis); + lastDeviceEngine->removeSpectrumSink(m_deviceUIs.back()->m_spectrumVis); ui->tabSpectraGUI->removeTab(ui->tabSpectraGUI->count() - 1); ui->tabSpectra->removeTab(ui->tabSpectra->count() - 1);