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