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