SDRPlusPlus/core/src/dsp/sink.h

140 lines
3.3 KiB
C
Raw Normal View History

2020-06-22 16:45:57 +02:00
#pragma once
#include <thread>
#include <dsp/stream.h>
#include <dsp/types.h>
#include <vector>
2020-10-24 14:51:55 +02:00
#include <spdlog/spdlog.h>
2020-06-22 16:45:57 +02:00
namespace dsp {
class HandlerSink {
public:
HandlerSink() {
}
HandlerSink(stream<complex_t>* input, complex_t* buffer, int bufferSize, void handler(complex_t*)) {
_in = input;
_bufferSize = bufferSize;
_buffer = buffer;
_handler = handler;
}
void init(stream<complex_t>* input, complex_t* buffer, int bufferSize, void handler(complex_t*)) {
_in = input;
_bufferSize = bufferSize;
_buffer = buffer;
_handler = handler;
}
void start() {
2020-07-19 15:59:44 +02:00
if (running) {
return;
}
2020-06-22 16:45:57 +02:00
_workerThread = std::thread(_worker, this);
2020-07-19 15:59:44 +02:00
running = true;
}
void stop() {
if (!running) {
return;
}
_in->stopReader();
_workerThread.join();
_in->clearReadStop();
running = false;
2020-06-22 16:45:57 +02:00
}
bool bypass;
private:
static void _worker(HandlerSink* _this) {
while (true) {
2020-07-19 15:59:44 +02:00
if (_this->_in->read(_this->_buffer, _this->_bufferSize) < 0) { break; };
2020-06-22 16:45:57 +02:00
_this->_handler(_this->_buffer);
}
}
stream<complex_t>* _in;
int _bufferSize;
complex_t* _buffer;
std::thread _workerThread;
void (*_handler)(complex_t*);
2020-07-19 15:59:44 +02:00
bool running = false;
2020-06-22 16:45:57 +02:00
};
2020-10-24 14:51:55 +02:00
template <class T>
2020-06-22 16:45:57 +02:00
class NullSink {
public:
NullSink() {
}
2020-10-24 14:51:55 +02:00
NullSink(stream<T>* input, int bufferSize) {
2020-06-22 16:45:57 +02:00
_in = input;
_bufferSize = bufferSize;
}
2020-10-24 14:51:55 +02:00
void init(stream<T>* input, int bufferSize) {
2020-06-22 16:45:57 +02:00
_in = input;
_bufferSize = bufferSize;
}
void start() {
_workerThread = std::thread(_worker, this);
}
bool bypass;
private:
static void _worker(NullSink* _this) {
2020-10-24 14:51:55 +02:00
T* buf = new T[_this->_bufferSize];
2020-06-22 16:45:57 +02:00
while (true) {
2020-10-24 14:51:55 +02:00
//spdlog::info("NS: Reading...");
2020-06-22 16:45:57 +02:00
_this->_in->read(buf, _this->_bufferSize);
}
}
2020-10-24 14:51:55 +02:00
stream<T>* _in;
2020-06-22 16:45:57 +02:00
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() {
2020-10-24 14:51:55 +02:00
spdlog::info("NS: Starting...");
2020-06-22 16:45:57 +02:00
_workerThread = std::thread(_worker, this);
}
bool bypass;
private:
static void _worker(FloatNullSink* _this) {
2020-10-24 14:51:55 +02:00
spdlog::info("NS: Started!");
2020-06-22 16:45:57 +02:00
float* buf = new float[_this->_bufferSize];
while (true) {
2020-10-24 14:51:55 +02:00
spdlog::info("NS: Reading...");
2020-06-22 16:45:57 +02:00
_this->_in->read(buf, _this->_bufferSize);
}
}
stream<float>* _in;
int _bufferSize;
std::thread _workerThread;
};
};