121 lines
2.8 KiB
C
Raw Normal View History

2020-09-18 00:23:03 +02:00
#pragma once
2020-11-02 03:57:44 +01:00
#include <stdio.h>
2020-09-18 00:23:03 +02:00
#include <dsp/stream.h>
2020-11-02 03:57:44 +01:00
#include <dsp/types.h>
#include <thread>
#include <vector>
#include <algorithm>
#define FL_M_PI 3.1415926535f
2020-09-18 00:23:03 +02:00
namespace dsp {
2020-11-02 03:57:44 +01:00
template <class BLOCK>
class generic_block {
2020-09-18 00:23:03 +02:00
public:
2020-11-02 03:57:44 +01:00
virtual void init() {}
2020-09-18 00:23:03 +02:00
2020-11-02 03:57:44 +01:00
virtual void start() {
std::lock_guard<std::mutex> lck(ctrlMtx);
2020-09-18 00:23:03 +02:00
if (running) {
return;
}
2020-11-02 03:57:44 +01:00
doStart();
2020-09-18 00:23:03 +02:00
}
2020-11-02 03:57:44 +01:00
virtual void stop() {
std::lock_guard<std::mutex> lck(ctrlMtx);
if (!running && !tempStopped) {
2020-09-18 00:23:03 +02:00
return;
}
2020-11-02 03:57:44 +01:00
doStop();
2020-09-18 00:23:03 +02:00
}
2020-11-02 03:57:44 +01:00
virtual int calcOutSize(int inSize) { return inSize; }
2020-09-18 00:23:03 +02:00
2020-11-02 03:57:44 +01:00
virtual int run() = 0;
friend BLOCK;
2020-09-18 00:23:03 +02:00
private:
2020-11-02 03:57:44 +01:00
void workerLoop() {
while (run() >= 0);
2020-09-18 00:23:03 +02:00
}
2020-11-02 03:57:44 +01:00
void aquire() {
ctrlMtx.lock();
2020-09-18 00:23:03 +02:00
}
2020-11-02 03:57:44 +01:00
void release() {
ctrlMtx.unlock();
2020-10-20 00:32:17 +02:00
}
2020-11-02 03:57:44 +01:00
void registerInput(untyped_steam* inStream) {
inputs.push_back(inStream);
2020-10-20 00:32:17 +02:00
}
2020-11-02 03:57:44 +01:00
void unregisterInput(untyped_steam* inStream) {
inputs.erase(std::remove(inputs.begin(), inputs.end(), inStream), inputs.end());
}
2020-10-24 14:51:55 +02:00
2020-11-02 03:57:44 +01:00
void registerOutput(untyped_steam* outStream) {
outputs.push_back(outStream);
2020-10-24 14:51:55 +02:00
}
2020-11-02 03:57:44 +01:00
void unregisterOutput(untyped_steam* outStream) {
outputs.erase(std::remove(outputs.begin(), outputs.end(), outStream), outputs.end());
2020-10-24 14:51:55 +02:00
}
2020-11-02 03:57:44 +01:00
virtual void doStart() {
running = true;
workerThread = std::thread(&generic_block::workerLoop, this);
2020-10-24 14:51:55 +02:00
}
2020-11-02 03:57:44 +01:00
virtual void doStop() {
for (auto const& in : inputs) {
in->stopReader();
}
for (auto const& out : outputs) {
out->stopWriter();
2020-10-24 14:51:55 +02:00
}
2020-11-02 03:57:44 +01:00
// TODO: Make sure this isn't needed, I don't know why it stops
if (workerThread.joinable()) {
workerThread.join();
}
for (auto const& in : inputs) {
in->clearReadStop();
}
for (auto const& out : outputs) {
out->clearWriteStop();
2020-10-24 14:51:55 +02:00
}
}
2020-11-02 03:57:44 +01:00
void tempStart() {
if (tempStopped) {
doStart();
tempStopped = false;
2020-10-24 14:51:55 +02:00
}
}
2020-11-02 03:57:44 +01:00
void tempStop() {
if (running && !tempStopped) {
doStop();
tempStopped = true;
2020-10-24 14:51:55 +02:00
}
}
2020-11-02 03:57:44 +01:00
std::vector<untyped_steam*> inputs;
std::vector<untyped_steam*> outputs;
2020-10-24 14:51:55 +02:00
bool running = false;
2020-11-02 03:57:44 +01:00
bool tempStopped = false;
2020-10-24 14:51:55 +02:00
std::thread workerThread;
2020-11-02 03:57:44 +01:00
protected:
std::mutex ctrlMtx;
2020-10-24 14:51:55 +02:00
};
2020-11-02 03:57:44 +01:00
}