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>
|
2019-11-01 23:50:00 +00:00
|
|
|
#include "samplesourcefifodb.h"
|
2016-10-09 23:13:12 +00:00
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
SampleSourceFifoDB::SampleSourceFifoDB(uint32_t size, QObject* parent) :
|
2019-05-18 04:30:37 +00:00
|
|
|
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-11-01 23:50:00 +00:00
|
|
|
SampleSourceFifoDB::SampleSourceFifoDB(const SampleSourceFifoDB& other) :
|
2019-05-18 04:30:37 +00:00
|
|
|
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
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
SampleSourceFifoDB::~SampleSourceFifoDB()
|
2016-10-09 23:13:12 +00:00
|
|
|
{}
|
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
void SampleSourceFifoDB::resize(uint32_t size)
|
2016-10-25 00:34:29 +00:00
|
|
|
{
|
2019-11-01 23:50:00 +00:00
|
|
|
qDebug("SampleSourceFifoDB::resize: %d", size);
|
2016-10-25 00:34:29 +00:00
|
|
|
|
|
|
|
m_size = size;
|
|
|
|
m_data.resize(2*m_size);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
void SampleSourceFifoDB::init()
|
2016-10-23 12:14:32 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
void SampleSourceFifoDB::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);
|
2020-11-04 19:05:11 +00:00
|
|
|
nbSamples = nbSamples > m_size/2 ? m_size/2 : nbSamples;
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
void SampleSourceFifoDB::readAdvance(SampleVector::const_iterator& readUntil, unsigned int nbSamples)
|
2019-10-19 03:07:24 +00:00
|
|
|
{
|
|
|
|
// QMutexLocker mutexLocker(&m_mutex);
|
2020-11-04 19:05:11 +00:00
|
|
|
nbSamples = nbSamples > m_size/2 ? m_size/2 : nbSamples;
|
2019-10-19 03:07:24 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
void SampleSourceFifoDB::write(const Sample& sample)
|
2016-10-09 23:13:12 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
void SampleSourceFifoDB::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
|
|
|
}
|
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
void SampleSourceFifoDB::getWriteIterator(SampleVector::iterator& writeAt)
|
2016-10-09 23:13:12 +00:00
|
|
|
{
|
2016-10-15 07:53:06 +00:00
|
|
|
writeAt = m_data.begin() + m_iw;
|
2016-10-09 23:13:12 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
void SampleSourceFifoDB::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
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
int SampleSourceFifoDB::getIteratorOffset(const SampleVector::iterator& iterator)
|
2019-05-10 11:20:30 +00:00
|
|
|
{
|
|
|
|
return iterator - m_data.begin();
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:50:00 +00:00
|
|
|
void SampleSourceFifoDB::setIteratorFromOffset(SampleVector::iterator& iterator, int offset)
|
2019-05-10 11:20:30 +00:00
|
|
|
{
|
|
|
|
iterator = m_data.begin() + offset;
|
|
|
|
}
|