2016-10-09 19:13:12 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2016 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 //
|
2019-04-11 08:32:15 -04:00
|
|
|
// (at your option) any later version. //
|
2016-10-09 19:13:12 -04:00
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-02-21 12:54:59 -05:00
|
|
|
#include <algorithm>
|
2016-10-15 03:53:06 -04:00
|
|
|
#include <assert.h>
|
2016-10-09 19:13:12 -04:00
|
|
|
#include "samplesourcefifo.h"
|
|
|
|
|
2019-05-18 00:30:37 -04:00
|
|
|
SampleSourceFifo::SampleSourceFifo(uint32_t size, QObject* parent) :
|
|
|
|
QObject(parent),
|
2016-10-09 19:13:12 -04:00
|
|
|
m_size(size),
|
2016-10-23 08:14:32 -04:00
|
|
|
m_init(false)
|
2016-10-09 19:13:12 -04:00
|
|
|
{
|
|
|
|
m_data.resize(2*m_size);
|
2016-10-23 08:14:32 -04:00
|
|
|
init();
|
2016-10-09 19:13:12 -04:00
|
|
|
}
|
|
|
|
|
2019-05-18 00:30:37 -04:00
|
|
|
SampleSourceFifo::SampleSourceFifo(const SampleSourceFifo& other) :
|
|
|
|
QObject(other.parent()),
|
2019-06-15 01:00:09 -04:00
|
|
|
m_size(other.m_size),
|
2019-05-18 00:30:37 -04:00
|
|
|
m_data(other.m_data)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
2016-10-09 19:13:12 -04:00
|
|
|
|
|
|
|
SampleSourceFifo::~SampleSourceFifo()
|
|
|
|
{}
|
|
|
|
|
2016-12-23 08:29:42 -05:00
|
|
|
void SampleSourceFifo::resize(uint32_t size)
|
2016-10-24 20:34:29 -04:00
|
|
|
{
|
2016-12-23 08:29:42 -05:00
|
|
|
qDebug("SampleSourceFifo::resize: %d", size);
|
2016-10-24 20:34:29 -04:00
|
|
|
|
|
|
|
m_size = size;
|
|
|
|
m_data.resize(2*m_size);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2016-10-23 08:14:32 -04:00
|
|
|
void SampleSourceFifo::init()
|
|
|
|
{
|
2018-02-21 12:54:59 -05:00
|
|
|
static Sample zero = {0,0};
|
|
|
|
std::fill(m_data.begin(), m_data.end(), zero);
|
2016-10-23 08:14:32 -04:00
|
|
|
m_ir = 0;
|
2016-12-22 17:39:06 -05:00
|
|
|
m_iw = m_size/2;
|
2016-10-23 08:14:32 -04:00
|
|
|
m_init = true;
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:31:36 -04:00
|
|
|
void SampleSourceFifo::readAdvance(SampleVector::iterator& readUntil, unsigned int nbSamples)
|
2016-10-09 19:13:12 -04:00
|
|
|
{
|
2016-12-20 20:24:49 -05:00
|
|
|
// QMutexLocker mutexLocker(&m_mutex);
|
2016-12-26 06:11:51 -05:00
|
|
|
assert(nbSamples <= m_size/2);
|
2018-01-01 19:04:04 -05:00
|
|
|
emit dataWrite(nbSamples);
|
2016-10-15 03:53:06 -04:00
|
|
|
|
|
|
|
m_ir = (m_ir + nbSamples) % m_size;
|
2016-10-24 21:31:36 -04:00
|
|
|
readUntil = m_data.begin() + m_size + m_ir;
|
2019-10-18 23:07:24 -04:00
|
|
|
emit dataRead(nbSamples);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SampleSourceFifo::readAdvance(SampleVector::const_iterator& readUntil, unsigned int nbSamples)
|
|
|
|
{
|
|
|
|
// QMutexLocker mutexLocker(&m_mutex);
|
|
|
|
assert(nbSamples <= m_size/2);
|
|
|
|
emit dataWrite(nbSamples);
|
|
|
|
|
|
|
|
m_ir = (m_ir + nbSamples) % m_size;
|
|
|
|
readUntil = m_data.begin() + m_size + m_ir;
|
2016-10-24 12:06:44 -04:00
|
|
|
emit dataRead(nbSamples);
|
2016-10-09 19:13:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SampleSourceFifo::write(const Sample& sample)
|
|
|
|
{
|
2016-10-15 03:53:06 -04:00
|
|
|
m_data[m_iw] = sample;
|
|
|
|
m_data[m_iw+m_size] = sample;
|
2016-10-09 19:13:12 -04:00
|
|
|
|
|
|
|
{
|
2016-12-20 20:24:49 -05:00
|
|
|
// QMutexLocker mutexLocker(&m_mutex);
|
2016-10-15 03:53:06 -04:00
|
|
|
m_iw = (m_iw+1) % m_size;
|
2016-10-09 19:13:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 20:34:29 -04:00
|
|
|
void SampleSourceFifo::getReadIterator(SampleVector::iterator& readUntil)
|
2016-10-24 12:06:44 -04:00
|
|
|
{
|
2016-10-24 20:34:29 -04:00
|
|
|
readUntil = m_data.begin() + m_size + m_ir;
|
2016-10-24 12:06:44 -04:00
|
|
|
}
|
|
|
|
|
2016-10-09 19:13:12 -04:00
|
|
|
void SampleSourceFifo::getWriteIterator(SampleVector::iterator& writeAt)
|
|
|
|
{
|
2016-10-15 03:53:06 -04:00
|
|
|
writeAt = m_data.begin() + m_iw;
|
2016-10-09 19:13:12 -04:00
|
|
|
}
|
|
|
|
|
2016-10-23 17:27:19 -04:00
|
|
|
void SampleSourceFifo::bumpIndex(SampleVector::iterator& writeAt)
|
2016-10-09 19:13:12 -04:00
|
|
|
{
|
2016-10-15 03:53:06 -04:00
|
|
|
m_data[m_iw+m_size] = m_data[m_iw];
|
2016-10-09 19:13:12 -04:00
|
|
|
|
|
|
|
{
|
2016-12-20 20:24:49 -05:00
|
|
|
// QMutexLocker mutexLocker(&m_mutex);
|
2016-10-15 03:53:06 -04:00
|
|
|
m_iw = (m_iw+1) % m_size;
|
2016-10-09 19:13:12 -04:00
|
|
|
}
|
2016-10-23 17:27:19 -04:00
|
|
|
|
|
|
|
writeAt = m_data.begin() + m_iw;
|
2016-10-09 19:13:12 -04:00
|
|
|
}
|
2019-05-10 07:20:30 -04:00
|
|
|
|
|
|
|
int SampleSourceFifo::getIteratorOffset(const SampleVector::iterator& iterator)
|
|
|
|
{
|
|
|
|
return iterator - m_data.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SampleSourceFifo::setIteratorFromOffset(SampleVector::iterator& iterator, int offset)
|
|
|
|
{
|
|
|
|
iterator = m_data.begin() + offset;
|
|
|
|
}
|