SDRPlusPlus/core/src/dsp/routing.h

183 lines
5.7 KiB
C
Raw Normal View History

2020-06-22 16:45:57 +02:00
#pragma once
2020-11-02 03:57:44 +01:00
#include <dsp/block.h>
2020-11-04 04:11:51 +01:00
#include <dsp/buffer.h>
2020-11-12 00:53:38 +01:00
#include <string.h>
2020-11-04 04:11:51 +01:00
#include <numeric>
2020-11-02 21:13:28 +01:00
#include <spdlog/spdlog.h>
2020-06-22 16:45:57 +02:00
namespace dsp {
2020-08-16 03:39:05 +02:00
template <class T>
2020-11-02 03:57:44 +01:00
class Splitter : public generic_block<Splitter<T>> {
2020-08-11 18:33:42 +02:00
public:
2020-11-02 03:57:44 +01:00
Splitter() {}
2020-08-11 18:33:42 +02:00
2020-11-02 03:57:44 +01:00
Splitter(stream<T>* in) { init(in); }
2020-08-11 18:33:42 +02:00
2020-11-02 03:57:44 +01:00
void init(stream<T>* in) {
_in = in;
generic_block<Splitter>::registerInput(_in);
2020-08-11 18:33:42 +02:00
}
2020-11-02 03:57:44 +01:00
void setInput(stream<T>* in) {
std::lock_guard<std::mutex> lck(generic_block<Splitter>::ctrlMtx);
generic_block<Splitter>::tempStop();
generic_block<Splitter>::unregisterInput(_in);
_in = in;
generic_block<Splitter>::registerInput(_in);
generic_block<Splitter>::tempStart();
2020-08-11 18:33:42 +02:00
}
2020-11-02 03:57:44 +01:00
void bindStream(stream<T>* stream) {
std::lock_guard<std::mutex> lck(generic_block<Splitter>::ctrlMtx);
generic_block<Splitter>::tempStop();
out.push_back(stream);
generic_block<Splitter>::registerOutput(stream);
generic_block<Splitter>::tempStart();
2020-08-11 18:33:42 +02:00
}
2020-11-02 03:57:44 +01:00
void unbindStream(stream<T>* stream) {
std::lock_guard<std::mutex> lck(generic_block<Splitter>::ctrlMtx);
generic_block<Splitter>::tempStop();
generic_block<Splitter>::unregisterOutput(stream);
out.erase(std::remove(out.begin(), out.end(), stream), out.end());
generic_block<Splitter>::tempStart();
2020-08-11 18:33:42 +02:00
}
private:
2020-11-02 03:57:44 +01:00
int run() {
// TODO: If too slow, buffering might be necessary
int count = _in->read();
if (count < 0) { return -1; }
for (const auto& stream : out) {
memcpy(stream->writeBuf, _in->readBuf, count * sizeof(T));
if (!stream->swap(count)) { return -1; }
2020-08-11 18:33:42 +02:00
}
2020-11-02 03:57:44 +01:00
_in->flush();
return count;
2020-08-11 18:33:42 +02:00
}
2020-08-16 03:39:05 +02:00
stream<T>* _in;
2020-11-02 03:57:44 +01:00
std::vector<stream<T>*> out;
2020-08-16 03:39:05 +02:00
};
2020-11-04 04:11:51 +01:00
// NOTE: I'm not proud of this, it's BAD and just taken from the previous DSP, but it works...
2020-11-02 03:57:44 +01:00
template <class T>
class Reshaper : public generic_block<Reshaper<T>> {
2020-08-16 03:39:05 +02:00
public:
2020-11-02 03:57:44 +01:00
Reshaper() {}
2020-08-16 03:39:05 +02:00
2020-11-04 04:11:51 +01:00
Reshaper(stream<T>* in, int keep, int skip) { init(in, keep, skip); }
2020-08-16 03:39:05 +02:00
2020-11-04 04:11:51 +01:00
void init(stream<T>* in, int keep, int skip) {
2020-11-02 03:57:44 +01:00
_in = in;
2020-11-04 04:11:51 +01:00
_keep = keep;
_skip = skip;
ringBuf.init(keep * 2);
2020-11-02 03:57:44 +01:00
generic_block<Reshaper<T>>::registerInput(_in);
generic_block<Reshaper<T>>::registerOutput(&out);
2020-08-16 03:39:05 +02:00
}
2020-11-02 03:57:44 +01:00
void setInput(stream<T>* in) {
std::lock_guard<std::mutex> lck(generic_block<Reshaper<T>>::ctrlMtx);
generic_block<Reshaper<T>>::tempStop();
2020-11-02 17:48:17 +01:00
generic_block<Reshaper<T>>::unregisterInput(_in);
2020-11-02 03:57:44 +01:00
_in = in;
2020-11-02 17:48:17 +01:00
generic_block<Reshaper<T>>::registerInput(_in);
2020-11-02 03:57:44 +01:00
generic_block<Reshaper<T>>::tempStart();
2020-08-16 03:39:05 +02:00
}
2020-11-04 04:11:51 +01:00
void setKeep(int keep) {
std::lock_guard<std::mutex> lck(generic_block<Reshaper<T>>::ctrlMtx);
generic_block<Reshaper<T>>::tempStop();
_keep = keep;
2020-11-04 05:08:42 +01:00
ringBuf.setMaxLatency(keep * 2);
2020-11-04 04:11:51 +01:00
generic_block<Reshaper<T>>::tempStart();
}
void setSkip(int skip) {
std::lock_guard<std::mutex> lck(generic_block<Reshaper<T>>::ctrlMtx);
generic_block<Reshaper<T>>::tempStop();
_skip = skip;
generic_block<Reshaper<T>>::tempStart();
}
2020-11-02 03:57:44 +01:00
int run() {
int count = _in->read();
2020-11-04 04:11:51 +01:00
if (count < 0) { return -1; }
ringBuf.write(_in->readBuf, count);
2020-11-02 03:57:44 +01:00
_in->flush();
2020-11-04 05:08:42 +01:00
return count;
2020-08-16 03:39:05 +02:00
}
2020-11-02 03:57:44 +01:00
stream<T> out;
2020-08-16 03:39:05 +02:00
private:
2020-11-04 04:11:51 +01:00
void doStart() {
workThread = std::thread(&Reshaper<T>::loop, this);
bufferWorkerThread = std::thread(&Reshaper<T>::bufferWorker, this);
}
void loop() {
while (run() >= 0);
}
void doStop() {
_in->stopReader();
ringBuf.stopReader();
out.stopWriter();
ringBuf.stopWriter();
if (workThread.joinable()) {
workThread.join();
}
if (bufferWorkerThread.joinable()) {
bufferWorkerThread.join();
}
_in->clearReadStop();
ringBuf.clearReadStop();
out.clearWriteStop();
ringBuf.clearWriteStop();
}
void bufferWorker() {
2021-03-20 21:53:44 +01:00
T* buf = new T[_keep];
2020-11-04 04:11:51 +01:00
bool delay = _skip < 0;
int readCount = std::min<int>(_keep + _skip, _keep);
int skip = std::max<int>(_skip, 0);
2021-03-20 21:53:44 +01:00
int delaySize = (-_skip) * sizeof(T);
2020-12-16 01:45:17 +01:00
int delayCount = (-_skip);
2020-11-04 04:11:51 +01:00
2021-03-20 21:53:44 +01:00
T* start = &buf[std::max<int>(-_skip, 0)];
T* delayStart = &buf[_keep + _skip];
2020-11-04 04:11:51 +01:00
while (true) {
if (delay) {
memmove(buf, delayStart, delaySize);
2021-03-20 21:53:44 +01:00
if constexpr (std::is_same_v<T, complex_t> || std::is_same_v<T, stereo_t>) {
for (int i = 0; i < delayCount; i++) {
2021-03-29 21:53:43 +02:00
buf[i].re /= 10.0f;
buf[i].im /= 10.0f;
2021-03-20 21:53:44 +01:00
}
2020-12-16 01:45:17 +01:00
}
2020-11-04 04:11:51 +01:00
}
if (ringBuf.readAndSkip(start, readCount, skip) < 0) { break; };
2021-03-20 21:53:44 +01:00
memcpy(out.writeBuf, buf, _keep * sizeof(T));
if (!out.swap(_keep)) { break; }
2020-11-04 04:11:51 +01:00
}
delete[] buf;
}
2020-11-02 03:57:44 +01:00
stream<T>* _in;
int _outBlockSize;
2020-11-04 04:11:51 +01:00
RingBuffer<T> ringBuf;
std::thread bufferWorkerThread;
std::thread workThread;
int _keep, _skip;
2020-08-16 03:39:05 +02:00
2020-08-11 18:33:42 +02:00
};
2020-11-02 03:57:44 +01:00
}