1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-16 05:11:49 -05:00
sdrangel/sdrbase/dsp/samplemofifo.cpp

235 lines
7.9 KiB
C++
Raw Normal View History

///////////////////////////////////////////////////////////////////////////////////
// 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 "samplemofifo.h"
const unsigned int SampleMOFifo::m_rwDivisor = 2;
const unsigned int SampleMOFifo::m_guardDivisor = 10;
2019-11-16 19:57:31 -05:00
SampleMOFifo::SampleMOFifo(QObject *parent) :
QObject(parent),
m_nbStreams(0)
2019-11-16 19:57:31 -05:00
{}
SampleMOFifo::SampleMOFifo(unsigned int nbStreams, unsigned int size, QObject *parent) :
QObject(parent)
2019-11-16 19:57:31 -05:00
{
init(nbStreams, size);
}
void SampleMOFifo::init(unsigned int nbStreams, unsigned int size)
{
2021-04-26 13:33:02 -04:00
QMutexLocker mutexLocker(&m_mutex);
m_data.resize(nbStreams);
2019-11-16 19:57:31 -05:00
m_vReadCount.resize(nbStreams);
m_vReadHead.resize(nbStreams);
m_vWriteHead.resize(nbStreams);
m_nbStreams = nbStreams;
resize(size);
}
void SampleMOFifo::resize(unsigned int size)
{
2019-11-16 19:57:31 -05:00
QMutexLocker mutexLocker(&m_mutex);
m_size = size;
m_lowGuard = m_size / m_guardDivisor;
m_highGuard = m_size - (m_size/m_guardDivisor);
m_midPoint = m_size / m_rwDivisor;
2019-11-16 19:57:31 -05:00
for (unsigned int stream = 0; stream < m_nbStreams; stream++) {
m_data[stream].resize(size);
}
2019-11-16 19:57:31 -05:00
reset();
}
void SampleMOFifo::reset()
{
2019-11-16 19:57:31 -05:00
QMutexLocker mutexLocker(&m_mutex);
m_readCount = 0;
m_readHead = 0;
2019-11-16 19:57:31 -05:00
m_writeHead = m_midPoint;
for (unsigned int stream = 0; stream < m_nbStreams; stream++)
{
m_vReadCount[stream] = 0;
m_vReadHead[stream] = 0;
2019-11-16 19:57:31 -05:00
m_vWriteHead[stream] = m_midPoint;
}
}
2019-11-16 19:57:31 -05:00
SampleMOFifo::~SampleMOFifo()
{}
void SampleMOFifo::readSync(
unsigned int amount,
2019-11-16 19:57:31 -05:00
unsigned int& ipart1Begin, unsigned int& ipart1End, // first part offsets where to read
unsigned int& ipart2Begin, unsigned int& ipart2End // second part offsets
)
{
QMutexLocker mutexLocker(&m_mutex);
unsigned int spaceLeft = m_size - m_readHead;
2019-11-16 21:16:24 -05:00
m_readCount = m_readCount + amount < m_size ? m_readCount + amount : m_size; // cannot exceed FIFO size
if (amount <= spaceLeft)
{
ipart1Begin = m_readHead;
ipart1End = m_readHead + amount;
ipart2Begin = m_size;
ipart2End = m_size;
m_readHead += amount;
}
else
{
unsigned int remaining = (amount < m_size ? amount : m_size) - spaceLeft;
ipart1Begin = m_readHead;
ipart1End = m_size;
ipart2Begin = 0;
ipart2End = remaining;
m_readHead = remaining;
}
2019-11-16 19:57:31 -05:00
emit dataReadSync();
}
void SampleMOFifo::writeSync(
unsigned int amount,
unsigned int& ipart1Begin, unsigned int& ipart1End, // first part offsets where to write
unsigned int& ipart2Begin, unsigned int& ipart2End // second part offsets
)
{
QMutexLocker mutexLocker(&m_mutex);
unsigned int rwDelta = m_writeHead >= m_readHead ? m_writeHead - m_readHead : m_size - (m_readHead - m_writeHead);
if (rwDelta < m_lowGuard)
{
2019-11-16 19:57:31 -05:00
qWarning("SampleMOFifo::write: underrun (write too slow) using %d old samples", m_midPoint - m_lowGuard);
m_writeHead = m_readHead + m_midPoint < m_size ? m_readHead + m_midPoint : m_readHead + m_midPoint - m_size;
}
else if (rwDelta > m_highGuard)
{
2019-11-16 19:57:31 -05:00
qWarning("SampleMOFifo::write: overrrun (read too slow) dropping %d samples", m_highGuard - m_midPoint);
m_writeHead = m_readHead + m_midPoint < m_size ? m_readHead + m_midPoint : m_readHead + m_midPoint - m_size;
}
2019-10-23 12:35:47 -04:00
unsigned int spaceLeft = m_size - m_writeHead;
if (amount <= spaceLeft)
{
ipart1Begin = m_writeHead;
ipart1End = m_writeHead + amount;
ipart2Begin = m_size;
ipart2End = m_size;
m_writeHead += amount;
}
else
{
unsigned int remaining = (amount < m_size ? amount : m_size) - spaceLeft;
2019-10-23 12:35:47 -04:00
ipart1Begin = m_writeHead;
ipart1End = m_size;
ipart2Begin = 0;
ipart2End = remaining;
m_writeHead = remaining;
}
2019-11-16 21:16:24 -05:00
m_readCount = amount < m_readCount ? m_readCount - amount : 0; // cannot be less than 0
}
void SampleMOFifo::readAsync(
unsigned int amount,
unsigned int& ipart1Begin, unsigned int& ipart1End,
unsigned int& ipart2Begin, unsigned int& ipart2End,
unsigned int stream
)
{
QMutexLocker mutexLocker(&m_mutex);
unsigned int spaceLeft = m_size - m_vReadHead[stream];
2019-11-16 21:16:24 -05:00
m_vReadCount[stream] = m_vReadCount[stream] + amount < m_size ? m_vReadCount[stream] + amount : m_size; // cannot exceed FIFO size
if (amount <= spaceLeft)
{
ipart1Begin = m_vReadHead[stream];
ipart1End = m_vReadHead[stream] + amount;
ipart2Begin = m_size;
ipart2End = m_size;
m_vReadHead[stream] += amount;
}
else
{
unsigned int remaining = (amount < m_size ? amount : m_size) - spaceLeft;
ipart1Begin = m_vReadHead[stream];
ipart1End = m_size;
ipart2Begin = 0;
ipart2End = remaining;
2019-11-16 19:57:31 -05:00
m_vReadHead[stream] = remaining;
}
2019-11-16 19:57:31 -05:00
emit dataReadAsync(stream);
}
2019-11-16 19:57:31 -05:00
void SampleMOFifo::writeAsync( //!< in place write with given amount
2019-10-23 12:35:47 -04:00
unsigned int amount,
unsigned int& ipart1Begin, unsigned int& ipart1End,
unsigned int& ipart2Begin, unsigned int& ipart2End,
unsigned int stream
)
{
QMutexLocker mutexLocker(&m_mutex);
2019-11-16 19:57:31 -05:00
unsigned int rwDelta = m_vWriteHead[stream] >= m_vReadHead[stream] ?
m_vWriteHead[stream] - m_vReadHead[stream] : m_size - (m_vReadHead[stream] - m_vWriteHead[stream]);
if (rwDelta < m_lowGuard)
{
2019-11-16 19:57:31 -05:00
qWarning("SampleMOFifo::write: underrun on stream %u (write too slow) using %d old samples", stream, m_midPoint - m_lowGuard);
m_vWriteHead[stream] = m_vReadHead[stream] + m_midPoint < m_size ?
m_vReadHead[stream] + m_midPoint : m_vReadHead[stream] + m_midPoint - m_size;
}
else if (rwDelta > m_highGuard)
{
2019-11-16 19:57:31 -05:00
qWarning("SampleMOFifo::write: overrrun on stream %u (read too slow) dropping %d samples", stream, m_highGuard - m_midPoint);
m_vWriteHead[stream] = m_vReadHead[stream] + m_midPoint < m_size ?
m_vReadHead[stream] + m_midPoint : m_vReadHead[stream] + m_midPoint - m_size;
}
2019-10-23 12:35:47 -04:00
unsigned int spaceLeft = m_size - m_vWriteHead[stream];
if (amount <= spaceLeft)
{
ipart1Begin = m_vWriteHead[stream];
ipart1End = m_vWriteHead[stream] + amount;
ipart2Begin = m_size;
ipart2End = m_size;
m_vWriteHead[stream] += amount;
}
else
{
unsigned int remaining = (amount < m_size ? amount : m_size) - spaceLeft;
2019-10-23 12:35:47 -04:00
ipart1Begin = m_vWriteHead[stream];
ipart1End = m_size;
ipart2Begin = 0;
ipart2End = remaining;
m_vWriteHead[stream] = remaining;
}
2019-11-16 21:16:24 -05:00
m_vReadCount[stream] = amount < m_vReadCount[stream] ? m_vReadCount[stream] - amount : 0; // cannot be less than 0
}
unsigned int SampleMOFifo::getSizePolicy(unsigned int sampleRate)
{
return (sampleRate/100)*64; // .64s
2021-04-26 13:33:02 -04:00
}