mirror of
				https://github.com/f4exb/sdrangel.git
				synced 2025-10-29 20:10:22 -04:00 
			
		
		
		
	FIFO for multiple input handling
This commit is contained in:
		
							parent
							
								
									fb8dfec0eb
								
							
						
					
					
						commit
						e3082d2ef2
					
				| @ -97,6 +97,7 @@ set(sdrbase_SOURCES | |||||||
|     dsp/phaselock.cpp |     dsp/phaselock.cpp | ||||||
|     dsp/phaselockcomplex.cpp |     dsp/phaselockcomplex.cpp | ||||||
|     dsp/projector.cpp |     dsp/projector.cpp | ||||||
|  |     dsp/samplemififo.cpp | ||||||
|     dsp/samplesinkfifo.cpp |     dsp/samplesinkfifo.cpp | ||||||
|     dsp/samplesinkvector.cpp |     dsp/samplesinkvector.cpp | ||||||
|     dsp/samplesourcefifo.cpp |     dsp/samplesourcefifo.cpp | ||||||
| @ -236,6 +237,7 @@ set(sdrbase_HEADERS | |||||||
|     dsp/phaselockcomplex.h |     dsp/phaselockcomplex.h | ||||||
|     dsp/projector.h |     dsp/projector.h | ||||||
|     dsp/recursivefilters.h |     dsp/recursivefilters.h | ||||||
|  |     dsp/samplemififo.h | ||||||
|     dsp/samplesinkfifo.h |     dsp/samplesinkfifo.h | ||||||
|     dsp/samplesinkvector.h |     dsp/samplesinkvector.h | ||||||
|     dsp/samplesourcefifo.h |     dsp/samplesourcefifo.h | ||||||
|  | |||||||
							
								
								
									
										196
									
								
								sdrbase/dsp/samplemififo.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										196
									
								
								sdrbase/dsp/samplemififo.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,196 @@ | |||||||
|  | ///////////////////////////////////////////////////////////////////////////////////
 | ||||||
|  | // Copyright (C) 2019 Edouard Griffiths, F4EXB                                   //
 | ||||||
|  | //                                                                               //
 | ||||||
|  | // 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 "samplemififo.h" | ||||||
|  | 
 | ||||||
|  | SampleMIFifo::SampleMIFifo(QObject *parent) : | ||||||
|  |     QObject(parent) | ||||||
|  | { | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | SampleMIFifo::SampleMIFifo(unsigned int nbStreams, unsigned int size, QObject *parent) : | ||||||
|  |     QObject(parent) | ||||||
|  | { | ||||||
|  |     for (unsigned int i = 0; i < nbStreams; i++) | ||||||
|  |     { | ||||||
|  |         m_data.push_back(SampleVector()); | ||||||
|  |         m_data.back().resize(size); | ||||||
|  |         m_vFill.push_back(0); | ||||||
|  |         m_vHead.push_back(0); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void SampleMIFifo::init(unsigned int nbStreams, unsigned int size) | ||||||
|  | { | ||||||
|  |     m_data.clear(); | ||||||
|  |     m_vFill.clear(); | ||||||
|  |     m_vHead.clear(); | ||||||
|  | 
 | ||||||
|  |     for (unsigned int i = 0; i < nbStreams; i++) | ||||||
|  |     { | ||||||
|  |         m_data.push_back(SampleVector()); | ||||||
|  |         m_data.back().resize(size); | ||||||
|  |         m_vFill.push_back(0); | ||||||
|  |         m_vHead.push_back(0); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void SampleMIFifo::writeSync(const std::vector<SampleVector::const_iterator>& vbegin, unsigned int size) | ||||||
|  | { | ||||||
|  |     if ((m_data.size() == 0) || (m_data.size() != vbegin.size())) { | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     QMutexLocker mutexLocker(&m_mutex); | ||||||
|  |     std::vector<SampleVector>::iterator dataIt = m_data.begin(); | ||||||
|  |     std::vector<SampleVector::const_iterator>::const_iterator vbeginIt = vbegin.begin(); | ||||||
|  |     int stream = 0; | ||||||
|  | 
 | ||||||
|  |     for (; dataIt != m_data.end(); ++dataIt, ++vbeginIt, ++stream) | ||||||
|  |     { | ||||||
|  |         int spaceLeft = dataIt->size() - m_vFill[stream]; | ||||||
|  | 
 | ||||||
|  |         if (size < spaceLeft) | ||||||
|  |         { | ||||||
|  |             std::copy(*vbeginIt, *vbeginIt + size, dataIt->begin() + m_vFill[stream]); | ||||||
|  |             m_vFill[stream] += size; | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             int remaining = size - spaceLeft; | ||||||
|  |             std::copy(*vbeginIt, *vbeginIt + spaceLeft, dataIt->begin() + m_vFill[stream]); | ||||||
|  |             std::copy(*vbeginIt + spaceLeft, *vbeginIt + size,   dataIt->begin()); | ||||||
|  |             m_vFill[stream] = remaining; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     m_head = m_vHead[0]; | ||||||
|  |     m_fill = m_vFill[0]; | ||||||
|  | 
 | ||||||
|  |     emit dataSyncReady(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void SampleMIFifo::writeAsync(unsigned int stream, const SampleVector::const_iterator& begin, unsigned int size) | ||||||
|  | { | ||||||
|  |     if (stream < m_data.size()) | ||||||
|  |     { | ||||||
|  |         QMutexLocker mutexLocker(&m_mutex); | ||||||
|  |         int spaceLeft = m_data[stream].size() - m_vFill[stream]; | ||||||
|  | 
 | ||||||
|  |         if (size < spaceLeft) | ||||||
|  |         { | ||||||
|  |             std::copy(begin, begin + size, m_data[stream].begin() + m_vFill[stream]); | ||||||
|  |             m_vFill[stream] += size; | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             int remaining = size - spaceLeft; | ||||||
|  |             std::copy(begin, begin + spaceLeft, m_data[stream].begin() + m_vFill[stream]); | ||||||
|  |             std::copy(begin + spaceLeft, begin + size,  m_data[stream].begin()); | ||||||
|  |             m_vFill[stream] = remaining; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         emit dataAsyncReady(stream); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void SampleMIFifo::readSync( | ||||||
|  |         std::vector<SampleVector::const_iterator>& vpart1Begin, std::vector<SampleVector::const_iterator>& vpart1End, | ||||||
|  |         std::vector<SampleVector::const_iterator>& vpart2Begin, std::vector<SampleVector::const_iterator>& vpart2End | ||||||
|  | ) | ||||||
|  | { | ||||||
|  |     QMutexLocker mutexLocker(&m_mutex); | ||||||
|  |     std::vector<SampleVector>::iterator dataIt = m_data.begin(); | ||||||
|  |     int stream = 0; | ||||||
|  | 
 | ||||||
|  |     for (; dataIt != m_data.end(); ++dataIt, ++stream) | ||||||
|  |     { | ||||||
|  |         if (m_vHead[stream] < m_vFill[stream]) | ||||||
|  |         { | ||||||
|  |             vpart1Begin.push_back(dataIt->begin() + m_vHead[stream]); | ||||||
|  |             vpart1End.push_back(dataIt->begin() + m_vFill[stream]); | ||||||
|  |             vpart2Begin.push_back(dataIt->begin()); | ||||||
|  |             vpart2End.push_back(dataIt->begin()); | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             vpart1Begin.push_back(dataIt->begin() + m_vHead[stream]); | ||||||
|  |             vpart1End.push_back(dataIt->end()); | ||||||
|  |             vpart2Begin.push_back(dataIt->begin()); | ||||||
|  |             vpart2End.push_back(dataIt->begin() + m_vFill[stream]); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         m_vHead[stream] = m_vFill[stream]; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void SampleMIFifo::readSync(int& part1Begin, int& part1End, int& part2Begin, int& part2End) | ||||||
|  | { | ||||||
|  |     if (m_data.size() == 0) { | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     QMutexLocker mutexLocker(&m_mutex); | ||||||
|  | 
 | ||||||
|  |     if (m_head < m_fill) | ||||||
|  |     { | ||||||
|  |         part1Begin = m_head; | ||||||
|  |         part1End = m_fill; | ||||||
|  |         part2Begin = 0; | ||||||
|  |         part2End = 0; | ||||||
|  |     } | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         part1Begin = m_head; | ||||||
|  |         part2End = m_data[0].size(); | ||||||
|  |         part2Begin = 0; | ||||||
|  |         part2End = m_fill; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     for (unsigned int stream = 0; stream < m_data.size(); stream++) { | ||||||
|  |         m_vHead[stream] = m_vFill[stream]; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     m_head = m_fill; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void SampleMIFifo::readAsync(unsigned int stream, | ||||||
|  | 		SampleVector::const_iterator& part1Begin, SampleVector::const_iterator& part1End, | ||||||
|  | 		SampleVector::const_iterator& part2Begin, SampleVector::const_iterator& part2End) | ||||||
|  | { | ||||||
|  |     if (stream < m_data.size()) | ||||||
|  |     { | ||||||
|  |         QMutexLocker mutexLocker(&m_mutex); | ||||||
|  | 
 | ||||||
|  |         if (m_vHead[stream] < m_vFill[stream]) | ||||||
|  |         { | ||||||
|  |             part1Begin = m_data[stream].begin() + m_vHead[stream]; | ||||||
|  |             part1End   = m_data[stream].begin() + m_vFill[stream]; | ||||||
|  |             part2Begin = m_data[stream].begin(); | ||||||
|  |             part2End   = m_data[stream].begin(); | ||||||
|  |         } | ||||||
|  |         else | ||||||
|  |         { | ||||||
|  |             part1Begin = m_data[stream].begin() + m_vHead[stream]; | ||||||
|  |             part1End   = m_data[stream].end(); | ||||||
|  |             part2Begin = m_data[stream].begin(); | ||||||
|  |             part2End   = m_data[stream].begin() + m_vFill[stream]; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         m_vHead[stream] = m_vFill[stream]; | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										60
									
								
								sdrbase/dsp/samplemififo.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								sdrbase/dsp/samplemififo.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,60 @@ | |||||||
|  | ///////////////////////////////////////////////////////////////////////////////////
 | ||||||
|  | // Copyright (C) 2019 Edouard Griffiths, F4EXB                                   //
 | ||||||
|  | //                                                                               //
 | ||||||
|  | // 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/>.          //
 | ||||||
|  | ///////////////////////////////////////////////////////////////////////////////////
 | ||||||
|  | 
 | ||||||
|  | #ifndef INCLUDE_SAMPLEMIFIFO_H | ||||||
|  | #define INCLUDE_SAMPLEMIFIFO_H | ||||||
|  | 
 | ||||||
|  | #include <QObject> | ||||||
|  | #include <QMutex> | ||||||
|  | #include <vector> | ||||||
|  | #include "dsp/dsptypes.h" | ||||||
|  | #include "export.h" | ||||||
|  | 
 | ||||||
|  | class SDRBASE_API SampleMIFifo : public QObject { | ||||||
|  | 	Q_OBJECT | ||||||
|  | 
 | ||||||
|  | public: | ||||||
|  |     SampleMIFifo(QObject *parent = nullptr); | ||||||
|  |     SampleMIFifo(unsigned int nbStreams, unsigned int size, QObject *parent = nullptr); | ||||||
|  |     void init(unsigned int nbStreams, unsigned int size); | ||||||
|  |     void writeSync(const std::vector<SampleVector::const_iterator>& vbegin, unsigned int size); | ||||||
|  |     void writeAsync(unsigned int stream, const SampleVector::const_iterator& begin, unsigned int size); | ||||||
|  |     void readSync( | ||||||
|  |         std::vector<SampleVector::const_iterator>& vpart1Begin, std::vector<SampleVector::const_iterator>& vpart1End, | ||||||
|  |         std::vector<SampleVector::const_iterator>& vpart2Begin, std::vector<SampleVector::const_iterator>& vpart2End | ||||||
|  |     ); | ||||||
|  |     void readSync(int& part1Begin, int& part1End, int& part2Begin, int& part2End); | ||||||
|  | 	void readAsync(unsigned int stream, | ||||||
|  | 		SampleVector::const_iterator& part1Begin, SampleVector::const_iterator& part1End, | ||||||
|  | 		SampleVector::const_iterator& part2Begin, SampleVector::const_iterator& part2End); | ||||||
|  |     const std::vector<SampleVector>& getData() { return m_data; } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | signals: | ||||||
|  | 	void dataSyncReady(); | ||||||
|  |     void dataAsyncReady(int streamIndex); | ||||||
|  | 
 | ||||||
|  | private: | ||||||
|  |     std::vector<SampleVector> m_data; | ||||||
|  |     std::vector<int> m_vFill; //!< Number of samples written from beginning of samples vector
 | ||||||
|  |     std::vector<int> m_vHead; //!< Number of samples read from beginning of samples vector
 | ||||||
|  |     int m_fill; | ||||||
|  |     int m_head; | ||||||
|  | 	QMutex m_mutex; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | #endif // INCLUDE_SAMPLEMIFIFO_H
 | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user