This commit is contained in:
Ryzerth 2020-11-02 03:57:44 +01:00
parent 50a73a380d
commit 75f8a45119
33 changed files with 1712 additions and 3339 deletions

92
core/src/dsp/audio.h Normal file
View File

@ -0,0 +1,92 @@
#pragma once
#include <dsp/block.h>
#include <spdlog/spdlog.h>
namespace dsp {
class MonoToStereo : public generic_block<MonoToStereo> {
public:
MonoToStereo() {}
MonoToStereo(stream<float>* in) { init(in); }
~MonoToStereo() { generic_block<MonoToStereo>::stop(); }
void init(stream<float>* in) {
_in = in;
generic_block<MonoToStereo>::registerInput(_in);
generic_block<MonoToStereo>::registerOutput(&out);
}
void setInput(stream<float>* in) {
std::lock_guard<std::mutex> lck(generic_block<MonoToStereo>::ctrlMtx);
generic_block<MonoToStereo>::tempStop();
_in = in;
generic_block<MonoToStereo>::tempStart();
}
int run() {
count = _in->read();
if (count < 0) { return -1; }
if (out.aquire() < 0) { return -1; }
for (int i = 0; i < count; i++) {
out.data[i].l = _in->data[i];
out.data[i].r = _in->data[i];
}
_in->flush();
out.write(count);
return count;
}
stream<stereo_t> out;
private:
int count;
stream<float>* _in;
};
class StereoToMono : public generic_block<StereoToMono> {
public:
StereoToMono() {}
StereoToMono(stream<stereo_t>* in) { init(in); }
~StereoToMono() { generic_block<StereoToMono>::stop(); }
void init(stream<stereo_t>* in) {
_in = in;
generic_block<StereoToMono>::registerInput(_in);
generic_block<StereoToMono>::registerOutput(&out);
}
void setInput(stream<stereo_t>* in) {
std::lock_guard<std::mutex> lck(generic_block<StereoToMono>::ctrlMtx);
generic_block<StereoToMono>::tempStop();
_in = in;
generic_block<StereoToMono>::tempStart();
}
int run() {
count = _in->read();
if (count < 0) { return -1; }
if (out.aquire() < 0) { return -1; }
for (int i = 0; i < count; i++) {
out.data[i] = (_in->data[i].l + _in->data[i].r) / 2.0f;
}
_in->flush();
out.write(count);
return count;
}
stream<float> out;
private:
int count;
stream<stereo_t>* _in;
};
}

View File

@ -1,251 +1,121 @@
#pragma once #pragma once
#include <vector> #include <stdio.h>
#include <dsp/stream.h> #include <dsp/stream.h>
#include <volk/volk.h> #include <dsp/types.h>
#include <thread>
#include <vector>
#include <algorithm>
#define FL_M_PI 3.1415926535f
namespace dsp { namespace dsp {
template <class D, class I, class O, int IC, int OC>
class Block { template <class BLOCK>
class generic_block {
public: public:
Block(std::vector<int> inBs, std::vector<int> outBs, D* inst, void (*workerFunc)(D* _this)) { virtual void init() {}
derived = inst;
worker = workerFunc;
inputBlockSize = inBs;
outputBlockSize = outBs;
in.reserve(IC);
out.reserve(OC);
for (int i = 0; i < IC; i++) {
in.push_back(NULL);
}
for (int i = 0; i < OC; i++) {
out.push_back(new stream<I>(outBs[i] * 2));
}
}
void start() { virtual void start() {
std::lock_guard<std::mutex> lck(ctrlMtx);
if (running) { if (running) {
return; return;
} }
doStart();
}
virtual void stop() {
std::lock_guard<std::mutex> lck(ctrlMtx);
if (!running && !tempStopped) {
return;
}
doStop();
}
virtual int calcOutSize(int inSize) { return inSize; }
virtual int run() = 0;
friend BLOCK;
private:
void workerLoop() {
while (run() >= 0);
}
void aquire() {
ctrlMtx.lock();
}
void release() {
ctrlMtx.unlock();
}
void registerInput(untyped_steam* inStream) {
inputs.push_back(inStream);
}
void unregisterInput(untyped_steam* inStream) {
inputs.erase(std::remove(inputs.begin(), inputs.end(), inStream), inputs.end());
}
void registerOutput(untyped_steam* outStream) {
outputs.push_back(outStream);
}
void unregisterOutput(untyped_steam* outStream) {
outputs.erase(std::remove(outputs.begin(), outputs.end(), outStream), outputs.end());
}
virtual void doStart() {
running = true; running = true;
startHandler(); workerThread = std::thread(&generic_block::workerLoop, this);
workerThread = std::thread(worker, derived);
} }
void stop() { virtual void doStop() {
if (!running) { for (auto const& in : inputs) {
return; in->stopReader();
} }
stopHandler(); for (auto const& out : outputs) {
for (auto is : in) { out->stopWriter();
is->stopReader();
}
for (auto os : out) {
os->stopWriter();
} }
// TODO: Make sure this isn't needed, I don't know why it stops
if (workerThread.joinable()) {
workerThread.join(); workerThread.join();
for (auto is : in) {
is->clearReadStop();
}
for (auto os : out) {
os->clearWriteStop();
}
running = false;
} }
virtual void setBlockSize(int blockSize) { for (auto const& in : inputs) {
if (running) { in->clearReadStop();
return;
} }
for (int i = 0; i < IC; i++) { for (auto const& out : outputs) {
in[i]->setMaxLatency(blockSize * 2); out->clearWriteStop();
inputBlockSize[i] = blockSize;
}
for (int i = 0; i < OC; i++) {
out[i]->setMaxLatency(blockSize * 2);
outputBlockSize[i] = blockSize;
} }
} }
std::vector<stream<I>*> out; void tempStart() {
if (tempStopped) {
doStart();
tempStopped = false;
}
}
void tempStop() {
if (running && !tempStopped) {
doStop();
tempStopped = true;
}
}
std::vector<untyped_steam*> inputs;
std::vector<untyped_steam*> outputs;
bool running = false;
bool tempStopped = false;
std::thread workerThread;
protected: protected:
virtual void startHandler() {} std::mutex ctrlMtx;
virtual void stopHandler() {}
std::vector<stream<I>*> in;
std::vector<int> inputBlockSize;
std::vector<int> outputBlockSize;
bool running = false;
private:
void (*worker)(D* _this);
std::thread workerThread;
D* derived;
}; };
}
class DemoMultiplier : public Block<DemoMultiplier, complex_t, complex_t, 2, 1> {
public:
DemoMultiplier() : Block({2}, {1}, this, worker) {}
void init(stream<complex_t>* a, stream<complex_t>* b, int blockSize) {
in[0] = a;
in[1] = b;
inputBlockSize[0] = blockSize;
inputBlockSize[1] = blockSize;
out[0]->setMaxLatency(blockSize * 2);
outputBlockSize[0] = blockSize;
}
private:
static void worker(DemoMultiplier* _this) {
int blockSize = _this->inputBlockSize[0];
stream<complex_t>* inA = _this->in[0];
stream<complex_t>* inB = _this->in[1];
stream<complex_t>* out = _this->out[0];
complex_t* aBuf = (complex_t*)volk_malloc(sizeof(complex_t) * blockSize, volk_get_alignment());
complex_t* bBuf = (complex_t*)volk_malloc(sizeof(complex_t) * blockSize, volk_get_alignment());
complex_t* outBuf = (complex_t*)volk_malloc(sizeof(complex_t) * blockSize, volk_get_alignment());
while (true) {
if (inA->read(aBuf, blockSize) < 0) { break; };
if (inB->read(bBuf, blockSize) < 0) { break; };
volk_32fc_x2_multiply_32fc((lv_32fc_t*)outBuf, (lv_32fc_t*)aBuf, (lv_32fc_t*)bBuf, blockSize);
if (out->write(outBuf, blockSize) < 0) { break; };
}
volk_free(aBuf);
volk_free(bBuf);
volk_free(outBuf);
}
};
class Squelch : public Block<Squelch, complex_t, complex_t, 1, 1> {
public:
Squelch() : Block({1}, {1}, this, worker) {}
void init(stream<complex_t>* input, int blockSize) {
in[0] = input;
inputBlockSize[0] = blockSize;
out[0]->setMaxLatency(blockSize * 2);
outputBlockSize[0] = blockSize;
level = -50.0f;
}
float level;
int onCount;
int offCount;
private:
static void worker(Squelch* _this) {
int blockSize = _this->inputBlockSize[0];
stream<complex_t>* in = _this->in[0];
stream<complex_t>* out = _this->out[0];
complex_t* buf = new complex_t[blockSize];
int _on = 0, _off = 0;
bool active = false;
while (true) {
if (in->read(buf, blockSize) < 0) { break; };
for (int i = 0; i < blockSize; i++) {
if (log10(sqrt((buf[i].i*buf[i].i) + (buf[i].q*buf[i].q))) * 10.0f > _this->level) {
_on++;
_off = 0;
}
else {
_on = 0;
_off++;
}
if (_on >= _this->onCount && !active) {
_on = _this->onCount;
active = true;
}
if (_off >= _this->offCount && active) {
_off = _this->offCount;
active = false;
}
if (!active) {
buf[i].i = 0.0f;
buf[i].q = 0.0f;
}
}
if (out->write(buf, blockSize) < 0) { break; };
}
delete[] buf;
}
};
template <class T>
class Reshaper {
public:
Reshaper() {
}
void init(int outBlockSize, dsp::stream<T>* input) {
outputBlockSize = outBlockSize;
in = input;
out.init(outputBlockSize * 2);
}
void setOutputBlockSize(int blockSize) {
if (running) {
return;
}
outputBlockSize = blockSize;
out.setMaxLatency(outputBlockSize * 2);
}
void setInput(dsp::stream<T>* input) {
if (running) {
return;
}
in = input;
}
void start() {
if (running) {
return;
}
workerThread = std::thread(_worker, this);
running = true;
}
void stop() {
if (!running) {
return;
}
in->stopReader();
out.stopWriter();
workerThread.join();
in->clearReadStop();
out.clearWriteStop();
running = false;
}
dsp::stream<T> out;
private:
static void _worker(Reshaper* _this) {
T* buf = new T[_this->outputBlockSize];
while (true) {
if (_this->in->read(buf, _this->outputBlockSize) < 0) { break; }
if (_this->out.write(buf, _this->outputBlockSize) < 0) { break; }
}
delete[] buf;
}
int outputBlockSize;
bool running = false;
std::thread workerThread;
dsp::stream<T>* in;
};
};

216
core/src/dsp/buffer.h Normal file
View File

@ -0,0 +1,216 @@
#pragma once
#include <dsp/block.h>
#define RING_BUF_SZ
namespace dsp {
template <class T>
class RingBuffer {
public:
RingBuffer() {
}
RingBuffer(int maxLatency) { init(maxLatency); }
~RingBuffer() { delete _buffer; }
void init(int maxLatency) {
size = RING_BUF_SZ;
_buffer = new T[size];
_stopReader = false;
_stopWriter = false;
this->maxLatency = maxLatency;
writec = 0;
readc = 0;
readable = 0;
writable = size;
memset(_buffer, 0, size * sizeof(T));
}
int read(T* data, int len) {
int dataRead = 0;
int toRead = 0;
while (dataRead < len) {
toRead = std::min<int>(waitUntilReadable(), len - dataRead);
if (toRead < 0) { return -1; };
if ((toRead + readc) > size) {
memcpy(&data[dataRead], &_buffer[readc], (size - readc) * sizeof(T));
memcpy(&data[dataRead + (size - readc)], &_buffer[0], (toRead - (size - readc)) * sizeof(T));
}
else {
memcpy(&data[dataRead], &_buffer[readc], toRead * sizeof(T));
}
dataRead += toRead;
_readable_mtx.lock();
readable -= toRead;
_readable_mtx.unlock();
_writable_mtx.lock();
writable += toRead;
_writable_mtx.unlock();
readc = (readc + toRead) % size;
canWriteVar.notify_one();
}
return len;
}
int readAndSkip(T* data, int len, int skip) {
int dataRead = 0;
int toRead = 0;
while (dataRead < len) {
toRead = std::min<int>(waitUntilReadable(), len - dataRead);
if (toRead < 0) { return -1; };
if ((toRead + readc) > size) {
memcpy(&data[dataRead], &_buffer[readc], (size - readc) * sizeof(T));
memcpy(&data[dataRead + (size - readc)], &_buffer[0], (toRead - (size - readc)) * sizeof(T));
}
else {
memcpy(&data[dataRead], &_buffer[readc], toRead * sizeof(T));
}
dataRead += toRead;
_readable_mtx.lock();
readable -= toRead;
_readable_mtx.unlock();
_writable_mtx.lock();
writable += toRead;
_writable_mtx.unlock();
readc = (readc + toRead) % size;
canWriteVar.notify_one();
}
dataRead = 0;
while (dataRead < skip) {
toRead = std::min<int>(waitUntilReadable(), skip - dataRead);
if (toRead < 0) { return -1; };
dataRead += toRead;
_readable_mtx.lock();
readable -= toRead;
_readable_mtx.unlock();
_writable_mtx.lock();
writable += toRead;
_writable_mtx.unlock();
readc = (readc + toRead) % size;
canWriteVar.notify_one();
}
return len;
}
int waitUntilReadable() {
if (_stopReader) { return -1; }
int _r = getReadable();
if (_r != 0) { return _r; }
std::unique_lock<std::mutex> lck(_readable_mtx);
canReadVar.wait(lck, [=](){ return ((this->getReadable(false) > 0) || this->getReadStop()); });
if (_stopReader) { return -1; }
return getReadable(false);
}
int getReadable(bool lock = true) {
if (lock) { _readable_mtx.lock(); };
int _r = readable;
if (lock) { _readable_mtx.unlock(); };
return _r;
}
int write(T* data, int len) {
int dataWritten = 0;
int toWrite = 0;
while (dataWritten < len) {
toWrite = std::min<int>(waitUntilwritable(), len - dataWritten);
if (toWrite < 0) { return -1; };
if ((toWrite + writec) > size) {
memcpy(&_buffer[writec], &data[dataWritten], (size - writec) * sizeof(T));
memcpy(&_buffer[0], &data[dataWritten + (size - writec)], (toWrite - (size - writec)) * sizeof(T));
}
else {
memcpy(&_buffer[writec], &data[dataWritten], toWrite * sizeof(T));
}
dataWritten += toWrite;
_readable_mtx.lock();
readable += toWrite;
_readable_mtx.unlock();
_writable_mtx.lock();
writable -= toWrite;
_writable_mtx.unlock();
writec = (writec + toWrite) % size;
canReadVar.notify_one();
}
return len;
}
int waitUntilwritable() {
if (_stopWriter) { return -1; }
int _w = getWritable();
if (_w != 0) { return _w; }
std::unique_lock<std::mutex> lck(_writable_mtx);
canWriteVar.wait(lck, [=](){ return ((this->getWritable(false) > 0) || this->getWriteStop()); });
if (_stopWriter) { return -1; }
return getWritable(false);
}
int getWritable(bool lock = true) {
if (lock) { _writable_mtx.lock(); };
int _w = writable;
if (lock) { _writable_mtx.unlock(); _readable_mtx.lock(); };
int _r = readable;
if (lock) { _readable_mtx.unlock(); };
return std::max<int>(std::min<int>(_w, maxLatency - _r), 0);
}
void stopReader() {
_stopReader = true;
canReadVar.notify_one();
}
void stopWriter() {
_stopWriter = true;
canWriteVar.notify_one();
}
bool getReadStop() {
return _stopReader;
}
bool getWriteStop() {
return _stopWriter;
}
void clearReadStop() {
_stopReader = false;
}
void clearWriteStop() {
_stopWriter = false;
}
void setMaxLatency(int maxLatency) {
this->maxLatency = maxLatency;
}
private:
T* _buffer;
int size;
int readc;
int writec;
int readable;
int writable;
int maxLatency;
bool _stopReader;
bool _stopWriter;
std::mutex _readable_mtx;
std::mutex _writable_mtx;
std::condition_variable canReadVar;
std::condition_variable canWriteVar;
};
};

View File

@ -1,234 +0,0 @@
#pragma once
#include <thread>
#include <dsp/stream.h>
#include <dsp/types.h>
#include <vector>
namespace dsp {
class DCBiasRemover {
public:
DCBiasRemover() {
}
DCBiasRemover(stream<complex_t>* input, int bufferSize) : output(bufferSize * 2) {
_in = input;
_bufferSize = bufferSize;
bypass = false;
}
void init(stream<complex_t>* input, int bufferSize) {
output.init(bufferSize * 2);
_in = input;
_bufferSize = bufferSize;
bypass = false;
}
void start() {
if (running) {
return;
}
_workerThread = std::thread(_worker, this);
running = true;
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
output.stopWriter();
_workerThread.join();
_in->clearReadStop();
output.clearWriteStop();
running = false;
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_bufferSize = blockSize;
output.setMaxLatency(blockSize * 2);
}
void setInput(stream<complex_t>* input) {
if (running) {
return;
}
_in = input;
}
stream<complex_t> output;
bool bypass;
private:
// static void _worker(DCBiasRemover* _this) {
// complex_t* buf = new complex_t[_this->_bufferSize];
// float ibias = 0.0f;
// float qbias = 0.0f;
// while (true) {
// if (_this->_in->read(buf, _this->_bufferSize) < 0) { break; };
// if (_this->bypass) {
// if (_this->output.write(buf, _this->_bufferSize) < 0) { break; };
// continue;
// }
// for (int i = 0; i < _this->_bufferSize; i++) {
// ibias += buf[i].i;
// qbias += buf[i].q;
// }
// ibias /= _this->_bufferSize;
// qbias /= _this->_bufferSize;
// for (int i = 0; i < _this->_bufferSize; i++) {
// buf[i].i -= ibias;
// buf[i].q -= qbias;
// }
// if (_this->output.write(buf, _this->_bufferSize) < 0) { break; };
// }
// delete[] buf;
// }
static void _worker(DCBiasRemover* _this) {
complex_t* buf = new complex_t[_this->_bufferSize];
complex_t* mixBuf = new complex_t[_this->_bufferSize];
float currentPhase = 0.0f;
float lastPhase = 0.0f;
double phase = 0.0f;
while (true) {
float ibias = 0.0f;
float qbias = 0.0f;
if (_this->_in->read(buf, _this->_bufferSize) < 0) { break; };
if (_this->bypass) {
if (_this->output.write(buf, _this->_bufferSize) < 0) { break; };
continue;
}
// Detect the frequency of the signal
double avgDiff = 0.0f;
// for (int i = 0; i < _this->_bufferSize; i++) {
// currentPhase = fast_arctan2(buf[i].i, buf[i].q);
// float diff = currentPhase - lastPhase;
// if (diff > 3.1415926535f) { diff -= 2 * 3.1415926535f; }
// else if (diff <= -3.1415926535f) { diff += 2 * 3.1415926535f; }
// avgDiff += diff;
// lastPhase = currentPhase;
// }
// avgDiff /= (double)_this->_bufferSize;
// avgDiff /= (double)_this->_bufferSize;
// Average the samples to "filter" the signal to the block frequency
for (int i = 0; i < _this->_bufferSize; i++) {
ibias += buf[i].i;
qbias += buf[i].q;
}
ibias /= _this->_bufferSize;
qbias /= _this->_bufferSize;
// Get the phase difference from the last block
currentPhase = fast_arctan2(ibias, qbias);
float diff = currentPhase - lastPhase;
if (diff > 3.1415926535f) { diff -= 2 * 3.1415926535f; }
else if (diff <= -3.1415926535f) { diff += 2 * 3.1415926535f; }
avgDiff += diff;
lastPhase = currentPhase;
avgDiff /= (double)_this->_bufferSize;
// Generate a correction signal using the phase difference
for (int i = 0; i < _this->_bufferSize; i++) {
mixBuf[i].i = sin(phase);
mixBuf[i].q = cos(phase);
phase -= avgDiff;
phase = fmodl(phase, 2.0 * 3.1415926535);
}
// Mix the correction signal with the original signal to shift the unwanted signal
// to the center. Also, null out the real component so that symetric
// frequencies are removed (at least I hope...)
float tq;
for (int i = 0; i < _this->_bufferSize; i++) {
buf[i].i = ((mixBuf[i].i * buf[i].q) + (mixBuf[i].q * buf[i].i)) * 1.4142;
buf[i].q = 0;
}
if (_this->output.write(buf, _this->_bufferSize) < 0) { break; };
}
delete[] buf;
}
stream<complex_t>* _in;
int _bufferSize;
std::thread _workerThread;
bool running = false;
};
class ComplexToStereo {
public:
ComplexToStereo() {
}
ComplexToStereo(stream<complex_t>* input, int bufferSize) : output(bufferSize * 2) {
_in = input;
_bufferSize = bufferSize;
}
void init(stream<complex_t>* input, int bufferSize) {
output.init(bufferSize * 2);
_in = input;
_bufferSize = bufferSize;
}
void start() {
if (running) {
return;
}
_workerThread = std::thread(_worker, this);
running = true;
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
output.stopWriter();
_workerThread.join();
_in->clearReadStop();
output.clearWriteStop();
running = false;
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_bufferSize = blockSize;
output.setMaxLatency(blockSize * 2);
}
stream<StereoFloat_t> output;
private:
static void _worker(ComplexToStereo* _this) {
complex_t* inBuf = new complex_t[_this->_bufferSize];
StereoFloat_t* outBuf = new StereoFloat_t[_this->_bufferSize];
while (true) {
if (_this->_in->read(inBuf, _this->_bufferSize) < 0) { break; };
for (int i = 0; i < _this->_bufferSize; i++) {
outBuf[i].l = inBuf[i].i;
outBuf[i].r = inBuf[i].q;
}
if (_this->output.write(outBuf, _this->_bufferSize) < 0) { break; };
}
delete[] inBuf;
delete[] outBuf;
}
stream<complex_t>* _in;
int _bufferSize;
std::thread _workerThread;
bool running = false;
};
};

View File

@ -1,22 +1,15 @@
#pragma once #pragma once
#include <thread> #include <dsp/block.h>
#include <dsp/stream.h> #include <spdlog/spdlog.h>
#include <dsp/types.h>
#include <dsp/source.h>
#include <dsp/math.h>
/* #define FAST_ATAN2_COEF1 FL_M_PI / 4.0f
TODO:
- Add a sample rate ajustment function to all demodulators
*/
#define FAST_ATAN2_COEF1 3.1415926535f / 4.0f
#define FAST_ATAN2_COEF2 3.0f * FAST_ATAN2_COEF1 #define FAST_ATAN2_COEF2 3.0f * FAST_ATAN2_COEF1
inline float fast_arctan2(float y, float x) { inline float fast_arctan2(float y, float x) {
float abs_y = fabs(y) + (1e-10); float abs_y = fabsf(y);
float r, angle; float r, angle;
if (x>=0) { if (x == 0.0f && y == 0.0f) { return 0.0f; }
if (x>=0.0f) {
r = (x - abs_y) / (x + abs_y); r = (x - abs_y) / (x + abs_y);
angle = FAST_ATAN2_COEF1 - FAST_ATAN2_COEF1 * r; angle = FAST_ATAN2_COEF1 - FAST_ATAN2_COEF1 * r;
} }
@ -24,401 +17,244 @@ inline float fast_arctan2(float y, float x) {
r = (x + abs_y) / (abs_y - x); r = (x + abs_y) / (abs_y - x);
angle = FAST_ATAN2_COEF2 - FAST_ATAN2_COEF1 * r; angle = FAST_ATAN2_COEF2 - FAST_ATAN2_COEF1 * r;
} }
if (y < 0) { if (y < 0.0f) {
return -angle; return -angle;
} }
return angle; return angle;
} }
namespace dsp { namespace dsp {
class FMDemodulator { class FMDemod : public generic_block<FMDemod> {
public: public:
FMDemodulator() { FMDemod() {}
} FMDemod(stream<complex_t>* in, float sampleRate, float deviation) { init(in, sampleRate, deviation); }
FMDemodulator(stream<complex_t>* in, float deviation, long sampleRate, int blockSize) : output(blockSize * 2) { ~FMDemod() { generic_block<FMDemod>::stop(); }
running = false;
_input = in; void init(stream<complex_t>* in, float sampleRate, float deviation) {
_blockSize = blockSize; _in = in;
_phase = 0.0f;
_deviation = deviation;
_sampleRate = sampleRate; _sampleRate = sampleRate;
_phasorSpeed = (2 * 3.1415926535) / (sampleRate / deviation); _deviation = deviation;
phasorSpeed = (_sampleRate / _deviation) / (2 * FL_M_PI);
generic_block<FMDemod>::registerInput(_in);
generic_block<FMDemod>::registerOutput(&out);
} }
void init(stream<complex_t>* in, float deviation, long sampleRate, int blockSize) { void setInput(stream<complex_t>* in) {
output.init(blockSize * 2); std::lock_guard<std::mutex> lck(generic_block<FMDemod>::ctrlMtx);
running = false; generic_block<FMDemod>::tempStop();
_input = in; _in = in;
_blockSize = blockSize; generic_block<FMDemod>::tempStart();
_phase = 0.0f;
_phasorSpeed = (2 * 3.1415926535) / (sampleRate / deviation);
}
void start() {
if (running) {
return;
}
running = true;
_workerThread = std::thread(_worker, this);
}
void stop() {
if (!running) {
return;
}
_input->stopReader();
output.stopWriter();
_workerThread.join();
running = false;
_input->clearReadStop();
output.clearWriteStop();
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_blockSize = blockSize;
output.setMaxLatency(_blockSize * 2);
} }
void setSampleRate(float sampleRate) { void setSampleRate(float sampleRate) {
std::lock_guard<std::mutex> lck(generic_block<FMDemod>::ctrlMtx);
generic_block<FMDemod>::tempStop();
_sampleRate = sampleRate; _sampleRate = sampleRate;
_phasorSpeed = (2 * 3.1415926535) / (sampleRate / _deviation); phasorSpeed = (_sampleRate / _deviation) / (2 * FL_M_PI);
generic_block<FMDemod>::tempStart();
}
float getSampleRate() {
return _sampleRate;
} }
void setDeviation(float deviation) { void setDeviation(float deviation) {
std::lock_guard<std::mutex> lck(generic_block<FMDemod>::ctrlMtx);
generic_block<FMDemod>::tempStop();
_deviation = deviation; _deviation = deviation;
_phasorSpeed = (2 * 3.1415926535) / (_sampleRate / _deviation); phasorSpeed = (_sampleRate / _deviation) / (2 * FL_M_PI);
generic_block<FMDemod>::tempStart();
} }
stream<float> output; float getDeviation() {
return _deviation;
}
int run() {
count = _in->read();
if (count < 0) { return -1; }
// This is somehow faster than volk...
float diff, currentPhase;
if (out.aquire() < 0) { return -1; }
for (int i = 0; i < count; i++) {
currentPhase = fast_arctan2(_in->data[i].i, _in->data[i].q);
diff = currentPhase - phase;
if (diff > FL_M_PI) { out.data[i] = (diff - 2 * FL_M_PI) * phasorSpeed; }
else if (diff <= -FL_M_PI) { out.data[i] = (diff + 2 * FL_M_PI) * phasorSpeed; }
phase = currentPhase;
}
_in->flush();
out.write(count);
return count;
}
stream<float> out;
private: private:
static void _worker(FMDemodulator* _this) { int count;
complex_t* inBuf = new complex_t[_this->_blockSize]; float phase, phasorSpeed, _sampleRate, _deviation;
float* outBuf = new float[_this->_blockSize]; stream<complex_t>* _in;
float diff = 0;
float currentPhase = 0;
while (true) {
if (_this->_input->read(inBuf, _this->_blockSize) < 0) { return; };
for (int i = 0; i < _this->_blockSize; i++) {
currentPhase = fast_arctan2(inBuf[i].i, inBuf[i].q);
diff = currentPhase - _this->_phase;
if (diff > 3.1415926535f) { diff -= 2 * 3.1415926535f; }
else if (diff <= -3.1415926535f) { diff += 2 * 3.1415926535f; }
outBuf[i] = diff / _this->_phasorSpeed;
_this->_phase = currentPhase;
}
if (_this->output.write(outBuf, _this->_blockSize) < 0) { return; };
}
}
stream<complex_t>* _input;
bool running;
int _blockSize;
float _phase;
float _phasorSpeed;
float _deviation;
float _sampleRate;
std::thread _workerThread;
}; };
class AMDemod : public generic_block<AMDemod> {
class AMDemodulator {
public: public:
AMDemodulator() { AMDemod() {}
AMDemod(stream<complex_t>* in) { init(in); }
~AMDemod() { generic_block<AMDemod>::stop(); }
void init(stream<complex_t>* in) {
_in = in;
generic_block<AMDemod>::registerInput(_in);
generic_block<AMDemod>::registerOutput(&out);
} }
AMDemodulator(stream<complex_t>* in, int blockSize) : output(blockSize * 2) { void setInput(stream<complex_t>* in) {
running = false; std::lock_guard<std::mutex> lck(generic_block<AMDemod>::ctrlMtx);
_input = in; generic_block<AMDemod>::tempStop();
_blockSize = blockSize; _in = in;
generic_block<AMDemod>::tempStart();
} }
void init(stream<complex_t>* in, int blockSize) { int run() {
output.init(blockSize * 2); count = _in->read();
running = false; if (count < 0) { return -1; }
_input = in;
_blockSize = blockSize; if (out.aquire() < 0) { return -1; }
volk_32fc_magnitude_32f(out.data, (lv_32fc_t*)_in->data, count);
_in->flush();
out.write(count);
return count;
} }
void start() { stream<float> out;
if (running) {
return;
}
running = true;
_workerThread = std::thread(_worker, this);
}
void stop() {
if (!running) {
return;
}
_input->stopReader();
output.stopWriter();
_workerThread.join();
running = false;
_input->clearReadStop();
output.clearWriteStop();
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_blockSize = blockSize;
output.setMaxLatency(_blockSize * 2);
}
stream<float> output;
private: private:
static void _worker(AMDemodulator* _this) { int count;
complex_t* inBuf = new complex_t[_this->_blockSize]; stream<complex_t>* _in;
float* outBuf = new float[_this->_blockSize];
float min, max, amp;
while (true) {
if (_this->_input->read(inBuf, _this->_blockSize) < 0) { break; };
min = INFINITY;
max = 0.0f;
for (int i = 0; i < _this->_blockSize; i++) {
outBuf[i] = sqrt((inBuf[i].i*inBuf[i].i) + (inBuf[i].q*inBuf[i].q));
if (outBuf[i] < min) {
min = outBuf[i];
}
if (outBuf[i] > max) {
max = outBuf[i];
}
}
amp = (max - min) / 2.0f;
for (int i = 0; i < _this->_blockSize; i++) {
outBuf[i] = (outBuf[i] - min - amp) / amp;
}
if (_this->output.write(outBuf, _this->_blockSize) < 0) { break; };
}
delete[] inBuf;
delete[] outBuf;
}
stream<complex_t>* _input;
bool running;
int _blockSize;
std::thread _workerThread;
}; };
class SSBDemod { class SSBDemod : public generic_block<SSBDemod> {
public: public:
SSBDemod() { SSBDemod() {}
} SSBDemod(stream<complex_t>* in, float sampleRate, float bandWidth, int mode) { init(in, sampleRate, bandWidth, mode); }
void init(stream<complex_t>* input, float sampleRate, float bandWidth, int blockSize) { ~SSBDemod() { generic_block<SSBDemod>::stop(); }
_blockSize = blockSize;
_bandWidth = bandWidth;
_mode = MODE_USB;
output.init(blockSize * 2);
lo.init(bandWidth / 2.0f, sampleRate, blockSize);
mixer.init(input, &lo.output, blockSize);
lo.start();
}
void start() {
mixer.start();
_workerThread = std::thread(_worker, this);
running = true;
}
void stop() {
mixer.stop();
mixer.output.stopReader();
output.stopWriter();
_workerThread.join();
mixer.output.clearReadStop();
output.clearWriteStop();
running = false;
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_blockSize = blockSize;
}
void setMode(int mode) {
if (mode < 0 && mode >= _MODE_COUNT) {
return;
}
_mode = mode;
if (mode == MODE_USB) {
lo.setFrequency(_bandWidth / 2.0f);
}
else if (mode == MODE_LSB) {
lo.setFrequency(-_bandWidth / 2.0f);
}
else if (mode == MODE_LSB) {
lo.setFrequency(0);
}
}
void setBandwidth(float bandwidth) {
_bandWidth = bandwidth;
if (_mode == MODE_USB) {
lo.setFrequency(_bandWidth / 2.0f);
}
else if (_mode == MODE_LSB) {
lo.setFrequency(-_bandWidth / 2.0f);
}
}
stream<float> output;
enum { enum {
MODE_USB, MODE_USB,
MODE_LSB, MODE_LSB,
MODE_DSB, MODE_DSB
_MODE_COUNT
}; };
void init(stream<complex_t>* in, float sampleRate, float bandWidth, int mode) {
_in = in;
_sampleRate = sampleRate;
_bandWidth = bandWidth;
_mode = mode;
phase = lv_cmake(1.0f, 0.0f);
switch (_mode) {
case MODE_USB:
phaseDelta = lv_cmake(std::cos((_bandWidth / _sampleRate) * FL_M_PI), std::sin((_bandWidth / _sampleRate) * FL_M_PI));
break;
case MODE_LSB:
phaseDelta = lv_cmake(std::cos(-(_bandWidth / _sampleRate) * FL_M_PI), std::sin(-(_bandWidth / _sampleRate) * FL_M_PI));
break;
case MODE_DSB:
phaseDelta = lv_cmake(1.0f, 0.0f);
break;
}
generic_block<SSBDemod>::registerInput(_in);
generic_block<SSBDemod>::registerOutput(&out);
}
void setInput(stream<complex_t>* in) {
std::lock_guard<std::mutex> lck(generic_block<SSBDemod>::ctrlMtx);
generic_block<SSBDemod>::tempStop();
_in = in;
generic_block<SSBDemod>::tempStart();
}
void setSampleRate(float sampleRate) {
// No need to restart
_sampleRate = sampleRate;
switch (_mode) {
case MODE_USB:
phaseDelta = lv_cmake(std::cos((_bandWidth / _sampleRate) * FL_M_PI), std::sin((_bandWidth / _sampleRate) * FL_M_PI));
break;
case MODE_LSB:
phaseDelta = lv_cmake(std::cos(-(_bandWidth / _sampleRate) * FL_M_PI), std::sin(-(_bandWidth / _sampleRate) * FL_M_PI));
break;
case MODE_DSB:
phaseDelta = lv_cmake(1.0f, 0.0f);
break;
}
}
void setBandWidth(float bandWidth) {
// No need to restart
_bandWidth = bandWidth;
switch (_mode) {
case MODE_USB:
phaseDelta = lv_cmake(std::cos((_bandWidth / _sampleRate) * FL_M_PI), std::sin((_bandWidth / _sampleRate) * FL_M_PI));
break;
case MODE_LSB:
phaseDelta = lv_cmake(std::cos(-(_bandWidth / _sampleRate) * FL_M_PI), std::sin(-(_bandWidth / _sampleRate) * FL_M_PI));
break;
case MODE_DSB:
phaseDelta = lv_cmake(1.0f, 0.0f);
break;
}
}
void setMode(int mode) {
_mode = mode;
switch (_mode) {
case MODE_USB:
phaseDelta = lv_cmake(std::cos((_bandWidth / _sampleRate) * FL_M_PI), std::sin((_bandWidth / _sampleRate) * FL_M_PI));
break;
case MODE_LSB:
phaseDelta = lv_cmake(std::cos(-(_bandWidth / _sampleRate) * FL_M_PI), std::sin(-(_bandWidth / _sampleRate) * FL_M_PI));
break;
case MODE_DSB:
phaseDelta = lv_cmake(1.0f, 0.0f);
break;
}
}
int run() {
count = _in->read();
if (count < 0) { return -1; }
if (out.aquire() < 0) { return -1; }
volk_32fc_s32fc_x2_rotator_32fc((lv_32fc_t*)out.data, (lv_32fc_t*)_in->data, phaseDelta, &phase, count);
volk_32fc_deinterleave_real_32f(out.data, (lv_32fc_t*)_in->data, count);
_in->flush();
out.write(count);
return count;
}
stream<float> out;
private: private:
static void _worker(SSBDemod* _this) { int count;
complex_t* inBuf = new complex_t[_this->_blockSize];
float* outBuf = new float[_this->_blockSize];
float min, max, factor;
while (true) {
if (_this->mixer.output.read(inBuf, _this->_blockSize) < 0) { break; };
min = INFINITY;
max = -INFINITY;
for (int i = 0; i < _this->_blockSize; i++) {
outBuf[i] = inBuf[i].q;
if (inBuf[i].q < min) {
min = inBuf[i].q;
}
if (inBuf[i].q > max) {
max = inBuf[i].q;
}
}
factor = (max - min) / 2;
for (int i = 0; i < _this->_blockSize; i++) {
outBuf[i] /= factor;
}
if (_this->output.write(outBuf, _this->_blockSize) < 0) { break; };
}
delete[] inBuf;
delete[] outBuf;
}
std::thread _workerThread;
SineSource lo;
Multiplier mixer;
int _blockSize;
float _bandWidth;
int _mode; int _mode;
bool running = false; float _sampleRate, _bandWidth;
stream<complex_t>* _in;
lv_32fc_t phase;
lv_32fc_t phaseDelta;
}; };
}
// class CWDemod {
// public:
// CWDemod() {
// }
// void init(stream<complex_t>* input, float sampleRate, float bandWidth, int blockSize) {
// _blockSize = blockSize;
// _bandWidth = bandWidth;
// _mode = MODE_USB;
// output.init(blockSize * 2);
// lo.init(bandWidth / 2.0f, sampleRate, blockSize);
// mixer.init(input, &lo.output, blockSize);
// lo.start();
// }
// void start() {
// mixer.start();
// _workerThread = std::thread(_worker, this);
// running = true;
// }
// void stop() {
// mixer.stop();
// mixer.output.stopReader();
// output.stopWriter();
// _workerThread.join();
// mixer.output.clearReadStop();
// output.clearWriteStop();
// running = false;
// }
// void setBlockSize(int blockSize) {
// if (running) {
// return;
// }
// _blockSize = blockSize;
// }
// void setMode(int mode) {
// if (mode < 0 && mode >= _MODE_COUNT) {
// return;
// }
// _mode = mode;
// if (mode == MODE_USB) {
// lo.setFrequency(_bandWidth / 2.0f);
// }
// else if (mode == MODE_LSB) {
// lo.setFrequency(-_bandWidth / 2.0f);
// }
// }
// stream<float> output;
// private:
// static void _worker(CWDemod* _this) {
// complex_t* inBuf = new complex_t[_this->_blockSize];
// float* outBuf = new float[_this->_blockSize];
// float min, max, factor;
// while (true) {
// if (_this->mixer.output.read(inBuf, _this->_blockSize) < 0) { break; };
// min = INFINITY;
// max = -INFINITY;
// for (int i = 0; i < _this->_blockSize; i++) {
// outBuf[i] = inBuf[i].q;
// if (inBuf[i].q < min) {
// min = inBuf[i].q;
// }
// if (inBuf[i].q > max) {
// max = inBuf[i].q;
// }
// }
// factor = (max - min) / 2;
// for (int i = 0; i < _this->_blockSize; i++) {
// outBuf[i] /= factor;
// }
// if (_this->output.write(outBuf, _this->_blockSize) < 0) { break; };
// }
// delete[] inBuf;
// delete[] outBuf;
// }
// std::thread _workerThread;
// SineSource lo;
// Multiplier mixer;
// int _blockSize;
// float _bandWidth;
// int _mode;
// bool running = false;
// };
};

View File

@ -1,489 +1,167 @@
#pragma once #pragma once
#include <thread> #include <dsp/block.h>
#include <dsp/stream.h> #include <dsp/window.h>
#include <dsp/types.h> #include <spdlog/
#include <vector>
#include <dsp/math.h>
#include <spdlog/spdlog.h>
#define GET_FROM_RIGHT_BUF(buffer, delayLine, delayLineSz, n) (((n) < 0) ? delayLine[(delayLineSz) + (n)] : buffer[(n)])
namespace dsp { namespace dsp {
inline void BlackmanWindow(std::vector<float>& taps, float sampleRate, float cutoff, float transWidth, int addedTaps = 0) {
taps.clear();
float fc = cutoff / sampleRate; template <class T>
if (fc > 1.0f) { class FIR : public generic_block<FIR<T>> {
fc = 1.0f;
}
int _M = (4.0f / (transWidth / sampleRate)) + (float)addedTaps;
if (_M < 4) {
_M = 4;
}
if (_M % 2 == 0) { _M++; }
float M = _M;
float sum = 0.0f;
float val;
for (int i = 0; i < _M; i++) {
val = (sin(2.0f * M_PI * fc * ((float)i - (M / 2))) / ((float)i - (M / 2))) * (0.42f - (0.5f * cos(2.0f * M_PI / M)) + (0.8f * cos(4.0f * M_PI / M)));
taps.push_back(val);
sum += val;
}
for (int i = 0; i < M; i++) {
taps[i] /= sum;
}
}
class DecimatingFIRFilter {
public: public:
DecimatingFIRFilter() { FIR() {}
FIR(stream<T>* in, dsp::filter_window::generic_window* window) { init(in, window); }
~FIR() {
generic_block<FIR<T>>::stop();
volk_free(buffer);
volk_free(taps);
} }
DecimatingFIRFilter(stream<complex_t>* input, std::vector<float> taps, int blockSize, float decim) { void init(stream<T>* in, dsp::filter_window::generic_window* window) {
output.init((blockSize * 2) / decim); _in = in;
_in = input;
_blockSize = blockSize;
_tapCount = taps.size();
delayBuf = new complex_t[_tapCount];
_taps = new float[_tapCount]; tapCount = window->getTapCount();
for (int i = 0; i < _tapCount; i++) { taps = (float*)volk_malloc(tapCount * sizeof(float), volk_get_alignment());
_taps[i] = taps[i]; window->createTaps(taps, tapCount);
buffer = (T*)volk_malloc(STREAM_BUFFER_SIZE * sizeof(T) * 2, volk_get_alignment());
bufStart = &buffer[tapCount];
generic_block<FIR<T>>::registerInput(_in);
generic_block<FIR<T>>::registerOutput(&out);
} }
_decim = decim; void setInput(stream<T>* in) {
std::lock_guard<std::mutex> lck(generic_block<FIR<T>>::ctrlMtx);
for (int i = 0; i < _tapCount; i++) { generic_block<FIR<T>>::tempStop();
delayBuf[i].i = 0.0f; _in = in;
delayBuf[i].q = 0.0f; generic_block<FIR<T>>::tempStart();
} }
running = false; void updateWindow(dsp::filter_window::generic_window* window) {
_window = window;
volk_free(taps);
tapCount = window->getTapCount();
taps = (float*)volk_malloc(tapCount * sizeof(float), volk_get_alignment());
window->createTaps(taps, tapCount);
} }
void init(stream<complex_t>* input, std::vector<float>& taps, int blockSize, float decim) { int run() {
output.init((blockSize * 2) / decim); count = _in->read();
_in = input; if (count < 0) { return -1; }
_blockSize = blockSize;
_tapCount = taps.size();
delayBuf = new complex_t[_tapCount];
_taps = new float[_tapCount]; memcpy(bufStart, _in->data, count * sizeof(T));
for (int i = 0; i < _tapCount; i++) { _in->flush();
_taps[i] = taps[i];
}
_decim = decim; // Write to output
if (out.aquire() < 0) { return -1; }
for (int i = 0; i < _tapCount; i++) { if constexpr (std::is_same_v<T, float>) {
delayBuf[i].i = 0.0f; for (int i = 0; i < count; i++) {
delayBuf[i].q = 0.0f; volk_32f_x2_dot_prod_32f((float*)&out.data[i], (float*)&buffer[i+1], taps, tapCount);
} }
running = false;
} }
if constexpr (std::is_same_v<T, complex_t>) {
void start() { for (int i = 0; i < count; i++) {
if (running) { volk_32fc_32f_dot_prod_32fc((lv_32fc_t*)&out.data[i], (lv_32fc_t*)&buffer[i+1], taps, tapCount);
return;
}
running = true;
_workerThread = std::thread(_worker, this);
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
output.stopWriter();
_workerThread.join();
_in->clearReadStop();
output.clearWriteStop();
running = false;
}
void setTaps(std::vector<float>& taps) {
if (running) {
return;
}
_tapCount = taps.size();
delete[] _taps;
delete[] delayBuf;
_taps = new float[_tapCount];
delayBuf = new complex_t[_tapCount];
for (int i = 0; i < _tapCount; i++) {
_taps[i] = taps[i];
delayBuf[i].i = 0;
delayBuf[i].q = 0;
} }
} }
void setInput(stream<complex_t>* input) { out.write(count);
if (running) {
return; memmove(buffer, &buffer[count], tapCount * sizeof(T));
}
_in = input; return count;
} }
void setDecimation(float decimation) { stream<T> out;
if (running) {
return;
}
_decim = decimation;
output.setMaxLatency((_blockSize * 2) / _decim);
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_blockSize = blockSize;
output.setMaxLatency(getOutputBlockSize() * 2);
}
int getOutputBlockSize() {
return _blockSize / _decim;
}
stream<complex_t> output;
private: private:
static void _worker(DecimatingFIRFilter* _this) { int count;
int outputSize = _this->_blockSize / _this->_decim; stream<T>* _in;
complex_t* inBuf = new complex_t[_this->_blockSize];
complex_t* outBuf = new complex_t[outputSize];
float tap = 0.0f;
int delayOff;
void* delayStart = &inBuf[_this->_blockSize - (_this->_tapCount - 1)];
int delaySize = (_this->_tapCount - 1) * sizeof(complex_t);
int blockSize = _this->_blockSize; dsp::filter_window::generic_window* _window;
int outBufferLength = outputSize * sizeof(complex_t);
int tapCount = _this->_tapCount;
int decim = _this->_decim;
complex_t* delayBuf = _this->delayBuf;
int id = 0;
while (true) { T* bufStart;
if (_this->_in->read(inBuf, blockSize) < 0) { break; }; T* buffer;
memset(outBuf, 0, outBufferLength); int tapCount;
float* taps;
for (int t = 0; t < tapCount; t++) {
tap = _this->_taps[t];
if (tap == 0.0f) {
continue;
}
delayOff = tapCount - t;
id = 0;
for (int i = 0; i < blockSize; i += decim) {
if (i < t) {
outBuf[id].i += tap * delayBuf[delayOff + i].i;
outBuf[id].q += tap * delayBuf[delayOff + i].q;
}
else {
outBuf[id].i += tap * inBuf[i - t].i;
outBuf[id].q += tap * inBuf[i - t].q;
}
id++;
}
}
memcpy(delayBuf, delayStart, delaySize);
if (_this->output.write(outBuf, outputSize) < 0) { break; };
}
delete[] inBuf;
delete[] outBuf;
}
stream<complex_t>* _in;
complex_t* delayBuf;
int _blockSize;
int _tapCount = 0;
float _decim;
std::thread _workerThread;
float* _taps;
bool running;
}; };
class BFMDeemp : public generic_block<BFMDeemp> {
class FloatDecimatingFIRFilter {
public: public:
FloatDecimatingFIRFilter() { BFMDeemp() {}
} BFMDeemp(stream<float>* in, float sampleRate, float tau) { init(in, sampleRate, tau); }
FloatDecimatingFIRFilter(stream<float>* input, std::vector<float> taps, int blockSize, float decim) { ~BFMDeemp() { generic_block<BFMDeemp>::stop(); }
output.init((blockSize * 2) / decim);
_in = input;
_blockSize = blockSize;
_tapCount = taps.size();
delayBuf = new float[_tapCount];
_taps = new float[_tapCount]; void init(stream<float>* in, float sampleRate, float tau) {
for (int i = 0; i < _tapCount; i++) { _in = in;
_taps[i] = taps[i]; _sampleRate = sampleRate;
}
_decim = decim;
for (int i = 0; i < _tapCount; i++) {
delayBuf[i] = 0.0f;
}
running = false;
}
void init(stream<float>* input, std::vector<float>& taps, int blockSize, float decim) {
output.init((blockSize * 2) / decim);
_in = input;
_blockSize = blockSize;
_tapCount = taps.size();
delayBuf = new float[_tapCount];
_taps = new float[_tapCount];
for (int i = 0; i < _tapCount; i++) {
_taps[i] = taps[i];
}
_decim = decim;
for (int i = 0; i < _tapCount; i++) {
delayBuf[i] = 0.0f;
}
running = false;
}
void start() {
if (running) {
return;
}
running = true;
_workerThread = std::thread(_worker, this);
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
output.stopWriter();
_workerThread.join();
_in->clearReadStop();
output.clearWriteStop();
running = false;
}
void setTaps(std::vector<float>& taps) {
if (running) {
return;
}
_tapCount = taps.size();
delete[] _taps;
delete[] delayBuf;
_taps = new float[_tapCount];
delayBuf = new float[_tapCount];
for (int i = 0; i < _tapCount; i++) {
_taps[i] = taps[i];
delayBuf[i] = 0;
}
}
void setInput(stream<float>* input) {
if (running) {
return;
}
_in = input;
}
void setDecimation(float decimation) {
if (running) {
return;
}
_decim = decimation;
output.setMaxLatency((_blockSize * 2) / _decim);
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_blockSize = blockSize;
output.setMaxLatency((_blockSize * 2) / _decim);
}
int getOutputBlockSize() {
return _blockSize / _decim;
}
stream<float> output;
private:
static void _worker(FloatDecimatingFIRFilter* _this) {
int outputSize = _this->_blockSize / _this->_decim;
float* inBuf = new float[_this->_blockSize];
float* outBuf = new float[outputSize];
float tap = 0.0f;
int delayOff;
void* delayStart = &inBuf[_this->_blockSize - (_this->_tapCount - 1)];
int delaySize = (_this->_tapCount - 1) * sizeof(float);
int blockSize = _this->_blockSize;
int outBufferLength = outputSize * sizeof(float);
int tapCount = _this->_tapCount;
int decim = _this->_decim;
float* delayBuf = _this->delayBuf;
int id = 0;
while (true) {
if (_this->_in->read(inBuf, blockSize) < 0) { break; };
memset(outBuf, 0, outBufferLength);
for (int t = 0; t < tapCount; t++) {
tap = _this->_taps[t];
if (tap == 0.0f) {
continue;
}
delayOff = tapCount - t;
id = 0;
for (int i = 0; i < blockSize; i += decim) {
if (i < t) {
outBuf[id] += tap * delayBuf[delayOff + i];
id++;
continue;
}
outBuf[id] += tap * inBuf[i - t];
id++;
}
}
memcpy(delayBuf, delayStart, delaySize);
if (_this->output.write(outBuf, outputSize) < 0) { break; };
}
delete[] inBuf;
delete[] outBuf;
}
stream<float>* _in;
float* delayBuf;
int _blockSize;
int _tapCount = 0;
float _decim;
std::thread _workerThread;
float* _taps;
bool running;
};
class FMDeemphasis {
public:
FMDeemphasis() {
}
FMDeemphasis(stream<float>* input, int bufferSize, float tau, float sampleRate) : output(bufferSize * 2) {
_in = input;
_bufferSize = bufferSize;
bypass = false;
_tau = tau; _tau = tau;
float dt = 1.0f / _sampleRate;
alpha = dt / (_tau + dt);
generic_block<BFMDeemp>::registerInput(_in);
generic_block<BFMDeemp>::registerOutput(&out);
}
void setInput(stream<float>* in) {
std::lock_guard<std::mutex> lck(generic_block<BFMDeemp>::ctrlMtx);
generic_block<BFMDeemp>::tempStop();
_in = in;
generic_block<BFMDeemp>::tempStart();
}
void setSampleRate(float sampleRate) {
_sampleRate = sampleRate; _sampleRate = sampleRate;
} float dt = 1.0f / _sampleRate;
alpha = dt / (_tau + dt);
void init(stream<float>* input, int bufferSize, float tau, float sampleRate) {
output.init(bufferSize * 2);
_in = input;
_bufferSize = bufferSize;
bypass = false;
_tau = tau;
_sampleRate = sampleRate;
}
void start() {
if (running) {
return;
}
_workerThread = std::thread(_worker, this);
running = true;
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
output.stopWriter();
_workerThread.join();
_in->clearReadStop();
output.clearWriteStop();
running = false;
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_bufferSize = blockSize;
output.setMaxLatency(blockSize * 2);
}
void setSamplerate(float sampleRate) {
if (running) {
return;
}
_sampleRate = sampleRate;
} }
void setTau(float tau) { void setTau(float tau) {
if (running) {
return;
}
_tau = tau; _tau = tau;
float dt = 1.0f / _sampleRate;
alpha = dt / (_tau + dt);
} }
stream<float> output; int run() {
bool bypass; count = _in->read();
if (count < 0) { return -1; }
private: if (bypass) {
static void _worker(FMDeemphasis* _this) { if (out.aquire() < 0) { return -1; }
float* inBuf = new float[_this->_bufferSize]; _in->flush();
float* outBuf = new float[_this->_bufferSize]; out.write(count);
int count = _this->_bufferSize;
float lastOut = 0.0f;
float dt = 1.0f / _this->_sampleRate;
float alpha = dt / (_this->_tau + dt);
while (true) {
if (_this->_in->read(inBuf, count) < 0) { break; };
if (_this->bypass) {
if (_this->output.write(inBuf, count) < 0) { break; };
continue;
} }
if (isnan(lastOut)) { if (isnan(lastOut)) {
lastOut = 0.0f; lastOut = 0.0f;
} }
outBuf[0] = (alpha * inBuf[0]) + ((1-alpha) * lastOut); if (out.aquire() < 0) { return -1; }
out.data[0] = (alpha * _in->data[0]) + ((1-alpha) * lastOut);
for (int i = 1; i < count; i++) { for (int i = 1; i < count; i++) {
outBuf[i] = (alpha * inBuf[i]) + ((1 - alpha) * outBuf[i - 1]); out.data[i] = (alpha * _in->data[i]) + ((1 - alpha) * out.data[i - 1]);
} }
lastOut = outBuf[count - 1]; lastOut = out.data[count - 1];
if (_this->output.write(outBuf, count) < 0) { break; }; _in->flush();
} out.write(count);
delete[] inBuf; return count;
delete[] outBuf;
} }
stream<float>* _in; bool bypass = false;
int _bufferSize;
std::thread _workerThread; stream<float> out;
bool running = false;
float _sampleRate; private:
int count;
float lastOut = 0.0f;
float alpha;
float _tau; float _tau;
float _sampleRate;
stream<float>* _in;
}; };
}; }

View File

@ -1,85 +1,107 @@
#pragma once #pragma once
#include <thread> #include <dsp/block.h>
#include <dsp/stream.h>
#include <dsp/types.h>
#include <volk/volk.h> #include <volk/volk.h>
#ifndef M_PI
#define M_PI 3.1415926535f
#endif
namespace dsp { namespace dsp {
class Multiplier { template <class T>
class Add : public generic_block<Add<T>> {
public: public:
Multiplier() { Add() {}
} Add(stream<T>* a, stream<T>* b) { init(a, b); }
Multiplier(stream<complex_t>* a, stream<complex_t>* b, int blockSize) : output(blockSize * 2) { ~Add() { generic_block<Add>::stop(); }
void init(stream<T>* a, stream<T>* b) {
_a = a; _a = a;
_b = b; _b = b;
_blockSize = blockSize; generic_block<Add>::registerInput(a);
generic_block<Add>::registerInput(b);
generic_block<Add>::registerOutput(&out);
} }
void init(stream<complex_t>* a, stream<complex_t>* b, int blockSize) { int run() {
output.init(blockSize * 2); a_count = _a->read();
_a = a; if (a_count < 0) { return -1; }
_b = b; b_count = _b->read();
_blockSize = blockSize; if (b_count < 0) { return -1; }
if (a_count != b_count) {
_a->flush();
_b->flush();
return 0;
} }
void start() { if (out.aquire() < 0) { return -1; }
if (running) { if constexpr (std::is_same_v<T, complex_t> || std::is_same_v<T, stereo_t>) {
return; volk_32fc_x2_add_32fc(out.data, _a->data, _b->data, a_count);
} }
running = true; else {
_workerThread = std::thread(_worker, this); volk_32f_x2_add_32f(out.data, _a->data, _b->data, a_count);
} }
void stop() { _a->flush();
if (!running) { _b->flush();
return; out.write(a_count);
} return a_count;
_a->stopReader();
_b->stopReader();
output.stopWriter();
_workerThread.join();
running = false;
_a->clearReadStop();
_b->clearReadStop();
output.clearWriteStop();
} }
void setBlockSize(int blockSize) { stream<T> out;
if (running) {
return;
}
_blockSize = blockSize;
output.setMaxLatency(blockSize * 2);
}
stream<complex_t> output;
private: private:
static void _worker(Multiplier* _this) { int a_count, b_count;
complex_t* aBuf = (complex_t*)volk_malloc(sizeof(complex_t) * _this->_blockSize, volk_get_alignment()); stream<T>* _a;
complex_t* bBuf = (complex_t*)volk_malloc(sizeof(complex_t) * _this->_blockSize, volk_get_alignment()); stream<T>* _b;
complex_t* outBuf = (complex_t*)volk_malloc(sizeof(complex_t) * _this->_blockSize, volk_get_alignment());
while (true) { };
if (_this->_a->read(aBuf, _this->_blockSize) < 0) { break; };
if (_this->_b->read(bBuf, _this->_blockSize) < 0) { break; }; template <class T>
volk_32fc_x2_multiply_32fc((lv_32fc_t*)outBuf, (lv_32fc_t*)aBuf, (lv_32fc_t*)bBuf, _this->_blockSize); class Multiply : public generic_block<Multiply<T>> {
if (_this->output.write(outBuf, _this->_blockSize) < 0) { break; }; public:
} Multiply() {}
volk_free(aBuf);
volk_free(bBuf); Multiply(stream<T>* a, stream<T>* b) { init(a, b); }
volk_free(outBuf);
~Multiply() { generic_block<Multiply>::stop(); }
void init(stream<T>* a, stream<T>* b) {
_a = a;
_b = b;
generic_block<Multiply>::registerInput(a);
generic_block<Multiply>::registerInput(b);
generic_block<Multiply>::registerOutput(&out);
} }
stream<complex_t>* _a; int run() {
stream<complex_t>* _b; a_count = _a->read();
int _blockSize; if (a_count < 0) { return -1; }
bool running = false; b_count = _b->read();
std::thread _workerThread; if (b_count < 0) { return -1; }
if (a_count != b_count) {
_a->flush();
_b->flush();
return 0;
}
if (out.aquire() < 0) { return -1; }
if constexpr (std::is_same_v<T, complex_t>) {
volk_32fc_x2_multiply_32fc(out.data, _a->data, _b->data, a_count);
}
else {
volk_32f_x2_multiply_32f(out.data, _a->data, _b->data, a_count);
}
_a->flush();
_b->flush();
out.write(a_count);
return a_count;
}
stream<T> out;
private:
int a_count, b_count;
stream<T>* _a;
stream<T>* _b;
}; };
}; }

73
core/src/dsp/processing.h Normal file
View File

@ -0,0 +1,73 @@
#pragma once
#include <dsp/block.h>
namespace dsp {
class FrequencyXlator : public generic_block<FrequencyXlator> {
public:
FrequencyXlator() {}
FrequencyXlator(stream<complex_t>* in, float sampleRate, float freq) { init(in, sampleRate, freq); }
~FrequencyXlator() { generic_block<FrequencyXlator>::stop(); }
void init(stream<complex_t>* in, float sampleRate, float freq) {
_in = in;
_sampleRate = sampleRate;
_freq = freq;
phase = lv_cmake(1.0f, 0.0f);
phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI));
generic_block<FrequencyXlator>::registerOutput(&out);
}
void setInputSize(stream<complex_t>* in) {
std::lock_guard<std::mutex> lck(generic_block<FrequencyXlator>::ctrlMtx);
generic_block<FrequencyXlator>::tempStop();
_in = in;
generic_block<FrequencyXlator>::tempStart();
}
void setSampleRate(float sampleRate) {
// No need to restart
_sampleRate = sampleRate;
phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI));
}
float getSampleRate() {
return _sampleRate;
}
void setFrequency(float freq) {
// No need to restart
_freq = freq;
phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI));
}
float getFrequency() {
return _freq;
}
int run() {
count = _in->read();
if (count < 0) { return -1; }
if (out.aquire() < 0) { return -1; }
volk_32fc_s32fc_x2_rotator_32fc((lv_32fc_t*)out.data, (lv_32fc_t*)_in->data, phaseDelta, &phase, count);
_in->flush();
out.write(count);
return count;
}
stream<complex_t> out;
private:
int count;
float _sampleRate;
float _freq;
lv_32fc_t phaseDelta;
lv_32fc_t phase;
stream<complex_t>* _in;
};
}

File diff suppressed because it is too large Load Diff

View File

@ -1,310 +1,102 @@
#pragma once #pragma once
#include <thread> #include <dsp/block.h>
#include <dsp/stream.h> #include <cstring>
#include <dsp/types.h>
#include <vector>
#include <spdlog/spdlog.h>
namespace dsp { namespace dsp {
class Splitter {
public:
Splitter() {
}
Splitter(stream<complex_t>* input, int bufferSize) {
_in = input;
_bufferSize = bufferSize;
output_a.init(bufferSize);
output_b.init(bufferSize);
}
void init(stream<complex_t>* input, int bufferSize) {
_in = input;
_bufferSize = bufferSize;
output_a.init(bufferSize);
output_b.init(bufferSize);
}
void start() {
if (running) {
return;
}
_workerThread = std::thread(_worker, this);
running = true;
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
output_a.stopWriter();
output_b.stopWriter();
_workerThread.join();
_in->clearReadStop();
output_a.clearWriteStop();
output_b.clearWriteStop();
running = false;
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_bufferSize = blockSize;
output_a.setMaxLatency(blockSize * 2);
output_b.setMaxLatency(blockSize * 2);
}
stream<complex_t> output_a;
stream<complex_t> output_b;
private:
static void _worker(Splitter* _this) {
complex_t* buf = new complex_t[_this->_bufferSize];
while (true) {
if (_this->_in->read(buf, _this->_bufferSize) < 0) { break; };
if (_this->output_a.write(buf, _this->_bufferSize) < 0) { break; };
if (_this->output_b.write(buf, _this->_bufferSize) < 0) { break; };
}
delete[] buf;
}
stream<complex_t>* _in;
int _bufferSize;
std::thread _workerThread;
bool running = false;
};
template <class T> template <class T>
class DynamicSplitter { class Splitter : public generic_block<Splitter<T>> {
public: public:
DynamicSplitter() { Splitter() {}
Splitter(stream<T>* in) { init(in); }
~Splitter() { generic_block<Splitter>::stop(); }
void init(stream<T>* in) {
_in = in;
generic_block<Splitter>::registerInput(_in);
} }
DynamicSplitter(stream<T>* input, int bufferSize) { void setInput(stream<T>* in) {
_in = input; std::lock_guard<std::mutex> lck(generic_block<Splitter>::ctrlMtx);
_bufferSize = bufferSize; generic_block<Splitter>::tempStop();
generic_block<Splitter>::unregisterInput(_in);
_in = in;
generic_block<Splitter>::registerInput(_in);
generic_block<Splitter>::tempStart();
} }
void init(stream<T>* input, int bufferSize) { void bindStream(stream<T>* stream) {
_in = input; std::lock_guard<std::mutex> lck(generic_block<Splitter>::ctrlMtx);
_bufferSize = bufferSize; generic_block<Splitter>::tempStop();
out.push_back(stream);
generic_block<Splitter>::registerOutput(stream);
generic_block<Splitter>::tempStart();
} }
void start() { void unbindStream(stream<T>* stream) {
if (running) { std::lock_guard<std::mutex> lck(generic_block<Splitter>::ctrlMtx);
return; generic_block<Splitter>::tempStop();
} generic_block<Splitter>::unregisterOutput(stream);
_workerThread = std::thread(_worker, this); out.erase(std::remove(out.begin(), out.end(), stream), out.end());
running = true; generic_block<Splitter>::tempStart();
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
int outputCount = outputs.size();
for (int i = 0; i < outputCount; i++) {
outputs[i]->stopWriter();
}
_workerThread.join();
_in->clearReadStop();
for (int i = 0; i < outputCount; i++) {
outputs[i]->clearWriteStop();
}
running = false;
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_bufferSize = blockSize;
int outputCount = outputs.size();
for (int i = 0; i < outputCount; i++) {
outputs[i]->setMaxLatency(blockSize * 2);
}
}
void bind(stream<T>* stream) {
if (running) {
return;
}
outputs.push_back(stream);
}
void unbind(stream<T>* stream) {
if (running) {
return;
}
int outputCount = outputs.size();
for (int i = 0; i < outputCount; i++) {
if (outputs[i] == stream) {
outputs.erase(outputs.begin() + i);
return;
}
}
} }
private: private:
static void _worker(DynamicSplitter* _this) { int run() {
T* buf = new T[_this->_bufferSize]; // TODO: If too slow, buffering might be necessary
int outputCount = _this->outputs.size(); int count = _in->read();
while (true) { if (count < 0) { return -1; }
if (_this->_in->read(buf, _this->_bufferSize) < 0) { break; }; for (const auto& stream : out) {
for (int i = 0; i < outputCount; i++) { if (stream->aquire() < 0) { return -1; }
if (_this->outputs[i]->write(buf, _this->_bufferSize) < 0) { break; }; memcpy(stream->data, _in->data, count * sizeof(T));
stream->write(count);
} }
} _in->flush();
delete[] buf; return count;
} }
stream<T>* _in; stream<T>* _in;
int _bufferSize; std::vector<stream<T>*> out;
std::thread _workerThread;
bool running = false;
std::vector<stream<T>*> outputs;
}; };
template <class T>
class MonoToStereo { class Reshaper : public generic_block<Reshaper<T>> {
public: public:
MonoToStereo() { Reshaper() {}
Reshaper(stream<T>* in) { init(in); }
~Reshaper() { generic_block<Reshaper<T>>::stop(); }
void init(stream<T>* in) {
_in = in;
buffer = (T*)volk_malloc(STREAM_BUFFER_SIZE * sizeof(T), volk_get_alignment());
generic_block<Reshaper<T>>::registerInput(_in);
generic_block<Reshaper<T>>::registerOutput(&out);
} }
MonoToStereo(stream<float>* input, int bufferSize) { void setInput(stream<T>* in) {
_in = input; std::lock_guard<std::mutex> lck(generic_block<Reshaper<T>>::ctrlMtx);
_bufferSize = bufferSize; generic_block<Reshaper<T>>::tempStop();
output.init(bufferSize * 2); _in = in;
generic_block<Reshaper<T>>::tempStart();
} }
void init(stream<float>* input, int bufferSize) { int run() {
_in = input; int count = _in->read();
_bufferSize = bufferSize; _in->flush();
output.init(bufferSize * 2); return count;
} }
void start() { stream<T> out;
if (running) {
return;
}
_workerThread = std::thread(_worker, this);
running = true;
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
output.stopWriter();
_workerThread.join();
_in->clearReadStop();
output.clearWriteStop();
running = false;
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_bufferSize = blockSize;
output.setMaxLatency(blockSize * 2);
}
stream<StereoFloat_t> output;
private: private:
static void _worker(MonoToStereo* _this) { stream<T>* _in;
float* inBuf = new float[_this->_bufferSize]; T* buffer;
StereoFloat_t* outBuf = new StereoFloat_t[_this->_bufferSize]; int _outBlockSize;
while (true) { int readCount;
if (_this->_in->read(inBuf, _this->_bufferSize) < 0) { break; };
for (int i = 0; i < _this->_bufferSize; i++) {
outBuf[i].l = inBuf[i];
outBuf[i].r = inBuf[i];
}
if (_this->output.write(outBuf, _this->_bufferSize) < 0) { break; };
}
delete[] inBuf;
delete[] outBuf;
}
stream<float>* _in;
int _bufferSize;
std::thread _workerThread;
bool running = false;
}; };
}
class StereoToMono {
public:
StereoToMono() {
}
StereoToMono(stream<StereoFloat_t>* input, int bufferSize) {
_in = input;
_bufferSize = bufferSize;
}
void init(stream<StereoFloat_t>* input, int bufferSize) {
_in = input;
_bufferSize = bufferSize;
}
void start() {
if (running) {
return;
}
_workerThread = std::thread(_worker, this);
running = true;
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
output.stopWriter();
_workerThread.join();
_in->clearReadStop();
output.clearWriteStop();
running = false;
}
void setBlockSize(int blockSize) {
if (running) {
return;
}
_bufferSize = blockSize;
output.setMaxLatency(blockSize * 2);
}
stream<float> output;
private:
static void _worker(StereoToMono* _this) {
StereoFloat_t* inBuf = new StereoFloat_t[_this->_bufferSize];
float* outBuf = new float[_this->_bufferSize];
while (true) {
if (_this->_in->read(inBuf, _this->_bufferSize) < 0) { break; };
for (int i = 0; i < _this->_bufferSize; i++) {
outBuf[i] = (inBuf[i].l + inBuf[i].r) / 2.0f;
}
if (_this->output.write(outBuf, _this->_bufferSize) < 0) { break; };
}
delete[] inBuf;
delete[] outBuf;
}
stream<StereoFloat_t>* _in;
int _bufferSize;
std::thread _workerThread;
bool running = false;
};
};

View File

@ -1,140 +1,99 @@
#pragma once #pragma once
#include <thread> #include <dsp/block.h>
#include <dsp/stream.h> #include <dsp/buffer.h>
#include <dsp/types.h>
#include <vector>
#include <spdlog/spdlog.h>
namespace dsp { namespace dsp {
class HandlerSink { template <class T>
class HandlerSink : public generic_block<HandlerSink<T>> {
public: public:
HandlerSink() { HandlerSink() {}
} HandlerSink(stream<T>* in, void (*handler)(T* data, int count, void* ctx), void* ctx) { init(in, handler, ctx); }
HandlerSink(stream<complex_t>* input, complex_t* buffer, int bufferSize, void handler(complex_t*)) { ~HandlerSink() { generic_block<HandlerSink<T>>::stop(); }
_in = input;
_bufferSize = bufferSize; void init(stream<T>* in, void (*handler)(T* data, int count, void* ctx), void* ctx) {
_buffer = buffer; _in = in;
_handler = handler; _handler = handler;
_ctx = ctx;
generic_block<HandlerSink<T>>::registerInput(_in);
} }
void init(stream<complex_t>* input, complex_t* buffer, int bufferSize, void handler(complex_t*)) { void setInput(stream<T>* in) {
_in = input; std::lock_guard<std::mutex> lck(generic_block<HandlerSink<T>>::ctrlMtx);
_bufferSize = bufferSize; generic_block<HandlerSink<T>>::tempStop();
_buffer = buffer; _in = in;
generic_block<HandlerSink<T>>::tempStart();
}
void setHandler(void (*handler)(T* data, int count, void* ctx), void* ctx) {
std::lock_guard<std::mutex> lck(generic_block<HandlerSink<T>>::ctrlMtx);
generic_block<HandlerSink<T>>::tempStop();
_handler = handler; _handler = handler;
_ctx = ctx;
generic_block<HandlerSink<T>>::tempStart();
} }
void start() { int run() {
if (running) { count = _in->read();
return; if (count < 0) { return -1; }
_handler(_in->data, count, _ctx);
_in->flush();
return count;
} }
_workerThread = std::thread(_worker, this);
running = true;
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
_workerThread.join();
_in->clearReadStop();
running = false;
}
bool bypass;
private: private:
static void _worker(HandlerSink* _this) { int count;
while (true) { stream<T>* _in;
if (_this->_in->read(_this->_buffer, _this->_bufferSize) < 0) { break; }; void (*_handler)(T* data, int count, void* ctx);
_this->_handler(_this->_buffer); void* _ctx;
}
}
stream<complex_t>* _in;
int _bufferSize;
complex_t* _buffer;
std::thread _workerThread;
void (*_handler)(complex_t*);
bool running = false;
}; };
template <class T> template <class T>
class NullSink { class RingBufferSink : public generic_block<RingBufferSink<T>> {
public: public:
NullSink() { RingBufferSink() {}
RingBufferSink(stream<T>* in) { init(in); }
~RingBufferSink() { generic_block<RingBufferSink<T>>::stop(); }
void init(stream<T>* in) {
_in = in;
generic_block<RingBufferSink<T>>::registerInput(_in);
} }
NullSink(stream<T>* input, int bufferSize) { void setInput(stream<T>* in) {
_in = input; std::lock_guard<std::mutex> lck(generic_block<RingBufferSink<T>>::ctrlMtx);
_bufferSize = bufferSize; generic_block<RingBufferSink<T>>::tempStop();
_in = in;
generic_block<RingBufferSink<T>>::tempStart();
} }
void init(stream<T>* input, int bufferSize) { int run() {
_in = input; count = _in->read();
_bufferSize = bufferSize; if (count < 0) { return -1; }
if (data.write(_in->data, count) < 0) { return -1; }
_in->flush();
return count;
} }
void start() { RingBuffer<T> data;
_workerThread = std::thread(_worker, this);
}
bool bypass;
private: private:
static void _worker(NullSink* _this) { void doStop() {
T* buf = new T[_this->_bufferSize]; _in->stopReader();
while (true) { data.stopWriter();
//spdlog::info("NS: Reading..."); if (generic_block<RingBufferSink<T>>::workerThread.joinable()) {
_this->_in->read(buf, _this->_bufferSize); generic_block<RingBufferSink<T>>::workerThread.join();
} }
_in->clearReadStop();
data.clearWriteStop();
} }
int count;
stream<T>* _in; stream<T>* _in;
int _bufferSize;
std::thread _workerThread;
}; };
}
class FloatNullSink {
public:
FloatNullSink() {
}
FloatNullSink(stream<float>* input, int bufferSize) {
_in = input;
_bufferSize = bufferSize;
}
void init(stream<float>* input, int bufferSize) {
_in = input;
_bufferSize = bufferSize;
}
void start() {
spdlog::info("NS: Starting...");
_workerThread = std::thread(_worker, this);
}
bool bypass;
private:
static void _worker(FloatNullSink* _this) {
spdlog::info("NS: Started!");
float* buf = new float[_this->_bufferSize];
while (true) {
spdlog::info("NS: Reading...");
_this->_in->read(buf, _this->_bufferSize);
}
}
stream<float>* _in;
int _bufferSize;
std::thread _workerThread;
};
};

View File

@ -1,92 +1,75 @@
#pragma once #pragma once
#include <thread> #include <dsp/block.h>
#include <dsp/stream.h>
#include <dsp/types.h>
#include <spdlog/spdlog.h>
namespace dsp { namespace dsp {
class SineSource { class SineSource : public generic_block<SineSource> {
public: public:
SineSource() { SineSource() {}
} SineSource(int blockSize, float sampleRate, float freq) { init(blockSize, sampleRate, freq); }
SineSource(float frequency, long sampleRate, int blockSize) : output(blockSize * 2) { ~SineSource() { generic_block<SineSource>::stop(); }
void init(int blockSize, float sampleRate, float freq) {
_blockSize = blockSize; _blockSize = blockSize;
_sampleRate = sampleRate; _sampleRate = sampleRate;
_frequency = frequency; _freq = freq;
_phasorSpeed = (2 * 3.1415926535 * frequency) / sampleRate; zeroPhase = (lv_32fc_t*)volk_malloc(STREAM_BUFFER_SIZE * sizeof(lv_32fc_t), volk_get_alignment());
_phase = 0; for (int i = 0; i < STREAM_BUFFER_SIZE; i++) {
zeroPhase[i] = lv_cmake(1.0f, 0.0f);
} }
phase = lv_cmake(1.0f, 0.0f);
void init(float frequency, long sampleRate, int blockSize) { phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI));
output.init(blockSize * 2); generic_block<SineSource>::registerOutput(&out);
_sampleRate = sampleRate;
_blockSize = blockSize;
_frequency = frequency;
_phasorSpeed = (2 * 3.1415926535 * frequency) / sampleRate;
_phase = 0;
}
void start() {
if (running) {
return;
}
_workerThread = std::thread(_worker, this);
running = true;
}
void stop() {
if (!running) {
return;
}
output.stopWriter();
_workerThread.join();
output.clearWriteStop();
running = false;
}
void setFrequency(float frequency) {
_frequency = frequency;
_phasorSpeed = (2 * 3.1415926535 * frequency) / _sampleRate;
} }
void setBlockSize(int blockSize) { void setBlockSize(int blockSize) {
if (running) { std::lock_guard<std::mutex> lck(generic_block<SineSource>::ctrlMtx);
return; generic_block<SineSource>::tempStop();
}
_blockSize = blockSize; _blockSize = blockSize;
output.setMaxLatency(blockSize * 2); generic_block<SineSource>::tempStart();
}
int getBlockSize() {
return _blockSize;
} }
void setSampleRate(float sampleRate) { void setSampleRate(float sampleRate) {
// No need to restart
_sampleRate = sampleRate; _sampleRate = sampleRate;
_phasorSpeed = (2 * 3.1415926535 * _frequency) / sampleRate; phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI));
} }
stream<complex_t> output; float getSampleRate() {
return _sampleRate;
}
void setFrequency(float freq) {
// No need to restart
_freq = freq;
phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI));
}
float getFrequency() {
return _freq;
}
int run() {
if (out.aquire() < 0) { return -1; }
volk_32fc_s32fc_x2_rotator_32fc((lv_32fc_t*)out.data, zeroPhase, phaseDelta, &phase, _blockSize);
out.write(_blockSize);
return _blockSize;
}
stream<complex_t> out;
private: private:
static void _worker(SineSource* _this) {
complex_t* outBuf = new complex_t[_this->_blockSize];
while (true) {
for (int i = 0; i < _this->_blockSize; i++) {
_this->_phase += _this->_phasorSpeed;
outBuf[i].i = sin(_this->_phase);
outBuf[i].q = cos(_this->_phase);
_this->_phase = fmodf(_this->_phase, 2.0f * 3.1415926535); // TODO: Get a more efficient generator
}
if (_this->output.write(outBuf, _this->_blockSize) < 0) { break; };
}
delete[] outBuf;
}
int _blockSize; int _blockSize;
float _phasorSpeed; float _sampleRate;
float _phase; float _freq;
long _sampleRate; lv_32fc_t phaseDelta;
float _frequency; lv_32fc_t phase;
std::thread _workerThread; lv_32fc_t* zeroPhase;
bool running = false;
}; };
}; }

View File

@ -1,228 +1,100 @@
#pragma once #pragma once
#include <mutex>
#include <condition_variable> #include <condition_variable>
#include <algorithm> #include <volk/volk.h>
#include <math.h>
#include <string.h>
#define STREAM_BUF_SZ 1000000 // 1MB buffer
#define STREAM_BUFFER_SIZE 1000000
namespace dsp { namespace dsp {
class untyped_steam {
public:
virtual int aquire() { return -1; }
virtual void write(int size) {}
virtual int read() { return -1; }
virtual void flush() {}
virtual void stopReader() {}
virtual void clearReadStop() {}
virtual void stopWriter() {}
virtual void clearWriteStop() {}
};
template <class T> template <class T>
class stream { class stream : public untyped_steam {
public: public:
stream() { stream() {
data = (T*)volk_malloc(STREAM_BUFFER_SIZE * sizeof(T), volk_get_alignment());
} }
stream(int maxLatency) { int aquire() {
size = STREAM_BUF_SZ; waitReady();
_buffer = new T[size]; if (writerStop) {
_stopReader = false; return -1;
_stopWriter = false; }
this->maxLatency = maxLatency; return 0;
writec = 0;
readc = 0;
readable = 0;
writable = size;
memset(_buffer, 0, size * sizeof(T));
} }
void init(int maxLatency) { void write(int size) {
size = STREAM_BUF_SZ; std::lock_guard<std::mutex> lck(sigMtx);
_buffer = new T[size]; contentSize = size;
_stopReader = false; dataReady = true;
_stopWriter = false; cv.notify_all();
this->maxLatency = maxLatency;
writec = 0;
readc = 0;
readable = 0;
writable = size;
memset(_buffer, 0, size * sizeof(T));
} }
int read(T* data, int len) { int read() {
int dataRead = 0; waitData();
int toRead = 0; if (readerStop) {
while (dataRead < len) { return -1;
toRead = std::min<int>(waitUntilReadable(), len - dataRead);
if (toRead < 0) { return -1; };
if ((toRead + readc) > size) {
memcpy(&data[dataRead], &_buffer[readc], (size - readc) * sizeof(T));
memcpy(&data[dataRead + (size - readc)], &_buffer[0], (toRead - (size - readc)) * sizeof(T));
} }
else { return contentSize;
memcpy(&data[dataRead], &_buffer[readc], toRead * sizeof(T));
} }
dataRead += toRead; void flush() {
std::lock_guard<std::mutex> lck(sigMtx);
_readable_mtx.lock(); dataReady = false;
readable -= toRead; cv.notify_all();
_readable_mtx.unlock();
_writable_mtx.lock();
writable += toRead;
_writable_mtx.unlock();
readc = (readc + toRead) % size;
canWriteVar.notify_one();
}
return len;
}
int readAndSkip(T* data, int len, int skip) {
int dataRead = 0;
int toRead = 0;
while (dataRead < len) {
toRead = std::min<int>(waitUntilReadable(), len - dataRead);
if (toRead < 0) { return -1; };
if ((toRead + readc) > size) {
memcpy(&data[dataRead], &_buffer[readc], (size - readc) * sizeof(T));
memcpy(&data[dataRead + (size - readc)], &_buffer[0], (toRead - (size - readc)) * sizeof(T));
}
else {
memcpy(&data[dataRead], &_buffer[readc], toRead * sizeof(T));
}
dataRead += toRead;
_readable_mtx.lock();
readable -= toRead;
_readable_mtx.unlock();
_writable_mtx.lock();
writable += toRead;
_writable_mtx.unlock();
readc = (readc + toRead) % size;
canWriteVar.notify_one();
}
dataRead = 0;
while (dataRead < skip) {
toRead = std::min<int>(waitUntilReadable(), skip - dataRead);
if (toRead < 0) { return -1; };
dataRead += toRead;
_readable_mtx.lock();
readable -= toRead;
_readable_mtx.unlock();
_writable_mtx.lock();
writable += toRead;
_writable_mtx.unlock();
readc = (readc + toRead) % size;
canWriteVar.notify_one();
}
return len;
}
int waitUntilReadable() {
if (_stopReader) { return -1; }
int _r = getReadable();
if (_r != 0) { return _r; }
std::unique_lock<std::mutex> lck(_readable_mtx);
canReadVar.wait(lck, [=](){ return ((this->getReadable(false) > 0) || this->getReadStop()); });
if (_stopReader) { return -1; }
return getReadable(false);
}
int getReadable(bool lock = true) {
if (lock) { _readable_mtx.lock(); };
int _r = readable;
if (lock) { _readable_mtx.unlock(); };
return _r;
}
int write(T* data, int len) {
int dataWritten = 0;
int toWrite = 0;
while (dataWritten < len) {
toWrite = std::min<int>(waitUntilwritable(), len - dataWritten);
if (toWrite < 0) { return -1; };
if ((toWrite + writec) > size) {
memcpy(&_buffer[writec], &data[dataWritten], (size - writec) * sizeof(T));
memcpy(&_buffer[0], &data[dataWritten + (size - writec)], (toWrite - (size - writec)) * sizeof(T));
}
else {
memcpy(&_buffer[writec], &data[dataWritten], toWrite * sizeof(T));
}
dataWritten += toWrite;
_readable_mtx.lock();
readable += toWrite;
_readable_mtx.unlock();
_writable_mtx.lock();
writable -= toWrite;
_writable_mtx.unlock();
writec = (writec + toWrite) % size;
canReadVar.notify_one();
}
return len;
}
int waitUntilwritable() {
if (_stopWriter) { return -1; }
int _w = getWritable();
if (_w != 0) { return _w; }
std::unique_lock<std::mutex> lck(_writable_mtx);
canWriteVar.wait(lck, [=](){ return ((this->getWritable(false) > 0) || this->getWriteStop()); });
if (_stopWriter) { return -1; }
return getWritable(false);
}
int getWritable(bool lock = true) {
if (lock) { _writable_mtx.lock(); };
int _w = writable;
if (lock) { _writable_mtx.unlock(); _readable_mtx.lock(); };
int _r = readable;
if (lock) { _readable_mtx.unlock(); };
return std::max<int>(std::min<int>(_w, maxLatency - _r), 0);
} }
void stopReader() { void stopReader() {
_stopReader = true; std::lock_guard<std::mutex> lck(sigMtx);
canReadVar.notify_one(); readerStop = true;
} cv.notify_all();
void stopWriter() {
_stopWriter = true;
canWriteVar.notify_one();
}
bool getReadStop() {
return _stopReader;
}
bool getWriteStop() {
return _stopWriter;
} }
void clearReadStop() { void clearReadStop() {
_stopReader = false; readerStop = false;
}
void stopWriter() {
std::lock_guard<std::mutex> lck(sigMtx);
writerStop = true;
cv.notify_all();
} }
void clearWriteStop() { void clearWriteStop() {
_stopWriter = false; writerStop = false;
} }
void setMaxLatency(int maxLatency) { T* data;
this->maxLatency = maxLatency;
}
private: private:
T* _buffer; void waitReady() {
int size; std::unique_lock<std::mutex> lck(sigMtx);
int readc; cv.wait(lck, [this]{ return !dataReady || writerStop; });
int writec; }
int readable;
int writable; void waitData() {
int maxLatency; std::unique_lock<std::mutex> lck(sigMtx);
bool _stopReader; cv.wait(lck, [this]{ return dataReady || readerStop; });
bool _stopWriter; }
std::mutex _readable_mtx;
std::mutex _writable_mtx; std::mutex sigMtx;
std::condition_variable canReadVar; std::condition_variable cv;
std::condition_variable canWriteVar; bool dataReady = false;
bool readerStop = false;
bool writerStop = false;
int contentSize = 0;
}; };
}; }

View File

@ -4,52 +4,10 @@ namespace dsp {
struct complex_t { struct complex_t {
float q; float q;
float i; float i;
complex_t operator+(complex_t& c) {
complex_t res;
res.i = c.i + i;
res.q = c.q + q;
return res;
}
complex_t operator-(complex_t& c) {
complex_t res;
res.i = i - c.i;
res.q = q - c.q;
return res;
}
complex_t operator*(float& f) {
complex_t res;
res.i = i * f;
res.q = q * f;
return res;
}
}; };
struct StereoFloat_t { struct stereo_t {
float l; float l;
float r; float r;
StereoFloat_t operator+(StereoFloat_t& s) {
StereoFloat_t res;
res.l = s.l + l;
res.r = s.r + r;
return res;
}
StereoFloat_t operator-(StereoFloat_t& s) {
StereoFloat_t res;
res.l = l - s.l;
res.r = r - s.r;
return res;
}
StereoFloat_t operator*(float& f) {
StereoFloat_t res;
res.l = l * f;
res.r = r * f;
return res;
}
}; };
}; }

View File

@ -1,103 +1,112 @@
#pragma once #pragma once
#include <dsp/source.h>
#include <dsp/math.h>
#include <dsp/resampling.h>
#include <dsp/filter.h>
#include <spdlog/spdlog.h>
#include <dsp/block.h> #include <dsp/block.h>
#include <dsp/window.h>
#include <dsp/resampling.h>
#include <dsp/processing.h>
#include <algorithm>
namespace dsp { namespace dsp {
class VFO { class VFO {
public: public:
VFO() { VFO() {}
} ~VFO() { stop(); }
void init(stream<complex_t>* in, float inputSampleRate, float outputSampleRate, float bandWidth, float offset, int blockSize) { VFO(stream<complex_t>* in, float offset, float inSampleRate, float outSampleRate, float bandWidth) {
_input = in; init(in, offset, inSampleRate, outSampleRate, bandWidth);
_outputSampleRate = outputSampleRate; };
_inputSampleRate = inputSampleRate;
void init(stream<complex_t>* in, float offset, float inSampleRate, float outSampleRate, float bandWidth) {
_in = in;
_offset = offset;
_inSampleRate = inSampleRate;
_outSampleRate = outSampleRate;
_bandWidth = bandWidth; _bandWidth = bandWidth;
_blockSize = blockSize;
output = &resamp.output;
lo.init(offset, inputSampleRate, blockSize); float realCutoff = std::min<float>(_bandWidth, std::min<float>(_inSampleRate, _outSampleRate)) / 2.0f;
mixer.init(in, &lo.output, blockSize);
//resamp.init(&mixer.output, inputSampleRate, outputSampleRate, blockSize, _bandWidth * 0.8f, _bandWidth); xlator.init(_in, _inSampleRate, -_offset);
resamp.init(mixer.out[0], inputSampleRate, outputSampleRate, blockSize, _bandWidth * 0.8f, _bandWidth); win.init(realCutoff, realCutoff, inSampleRate);
resamp.init(&xlator.out, &win, _inSampleRate, _outSampleRate);
win.setSampleRate(_inSampleRate * resamp.getInterpolation());
resamp.updateWindow(&win);
out = &resamp.out;
} }
void start() { void start() {
lo.start(); if (running) { return; }
mixer.start(); xlator.start();
resamp.start(); resamp.start();
} }
void stop(bool resampler = true) { void stop() {
lo.stop(); if (!running) { return; }
mixer.stop(); xlator.stop();
if (resampler) { resamp.stop(); }; resamp.stop();
} }
void setInputSampleRate(float inputSampleRate, int blockSize = -1) { void setInSampleRate(float inSampleRate) {
lo.stop(); _inSampleRate = inSampleRate;
lo.setSampleRate(inputSampleRate); if (running) { xlator.stop(); resamp.stop(); }
xlator.setSampleRate(_inSampleRate);
_inputSampleRate = inputSampleRate; resamp.setInSampleRate(_inSampleRate);
float realCutoff = std::min<float>(_bandWidth, std::min<float>(_inSampleRate, _outSampleRate)) / 2.0f;
if (blockSize > 0) { win.setSampleRate(_inSampleRate * resamp.getInterpolation());
_blockSize = blockSize; win.setCutoff(realCutoff);
mixer.stop(); win.setTransWidth(realCutoff);
lo.setBlockSize(_blockSize); resamp.updateWindow(&win);
mixer.setBlockSize(_blockSize); if (running) { xlator.start(); resamp.start(); }
mixer.start();
}
resamp.setInputSampleRate(inputSampleRate, _blockSize, _bandWidth * 0.8f, _bandWidth);
lo.start();
} }
void setOutputSampleRate(float outputSampleRate, float bandWidth = -1) { void setOutSampleRate(float outSampleRate) {
if (bandWidth > 0) { _outSampleRate = outSampleRate;
if (running) { resamp.stop(); }
resamp.setOutSampleRate(_outSampleRate);
float realCutoff = std::min<float>(_bandWidth, std::min<float>(_inSampleRate, _outSampleRate)) / 2.0f;
win.setSampleRate(_inSampleRate * resamp.getInterpolation());
win.setCutoff(realCutoff);
win.setTransWidth(realCutoff);
resamp.updateWindow(&win);
if (running) { resamp.start(); }
}
void setOutSampleRate(float outSampleRate, float bandWidth) {
_outSampleRate = outSampleRate;
_bandWidth = bandWidth; _bandWidth = bandWidth;
if (running) { resamp.stop(); }
resamp.setOutSampleRate(_outSampleRate);
float realCutoff = std::min<float>(_bandWidth, std::min<float>(_inSampleRate, _outSampleRate)) / 2.0f;
win.setSampleRate(_inSampleRate * resamp.getInterpolation());
win.setCutoff(realCutoff);
win.setTransWidth(realCutoff);
resamp.updateWindow(&win);
if (running) { resamp.start(); }
} }
resamp.setOutputSampleRate(outputSampleRate, _bandWidth * 0.8f, _bandWidth);
void setOffset(float offset) {
_offset = offset;
xlator.setFrequency(-_offset);
} }
void setBandwidth(float bandWidth) { void setBandwidth(float bandWidth) {
_bandWidth = bandWidth; _bandWidth = bandWidth;
resamp.setFilterParams(_bandWidth * 0.8f, _bandWidth); float realCutoff = std::min<float>(_bandWidth, std::min<float>(_inSampleRate, _outSampleRate)) / 2.0f;
win.setCutoff(realCutoff);
win.setTransWidth(realCutoff);
resamp.updateWindow(&win);
} }
void setOffset(float offset) { stream<complex_t>* out;
lo.setFrequency(-offset);
}
void setBlockSize(int blockSize) {
stop(false);
_blockSize = blockSize;
lo.setBlockSize(_blockSize);
mixer.setBlockSize(_blockSize);
resamp.setBlockSize(_blockSize);
start();
}
int getOutputBlockSize() {
return resamp.getOutputBlockSize();
}
stream<complex_t>* output;
private: private:
SineSource lo; bool running = false;
//Multiplier mixer; float _offset, _inSampleRate, _outSampleRate, _bandWidth;
DemoMultiplier mixer; filter_window::BlackmanWindow win;
FIRResampler<complex_t> resamp; stream<complex_t>* _in;
DecimatingFIRFilter filter; FrequencyXlator xlator;
stream<complex_t>* _input; PolyphaseResampler<complex_t> resamp;
float _outputSampleRate;
float _inputSampleRate;
float _bandWidth;
float _blockSize;
}; };
}; }

75
core/src/dsp/window.h Normal file
View File

@ -0,0 +1,75 @@
#pragma once
#include <dsp/block.h>
namespace dsp {
namespace filter_window {
class generic_window {
public:
virtual int getTapCount() { return -1; }
virtual void createTaps(float* taps, int tapCount) {}
};
class BlackmanWindow : public filter_window::generic_window {
public:
BlackmanWindow() {}
BlackmanWindow(float cutoff, float transWidth, float sampleRate) { init(cutoff, transWidth, sampleRate); }
void init(float cutoff, float transWidth, float sampleRate) {
_cutoff = cutoff;
_transWidth = transWidth;
_sampleRate = sampleRate;
}
void setSampleRate(float sampleRate) {
_sampleRate = sampleRate;
}
void setCutoff(float cutoff) {
_cutoff = cutoff;
}
void setTransWidth(float transWidth) {
_transWidth = transWidth;
}
int getTapCount() {
float fc = _cutoff / _sampleRate;
if (fc > 1.0f) {
fc = 1.0f;
}
int _M = 4.0f / (_transWidth / _sampleRate);
if (_M < 4) {
_M = 4;
}
if (_M % 2 == 0) { _M++; }
return _M;
}
void createTaps(float* taps, int tapCount) {
float fc = _cutoff / _sampleRate;
if (fc > 1.0f) {
fc = 1.0f;
}
float tc = tapCount;
float sum = 0.0f;
float val;
for (int i = 0; i < tapCount; i++) {
val = (sin(2.0f * FL_M_PI * fc * ((float)i - (tc / 2))) / ((float)i - (tc / 2))) *
(0.42f - (0.5f * cos(2.0f * FL_M_PI / tc)) + (0.8f * cos(4.0f * FL_M_PI / tc)));
taps[tapCount - i - 1] = val; // tapCount - i - 1
sum += val;
}
for (int i = 0; i < tapCount; i++) {
taps[i] /= sum;
}
}
private:
float _cutoff, _transWidth, _sampleRate;
};
}
}

View File

@ -47,7 +47,7 @@ int fftSize = 8192 * 8;
std::vector<float> _data; std::vector<float> _data;
std::vector<float> fftTaps; std::vector<float> fftTaps;
void fftHandler(dsp::complex_t* samples) { void fftHandler(dsp::complex_t* samples, int count, void* ctx) {
fftwf_execute(p); fftwf_execute(p);
int half = fftSize / 2; int half = fftSize / 2;
@ -414,7 +414,7 @@ void drawWindow() {
ImGui::Text("Center Frequency: %.0f Hz", gui::waterfall.getCenterFrequency()); ImGui::Text("Center Frequency: %.0f Hz", gui::waterfall.getCenterFrequency());
ImGui::Text("Source name: %s", sourceName.c_str()); ImGui::Text("Source name: %s", sourceName.c_str());
if (ImGui::Checkbox("Test technique", &dcbias.val)) { if (ImGui::Checkbox("Test technique", &dcbias.val)) {
sigpath::signalPath.setDCBiasCorrection(dcbias.val); //sigpath::signalPath.setDCBiasCorrection(dcbias.val);
} }
ImGui::Spacing(); ImGui::Spacing();
} }

View File

@ -5,6 +5,7 @@
#include <fstream> #include <fstream>
#include <portaudio.h> #include <portaudio.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <dsp/sink.h>
namespace io { namespace io {
class AudioSink { class AudioSink {
@ -30,7 +31,7 @@ namespace io {
AudioSink(int bufferSize) { AudioSink(int bufferSize) {
_bufferSize = bufferSize; _bufferSize = bufferSize;
monoBuffer = new float[_bufferSize]; monoBuffer = new float[_bufferSize];
stereoBuffer = new dsp::StereoFloat_t[_bufferSize]; stereoBuffer = new dsp::stereo_t[_bufferSize];
_volume = 1.0f; _volume = 1.0f;
Pa_Initialize(); Pa_Initialize();
@ -81,7 +82,7 @@ namespace io {
void init(int bufferSize) { void init(int bufferSize) {
_bufferSize = bufferSize; _bufferSize = bufferSize;
monoBuffer = new float[_bufferSize]; monoBuffer = new float[_bufferSize];
stereoBuffer = new dsp::StereoFloat_t[_bufferSize]; stereoBuffer = new dsp::stereo_t[_bufferSize];
_volume = 1.0f; _volume = 1.0f;
Pa_Initialize(); Pa_Initialize();
@ -130,11 +131,11 @@ namespace io {
} }
void setMonoInput(dsp::stream<float>* input) { void setMonoInput(dsp::stream<float>* input) {
_monoInput = input; monoSink.setInput(input);
} }
void setStereoInput(dsp::stream<dsp::StereoFloat_t>* input) { void setStereoInput(dsp::stream<dsp::stereo_t>* input) {
_stereoInput = input; stereoSink.setInput(input);
} }
void setVolume(float volume) { void setVolume(float volume) {
@ -158,10 +159,12 @@ namespace io {
if (streamType == MONO) { if (streamType == MONO) {
err = Pa_OpenStream(&stream, NULL, &outputParams, _sampleRate, _bufferSize, 0, err = Pa_OpenStream(&stream, NULL, &outputParams, _sampleRate, _bufferSize, 0,
(dev.channels == 2) ? _mono_to_stereo_callback : _mono_to_mono_callback, this); (dev.channels == 2) ? _mono_to_stereo_callback : _mono_to_mono_callback, this);
monoSink.start();
} }
else { else {
err = Pa_OpenStream(&stream, NULL, &outputParams, _sampleRate, _bufferSize, 0, err = Pa_OpenStream(&stream, NULL, &outputParams, _sampleRate, _bufferSize, 0,
(dev.channels == 2) ? _stereo_to_stereo_callback : _stereo_to_mono_callback, this); (dev.channels == 2) ? _stereo_to_stereo_callback : _stereo_to_mono_callback, this);
stereoSink.start();
} }
if (err != 0) { if (err != 0) {
@ -182,18 +185,20 @@ namespace io {
return; return;
} }
if (streamType == MONO) { if (streamType == MONO) {
_monoInput->stopReader(); monoSink.stop();
monoSink.data.stopReader();
} }
else { else {
_stereoInput->stopReader(); stereoSink.stop();
stereoSink.data.stopReader();
} }
Pa_StopStream(stream); Pa_StopStream(stream);
Pa_CloseStream(stream); Pa_CloseStream(stream);
if (streamType == MONO) { if (streamType == MONO) {
_monoInput->clearReadStop(); monoSink.data.clearReadStop();
} }
else { else {
_stereoInput->clearReadStop(); stereoSink.data.clearReadStop();
} }
running = false; running = false;
} }
@ -206,7 +211,7 @@ namespace io {
delete[] monoBuffer; delete[] monoBuffer;
delete[] stereoBuffer; delete[] stereoBuffer;
monoBuffer = new float[_bufferSize]; monoBuffer = new float[_bufferSize];
stereoBuffer = new dsp::StereoFloat_t[_bufferSize]; stereoBuffer = new dsp::stereo_t[_bufferSize];
} }
void setSampleRate(float sampleRate) { void setSampleRate(float sampleRate) {
@ -248,7 +253,7 @@ namespace io {
PaStreamCallbackFlags statusFlags, void *userData ) { PaStreamCallbackFlags statusFlags, void *userData ) {
AudioSink* _this = (AudioSink*)userData; AudioSink* _this = (AudioSink*)userData;
float* outbuf = (float*)output; float* outbuf = (float*)output;
if (_this->_monoInput->read(_this->monoBuffer, frameCount) < 0) { if (_this->monoSink.data.read(_this->monoBuffer, frameCount) < 0) {
memset(outbuf, 0, sizeof(float) * frameCount); memset(outbuf, 0, sizeof(float) * frameCount);
return 0; return 0;
} }
@ -266,9 +271,9 @@ namespace io {
const PaStreamCallbackTimeInfo* timeInfo, const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData ) { PaStreamCallbackFlags statusFlags, void *userData ) {
AudioSink* _this = (AudioSink*)userData; AudioSink* _this = (AudioSink*)userData;
dsp::StereoFloat_t* outbuf = (dsp::StereoFloat_t*)output; dsp::stereo_t* outbuf = (dsp::stereo_t*)output;
if (_this->_stereoInput->read(_this->stereoBuffer, frameCount) < 0) { if (_this->stereoSink.data.read(_this->stereoBuffer, frameCount) < 0) {
memset(outbuf, 0, sizeof(dsp::StereoFloat_t) * frameCount); memset(outbuf, 0, sizeof(dsp::stereo_t) * frameCount);
return 0; return 0;
} }
@ -288,9 +293,9 @@ namespace io {
const PaStreamCallbackTimeInfo* timeInfo, const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData ) { PaStreamCallbackFlags statusFlags, void *userData ) {
AudioSink* _this = (AudioSink*)userData; AudioSink* _this = (AudioSink*)userData;
dsp::StereoFloat_t* outbuf = (dsp::StereoFloat_t*)output; dsp::stereo_t* outbuf = (dsp::stereo_t*)output;
if (_this->_monoInput->read(_this->monoBuffer, frameCount) < 0) { if (_this->monoSink.data.read(_this->monoBuffer, frameCount) < 0) {
memset(outbuf, 0, sizeof(dsp::StereoFloat_t) * frameCount); memset(outbuf, 0, sizeof(dsp::stereo_t) * frameCount);
return 0; return 0;
} }
@ -309,7 +314,7 @@ namespace io {
PaStreamCallbackFlags statusFlags, void *userData ) { PaStreamCallbackFlags statusFlags, void *userData ) {
AudioSink* _this = (AudioSink*)userData; AudioSink* _this = (AudioSink*)userData;
float* outbuf = (float*)output; float* outbuf = (float*)output;
if (_this->_stereoInput->read(_this->stereoBuffer, frameCount) < 0) { if (_this->stereoSink.data.read(_this->stereoBuffer, frameCount) < 0) {
memset(outbuf, 0, sizeof(float) * frameCount); memset(outbuf, 0, sizeof(float) * frameCount);
return 0; return 0;
} }
@ -338,10 +343,10 @@ namespace io {
int defaultDev; int defaultDev;
double _sampleRate; double _sampleRate;
int _bufferSize; int _bufferSize;
dsp::stream<float>* _monoInput; dsp::RingBufferSink<float> monoSink;
dsp::stream<dsp::StereoFloat_t>* _stereoInput; dsp::RingBufferSink<dsp::stereo_t> stereoSink;
float* monoBuffer; float* monoBuffer;
dsp::StereoFloat_t* stereoBuffer; dsp::stereo_t* stereoBuffer;
float _volume = 1.0f; float _volume = 1.0f;
PaStream *stream; PaStream *stream;
bool running = false; bool running = false;

View File

@ -12,7 +12,7 @@ namespace audio {
astr->deviceId = astr->audio->getDeviceId(); astr->deviceId = astr->audio->getDeviceId();
double sampleRate = astr->audio->devices[astr->deviceId].sampleRates[0]; double sampleRate = astr->audio->devices[astr->deviceId].sampleRates[0];
int blockSize = sampleRate / 200.0; // default block size int blockSize = sampleRate / 200.0; // default block size
astr->monoAudioStream = new dsp::stream<float>(blockSize * 2); astr->monoAudioStream = new dsp::stream<float>;
astr->audio->setBlockSize(blockSize); astr->audio->setBlockSize(blockSize);
astr->audio->setStreamType(io::AudioSink::MONO); astr->audio->setStreamType(io::AudioSink::MONO);
astr->audio->setMonoInput(astr->monoAudioStream); astr->audio->setMonoInput(astr->monoAudioStream);
@ -21,8 +21,8 @@ namespace audio {
astr->sampleRate = sampleRate; astr->sampleRate = sampleRate;
astr->monoStream = stream; astr->monoStream = stream;
astr->sampleRateChangeHandler = sampleRateChangeHandler; astr->sampleRateChangeHandler = sampleRateChangeHandler;
astr->monoDynSplit = new dsp::DynamicSplitter<float>(stream, blockSize); astr->monoDynSplit = new dsp::Splitter<float>(stream);
astr->monoDynSplit->bind(astr->monoAudioStream); astr->monoDynSplit->bindStream(astr->monoAudioStream);
astr->running = false; astr->running = false;
astr->volume = 1.0f; astr->volume = 1.0f;
astr->sampleRateId = 0; astr->sampleRateId = 0;
@ -31,7 +31,7 @@ namespace audio {
return sampleRate; return sampleRate;
} }
double registerStereoStream(dsp::stream<dsp::StereoFloat_t>* stream, std::string name, std::string vfoName, int (*sampleRateChangeHandler)(void* ctx, double sampleRate), void* ctx) { double registerStereoStream(dsp::stream<dsp::stereo_t>* stream, std::string name, std::string vfoName, int (*sampleRateChangeHandler)(void* ctx, double sampleRate), void* ctx) {
AudioStream_t* astr = new AudioStream_t; AudioStream_t* astr = new AudioStream_t;
astr->type = STREAM_TYPE_STEREO; astr->type = STREAM_TYPE_STEREO;
astr->ctx = ctx; astr->ctx = ctx;
@ -39,7 +39,7 @@ namespace audio {
astr->audio->init(1); astr->audio->init(1);
double sampleRate = astr->audio->devices[astr->audio->getDeviceId()].sampleRates[0]; double sampleRate = astr->audio->devices[astr->audio->getDeviceId()].sampleRates[0];
int blockSize = sampleRate / 200.0; // default block size int blockSize = sampleRate / 200.0; // default block size
astr->stereoAudioStream = new dsp::stream<dsp::StereoFloat_t>(blockSize * 2); astr->stereoAudioStream = new dsp::stream<dsp::stereo_t>;
astr->audio->setBlockSize(blockSize); astr->audio->setBlockSize(blockSize);
astr->audio->setStreamType(io::AudioSink::STEREO); astr->audio->setStreamType(io::AudioSink::STEREO);
astr->audio->setStereoInput(astr->stereoAudioStream); astr->audio->setStereoInput(astr->stereoAudioStream);
@ -48,8 +48,8 @@ namespace audio {
astr->sampleRate = sampleRate; astr->sampleRate = sampleRate;
astr->stereoStream = stream; astr->stereoStream = stream;
astr->sampleRateChangeHandler = sampleRateChangeHandler; astr->sampleRateChangeHandler = sampleRateChangeHandler;
astr->stereoDynSplit = new dsp::DynamicSplitter<dsp::StereoFloat_t>(stream, blockSize); astr->stereoDynSplit = new dsp::Splitter<dsp::stereo_t>(stream);
astr->stereoDynSplit->bind(astr->stereoAudioStream); astr->stereoDynSplit->bindStream(astr->stereoAudioStream);
astr->running = false; astr->running = false;
streams[name] = astr; streams[name] = astr;
astr->vfoName = vfoName; astr->vfoName = vfoName;
@ -103,20 +103,20 @@ namespace audio {
bstr.streamRemovedHandler = streamRemovedHandler; bstr.streamRemovedHandler = streamRemovedHandler;
bstr.sampleRateChangeHandler = sampleRateChangeHandler; bstr.sampleRateChangeHandler = sampleRateChangeHandler;
if (astr->type == STREAM_TYPE_MONO) { if (astr->type == STREAM_TYPE_MONO) {
bstr.monoStream = new dsp::stream<float>(astr->blockSize * 2); bstr.monoStream = new dsp::stream<float>;
astr->monoDynSplit->stop(); astr->monoDynSplit->stop();
astr->monoDynSplit->bind(bstr.monoStream); astr->monoDynSplit->bindStream(bstr.monoStream);
if (astr->running) { if (astr->running) {
astr->monoDynSplit->start(); astr->monoDynSplit->start();
} }
astr->boundStreams.push_back(bstr); astr->boundStreams.push_back(bstr);
return bstr.monoStream; return bstr.monoStream;
} }
bstr.stereoStream = new dsp::stream<dsp::StereoFloat_t>(astr->blockSize * 2); bstr.stereoStream = new dsp::stream<dsp::stereo_t>;
bstr.s2m = new dsp::StereoToMono(bstr.stereoStream, astr->blockSize * 2); bstr.s2m = new dsp::StereoToMono(bstr.stereoStream);
bstr.monoStream = &bstr.s2m->output; bstr.monoStream = &bstr.s2m->out;
astr->stereoDynSplit->stop(); astr->stereoDynSplit->stop();
astr->stereoDynSplit->bind(bstr.stereoStream); astr->stereoDynSplit->bindStream(bstr.stereoStream);
if (astr->running) { if (astr->running) {
astr->stereoDynSplit->start(); astr->stereoDynSplit->start();
} }
@ -125,7 +125,7 @@ namespace audio {
return bstr.monoStream; return bstr.monoStream;
} }
dsp::stream<dsp::StereoFloat_t>* bindToStreamStereo(std::string name, void (*streamRemovedHandler)(void* ctx), void (*sampleRateChangeHandler)(void* ctx, double sampleRate, int blockSize), void* ctx) { dsp::stream<dsp::stereo_t>* bindToStreamStereo(std::string name, void (*streamRemovedHandler)(void* ctx), void (*sampleRateChangeHandler)(void* ctx, double sampleRate, int blockSize), void* ctx) {
AudioStream_t* astr = streams[name]; AudioStream_t* astr = streams[name];
BoundStream_t bstr; BoundStream_t bstr;
bstr.type = STREAM_TYPE_STEREO; bstr.type = STREAM_TYPE_STEREO;
@ -133,20 +133,20 @@ namespace audio {
bstr.streamRemovedHandler = streamRemovedHandler; bstr.streamRemovedHandler = streamRemovedHandler;
bstr.sampleRateChangeHandler = sampleRateChangeHandler; bstr.sampleRateChangeHandler = sampleRateChangeHandler;
if (astr->type == STREAM_TYPE_STEREO) { if (astr->type == STREAM_TYPE_STEREO) {
bstr.stereoStream = new dsp::stream<dsp::StereoFloat_t>(astr->blockSize * 2); bstr.stereoStream = new dsp::stream<dsp::stereo_t>;
astr->stereoDynSplit->stop(); astr->stereoDynSplit->stop();
astr->stereoDynSplit->bind(bstr.stereoStream); astr->stereoDynSplit->bindStream(bstr.stereoStream);
if (astr->running) { if (astr->running) {
astr->stereoDynSplit->start(); astr->stereoDynSplit->start();
} }
astr->boundStreams.push_back(bstr); astr->boundStreams.push_back(bstr);
return bstr.stereoStream; return bstr.stereoStream;
} }
bstr.monoStream = new dsp::stream<float>(astr->blockSize * 2); bstr.monoStream = new dsp::stream<float>;
bstr.m2s = new dsp::MonoToStereo(bstr.monoStream, astr->blockSize * 2); bstr.m2s = new dsp::MonoToStereo(bstr.monoStream);
bstr.stereoStream = &bstr.m2s->output; bstr.stereoStream = &bstr.m2s->out;
astr->monoDynSplit->stop(); astr->monoDynSplit->stop();
astr->monoDynSplit->bind(bstr.monoStream); astr->monoDynSplit->bindStream(bstr.monoStream);
if (astr->running) { if (astr->running) {
astr->monoDynSplit->start(); astr->monoDynSplit->start();
} }
@ -156,35 +156,37 @@ namespace audio {
} }
void setBlockSize(std::string name, int blockSize) { void setBlockSize(std::string name, int blockSize) {
AudioStream_t* astr = streams[name]; // NOTE: THIS SHOULD NOT BE NEEDED ANYMORE
if (astr->running) {
return; // AudioStream_t* astr = streams[name];
} // if (astr->running) {
if (astr->type == STREAM_TYPE_MONO) { // return;
astr->monoDynSplit->setBlockSize(blockSize); // }
for (int i = 0; i < astr->boundStreams.size(); i++) { // if (astr->type == STREAM_TYPE_MONO) {
BoundStream_t bstr = astr->boundStreams[i]; // astr->monoDynSplit->setBlockSize(blockSize);
bstr.monoStream->setMaxLatency(blockSize * 2); // for (int i = 0; i < astr->boundStreams.size(); i++) {
if (bstr.type == STREAM_TYPE_STEREO) { // BoundStream_t bstr = astr->boundStreams[i];
bstr.m2s->stop(); // bstr.monoStream->setMaxLatency(blockSize * 2);
bstr.m2s->setBlockSize(blockSize); // if (bstr.type == STREAM_TYPE_STEREO) {
bstr.m2s->start(); // bstr.m2s->stop();
} // bstr.m2s->setBlockSize(blockSize);
} // bstr.m2s->start();
astr->blockSize = blockSize; // }
return; // }
} // astr->blockSize = blockSize;
astr->monoDynSplit->setBlockSize(blockSize); // return;
for (int i = 0; i < astr->boundStreams.size(); i++) { // }
BoundStream_t bstr = astr->boundStreams[i]; // astr->monoDynSplit->setBlockSize(blockSize);
bstr.stereoStream->setMaxLatency(blockSize * 2); // for (int i = 0; i < astr->boundStreams.size(); i++) {
if (bstr.type == STREAM_TYPE_MONO) { // BoundStream_t bstr = astr->boundStreams[i];
bstr.s2m->stop(); // bstr.stereoStream->setMaxLatency(blockSize * 2);
bstr.s2m->setBlockSize(blockSize); // if (bstr.type == STREAM_TYPE_MONO) {
bstr.s2m->start(); // bstr.s2m->stop();
} // bstr.s2m->setBlockSize(blockSize);
} // bstr.s2m->start();
astr->blockSize = blockSize; // }
// }
// astr->blockSize = blockSize;
} }
void unbindFromStreamMono(std::string name, dsp::stream<float>* stream) { void unbindFromStreamMono(std::string name, dsp::stream<float>* stream) {
@ -196,7 +198,7 @@ namespace audio {
} }
if (astr->type == STREAM_TYPE_STEREO) { if (astr->type == STREAM_TYPE_STEREO) {
astr->stereoDynSplit->stop(); astr->stereoDynSplit->stop();
astr->stereoDynSplit->unbind(bstr.stereoStream); astr->stereoDynSplit->unbindStream(bstr.stereoStream);
if (astr->running) { if (astr->running) {
astr->stereoDynSplit->start(); astr->stereoDynSplit->start();
} }
@ -205,7 +207,7 @@ namespace audio {
return; return;
} }
astr->monoDynSplit->stop(); astr->monoDynSplit->stop();
astr->monoDynSplit->unbind(bstr.monoStream); astr->monoDynSplit->unbindStream(bstr.monoStream);
if (astr->running) { if (astr->running) {
astr->monoDynSplit->start(); astr->monoDynSplit->start();
} }
@ -214,7 +216,7 @@ namespace audio {
} }
} }
void unbindFromStreamStereo(std::string name, dsp::stream<dsp::StereoFloat_t>* stream) { void unbindFromStreamStereo(std::string name, dsp::stream<dsp::stereo_t>* stream) {
AudioStream_t* astr = streams[name]; AudioStream_t* astr = streams[name];
for (int i = 0; i < astr->boundStreams.size(); i++) { for (int i = 0; i < astr->boundStreams.size(); i++) {
BoundStream_t bstr = astr->boundStreams[i]; BoundStream_t bstr = astr->boundStreams[i];
@ -223,7 +225,7 @@ namespace audio {
} }
if (astr->type == STREAM_TYPE_MONO) { if (astr->type == STREAM_TYPE_MONO) {
astr->monoDynSplit->stop(); astr->monoDynSplit->stop();
astr->monoDynSplit->unbind(bstr.monoStream); astr->monoDynSplit->unbindStream(bstr.monoStream);
if (astr->running) { if (astr->running) {
astr->monoDynSplit->start(); astr->monoDynSplit->start();
} }
@ -232,7 +234,7 @@ namespace audio {
return; return;
} }
astr->stereoDynSplit->stop(); astr->stereoDynSplit->stop();
astr->stereoDynSplit->unbind(bstr.stereoStream); astr->stereoDynSplit->unbindStream(bstr.stereoStream);
if (astr->running) { if (astr->running) {
astr->stereoDynSplit->start(); astr->stereoDynSplit->start();
} }
@ -255,16 +257,19 @@ namespace audio {
if (astr->running) { if (astr->running) {
return; return;
} }
// NOTE: All the blocksize stuff needs removal
int blockSize = astr->sampleRateChangeHandler(astr->ctx, sampleRate); int blockSize = astr->sampleRateChangeHandler(astr->ctx, sampleRate);
astr->audio->setSampleRate(sampleRate); astr->audio->setSampleRate(sampleRate);
astr->audio->setBlockSize(blockSize); //astr->audio->setBlockSize(blockSize);
if (astr->type == STREAM_TYPE_MONO) { if (astr->type == STREAM_TYPE_MONO) {
astr->monoDynSplit->setBlockSize(blockSize); //astr->monoDynSplit->setBlockSize(blockSize);
for (int i = 0; i < astr->boundStreams.size(); i++) { for (int i = 0; i < astr->boundStreams.size(); i++) {
BoundStream_t bstr = astr->boundStreams[i]; BoundStream_t bstr = astr->boundStreams[i];
if (bstr.type == STREAM_TYPE_STEREO) { if (bstr.type == STREAM_TYPE_STEREO) {
bstr.m2s->stop(); bstr.m2s->stop();
bstr.m2s->setBlockSize(blockSize); //bstr.m2s->setBlockSize(blockSize);
bstr.sampleRateChangeHandler(bstr.ctx, sampleRate, blockSize); bstr.sampleRateChangeHandler(bstr.ctx, sampleRate, blockSize);
bstr.m2s->start(); bstr.m2s->start();
continue; continue;
@ -273,12 +278,12 @@ namespace audio {
} }
} }
else { else {
astr->stereoDynSplit->setBlockSize(blockSize); //astr->stereoDynSplit->setBlockSize(blockSize);
for (int i = 0; i < astr->boundStreams.size(); i++) { for (int i = 0; i < astr->boundStreams.size(); i++) {
BoundStream_t bstr = astr->boundStreams[i]; BoundStream_t bstr = astr->boundStreams[i];
if (bstr.type == STREAM_TYPE_MONO) { if (bstr.type == STREAM_TYPE_MONO) {
bstr.s2m->stop(); bstr.s2m->stop();
bstr.s2m->setBlockSize(blockSize); //bstr.s2m->setBlockSize(blockSize);
bstr.sampleRateChangeHandler(bstr.ctx, sampleRate, blockSize); bstr.sampleRateChangeHandler(bstr.ctx, sampleRate, blockSize);
bstr.s2m->start(); bstr.s2m->start();
continue; continue;

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <dsp/stream.h> #include <dsp/stream.h>
#include <dsp/routing.h> #include <dsp/routing.h>
#include <dsp/audio.h>
#include <io/audio.h> #include <io/audio.h>
#include <map> #include <map>
@ -15,7 +16,7 @@ namespace audio {
struct BoundStream_t { struct BoundStream_t {
dsp::stream<float>* monoStream; dsp::stream<float>* monoStream;
dsp::stream<dsp::StereoFloat_t>* stereoStream; dsp::stream<dsp::stereo_t>* stereoStream;
dsp::StereoToMono* s2m; dsp::StereoToMono* s2m;
dsp::MonoToStereo* m2s; dsp::MonoToStereo* m2s;
void (*streamRemovedHandler)(void* ctx); void (*streamRemovedHandler)(void* ctx);
@ -27,12 +28,12 @@ namespace audio {
struct AudioStream_t { struct AudioStream_t {
io::AudioSink* audio; io::AudioSink* audio;
dsp::stream<float>* monoAudioStream; dsp::stream<float>* monoAudioStream;
dsp::stream<dsp::StereoFloat_t>* stereoAudioStream; dsp::stream<dsp::stereo_t>* stereoAudioStream;
std::vector<BoundStream_t> boundStreams; std::vector<BoundStream_t> boundStreams;
dsp::stream<float>* monoStream; dsp::stream<float>* monoStream;
dsp::DynamicSplitter<float>* monoDynSplit; dsp::Splitter<float>* monoDynSplit;
dsp::stream<dsp::StereoFloat_t>* stereoStream; dsp::stream<dsp::stereo_t>* stereoStream;
dsp::DynamicSplitter<dsp::StereoFloat_t>* stereoDynSplit; dsp::Splitter<dsp::stereo_t>* stereoDynSplit;
int (*sampleRateChangeHandler)(void* ctx, double sampleRate); int (*sampleRateChangeHandler)(void* ctx, double sampleRate);
double sampleRate; double sampleRate;
int blockSize; int blockSize;
@ -48,15 +49,15 @@ namespace audio {
extern std::map<std::string, AudioStream_t*> streams; extern std::map<std::string, AudioStream_t*> streams;
double registerMonoStream(dsp::stream<float>* stream, std::string name, std::string vfoName, int (*sampleRateChangeHandler)(void* ctx, double sampleRate), void* ctx); double registerMonoStream(dsp::stream<float>* stream, std::string name, std::string vfoName, int (*sampleRateChangeHandler)(void* ctx, double sampleRate), void* ctx);
double registerStereoStream(dsp::stream<dsp::StereoFloat_t>* stream, std::string name, std::string vfoName, int (*sampleRateChangeHandler)(void* ctx, double sampleRate), void* ctx); double registerStereoStream(dsp::stream<dsp::stereo_t>* stream, std::string name, std::string vfoName, int (*sampleRateChangeHandler)(void* ctx, double sampleRate), void* ctx);
void startStream(std::string name); void startStream(std::string name);
void stopStream(std::string name); void stopStream(std::string name);
void removeStream(std::string name); void removeStream(std::string name);
dsp::stream<float>* bindToStreamMono(std::string name, void (*streamRemovedHandler)(void* ctx), void (*sampleRateChangeHandler)(void* ctx, double sampleRate, int blockSize), void* ctx); dsp::stream<float>* bindToStreamMono(std::string name, void (*streamRemovedHandler)(void* ctx), void (*sampleRateChangeHandler)(void* ctx, double sampleRate, int blockSize), void* ctx);
dsp::stream<dsp::StereoFloat_t>* bindToStreamStereo(std::string name, void (*streamRemovedHandler)(void* ctx), void (*sampleRateChangeHandler)(void* ctx, double sampleRate, int blockSize), void* ctx); dsp::stream<dsp::stereo_t>* bindToStreamStereo(std::string name, void (*streamRemovedHandler)(void* ctx), void (*sampleRateChangeHandler)(void* ctx, double sampleRate, int blockSize), void* ctx);
void setBlockSize(std::string name, int blockSize); void setBlockSize(std::string name, int blockSize);
void unbindFromStreamMono(std::string name, dsp::stream<float>* stream); void unbindFromStreamMono(std::string name, dsp::stream<float>* stream);
void unbindFromStreamStereo(std::string name, dsp::stream<dsp::StereoFloat_t>* stream); void unbindFromStreamStereo(std::string name, dsp::stream<dsp::stereo_t>* stream);
std::string getNameFromVFO(std::string vfoName); std::string getNameFromVFO(std::string vfoName);
void setSampleRate(std::string name, double sampleRate); void setSampleRate(std::string name, double sampleRate);
void setAudioDevice(std::string name, int deviceId, double sampleRate); void setAudioDevice(std::string name, int deviceId, double sampleRate);

View File

@ -4,54 +4,43 @@ SignalPath::SignalPath() {
} }
void SignalPath::init(uint64_t sampleRate, int fftRate, int fftSize, dsp::stream<dsp::complex_t>* input, dsp::complex_t* fftBuffer, void fftHandler(dsp::complex_t*)) { void SignalPath::init(uint64_t sampleRate, int fftRate, int fftSize, dsp::stream<dsp::complex_t>* input, dsp::complex_t* fftBuffer, void fftHandler(dsp::complex_t*,int,void*)) {
this->sampleRate = sampleRate; this->sampleRate = sampleRate;
this->fftRate = fftRate; this->fftRate = fftRate;
this->fftSize = fftSize; this->fftSize = fftSize;
inputBlockSize = sampleRate / 200.0f; inputBlockSize = sampleRate / 200.0f;
dcBiasRemover.init(input, 32000); split.init(input);
dcBiasRemover.bypass = true;
split.init(&dcBiasRemover.output, 32000);
fftBlockDec.init(&split.output_a, (sampleRate / fftRate) - fftSize, fftSize); reshape.init(&fftStream);
fftHandlerSink.init(&fftBlockDec.output, fftBuffer, fftSize, fftHandler); fftHandlerSink.init(&reshape.out, fftHandler, NULL);
dynSplit.init(&split.output_b, 32000);
} }
void SignalPath::setSampleRate(double sampleRate) { void SignalPath::setSampleRate(double sampleRate) {
this->sampleRate = sampleRate; this->sampleRate = sampleRate;
inputBlockSize = sampleRate / 200.0f;
dcBiasRemover.stop();
split.stop(); split.stop();
fftBlockDec.stop(); //fftBlockDec.stop();
fftHandlerSink.stop(); //fftHandlerSink.stop();
dynSplit.stop();
for (auto const& [name, vfo] : vfos) { for (auto const& [name, vfo] : vfos) {
vfo.vfo->stop(); vfo.vfo->stop();
} }
dcBiasRemover.setBlockSize(inputBlockSize); // Claculate skip to maintain a constant fft rate
split.setBlockSize(inputBlockSize); //int skip = (sampleRate / fftRate) - fftSize;
int skip = (sampleRate / fftRate) - fftSize; //fftBlockDec.setSkip(skip);
fftBlockDec.setSkip(skip);
dynSplit.setBlockSize(inputBlockSize);
// TODO: Tell modules that the block size has changed // TODO: Tell modules that the block size has changed (maybe?)
for (auto const& [name, vfo] : vfos) { for (auto const& [name, vfo] : vfos) {
vfo.vfo->setInputSampleRate(sampleRate, inputBlockSize); vfo.vfo->setInSampleRate(sampleRate);
vfo.vfo->start(); vfo.vfo->start();
} }
fftHandlerSink.start(); //fftHandlerSink.start();
fftBlockDec.start(); //fftBlockDec.start();
split.start(); split.start();
dcBiasRemover.start();
dynSplit.start();
} }
double SignalPath::getSampleRate() { double SignalPath::getSampleRate() {
@ -59,32 +48,23 @@ double SignalPath::getSampleRate() {
} }
void SignalPath::start() { void SignalPath::start() {
dcBiasRemover.start();
split.start(); split.start();
reshape.start();
fftBlockDec.start();
fftHandlerSink.start(); fftHandlerSink.start();
dynSplit.start();
}
void SignalPath::setDCBiasCorrection(bool enabled) {
dcBiasRemover.bypass = !enabled;
} }
dsp::VFO* SignalPath::addVFO(std::string name, double outSampleRate, double bandwidth, double offset) { dsp::VFO* SignalPath::addVFO(std::string name, double outSampleRate, double bandwidth, double offset) {
if (vfos.find(name) != vfos.end()) { if (vfos.find(name) != vfos.end()) {
return NULL; return NULL;
} }
dynSplit.stop();
VFO_t vfo; VFO_t vfo;
vfo.inputStream = new dsp::stream<dsp::complex_t>(inputBlockSize * 2); vfo.inputStream = new dsp::stream<dsp::complex_t>;
dynSplit.bind(vfo.inputStream); split.bindStream(vfo.inputStream);
vfo.vfo = new dsp::VFO(); vfo.vfo = new dsp::VFO();
vfo.vfo->init(vfo.inputStream, sampleRate, outSampleRate, bandwidth, offset, inputBlockSize); vfo.vfo->init(vfo.inputStream, offset, sampleRate, outSampleRate, bandwidth);
vfo.vfo->start(); vfo.vfo->start();
vfos[name] = vfo; vfos[name] = vfo;
dynSplit.start();
return vfo.vfo; return vfo.vfo;
} }
@ -93,30 +73,22 @@ void SignalPath::removeVFO(std::string name) {
return; return;
} }
dynSplit.stop();
VFO_t vfo = vfos[name]; VFO_t vfo = vfos[name];
vfo.vfo->stop(); vfo.vfo->stop();
dynSplit.unbind(vfo.inputStream); split.unbindStream(vfo.inputStream);
delete vfo.vfo; delete vfo.vfo;
delete vfo.inputStream; delete vfo.inputStream;
dynSplit.start();
vfos.erase(name); vfos.erase(name);
} }
void SignalPath::setInput(dsp::stream<dsp::complex_t>* input) { void SignalPath::setInput(dsp::stream<dsp::complex_t>* input) {
dcBiasRemover.stop(); split.setInput(input);
dcBiasRemover.setInput(input);
dcBiasRemover.start();
} }
void SignalPath::bindIQStream(dsp::stream<dsp::complex_t>* stream) { void SignalPath::bindIQStream(dsp::stream<dsp::complex_t>* stream) {
dynSplit.stop(); split.bindStream(stream);
dynSplit.bind(stream);
dynSplit.start();
} }
void SignalPath::unbindIQStream(dsp::stream<dsp::complex_t>* stream) { void SignalPath::unbindIQStream(dsp::stream<dsp::complex_t>* stream) {
dynSplit.stop(); split.unbindStream(stream);
dynSplit.unbind(stream);
dynSplit.start();
} }

View File

@ -6,7 +6,6 @@
#include <dsp/demodulator.h> #include <dsp/demodulator.h>
#include <dsp/routing.h> #include <dsp/routing.h>
#include <dsp/sink.h> #include <dsp/sink.h>
#include <dsp/correction.h>
#include <dsp/vfo.h> #include <dsp/vfo.h>
#include <map> #include <map>
#include <module.h> #include <module.h>
@ -14,10 +13,9 @@
class SignalPath { class SignalPath {
public: public:
SignalPath(); SignalPath();
void init(uint64_t sampleRate, int fftRate, int fftSize, dsp::stream<dsp::complex_t>* input, dsp::complex_t* fftBuffer, void fftHandler(dsp::complex_t*)); void init(uint64_t sampleRate, int fftRate, int fftSize, dsp::stream<dsp::complex_t>* input, dsp::complex_t* fftBuffer, void fftHandler(dsp::complex_t*,int,void*));
void start(); void start();
void setSampleRate(double sampleRate); void setSampleRate(double sampleRate);
void setDCBiasCorrection(bool enabled);
void setFFTRate(double rate); void setFFTRate(double rate);
double getSampleRate(); double getSampleRate();
dsp::VFO* addVFO(std::string name, double outSampleRate, double bandwidth, double offset); dsp::VFO* addVFO(std::string name, double outSampleRate, double bandwidth, double offset);
@ -32,15 +30,14 @@ private:
dsp::VFO* vfo; dsp::VFO* vfo;
}; };
dsp::DCBiasRemover dcBiasRemover; dsp::Splitter<dsp::complex_t> split;
dsp::Splitter split;
// FFT // FFT
dsp::BlockDecimator fftBlockDec; dsp::stream<dsp::complex_t> fftStream;
dsp::HandlerSink fftHandlerSink; dsp::Reshaper<dsp::complex_t> reshape;
dsp::HandlerSink<dsp::complex_t> fftHandlerSink;
// VFO // VFO
dsp::DynamicSplitter<dsp::complex_t> dynSplit;
std::map<std::string, VFO_t> vfos; std::map<std::string, VFO_t> vfos;
double sampleRate; double sampleRate;

View File

@ -8,7 +8,7 @@ VFOManager::VFO::VFO(std::string name, int reference, double offset, double band
wtfVFO->setReference(reference); wtfVFO->setReference(reference);
wtfVFO->setBandwidth(bandwidth); wtfVFO->setBandwidth(bandwidth);
wtfVFO->setOffset(offset); wtfVFO->setOffset(offset);
output = dspVFO->output; output = dspVFO->out;
gui::waterfall.vfos[name] = wtfVFO; gui::waterfall.vfos[name] = wtfVFO;
} }
@ -34,7 +34,7 @@ void VFOManager::VFO::setBandwidth(double bandwidth) {
} }
void VFOManager::VFO::setSampleRate(double sampleRate, double bandwidth) { void VFOManager::VFO::setSampleRate(double sampleRate, double bandwidth) {
dspVFO->setOutputSampleRate(sampleRate, bandwidth); dspVFO->setOutSampleRate(sampleRate, bandwidth);
wtfVFO->setBandwidth(bandwidth); wtfVFO->setBandwidth(bandwidth);
} }
@ -43,7 +43,8 @@ void VFOManager::VFO::setReference(int ref) {
} }
int VFOManager::VFO::getOutputBlockSize() { int VFOManager::VFO::getOutputBlockSize() {
return dspVFO->getOutputBlockSize(); // NOTE: This shouldn't be needed anymore
return 1; //dspVFO->getOutputBlockSize();
} }
void VFOManager::VFO::setSnapInterval(double interval) { void VFOManager::VFO::setSnapInterval(double interval) {

View File

@ -20,8 +20,6 @@ public:
FileSourceModule(std::string name) { FileSourceModule(std::string name) {
this->name = name; this->name = name;
stream.init(100);
handler.ctx = this; handler.ctx = this;
handler.selectHandler = menuSelected; handler.selectHandler = menuSelected;
handler.deselectHandler = menuDeselected; handler.deselectHandler = menuDeselected;
@ -84,22 +82,19 @@ private:
double sampleRate = _this->reader->getSampleRate(); double sampleRate = _this->reader->getSampleRate();
int blockSize = sampleRate / 200.0; int blockSize = sampleRate / 200.0;
int16_t* inBuf = new int16_t[blockSize * 2]; int16_t* inBuf = new int16_t[blockSize * 2];
dsp::complex_t* outBuf = new dsp::complex_t[blockSize];
_this->stream.setMaxLatency(blockSize * 2);
while (true) { while (true) {
_this->reader->readSamples(inBuf, blockSize * 2 * sizeof(int16_t)); _this->reader->readSamples(inBuf, blockSize * 2 * sizeof(int16_t));
if (_this->stream.aquire() < 0) { break; };
for (int i = 0; i < blockSize; i++) { for (int i = 0; i < blockSize; i++) {
outBuf[i].q = (float)inBuf[i * 2] / (float)0x7FFF; _this->stream.data[i].q = (float)inBuf[i * 2] / (float)0x7FFF;
outBuf[i].i = (float)inBuf[(i * 2) + 1] / (float)0x7FFF; _this->stream.data[i].i = (float)inBuf[(i * 2) + 1] / (float)0x7FFF;
} }
if (_this->stream.write(outBuf, blockSize) < 0) { break; }; _this->stream.write(blockSize);
//std::this_thread::sleep_for(std::chrono::milliseconds(5)); //std::this_thread::sleep_for(std::chrono::milliseconds(5));
} }
delete[] inBuf; delete[] inBuf;
delete[] outBuf;
} }
std::string name; std::string name;

View File

@ -122,11 +122,6 @@ private:
_this->sigPath.setBandwidth(_this->bandWidth); _this->sigPath.setBandwidth(_this->bandWidth);
} }
ImGui::Text("Squelch");
ImGui::SameLine();
ImGui::SetNextItemWidth(menuColumnWidth - ImGui::GetCursorPosX());
ImGui::SliderFloat(CONCAT("##_squelch_select_", _this->name), &_this->sigPath.squelch.level, -100, 0);
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::Text("Snap Interval"); ImGui::Text("Snap Interval");

View File

@ -11,12 +11,17 @@ int SigPath::sampleRateChangeHandler(void* ctx, double sampleRate) {
_this->audioResamp.stop(); _this->audioResamp.stop();
_this->deemp.stop(); _this->deemp.stop();
float bw = std::min<float>(_this->bandwidth, sampleRate / 2.0f); float bw = std::min<float>(_this->bandwidth, sampleRate / 2.0f);
_this->audioResamp.setOutputSampleRate(sampleRate, bw, bw);
_this->deemp.setBlockSize(_this->audioResamp.getOutputBlockSize());
_this->deemp.setSamplerate(sampleRate); _this->audioResamp.setOutSampleRate(sampleRate);
_this->audioWin.setSampleRate(_this->sampleRate * _this->audioResamp.getInterpolation());
_this->audioResamp.updateWindow(&_this->audioWin);
_this->deemp.setSampleRate(sampleRate);
_this->audioResamp.start(); _this->audioResamp.start();
_this->deemp.start(); _this->deemp.start();
return _this->audioResamp.getOutputBlockSize(); // Note: returning a block size should not be needed anymore
return 1;
} }
void SigPath::init(std::string vfoName, uint64_t sampleRate, int blockSize) { void SigPath::init(std::string vfoName, uint64_t sampleRate, int blockSize) {
@ -35,21 +40,18 @@ void SigPath::init(std::string vfoName, uint64_t sampleRate, int blockSize) {
// TODO: ajust deemphasis for different output sample rates // TODO: ajust deemphasis for different output sample rates
// TODO: Add a mono to stereo for different modes // TODO: Add a mono to stereo for different modes
squelch.init(vfo->output, 800); demod.init(vfo->output, 200000, 100000);
squelch.level = -100.0; amDemod.init(vfo->output);
squelch.onCount = 1; ssbDemod.init(vfo->output, 6000, 3000, dsp::SSBDemod::MODE_USB);
squelch.offCount = 2560;
demod.init(squelch.out[0], 100000, 200000, 800); audioWin.init(24000, 24000, 200000);
amDemod.init(squelch.out[0], 50); audioResamp.init(&demod.out, &audioWin, 200000, 48000);
ssbDemod.init(squelch.out[0], 6000, 3000, 22); audioWin.setSampleRate(audioResamp.getInterpolation() * 200000);
cpx2stereo.init(squelch.out[0], 22); audioResamp.updateWindow(&audioWin);
audioResamp.init(&demod.output, 200000, 48000, 800); deemp.init(&audioResamp.out, 48000, 50e-6);
deemp.init(&audioResamp.output, 800, 50e-6, 48000);
outputSampleRate = audio::registerMonoStream(&deemp.output, vfoName, vfoName, sampleRateChangeHandler, this); outputSampleRate = audio::registerMonoStream(&deemp.out, vfoName, vfoName, sampleRateChangeHandler, this);
audio::setBlockSize(vfoName, audioResamp.getOutputBlockSize());
setDemodulator(_demod, bandwidth); setDemodulator(_demod, bandwidth);
} }
@ -90,27 +92,27 @@ void SigPath::setDemodulator(int demId, float bandWidth) {
else if (_demod == DEMOD_DSB) { else if (_demod == DEMOD_DSB) {
ssbDemod.stop(); ssbDemod.stop();
} }
else if (_demod == DEMOD_RAW) {
cpx2stereo.stop();
}
else { else {
spdlog::error("UNIMPLEMENTED DEMODULATOR IN SigPath::setDemodulator (stop)"); spdlog::error("UNIMPLEMENTED DEMODULATOR IN SigPath::setDemodulator (stop)");
} }
_demod = demId; _demod = demId;
squelch.stop();
// Set input of the audio resampler // Set input of the audio resampler
// TODO: Set bandwidth from argument // TODO: Set bandwidth from argument
if (demId == DEMOD_FM) { if (demId == DEMOD_FM) {
demodOutputSamplerate = 200000; demodOutputSamplerate = 200000;
vfo->setSampleRate(200000, bandwidth); vfo->setSampleRate(200000, bandwidth);
demod.setBlockSize(vfo->getOutputBlockSize());
demod.setSampleRate(200000); demod.setSampleRate(200000);
demod.setDeviation(bandwidth / 2.0f); demod.setDeviation(bandwidth / 2.0f);
audioResamp.setInput(&demod.output); audioResamp.setInput(&demod.out);
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f); audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(200000, vfo->getOutputBlockSize(), audioBw, audioBw);
audioResamp.setInSampleRate(200000);
audioWin.setSampleRate(200000 * audioResamp.getInterpolation());
audioWin.setCutoff(audioBw);
audioWin.setTransWidth(audioBw);
audioResamp.updateWindow(&audioWin);
deemp.bypass = (_deemp == DEEMP_NONE); deemp.bypass = (_deemp == DEEMP_NONE);
vfo->setReference(ImGui::WaterfallVFO::REF_CENTER); vfo->setReference(ImGui::WaterfallVFO::REF_CENTER);
demod.start(); demod.start();
@ -118,12 +120,17 @@ void SigPath::setDemodulator(int demId, float bandWidth) {
else if (demId == DEMOD_NFM) { else if (demId == DEMOD_NFM) {
demodOutputSamplerate = 16000; demodOutputSamplerate = 16000;
vfo->setSampleRate(16000, bandwidth); vfo->setSampleRate(16000, bandwidth);
demod.setBlockSize(vfo->getOutputBlockSize());
demod.setSampleRate(16000); demod.setSampleRate(16000);
demod.setDeviation(bandwidth / 2.0f); demod.setDeviation(bandwidth / 2.0f);
audioResamp.setInput(&demod.output); audioResamp.setInput(&demod.out);
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f); audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(16000, vfo->getOutputBlockSize(), audioBw, audioBw);
audioResamp.setInSampleRate(16000);
audioWin.setSampleRate(16000 * audioResamp.getInterpolation());
audioWin.setCutoff(audioBw);
audioWin.setTransWidth(audioBw);
audioResamp.updateWindow(&audioWin);
deemp.bypass = true; deemp.bypass = true;
vfo->setReference(ImGui::WaterfallVFO::REF_CENTER); vfo->setReference(ImGui::WaterfallVFO::REF_CENTER);
demod.start(); demod.start();
@ -131,10 +138,15 @@ void SigPath::setDemodulator(int demId, float bandWidth) {
else if (demId == DEMOD_AM) { else if (demId == DEMOD_AM) {
demodOutputSamplerate = 125000; demodOutputSamplerate = 125000;
vfo->setSampleRate(12500, bandwidth); vfo->setSampleRate(12500, bandwidth);
amDemod.setBlockSize(vfo->getOutputBlockSize()); audioResamp.setInput(&amDemod.out);
audioResamp.setInput(&amDemod.output);
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f); audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(12500, vfo->getOutputBlockSize(), audioBw, audioBw);
audioResamp.setInSampleRate(12500);
audioWin.setSampleRate(12500 * audioResamp.getInterpolation());
audioWin.setCutoff(audioBw);
audioWin.setTransWidth(audioBw);
audioResamp.updateWindow(&audioWin);
deemp.bypass = true; deemp.bypass = true;
vfo->setReference(ImGui::WaterfallVFO::REF_CENTER); vfo->setReference(ImGui::WaterfallVFO::REF_CENTER);
amDemod.start(); amDemod.start();
@ -142,11 +154,16 @@ void SigPath::setDemodulator(int demId, float bandWidth) {
else if (demId == DEMOD_USB) { else if (demId == DEMOD_USB) {
demodOutputSamplerate = 6000; demodOutputSamplerate = 6000;
vfo->setSampleRate(6000, bandwidth); vfo->setSampleRate(6000, bandwidth);
ssbDemod.setBlockSize(vfo->getOutputBlockSize());
ssbDemod.setMode(dsp::SSBDemod::MODE_USB); ssbDemod.setMode(dsp::SSBDemod::MODE_USB);
audioResamp.setInput(&ssbDemod.output); audioResamp.setInput(&ssbDemod.out);
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f); audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(6000, vfo->getOutputBlockSize(), audioBw, audioBw);
audioResamp.setInSampleRate(6000);
audioWin.setSampleRate(6000 * audioResamp.getInterpolation());
audioWin.setCutoff(audioBw);
audioWin.setTransWidth(audioBw);
audioResamp.updateWindow(&audioWin);
deemp.bypass = true; deemp.bypass = true;
vfo->setReference(ImGui::WaterfallVFO::REF_LOWER); vfo->setReference(ImGui::WaterfallVFO::REF_LOWER);
ssbDemod.start(); ssbDemod.start();
@ -154,11 +171,16 @@ void SigPath::setDemodulator(int demId, float bandWidth) {
else if (demId == DEMOD_LSB) { else if (demId == DEMOD_LSB) {
demodOutputSamplerate = 6000; demodOutputSamplerate = 6000;
vfo->setSampleRate(6000, bandwidth); vfo->setSampleRate(6000, bandwidth);
ssbDemod.setBlockSize(vfo->getOutputBlockSize());
ssbDemod.setMode(dsp::SSBDemod::MODE_LSB); ssbDemod.setMode(dsp::SSBDemod::MODE_LSB);
audioResamp.setInput(&ssbDemod.output); audioResamp.setInput(&ssbDemod.out);
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f); audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(6000, vfo->getOutputBlockSize(), audioBw, audioBw);
audioResamp.setInSampleRate(6000);
audioWin.setSampleRate(6000 * audioResamp.getInterpolation());
audioWin.setCutoff(audioBw);
audioWin.setTransWidth(audioBw);
audioResamp.updateWindow(&audioWin);
deemp.bypass = true; deemp.bypass = true;
vfo->setReference(ImGui::WaterfallVFO::REF_UPPER); vfo->setReference(ImGui::WaterfallVFO::REF_UPPER);
ssbDemod.start(); ssbDemod.start();
@ -166,34 +188,24 @@ void SigPath::setDemodulator(int demId, float bandWidth) {
else if (demId == DEMOD_DSB) { else if (demId == DEMOD_DSB) {
demodOutputSamplerate = 6000; demodOutputSamplerate = 6000;
vfo->setSampleRate(6000, bandwidth); vfo->setSampleRate(6000, bandwidth);
ssbDemod.setBlockSize(vfo->getOutputBlockSize());
ssbDemod.setMode(dsp::SSBDemod::MODE_DSB); ssbDemod.setMode(dsp::SSBDemod::MODE_DSB);
audioResamp.setInput(&ssbDemod.output); audioResamp.setInput(&ssbDemod.out);
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f); audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(6000, vfo->getOutputBlockSize(), audioBw, audioBw);
audioResamp.setInSampleRate(6000);
audioWin.setSampleRate(6000 * audioResamp.getInterpolation());
audioWin.setCutoff(audioBw);
audioWin.setTransWidth(audioBw);
audioResamp.updateWindow(&audioWin);
deemp.bypass = true; deemp.bypass = true;
vfo->setReference(ImGui::WaterfallVFO::REF_CENTER); vfo->setReference(ImGui::WaterfallVFO::REF_CENTER);
ssbDemod.start(); ssbDemod.start();
} }
else if (demId == DEMOD_RAW) {
demodOutputSamplerate = 10000;
vfo->setSampleRate(10000, bandwidth);
cpx2stereo.setBlockSize(vfo->getOutputBlockSize());
//audioResamp.setInput(&cpx2stereo.output);
audioBw = std::min<float>(bandwidth, outputSampleRate / 2.0f);
audioResamp.setInputSampleRate(10000, vfo->getOutputBlockSize(), audioBw, audioBw);
vfo->setReference(ImGui::WaterfallVFO::REF_LOWER);
cpx2stereo.start();
}
else { else {
spdlog::error("UNIMPLEMENTED DEMODULATOR IN SigPath::setDemodulator (start): {0}", demId); spdlog::error("UNIMPLEMENTED DEMODULATOR IN SigPath::setDemodulator (start): {0}", demId);
} }
squelch.setBlockSize(vfo->getOutputBlockSize());
squelch.start();
deemp.setBlockSize(audioResamp.getOutputBlockSize());
audioResamp.start(); audioResamp.start();
deemp.start(); deemp.start();
} }
@ -237,17 +249,17 @@ void SigPath::setBandwidth(float bandWidth) {
} }
else if (_demod == DEMOD_USB) { else if (_demod == DEMOD_USB) {
ssbDemod.stop(); ssbDemod.stop();
ssbDemod.setBandwidth(bandwidth); ssbDemod.setBandWidth(bandwidth);
ssbDemod.start(); ssbDemod.start();
} }
else if (_demod == DEMOD_LSB) { else if (_demod == DEMOD_LSB) {
ssbDemod.stop(); ssbDemod.stop();
ssbDemod.setBandwidth(bandwidth); ssbDemod.setBandWidth(bandwidth);
ssbDemod.start(); ssbDemod.start();
} }
else if (_demod == DEMOD_DSB) { else if (_demod == DEMOD_DSB) {
ssbDemod.stop(); ssbDemod.stop();
ssbDemod.setBandwidth(bandwidth); ssbDemod.setBandWidth(bandwidth);
ssbDemod.start(); ssbDemod.start();
} }
else if (_demod == DEMOD_RAW) { else if (_demod == DEMOD_RAW) {
@ -260,15 +272,16 @@ void SigPath::setBandwidth(float bandWidth) {
if (audioBw != _audioBw) { if (audioBw != _audioBw) {
audioBw = _audioBw; audioBw = _audioBw;
audioResamp.stop(); audioResamp.stop();
audioResamp.setFilterParams(audioBw, audioBw);
audioResamp.setBlockSize(vfo->getOutputBlockSize()); audioWin.setCutoff(audioBw);
//audioResamp.setInputSampleRate(demodOutputSamplerate, vfo->getOutputBlockSize(), audioBw, audioBw); audioWin.setTransWidth(audioBw);
audioResamp.updateWindow(&audioWin);
audioResamp.start(); audioResamp.start();
} }
} }
void SigPath::start() { void SigPath::start() {
squelch.start();
demod.start(); demod.start();
audioResamp.start(); audioResamp.start();
deemp.start(); deemp.start();

View File

@ -6,9 +6,10 @@
#include <dsp/demodulator.h> #include <dsp/demodulator.h>
#include <dsp/routing.h> #include <dsp/routing.h>
#include <dsp/sink.h> #include <dsp/sink.h>
#include <dsp/correction.h>
#include <dsp/vfo.h> #include <dsp/vfo.h>
#include <dsp/block.h> #include <dsp/block.h>
#include <dsp/window.h>
#include <dsp/audio.h>
#include <io/audio.h> #include <io/audio.h>
#include <module.h> #include <module.h>
#include <signal_path/signal_path.h> #include <signal_path/signal_path.h>
@ -44,28 +45,23 @@ public:
}; };
dsp::FMDeemphasis deemp; dsp::BFMDeemp deemp;
dsp::Squelch squelch;
VFOManager::VFO* vfo; VFOManager::VFO* vfo;
private: private:
static int sampleRateChangeHandler(void* ctx, double sampleRate); static int sampleRateChangeHandler(void* ctx, double sampleRate);
dsp::stream<dsp::complex_t> input; dsp::stream<dsp::complex_t> input;
// Demodulators // Demodulators
dsp::FMDemodulator demod; dsp::FMDemod demod;
dsp::AMDemodulator amDemod; dsp::AMDemod amDemod;
dsp::SSBDemod ssbDemod; dsp::SSBDemod ssbDemod;
dsp::ComplexToStereo cpx2stereo;
// Audio output // Audio output
dsp::MonoToStereo m2s; dsp::MonoToStereo m2s;
dsp::FIRResampler<float> audioResamp; dsp::filter_window::BlackmanWindow audioWin;
dsp::PolyphaseResampler<float> audioResamp;
std::string vfoName; std::string vfoName;

View File

@ -104,8 +104,7 @@ private:
_this->samplesWritten = 0; _this->samplesWritten = 0;
_this->sampleRate = sigpath::signalPath.getSampleRate(); _this->sampleRate = sigpath::signalPath.getSampleRate();
_this->writer = new WavWriter(ROOT_DIR "/recordings/" + genFileName("baseband_"), 16, 2, _this->sampleRate); _this->writer = new WavWriter(ROOT_DIR "/recordings/" + genFileName("baseband_"), 16, 2, _this->sampleRate);
_this->iqStream = new dsp::stream<dsp::complex_t>(); _this->iqStream = new dsp::stream<dsp::complex_t>;
_this->iqStream->init(_this->sampleRate / 200.0);
sigpath::signalPath.bindIQStream(_this->iqStream); sigpath::signalPath.bindIQStream(_this->iqStream);
_this->workerThread = std::thread(_iqWriteWorker, _this); _this->workerThread = std::thread(_iqWriteWorker, _this);
_this->recording = true; _this->recording = true;
@ -176,20 +175,18 @@ private:
} }
static void _audioWriteWorker(RecorderModule* _this) { static void _audioWriteWorker(RecorderModule* _this) {
dsp::StereoFloat_t* floatBuf = new dsp::StereoFloat_t[1024]; int16_t* sampleBuf = new int16_t[STREAM_BUFFER_SIZE * 2];
int16_t* sampleBuf = new int16_t[2048];
while (true) { while (true) {
if (_this->audioStream->read(floatBuf, 1024) < 0) { int count = _this->audioStream->read();
break; if (count < 0) { break; }
}
for (int i = 0; i < 1024; i++) { for (int i = 0; i < 1024; i++) {
sampleBuf[(i * 2) + 0] = floatBuf[i].l * 0x7FFF; sampleBuf[(i * 2) + 0] = _this->audioStream->data[i].l * 0x7FFF;
sampleBuf[(i * 2) + 1] = floatBuf[i].r * 0x7FFF; sampleBuf[(i * 2) + 1] = _this->audioStream->data[i].r * 0x7FFF;
} }
_this->audioStream->flush();
_this->samplesWritten += 1024; _this->samplesWritten += 1024;
_this->writer->writeSamples(sampleBuf, 2048 * sizeof(int16_t)); _this->writer->writeSamples(sampleBuf, 2048 * sizeof(int16_t));
} }
delete[] floatBuf;
delete[] sampleBuf; delete[] sampleBuf;
} }
@ -197,13 +194,13 @@ private:
dsp::complex_t* iqBuf = new dsp::complex_t[1024]; dsp::complex_t* iqBuf = new dsp::complex_t[1024];
int16_t* sampleBuf = new int16_t[2048]; int16_t* sampleBuf = new int16_t[2048];
while (true) { while (true) {
if (_this->iqStream->read(iqBuf, 1024) < 0) { int count = _this->iqStream->read();
break; if (count < 0) { break; }
}
for (int i = 0; i < 1024; i++) { for (int i = 0; i < 1024; i++) {
sampleBuf[(i * 2) + 0] = iqBuf[i].q * 0x7FFF; sampleBuf[(i * 2) + 0] = _this->iqStream->data[i].q * 0x7FFF;
sampleBuf[(i * 2) + 1] = iqBuf[i].i * 0x7FFF; sampleBuf[(i * 2) + 1] = _this->iqStream->data[i].i * 0x7FFF;
} }
_this->iqStream->flush();
_this->samplesWritten += 1024; _this->samplesWritten += 1024;
_this->writer->writeSamples(sampleBuf, 2048 * sizeof(int16_t)); _this->writer->writeSamples(sampleBuf, 2048 * sizeof(int16_t));
} }
@ -212,7 +209,7 @@ private:
} }
std::string name; std::string name;
dsp::stream<dsp::StereoFloat_t>* audioStream; dsp::stream<dsp::stereo_t>* audioStream;
dsp::stream<dsp::complex_t>* iqStream; dsp::stream<dsp::complex_t>* iqStream;
WavWriter* writer; WavWriter* writer;
std::thread workerThread; std::thread workerThread;

View File

@ -3,7 +3,7 @@
"Radio": { "Radio": {
"device": "default", "device": "default",
"sampleRate": 48000.0, "sampleRate": 48000.0,
"volume": 0.4407407343387604 "volume": 0.0
}, },
"Radio 1": { "Radio 1": {
"device": "Speakers (Realtek High Definition Audio)", "device": "Speakers (Realtek High Definition Audio)",
@ -19,9 +19,9 @@
"bandPlan": "General", "bandPlan": "General",
"bandPlanEnabled": true, "bandPlanEnabled": true,
"fftHeight": 298, "fftHeight": 298,
"frequency": 4620379, "frequency": 99000000,
"max": 0.0, "max": 0.0,
"maximized": true, "maximized": false,
"menuOrder": [ "menuOrder": [
"Source", "Source",
"Radio", "Radio",

View File

@ -1,6 +1,6 @@
{ {
"Radio": "./radio/Release/radio.dll", "Radio": "./radio/RelWithDebInfo/radio.dll",
"Recorder": "./recorder/Release/recorder.dll", "Recorder": "./recorder/RelWithDebInfo/recorder.dll",
"Soapy": "./soapy/Release/soapy.dll", "Soapy": "./soapy/RelWithDebInfo/soapy.dll",
"RTLTCPSource": "./rtl_tcp_source/Release/rtl_tcp_source.dll" "RTLTCPSource": "./rtl_tcp_source/RelWithDebInfo/rtl_tcp_source.dll"
} }

View File

@ -1,5 +1,5 @@
{ {
"device": "HackRF One #0 901868dc282c8f8b", "device": "Generic RTL2832U OEM :: 00000001",
"devices": { "devices": {
"AirSpy HF+ [c852435de0224af7]": { "AirSpy HF+ [c852435de0224af7]": {
"gains": { "gains": {
@ -8,6 +8,9 @@
}, },
"sampleRate": 768000.0 "sampleRate": 768000.0
}, },
"Default Device": {
"sampleRate": 32000.0
},
"Generic RTL2832U OEM :: 00000001": { "Generic RTL2832U OEM :: 00000001": {
"gains": { "gains": {
"TUNER": 49.599998474121094 "TUNER": 49.599998474121094
@ -22,6 +25,9 @@
}, },
"sampleRate": 8000000.0 "sampleRate": 8000000.0
}, },
"Microphone (Realtek High Definition Audio)": {
"sampleRate": 96000.0
},
"PulseAudio": { "PulseAudio": {
"sampleRate": 96000.0 "sampleRate": 96000.0
} }

View File

@ -21,8 +21,6 @@ public:
RTLTCPSourceModule(std::string name) { RTLTCPSourceModule(std::string name) {
this->name = name; this->name = name;
stream.init(100);
sampleRate = 2560000.0; sampleRate = 2560000.0;
handler.ctx = this; handler.ctx = this;
@ -143,22 +141,19 @@ private:
RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx; RTLTCPSourceModule* _this = (RTLTCPSourceModule*)ctx;
int blockSize = _this->sampleRate / 200.0; int blockSize = _this->sampleRate / 200.0;
uint8_t* inBuf = new uint8_t[blockSize * 2]; uint8_t* inBuf = new uint8_t[blockSize * 2];
dsp::complex_t* outBuf = new dsp::complex_t[blockSize];
_this->stream.setMaxLatency(blockSize * 2);
while (true) { while (true) {
// Read samples here // Read samples here
_this->client.receiveData(inBuf, blockSize * 2); _this->client.receiveData(inBuf, blockSize * 2);
if (_this->stream.aquire() < 0) { break; }
for (int i = 0; i < blockSize; i++) { for (int i = 0; i < blockSize; i++) {
outBuf[i].q = ((double)inBuf[i * 2] - 128.0) / 128.0; _this->stream.data[i].q = ((double)inBuf[i * 2] - 128.0) / 128.0;
outBuf[i].i = ((double)inBuf[(i * 2) + 1] - 128.0) / 128.0; _this->stream.data[i].i = ((double)inBuf[(i * 2) + 1] - 128.0) / 128.0;
} }
if (_this->stream.write(outBuf, blockSize) < 0) { break; }; _this->stream.write(blockSize);
} }
delete[] inBuf; delete[] inBuf;
delete[] outBuf;
} }
std::string name; std::string name;

View File

@ -31,8 +31,6 @@ public:
refresh(); refresh();
stream.init(100);
// Select default device // Select default device
config.aquire(); config.aquire();
std::string devName = config.conf["device"]; std::string devName = config.conf["device"];
@ -208,7 +206,9 @@ private:
_this->running = false; _this->running = false;
_this->dev->deactivateStream(_this->devStream); _this->dev->deactivateStream(_this->devStream);
_this->dev->closeStream(_this->devStream); _this->dev->closeStream(_this->devStream);
_this->stream.stopWriter();
_this->workerThread.join(); _this->workerThread.join();
_this->stream.clearWriteStop();
SoapySDR::Device::unmake(_this->dev); SoapySDR::Device::unmake(_this->dev);
spdlog::info("SoapyModule '{0}': Stop!", _this->name); spdlog::info("SoapyModule '{0}': Stop!", _this->name);
@ -289,18 +289,17 @@ private:
static void _worker(SoapyModule* _this) { static void _worker(SoapyModule* _this) {
spdlog::info("SOAPY: WORKER STARTED {0}", _this->sampleRate); spdlog::info("SOAPY: WORKER STARTED {0}", _this->sampleRate);
int blockSize = _this->sampleRate / 200.0f; int blockSize = _this->sampleRate / 200.0f;
dsp::complex_t* buf = new dsp::complex_t[blockSize];
int flags = 0; int flags = 0;
long long timeMs = 0; long long timeMs = 0;
while (_this->running) { while (_this->running) {
int res = _this->dev->readStream(_this->devStream, (void**)&buf, blockSize, flags, timeMs); if (_this->stream.aquire() < 0) { break; }
int res = _this->dev->readStream(_this->devStream, (void**)&_this->stream.data, blockSize, flags, timeMs);
if (res < 1) { if (res < 1) {
continue; continue;
} }
_this->stream.write(buf, res); _this->stream.write(res);
} }
delete[] buf;
} }
std::string name; std::string name;