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 //
|
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-15 03:53:06 -04:00
|
|
|
#include <assert.h>
|
2016-10-09 19:13:12 -04:00
|
|
|
#include "samplesourcefifo.h"
|
|
|
|
|
2016-12-22 17:39:06 -05:00
|
|
|
SampleSourceFifo::SampleSourceFifo(uint32_t size) :
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SampleSourceFifo::~SampleSourceFifo()
|
|
|
|
{}
|
|
|
|
|
2016-10-24 20:34:29 -04:00
|
|
|
void SampleSourceFifo::resize(uint32_t size, uint32_t samplesChunkSize)
|
|
|
|
{
|
|
|
|
qDebug("SampleSourceFifo::resize: %d, %d", size, samplesChunkSize);
|
|
|
|
assert(samplesChunkSize <= size/4);
|
|
|
|
|
|
|
|
m_size = size;
|
|
|
|
m_data.resize(2*m_size);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2016-10-23 08:14:32 -04:00
|
|
|
void SampleSourceFifo::init()
|
|
|
|
{
|
|
|
|
memset(&m_data[0], 0, sizeof(2*m_size*sizeof(Sample)));
|
|
|
|
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-22 17:39:06 -05:00
|
|
|
assert(nbSamples < m_size/4);
|
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;
|
2016-10-24 12:06:44 -04:00
|
|
|
emit dataRead(nbSamples);
|
|
|
|
|
2016-10-15 03:53:06 -04:00
|
|
|
int i_delta = m_iw - m_ir;
|
|
|
|
|
2016-10-23 08:14:32 -04:00
|
|
|
if (m_init)
|
|
|
|
{
|
2016-12-20 20:24:49 -05:00
|
|
|
emit dataWrite(m_size);
|
2016-10-23 08:14:32 -04:00
|
|
|
m_init = false;
|
|
|
|
}
|
|
|
|
else if (i_delta > 0)
|
2016-10-15 03:53:06 -04:00
|
|
|
{
|
2016-12-20 20:24:49 -05:00
|
|
|
if (i_delta <= m_size/2) // m_samplesChunkSize)
|
2016-10-15 03:53:06 -04:00
|
|
|
{
|
2016-12-20 20:24:49 -05:00
|
|
|
emit dataWrite(m_size/2);
|
2016-10-15 03:53:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-20 20:24:49 -05:00
|
|
|
if (i_delta + m_size <= m_size/2) //m_samplesChunkSize)
|
2016-10-15 03:53:06 -04:00
|
|
|
{
|
2016-12-20 20:24:49 -05:00
|
|
|
emit dataWrite(m_size/2);
|
2016-10-15 03:53:06 -04:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|