#pragma once #include #include namespace dsp { template class HandlerSink : public generic_block> { public: HandlerSink() {} HandlerSink(stream* in, void (*handler)(T* data, int count, void* ctx), void* ctx) { init(in, handler, ctx); } ~HandlerSink() { generic_block>::stop(); } void init(stream* in, void (*handler)(T* data, int count, void* ctx), void* ctx) { _in = in; _handler = handler; _ctx = ctx; generic_block>::registerInput(_in); } void setInput(stream* in) { std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); _in = in; generic_block>::registerInput(_in); generic_block>::tempStart(); } void setHandler(void (*handler)(T* data, int count, void* ctx), void* ctx) { std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); _handler = handler; _ctx = ctx; generic_block>::tempStart(); } int run() { count = _in->read(); if (count < 0) { return -1; } _handler(_in->readBuf, count, _ctx); _in->flush(); return count; } private: int count; stream* _in; void (*_handler)(T* data, int count, void* ctx); void* _ctx; }; template class RingBufferSink : public generic_block> { public: RingBufferSink() {} RingBufferSink(stream* in) { init(in); } ~RingBufferSink() { generic_block>::stop(); } void init(stream* in) { _in = in; data.init(480); // TODO: Use an argument generic_block>::registerInput(_in); } void setInput(stream* in) { std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); _in = in; generic_block>::registerInput(_in); generic_block>::tempStart(); } int run() { count = _in->read(); if (count < 0) { return -1; } if (data.write(_in->readBuf, count) < 0) { return -1; } _in->flush(); return count; } RingBuffer data; private: void doStop() { _in->stopReader(); data.stopWriter(); if (generic_block>::workerThread.joinable()) { generic_block>::workerThread.join(); } _in->clearReadStop(); data.clearWriteStop(); } int count; stream* _in; }; template class NullSink : public generic_block> { public: NullSink() {} NullSink(stream* in) { init(in); } ~NullSink() { generic_block>::stop(); } void init(stream* in) { _in = in; generic_block>::registerInput(_in); } void setInput(stream* in) { std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); _in = in; generic_block>::registerInput(_in); generic_block>::tempStart(); } int run() { count = _in->read(); if (count < 0) { return -1; } _in->flush(); return count; } private: int count; stream* _in; }; }