1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-17 05:41:56 -05:00

Added a simple circular sample FIFO

This commit is contained in:
f4exb 2020-07-16 16:57:51 +02:00
parent 461de8e536
commit 705ed9d0e3
3 changed files with 209 additions and 0 deletions

View File

@ -115,6 +115,7 @@ set(sdrbase_SOURCES
dsp/samplemififo.cpp
dsp/samplemofifo.cpp
dsp/samplesinkfifo.cpp
dsp/samplesimplefifo.cpp
dsp/samplesourcefifo.cpp
dsp/samplesourcefifodb.cpp
dsp/basebandsamplesink.cpp
@ -261,6 +262,7 @@ set(sdrbase_HEADERS
dsp/samplemififo.h
dsp/samplemofifo.h
dsp/samplesinkfifo.h
dsp/samplesimplefifo.h
dsp/samplesourcefifo.h
dsp/samplesourcefifodb.h
dsp/basebandsamplesink.h

View File

@ -0,0 +1,157 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2020 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 "samplesimplefifo.h"
void SampleSimpleFifo::create(unsigned int s)
{
m_size = 0;
m_fill = 0;
m_head = 0;
m_tail = 0;
m_data.resize(s);
m_size = m_data.size();
}
void SampleSimpleFifo::reset()
{
m_fill = 0;
m_head = 0;
m_tail = 0;
}
SampleSimpleFifo::SampleSimpleFifo() :
m_data()
{
m_size = 0;
m_fill = 0;
m_head = 0;
m_tail = 0;
}
SampleSimpleFifo::SampleSimpleFifo(int size) :
m_data()
{
create(size);
}
SampleSimpleFifo::SampleSimpleFifo(const SampleSimpleFifo& other) :
m_data(other.m_data)
{
m_size = m_data.size();
m_fill = 0;
m_head = 0;
m_tail = 0;
}
SampleSimpleFifo::~SampleSimpleFifo()
{
m_size = 0;
}
bool SampleSimpleFifo::setSize(int size)
{
create(size);
return m_data.size() == (unsigned int) size;
}
unsigned int SampleSimpleFifo::write(SampleVector::const_iterator begin, SampleVector::const_iterator end)
{
unsigned int count = end - begin;
unsigned int total;
unsigned int remaining;
unsigned int len;
total = count;
remaining = total;
while (remaining > 0)
{
len = std::min(remaining, m_size - m_tail);
std::copy(begin, begin + len, m_data.begin() + m_tail);
m_tail += len;
m_tail %= m_size;
m_fill += len;
begin += len;
remaining -= len;
}
if (m_fill >= m_size) // has wrapped around at least once
{
m_head = m_tail;
m_fill = m_size;
}
return m_fill;
}
unsigned int SampleSimpleFifo::readBegin(unsigned int count,
SampleVector::iterator* part1Begin, SampleVector::iterator* part1End,
SampleVector::iterator* part2Begin, SampleVector::iterator* part2End)
{
unsigned int total;
unsigned int remaining;
unsigned int len;
unsigned int head = m_head;
total = count;
remaining = total;
if (remaining > 0)
{
len = std::min(remaining, m_size - head);
*part1Begin = m_data.begin() + head;
*part1End = m_data.begin() + head + len;
head += len;
head %= m_size;
remaining -= len;
}
else
{
*part1Begin = m_data.end();
*part1End = m_data.end();
}
if (remaining > 0)
{
len = std::min(remaining, m_size - head);
*part2Begin = m_data.begin() + head;
*part2End = m_data.begin() + head + len;
}
else
{
*part2Begin = m_data.end();
*part2End = m_data.end();
}
return total;
}
unsigned int SampleSimpleFifo::readCommit(unsigned int count)
{
if (count > m_fill)
{
qCritical("SampleSinkFifo::readCommit: cannot commit more than available samples");
count = m_fill;
}
m_head = (m_head + count) % m_size;
m_fill -= count;
return count;
}

View File

@ -0,0 +1,50 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2020 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_SAMPLESIMPLEFIFO_H
#define INCLUDE_SAMPLESIMPLEFIFO_H
#include "dsp/dsptypes.h"
#include "export.h"
class SDRBASE_API SampleSimpleFifo {
public:
SampleSimpleFifo();
SampleSimpleFifo(int size);
SampleSimpleFifo(const SampleSimpleFifo& other);
~SampleSimpleFifo();
bool setSize(int size);
void reset();
unsigned int getSize() { return m_size; }
unsigned int write(SampleVector::const_iterator begin, SampleVector::const_iterator end);
unsigned int readBegin(unsigned int count,
SampleVector::iterator* part1Begin, SampleVector::iterator* part1End,
SampleVector::iterator* part2Begin, SampleVector::iterator* part2End);
unsigned int readCommit(unsigned int count);
private:
SampleVector m_data; //!< FIFO container
unsigned int m_size; //!< size of FIFO (above)
unsigned int m_fill; //!< number of samples in FIFO
unsigned int m_head; //!< read point
unsigned int m_tail; //!< write point
void create(unsigned int s);
};
#endif // INCLUDE_SAMPLESIMPLEFIFO_H