#pragma once #include #include #include #include namespace cdsp { class Multiplier { public: Multiplier() { } Multiplier(stream* a, stream* b, int bufferSize) : output(bufferSize * 2) { _a = a; _b = b; _bufferSize = bufferSize; } void init(stream* a, stream* b, int bufferSize) { output.init(bufferSize * 2); _a = a; _b = b; _bufferSize = bufferSize; } void start() { if (running) { return; } running = true; _workerThread = std::thread(_worker, this); } void stop() { if (!running) { return; } _a->stopReader(); _b->stopReader(); output.stopWriter(); _workerThread.join(); running = false; _a->clearReadStop(); _b->clearReadStop(); output.clearWriteStop(); } stream output; private: static void _worker(Multiplier* _this) { complex_t* aBuf = new complex_t[_this->_bufferSize]; complex_t* bBuf = new complex_t[_this->_bufferSize]; complex_t* outBuf = new complex_t[_this->_bufferSize]; while (true) { if (_this->_a->read(aBuf, _this->_bufferSize) < 0) { printf("A Received stop signal\n"); return; }; if (_this->_b->read(bBuf, _this->_bufferSize) < 0) { printf("B Received stop signal\n"); return; }; do_mul(aBuf, bBuf, _this->_bufferSize); if (_this->output.write(aBuf, _this->_bufferSize) < 0) { printf("OUT Received stop signal\n"); return; }; } } stream* _a; stream* _b; int _bufferSize; bool running = false; std::thread _workerThread; }; };