2015-09-07 20:46:03 -04:00
|
|
|
#include "FFTDataDistributor.h"
|
|
|
|
|
|
|
|
FFTDataDistributor::FFTDataDistributor() : linesPerSecond(DEFAULT_WATERFALL_LPS), lineRateAccum(0.0), fftSize(DEFAULT_FFT_SIZE) {
|
2015-10-27 01:56:49 -04:00
|
|
|
bufferedItems = 0;
|
2015-09-07 20:46:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FFTDataDistributor::setFFTSize(int fftSize) {
|
|
|
|
this->fftSize = fftSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FFTDataDistributor::setLinesPerSecond(int lines) {
|
|
|
|
this->linesPerSecond = lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FFTDataDistributor::getLinesPerSecond() {
|
|
|
|
return this->linesPerSecond;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FFTDataDistributor::process() {
|
2015-09-09 23:46:05 -04:00
|
|
|
while (!input->empty()) {
|
2015-09-07 20:46:03 -04:00
|
|
|
if (!isAnyOutputEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DemodulatorThreadIQData *inp;
|
|
|
|
input->pop(inp);
|
|
|
|
|
|
|
|
if (inp) {
|
|
|
|
if (inputBuffer.sampleRate != inp->sampleRate || inputBuffer.frequency != inp->frequency) {
|
2015-09-29 22:34:34 -04:00
|
|
|
|
2015-10-01 22:23:02 -04:00
|
|
|
bufferMax = inp->sampleRate / 4;
|
2015-09-29 22:34:34 -04:00
|
|
|
// std::cout << "Buffer Max: " << bufferMax << std::endl;
|
|
|
|
bufferOffset = 0;
|
|
|
|
|
2015-09-07 20:46:03 -04:00
|
|
|
inputBuffer.sampleRate = inp->sampleRate;
|
|
|
|
inputBuffer.frequency = inp->frequency;
|
2015-09-29 22:34:34 -04:00
|
|
|
inputBuffer.data.resize(bufferMax);
|
2015-09-07 20:46:03 -04:00
|
|
|
}
|
2015-09-29 22:34:34 -04:00
|
|
|
if ((bufferOffset + bufferedItems + inp->data.size()) > bufferMax) {
|
2015-09-30 02:07:00 -04:00
|
|
|
memmove(&inputBuffer.data[0], &inputBuffer.data[bufferOffset], bufferedItems*sizeof(liquid_float_complex));
|
|
|
|
bufferOffset = 0;
|
2015-09-29 22:34:34 -04:00
|
|
|
} else {
|
|
|
|
memcpy(&inputBuffer.data[bufferOffset+bufferedItems],&inp->data[0],inp->data.size()*sizeof(liquid_float_complex));
|
|
|
|
bufferedItems += inp->data.size();
|
|
|
|
}
|
2015-09-07 20:46:03 -04:00
|
|
|
inp->decRefCount();
|
|
|
|
} else {
|
2015-09-09 23:46:05 -04:00
|
|
|
continue;
|
2015-09-07 20:46:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// number of seconds contained in input
|
2015-09-29 22:34:34 -04:00
|
|
|
double inputTime = (double)bufferedItems / (double)inputBuffer.sampleRate;
|
2015-09-07 20:46:03 -04:00
|
|
|
// number of lines in input
|
2015-09-29 22:34:34 -04:00
|
|
|
double inputLines = (double)bufferedItems / (double)fftSize;
|
2015-09-07 20:46:03 -04:00
|
|
|
|
|
|
|
// ratio required to achieve the desired rate
|
|
|
|
double lineRateStep = ((double)linesPerSecond * inputTime)/(double)inputLines;
|
|
|
|
|
2015-09-29 22:34:34 -04:00
|
|
|
if (bufferedItems >= fftSize) {
|
2015-09-07 20:46:03 -04:00
|
|
|
int numProcessed = 0;
|
|
|
|
|
2015-09-29 22:34:34 -04:00
|
|
|
if (lineRateAccum + (lineRateStep * ((double)bufferedItems/(double)fftSize)) < 1.0) {
|
2015-09-09 23:29:38 -04:00
|
|
|
// move along, nothing to see here..
|
2015-09-29 22:34:34 -04:00
|
|
|
lineRateAccum += (lineRateStep * ((double)bufferedItems/(double)fftSize));
|
|
|
|
numProcessed = bufferedItems;
|
2015-09-09 23:29:38 -04:00
|
|
|
} else {
|
2015-09-29 22:34:34 -04:00
|
|
|
for (int i = 0, iMax = bufferedItems; i < iMax; i += fftSize) {
|
2015-09-07 20:46:03 -04:00
|
|
|
if ((i + fftSize) > iMax) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lineRateAccum += lineRateStep;
|
|
|
|
|
|
|
|
if (lineRateAccum >= 1.0) {
|
|
|
|
DemodulatorThreadIQData *outp = outputBuffers.getBuffer();
|
|
|
|
outp->frequency = inputBuffer.frequency;
|
|
|
|
outp->sampleRate = inputBuffer.sampleRate;
|
2015-09-29 22:34:34 -04:00
|
|
|
outp->data.assign(inputBuffer.data.begin()+bufferOffset+i,inputBuffer.data.begin()+bufferOffset+i+fftSize);
|
2015-09-07 20:46:03 -04:00
|
|
|
distribute(outp);
|
|
|
|
|
|
|
|
while (lineRateAccum >= 1.0) {
|
|
|
|
lineRateAccum -= 1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
numProcessed += fftSize;
|
|
|
|
}
|
2015-09-09 23:29:38 -04:00
|
|
|
}
|
2015-09-07 20:46:03 -04:00
|
|
|
if (numProcessed) {
|
2015-09-29 22:34:34 -04:00
|
|
|
bufferedItems -= numProcessed;
|
|
|
|
bufferOffset += numProcessed;
|
|
|
|
}
|
|
|
|
if (bufferedItems <= 0) {
|
|
|
|
bufferedItems = 0;
|
|
|
|
bufferOffset = 0;
|
|
|
|
}
|
2015-09-07 20:46:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|