diff --git a/core/src/dsp/audio.h b/core/src/dsp/audio.h index 795b58c3..d8575e43 100644 --- a/core/src/dsp/audio.h +++ b/core/src/dsp/audio.h @@ -12,9 +12,11 @@ namespace dsp { _in = in; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -53,9 +55,11 @@ namespace dsp { generic_block::registerInput(_in_left); generic_block::registerInput(_in_right); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in_left, stream* in_right) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in_left); @@ -100,9 +104,11 @@ namespace dsp { StereoToMono(stream* in) { init(in); } ~StereoToMono() { + if (!generic_block::_block_init) { return; } generic_block::stop(); delete[] l_buf; delete[] r_buf; + generic_block::_block_init = false; } void init(stream* in) { @@ -111,9 +117,11 @@ namespace dsp { r_buf = new float[STREAM_BUFFER_SIZE]; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -155,9 +163,11 @@ namespace dsp { generic_block::registerInput(_in); generic_block::registerOutput(&out_left); generic_block::registerOutput(&out_right); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); diff --git a/core/src/dsp/block.h b/core/src/dsp/block.h index 59b56bf7..2f8f5736 100644 --- a/core/src/dsp/block.h +++ b/core/src/dsp/block.h @@ -24,10 +24,13 @@ namespace dsp { virtual void init() {} virtual ~generic_block() { + if (!_block_init) { return; } stop(); + _block_init = false; } virtual void start() { + assert(_block_init); std::lock_guard lck(ctrlMtx); if (running) { return; @@ -37,6 +40,7 @@ namespace dsp { } virtual void stop() { + assert(_block_init); std::lock_guard lck(ctrlMtx); if (!running) { return; @@ -46,6 +50,7 @@ namespace dsp { } void tempStart() { + assert(_block_init); if (tempStopped) { doStart(); tempStopped = false; @@ -53,13 +58,17 @@ namespace dsp { } void tempStop() { + assert(_block_init); if (running && !tempStopped) { doStop(); tempStopped = true; } } - virtual int calcOutSize(int inSize) { return inSize; } + virtual int calcOutSize(int inSize) { + assert(_block_init); + return inSize; + } virtual int run() = 0; @@ -119,17 +128,18 @@ namespace dsp { } } + protected: + bool _block_init = false; + + std::mutex ctrlMtx; + std::vector inputs; std::vector outputs; bool running = false; bool tempStopped = false; - std::thread workerThread; - protected: - std::mutex ctrlMtx; - }; template @@ -138,10 +148,13 @@ namespace dsp { virtual void init() {} virtual ~generic_hier_block() { + if (!_block_init) { return; } stop(); + _block_init = false; } virtual void start() { + assert(_block_init); std::lock_guard lck(ctrlMtx); if (running) { return; @@ -151,6 +164,7 @@ namespace dsp { } virtual void stop() { + assert(_block_init); std::lock_guard lck(ctrlMtx); if (!running) { return; @@ -160,6 +174,7 @@ namespace dsp { } void tempStart() { + assert(_block_init); if (tempStopped) { doStart(); tempStopped = false; @@ -167,13 +182,17 @@ namespace dsp { } void tempStop() { + assert(_block_init); if (running && !tempStopped) { doStop(); tempStopped = true; } } - virtual int calcOutSize(int inSize) { return inSize; } + virtual int calcOutSize(int inSize) { + assert(_block_init); + return inSize; + } friend BLOCK; @@ -203,6 +222,7 @@ namespace dsp { bool running = false; protected: + bool _block_init = false; std::mutex ctrlMtx; }; diff --git a/core/src/dsp/buffer.h b/core/src/dsp/buffer.h index a43c48e8..8a3f33af 100644 --- a/core/src/dsp/buffer.h +++ b/core/src/dsp/buffer.h @@ -12,7 +12,11 @@ namespace dsp { RingBuffer(int maxLatency) { init(maxLatency); } - ~RingBuffer() { delete _buffer; } + ~RingBuffer() { + if (!_init) { return; } + delete _buffer; + _init = false; + } void init(int maxLatency) { size = RING_BUF_SZ; @@ -25,9 +29,11 @@ namespace dsp { readable = 0; writable = size; memset(_buffer, 0, size * sizeof(T)); + _init = true; } int read(T* data, int len) { + assert(_init); int dataRead = 0; int toRead = 0; while (dataRead < len) { @@ -57,6 +63,7 @@ namespace dsp { } int readAndSkip(T* data, int len, int skip) { + assert(_init); int dataRead = 0; int toRead = 0; while (dataRead < len) { @@ -102,6 +109,7 @@ namespace dsp { } int waitUntilReadable() { + assert(_init); if (_stopReader) { return -1; } int _r = getReadable(); if (_r != 0) { return _r; } @@ -112,6 +120,7 @@ namespace dsp { } int getReadable(bool lock = true) { + assert(_init); if (lock) { _readable_mtx.lock(); }; int _r = readable; if (lock) { _readable_mtx.unlock(); }; @@ -119,6 +128,7 @@ namespace dsp { } int write(T* data, int len) { + assert(_init); int dataWritten = 0; int toWrite = 0; while (dataWritten < len) { @@ -149,6 +159,7 @@ namespace dsp { } int waitUntilwritable() { + assert(_init); if (_stopWriter) { return -1; } int _w = getWritable(); if (_w != 0) { return _w; } @@ -159,6 +170,7 @@ namespace dsp { } int getWritable(bool lock = true) { + assert(_init); if (lock) { _writable_mtx.lock(); }; int _w = writable; if (lock) { _writable_mtx.unlock(); _readable_mtx.lock(); }; @@ -168,37 +180,45 @@ namespace dsp { } void stopReader() { + assert(_init); _stopReader = true; canReadVar.notify_one(); } void stopWriter() { + assert(_init); _stopWriter = true; canWriteVar.notify_one(); } bool getReadStop() { + assert(_init); return _stopReader; } bool getWriteStop() { + assert(_init); return _stopWriter; } void clearReadStop() { + assert(_init); _stopReader = false; } void clearWriteStop() { + assert(_init); _stopWriter = false; } void setMaxLatency(int maxLatency) { + assert(_init); this->maxLatency = maxLatency; } private: - T* _buffer = NULL; + bool _init = false; + T* _buffer; int size; int readc; int writec; @@ -231,9 +251,11 @@ namespace dsp { generic_block>::registerInput(in); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); diff --git a/core/src/dsp/clock_recovery.h b/core/src/dsp/clock_recovery.h index 5000fd9b..5189ec7e 100644 --- a/core/src/dsp/clock_recovery.h +++ b/core/src/dsp/clock_recovery.h @@ -7,6 +7,7 @@ namespace dsp { class EdgeTrigClockRecovery : public generic_block { public: EdgeTrigClockRecovery() {} + EdgeTrigClockRecovery(stream* in, int omega) { init(in, omega); } void init(stream* in, int omega) { @@ -14,9 +15,11 @@ namespace dsp { samplesPerSymbol = omega; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); generic_block::tempStop(); generic_block::unregisterInput(_in); _in = in; @@ -69,6 +72,7 @@ namespace dsp { class MMClockRecovery : public generic_block> { public: MMClockRecovery() {} + MMClockRecovery(stream* in, float omega, float gainOmega, float muGain, float omegaRelLimit) { init(in, omega, gainOmega, muGain, omegaRelLimit); } @@ -88,9 +92,11 @@ namespace dsp { generic_block>::registerInput(_in); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } void setOmega(float omega, float omegaRelLimit) { + assert(generic_block>::_block_init); generic_block>::tempStop(); omegaMin = _omega - (_omega * _omegaRelLimit); omegaMax = _omega + (_omega * _omegaRelLimit); @@ -100,6 +106,7 @@ namespace dsp { } void setGains(float omegaGain, float muGain) { + assert(generic_block>::_block_init); generic_block>::tempStop(); _gainOmega = omegaGain; _muGain = muGain; @@ -107,6 +114,7 @@ namespace dsp { } void setOmegaRelLimit(float omegaRelLimit) { + assert(generic_block>::_block_init); generic_block>::tempStop(); _omegaRelLimit = omegaRelLimit; omegaMin = _omega - (_omega * _omegaRelLimit); @@ -115,6 +123,7 @@ namespace dsp { } void setInput(stream* in) { + assert(generic_block>::_block_init); generic_block>::tempStop(); generic_block>::unregisterInput(_in); _in = in; diff --git a/core/src/dsp/convertion.h b/core/src/dsp/convertion.h index 008b17af..519d70dd 100644 --- a/core/src/dsp/convertion.h +++ b/core/src/dsp/convertion.h @@ -14,9 +14,11 @@ namespace dsp { _in = in; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -53,9 +55,11 @@ namespace dsp { _in = in; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -92,9 +96,11 @@ namespace dsp { _in = in; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -129,8 +135,10 @@ namespace dsp { RealToComplex(stream* in) { init(in); } ~RealToComplex() { + if (!generic_block::_block_init) { return; } generic_block::stop(); delete[] nullBuffer; + generic_block::_block_init = false; } void init(stream* in) { @@ -139,9 +147,11 @@ namespace dsp { memset(nullBuffer, 0, STREAM_BUFFER_SIZE * sizeof(float)); generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -179,9 +189,11 @@ namespace dsp { _in = in; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); diff --git a/core/src/dsp/deframing.h b/core/src/dsp/deframing.h index 1daa72d9..1a6f9362 100644 --- a/core/src/dsp/deframing.h +++ b/core/src/dsp/deframing.h @@ -13,7 +13,9 @@ namespace dsp { Deframer(stream* in, int frameLen, uint8_t* syncWord, int syncLen) { init(in, frameLen, syncWord, syncLen); } ~Deframer() { + if (!generic_block::_block_init) { return; } generic_block::stop(); + generic_block::_block_init = false; } void init(stream* in, int frameLen, uint8_t* syncWord, int syncLen) { @@ -29,6 +31,17 @@ namespace dsp { generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; + } + + void setInput(stream* in) { + assert(generic_block::_block_init); + 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() { @@ -148,6 +161,17 @@ namespace dsp { generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; + } + + void setInput(stream* in) { + assert(generic_block::_block_init); + 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() { @@ -226,6 +250,17 @@ namespace dsp { generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; + } + + void setInput(stream* in) { + assert(generic_block::_block_init); + 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() { @@ -296,6 +331,17 @@ namespace dsp { _inverted = inverted; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; + } + + void setInput(stream* in) { + assert(generic_block::_block_init); + 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() { @@ -337,6 +383,17 @@ namespace dsp { generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; + } + + void setInput(stream* in) { + assert(generic_block::_block_init); + 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() { diff --git a/core/src/dsp/demodulator.h b/core/src/dsp/demodulator.h index 3c5ea4c1..8a93af09 100644 --- a/core/src/dsp/demodulator.h +++ b/core/src/dsp/demodulator.h @@ -44,9 +44,11 @@ namespace dsp { phasorSpeed = (2 * FL_M_PI) / (_sampleRate / _deviation); generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -56,6 +58,7 @@ namespace dsp { } void setSampleRate(float sampleRate) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); _sampleRate = sampleRate; @@ -64,10 +67,12 @@ namespace dsp { } float getSampleRate() { + assert(generic_block::_block_init); return _sampleRate; } void setDeviation(float deviation) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); _deviation = deviation; @@ -76,6 +81,7 @@ namespace dsp { } float getDeviation() { + assert(generic_block::_block_init); return _deviation; } @@ -121,9 +127,11 @@ namespace dsp { phasorSpeed = (2 * FL_M_PI) / (_sampleRate / _deviation); generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -133,6 +141,7 @@ namespace dsp { } void setSampleRate(float sampleRate) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); _sampleRate = sampleRate; @@ -141,15 +150,18 @@ namespace dsp { } float getSampleRate() { + assert(generic_block::_block_init); return _sampleRate; } void setDeviation(float deviation) { + assert(generic_block::_block_init); _deviation = deviation; phasorSpeed = (2 * FL_M_PI) / (_sampleRate / _deviation); } float getDeviation() { + assert(generic_block::_block_init); return _deviation; } @@ -194,9 +206,11 @@ namespace dsp { _in = in; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -237,8 +251,10 @@ namespace dsp { SSBDemod(stream* in, float sampleRate, float bandWidth, int mode) { init(in, sampleRate, bandWidth, mode); } ~SSBDemod() { + if (!generic_block::_block_init) { return; } generic_block::stop(); delete[] buffer; + generic_block::_block_init = false; } enum { @@ -267,9 +283,11 @@ namespace dsp { buffer = new lv_32fc_t[STREAM_BUFFER_SIZE]; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -279,7 +297,7 @@ namespace dsp { } void setSampleRate(float sampleRate) { - // No need to restart + assert(generic_block::_block_init); _sampleRate = sampleRate; switch (_mode) { case MODE_USB: @@ -295,7 +313,7 @@ namespace dsp { } void setBandWidth(float bandWidth) { - // No need to restart + assert(generic_block::_block_init); _bandWidth = bandWidth; switch (_mode) { case MODE_USB: @@ -311,6 +329,7 @@ namespace dsp { } void setMode(int mode) { + assert(generic_block::_block_init); _mode = mode; switch (_mode) { case MODE_USB: @@ -352,6 +371,7 @@ namespace dsp { class MSKDemod : public generic_hier_block { public: MSKDemod() {} + MSKDemod(stream* input, float sampleRate, float deviation, float baudRate, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) { init(input, sampleRate, deviation, baudRate, omegaGain, muGain, omegaRelLimit); } @@ -370,9 +390,11 @@ namespace dsp { generic_hier_block::registerBlock(&demod); generic_hier_block::registerBlock(&recov); + generic_hier_block::_block_init = true; } void setSampleRate(float sampleRate) { + assert(generic_hier_block::_block_init); generic_hier_block::tempStop(); _sampleRate = sampleRate; demod.setSampleRate(_sampleRate); @@ -381,23 +403,27 @@ namespace dsp { } void setDeviation(float deviation) { + assert(generic_hier_block::_block_init); _deviation = deviation; demod.setDeviation(deviation); } void setBaudRate(float baudRate, float omegaRelLimit) { + assert(generic_hier_block::_block_init); _baudRate = baudRate; _omegaRelLimit = omegaRelLimit; recov.setOmega(_sampleRate / _baudRate, _omegaRelLimit); } void setMMGains(float omegaGain, float myGain) { + assert(generic_hier_block::_block_init); _omegaGain = omegaGain; _muGain = myGain; recov.setGains(_omegaGain, _muGain); } void setOmegaRelLimit(float omegaRelLimit) { + assert(generic_hier_block::_block_init); _omegaRelLimit = omegaRelLimit; recov.setOmegaRelLimit(_omegaRelLimit); } @@ -420,6 +446,7 @@ namespace dsp { class PSKDemod : public generic_hier_block> { public: PSKDemod() {} + PSKDemod(stream* input, float sampleRate, float baudRate, int RRCTapCount = 31, float RRCAlpha = 0.32f, float agcRate = 10e-4, float costasLoopBw = 0.004f, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) { init(input, sampleRate, baudRate, RRCTapCount, RRCAlpha, agcRate, costasLoopBw, omegaGain, muGain, omegaRelLimit); } @@ -456,13 +483,17 @@ namespace dsp { generic_hier_block>::registerBlock(&recov); out = &recov.out; + + generic_hier_block>::_block_init = true; } void setInput(stream* input) { + assert((generic_hier_block>::_block_init)); agc.setInput(input); } void setSampleRate(float sampleRate) { + assert((generic_hier_block>::_block_init)); _sampleRate = sampleRate; rrc.tempStop(); recov.tempStop(); @@ -474,6 +505,7 @@ namespace dsp { } void setBaudRate(float baudRate) { + assert((generic_hier_block>::_block_init)); _baudRate = baudRate; rrc.tempStop(); recov.tempStop(); @@ -485,6 +517,7 @@ namespace dsp { } void setRRCParams(int RRCTapCount, float RRCAlpha) { + assert((generic_hier_block>::_block_init)); _RRCTapCount = RRCTapCount; _RRCAlpha = RRCAlpha; taps.setTapCount(_RRCTapCount); @@ -493,22 +526,26 @@ namespace dsp { } void setAgcRate(float agcRate) { + assert((generic_hier_block>::_block_init)); _agcRate = agcRate; agc.setRate(_agcRate); } void setCostasLoopBw(float costasLoopBw) { + assert((generic_hier_block>::_block_init)); _costasLoopBw = costasLoopBw; demod.setLoopBandwidth(_costasLoopBw); } void setMMGains(float omegaGain, float myGain) { + assert((generic_hier_block>::_block_init)); _omegaGain = omegaGain; _muGain = myGain; recov.setGains(_omegaGain, _muGain); } void setOmegaRelLimit(float omegaRelLimit) { + assert((generic_hier_block>::_block_init)); _omegaRelLimit = omegaRelLimit; recov.setOmegaRelLimit(_omegaRelLimit); } @@ -537,6 +574,7 @@ namespace dsp { class PMDemod : public generic_hier_block { public: PMDemod() {} + PMDemod(stream* input, float sampleRate, float baudRate, float agcRate = 0.02e-3f, float pllLoopBandwidth = (0.06f*0.06f) / 4.0f, int rrcTapCount = 31, float rrcAlpha = 0.6f, float omegaGain = (0.01*0.01) / 4, float muGain = 0.01f, float omegaRelLimit = 0.005f) { init(input, sampleRate, baudRate, agcRate, pllLoopBandwidth, rrcTapCount, rrcAlpha, omegaGain, muGain, omegaRelLimit); } @@ -564,23 +602,28 @@ namespace dsp { generic_hier_block::registerBlock(&pll); generic_hier_block::registerBlock(&rrc); generic_hier_block::registerBlock(&recov); + generic_hier_block::_block_init = true; } void setInput(stream* input) { + assert(generic_hier_block::_block_init); agc.setInput(input); } void setAgcRate(float agcRate) { + assert(generic_hier_block::_block_init); _agcRate = agcRate; agc.setRate(_agcRate); } void setPllLoopBandwidth(float pllLoopBandwidth) { + assert(generic_hier_block::_block_init); _pllLoopBandwidth = pllLoopBandwidth; pll.setLoopBandwidth(_pllLoopBandwidth); } void setRRCParams(int rrcTapCount, float rrcAlpha) { + assert(generic_hier_block::_block_init); _rrcTapCount = rrcTapCount; _rrcAlpha = rrcAlpha; rrcwin.setTapCount(_rrcTapCount); @@ -589,12 +632,14 @@ namespace dsp { } void setMMGains(float omegaGain, float muGain) { + assert(generic_hier_block::_block_init); _omegaGain = omegaGain; _muGain = muGain; recov.setGains(_omegaGain, _muGain); } void setOmegaRelLimit(float omegaRelLimit) { + assert(generic_hier_block::_block_init); _omegaRelLimit = omegaRelLimit; recov.setOmegaRelLimit(_omegaRelLimit); } diff --git a/core/src/dsp/falcon_fec.h b/core/src/dsp/falcon_fec.h index 560895d6..8c579735 100644 --- a/core/src/dsp/falcon_fec.h +++ b/core/src/dsp/falcon_fec.h @@ -62,10 +62,6 @@ namespace dsp { FalconRS(stream* in) { init(in); } - ~FalconRS() { - generic_block::stop(); - } - void init(stream* in) { _in = in; @@ -76,6 +72,17 @@ namespace dsp { generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; + } + + void setInput(stream* in) { + assert(generic_block::_block_init); + 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() { diff --git a/core/src/dsp/falcon_packet.h b/core/src/dsp/falcon_packet.h index e1d7aca9..c4cdce5e 100644 --- a/core/src/dsp/falcon_packet.h +++ b/core/src/dsp/falcon_packet.h @@ -14,15 +14,22 @@ namespace dsp { FalconPacketSync(stream* in) { init(in); } - ~FalconPacketSync() { - generic_block::stop(); - } - void init(stream* in) { _in = in; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; + } + + void setInput(stream* in) { + assert(generic_block::_block_init); + 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() { diff --git a/core/src/dsp/filter.h b/core/src/dsp/filter.h index e02bd71e..404e9107 100644 --- a/core/src/dsp/filter.h +++ b/core/src/dsp/filter.h @@ -13,9 +13,11 @@ namespace dsp { FIR(stream* in, dsp::filter_window::generic_window* window) { init(in, window); } ~FIR() { + if (!generic_block>::_block_init) { return; } generic_block>::stop(); volk_free(buffer); volk_free(taps); + generic_block>::_block_init = false; } void init(stream* in, dsp::filter_window::generic_window* window) { @@ -29,9 +31,11 @@ namespace dsp { bufStart = &buffer[tapCount]; generic_block>::registerInput(_in); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); @@ -41,6 +45,7 @@ namespace dsp { } void updateWindow(dsp::filter_window::generic_window* window) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); _window = window; volk_free(taps); @@ -99,8 +104,6 @@ namespace dsp { BFMDeemp(stream* in, float sampleRate, float tau) { init(in, sampleRate, tau); } - ~BFMDeemp() { generic_block::stop(); } - void init(stream* in, float sampleRate, float tau) { _in = in; _sampleRate = sampleRate; @@ -109,9 +112,11 @@ namespace dsp { alpha = dt / (_tau + dt); generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -121,12 +126,14 @@ namespace dsp { } void setSampleRate(float sampleRate) { + assert(generic_block::_block_init); _sampleRate = sampleRate; float dt = 1.0f / _sampleRate; alpha = dt / (_tau + dt); } void setTau(float tau) { + assert(generic_block::_block_init); _tau = tau; float dt = 1.0f / _sampleRate; alpha = dt / (_tau + dt); diff --git a/core/src/dsp/math.h b/core/src/dsp/math.h index 62241d1f..f4b07c4a 100644 --- a/core/src/dsp/math.h +++ b/core/src/dsp/math.h @@ -16,6 +16,40 @@ namespace dsp { generic_block>::registerInput(a); generic_block>::registerInput(b); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; + } + + void setInputs(stream* a, stream* b) { + assert(generic_block>::_block_init); + std::lock_guard lck(generic_block>::ctrlMtx); + generic_block>::tempStop(); + generic_block>::unregisterInput(_a); + generic_block>::unregisterInput(_b); + _a = a; + _b = b; + generic_block>::registerInput(_a); + generic_block>::registerInput(_b); + generic_block>::tempStart(); + } + + void setInputA(stream* a) { + assert(generic_block>::_block_init); + std::lock_guard lck(generic_block>::ctrlMtx); + generic_block>::tempStop(); + generic_block>::unregisterInput(_a); + _a = a; + generic_block>::registerInput(_a); + generic_block>::tempStart(); + } + + void setInputB(stream* b) { + assert(generic_block>::_block_init); + std::lock_guard lck(generic_block>::ctrlMtx); + generic_block>::tempStop(); + generic_block>::unregisterInput(_b); + _b = b; + generic_block>::registerInput(_b); + generic_block>::tempStart(); } int run() { @@ -51,18 +85,52 @@ namespace dsp { }; template - class Substract : public generic_block> { + class Subtract : public generic_block> { public: - Substract() {} + Subtract() {} - Substract(stream* a, stream* b) { init(a, b); } + Subtract(stream* a, stream* b) { init(a, b); } void init(stream* a, stream* b) { _a = a; _b = b; - generic_block>::registerInput(a); - generic_block>::registerInput(b); - generic_block>::registerOutput(&out); + generic_block>::registerInput(a); + generic_block>::registerInput(b); + generic_block>::registerOutput(&out); + generic_block>::_block_init = true; + } + + void setInputs(stream* a, stream* b) { + assert(generic_block>::_block_init); + std::lock_guard lck(generic_block>::ctrlMtx); + generic_block>::tempStop(); + generic_block>::unregisterInput(_a); + generic_block>::unregisterInput(_b); + _a = a; + _b = b; + generic_block>::registerInput(_a); + generic_block>::registerInput(_b); + generic_block>::tempStart(); + } + + void setInputA(stream* a) { + assert(generic_block>::_block_init); + std::lock_guard lck(generic_block>::ctrlMtx); + generic_block>::tempStop(); + generic_block>::unregisterInput(_a); + _a = a; + generic_block>::registerInput(_a); + generic_block>::tempStart(); + } + + void setInputB(stream* b) { + assert(generic_block>::_block_init); + std::lock_guard lck(generic_block>::ctrlMtx); + generic_block>::tempStop(); + generic_block>::unregisterInput(_b); + _b = b; + generic_block>::registerInput(_b); + generic_block>::tempStart(); } int run() { @@ -110,6 +178,40 @@ namespace dsp { generic_block::registerInput(a); generic_block::registerInput(b); generic_block::registerOutput(&out); + generic_block::_block_init = true; + } + + void setInputs(stream* a, stream* b) { + assert(generic_block>::_block_init); + std::lock_guard lck(generic_block>::ctrlMtx); + generic_block>::tempStop(); + generic_block>::unregisterInput(_a); + generic_block>::unregisterInput(_b); + _a = a; + _b = b; + generic_block>::registerInput(_a); + generic_block>::registerInput(_b); + generic_block>::tempStart(); + } + + void setInputA(stream* a) { + assert(generic_block>::_block_init); + std::lock_guard lck(generic_block>::ctrlMtx); + generic_block>::tempStop(); + generic_block>::unregisterInput(_a); + _a = a; + generic_block>::registerInput(_a); + generic_block>::tempStart(); + } + + void setInputB(stream* b) { + assert(generic_block>::_block_init); + std::lock_guard lck(generic_block>::ctrlMtx); + generic_block>::tempStop(); + generic_block>::unregisterInput(_b); + _b = b; + generic_block>::registerInput(_b); + generic_block>::tempStart(); } int run() { diff --git a/core/src/dsp/measure.h b/core/src/dsp/measure.h index 2abe7557..fa1a8721 100644 --- a/core/src/dsp/measure.h +++ b/core/src/dsp/measure.h @@ -16,9 +16,11 @@ namespace dsp { void init(stream* in) { _in = in; generic_block::registerInput(_in); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); diff --git a/core/src/dsp/meteor/hrpt.h b/core/src/dsp/meteor/hrpt.h index 33ca08c4..74c14064 100644 --- a/core/src/dsp/meteor/hrpt.h +++ b/core/src/dsp/meteor/hrpt.h @@ -18,9 +18,11 @@ namespace dsp { generic_block::registerOutput(&SSPDOut); generic_block::registerOutput(&MTVZAOut); generic_block::registerOutput(&MSUMROut); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); diff --git a/core/src/dsp/meteor/msumr.h b/core/src/dsp/meteor/msumr.h index 10195adf..d05e4925 100644 --- a/core/src/dsp/meteor/msumr.h +++ b/core/src/dsp/meteor/msumr.h @@ -22,9 +22,11 @@ namespace dsp { generic_block::registerOutput(&msumr4Out); generic_block::registerOutput(&msumr5Out); generic_block::registerOutput(&msumr6Out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); diff --git a/core/src/dsp/noaa/hrpt.h b/core/src/dsp/noaa/hrpt.h index e28ba3db..4c4a4ae8 100644 --- a/core/src/dsp/noaa/hrpt.h +++ b/core/src/dsp/noaa/hrpt.h @@ -31,9 +31,11 @@ namespace dsp { generic_block::registerOutput(&AVHRRChan3Out); generic_block::registerOutput(&AVHRRChan4Out); generic_block::registerOutput(&AVHRRChan5Out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); diff --git a/core/src/dsp/noaa/tip.h b/core/src/dsp/noaa/tip.h index c7bf7e18..6467a095 100644 --- a/core/src/dsp/noaa/tip.h +++ b/core/src/dsp/noaa/tip.h @@ -17,9 +17,11 @@ namespace dsp { generic_block::registerOutput(&SEMOut); generic_block::registerOutput(&DCSOut); generic_block::registerOutput(&SBUVOut); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); diff --git a/core/src/dsp/pll.h b/core/src/dsp/pll.h index 025657af..5ab22b1e 100644 --- a/core/src/dsp/pll.h +++ b/core/src/dsp/pll.h @@ -10,6 +10,7 @@ namespace dsp { class CostasLoop: public generic_block> { public: CostasLoop() {} + CostasLoop(stream* in, float loopBandwidth) { init(in, loopBandwidth); } void init(stream* in, float loopBandwidth) { @@ -25,9 +26,11 @@ namespace dsp { generic_block>::registerInput(_in); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); generic_block>::tempStop(); generic_block>::unregisterInput(_in); _in = in; @@ -36,6 +39,7 @@ namespace dsp { } void setLoopBandwidth(float loopBandwidth) { + assert(generic_block>::_block_init); generic_block>::tempStop(); _loopBandwidth = loopBandwidth; float dampningFactor = sqrtf(2.0f) / 2.0f; @@ -121,6 +125,7 @@ namespace dsp { class CarrierTrackingPLL: public generic_block> { public: CarrierTrackingPLL() {} + CarrierTrackingPLL(stream* in, float loopBandwidth) { init(in, loopBandwidth); } void init(stream* in, float loopBandwidth) { @@ -136,9 +141,11 @@ namespace dsp { generic_block>::registerInput(_in); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); generic_block>::tempStop(); generic_block>::unregisterInput(_in); _in = in; @@ -147,6 +154,7 @@ namespace dsp { } void setLoopBandwidth(float loopBandwidth) { + assert(generic_block>::_block_init); generic_block>::tempStop(); _loopBandwidth = loopBandwidth; float dampningFactor = sqrtf(2.0f) / 2.0f; @@ -224,6 +232,7 @@ namespace dsp { class PLL: public generic_block { public: PLL() {} + PLL(stream* in, float loopBandwidth) { init(in, loopBandwidth); } void init(stream* in, float loopBandwidth) { @@ -239,9 +248,11 @@ namespace dsp { generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); generic_block::tempStop(); generic_block::unregisterInput(_in); _in = in; @@ -250,6 +261,7 @@ namespace dsp { } void setLoopBandwidth(float loopBandwidth) { + assert(generic_block::_block_init); generic_block::tempStop(); _loopBandwidth = loopBandwidth; float dampningFactor = sqrtf(2.0f) / 2.0f; diff --git a/core/src/dsp/processing.h b/core/src/dsp/processing.h index 87e95734..f8c23047 100644 --- a/core/src/dsp/processing.h +++ b/core/src/dsp/processing.h @@ -22,9 +22,11 @@ namespace dsp { phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI)); generic_block>::registerInput(_in); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } - void setInputSize(stream* in) { + void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); @@ -34,22 +36,24 @@ namespace dsp { } void setSampleRate(float sampleRate) { - // No need to restart + assert(generic_block>::_block_init); _sampleRate = sampleRate; phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI)); } float getSampleRate() { + assert(generic_block>::_block_init); return _sampleRate; } void setFrequency(float freq) { - // No need to restart + assert(generic_block>::_block_init); _freq = freq; phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI)); } float getFrequency() { + assert(generic_block>::_block_init); return _freq; } @@ -94,9 +98,11 @@ namespace dsp { _CorrectedFallRate = _fallRate / _sampleRate; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -106,12 +112,14 @@ namespace dsp { } void setSampleRate(float sampleRate) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); _sampleRate = sampleRate; _CorrectedFallRate = _fallRate / _sampleRate; } void setFallRate(float fallRate) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); _fallRate = fallRate; _CorrectedFallRate = _fallRate / _sampleRate; @@ -160,9 +168,11 @@ namespace dsp { _rate = rate; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -172,14 +182,17 @@ namespace dsp { } void setSetPoint(float setPoint) { + assert(generic_block::_block_init); _setPoint = setPoint; } void setMaxGain(float maxGain) { + assert(generic_block::_block_init); _maxGain = maxGain; } void setRate(float rate) { + assert(generic_block::_block_init); _rate = rate; } @@ -222,9 +235,11 @@ namespace dsp { _in = in; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -272,9 +287,11 @@ namespace dsp { _volume = volume; generic_block>::registerInput(_in); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } - void setInputSize(stream* in) { + void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); @@ -284,19 +301,23 @@ namespace dsp { } void setVolume(float volume) { + assert(generic_block>::_block_init); _volume = volume; level = powf(_volume, 2); } float getVolume() { + assert(generic_block>::_block_init); return _volume; } void setMuted(bool muted) { + assert(generic_block>::_block_init); _muted = muted; } bool getMuted() { + assert(generic_block>::_block_init); return _muted; } @@ -343,8 +364,10 @@ namespace dsp { Squelch(stream* in, float level) { init(in, level); } ~Squelch() { + if (!generic_block::_block_init) { return; } generic_block::stop(); delete[] normBuffer; + generic_block::_block_init = false; } void init(stream* in, float level) { @@ -353,9 +376,11 @@ namespace dsp { normBuffer = new float[STREAM_BUFFER_SIZE]; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -365,10 +390,12 @@ namespace dsp { } void setLevel(float level) { + assert(generic_block::_block_init); _level = level; } float getLevel() { + assert(generic_block::_block_init); return _level; } @@ -415,9 +442,11 @@ namespace dsp { samples = count; generic_block>::registerInput(_in); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); @@ -427,6 +456,7 @@ namespace dsp { } void setSampleCount(int count) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); samples = count; @@ -473,8 +503,10 @@ namespace dsp { Threshold(stream* in) { init(in); } ~Threshold() { + if (!generic_block::_block_init) { return; } generic_block::stop(); delete[] normBuffer; + generic_block::_block_init = false; } void init(stream* in) { @@ -482,9 +514,11 @@ namespace dsp { normBuffer = new float[STREAM_BUFFER_SIZE]; generic_block::registerInput(_in); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -494,10 +528,12 @@ namespace dsp { } void setLevel(float level) { + assert(generic_block::_block_init); _level = level; } float getLevel() { + assert(generic_block::_block_init); return _level; } diff --git a/core/src/dsp/resampling.h b/core/src/dsp/resampling.h index bd761e6c..bf8edc24 100644 --- a/core/src/dsp/resampling.h +++ b/core/src/dsp/resampling.h @@ -14,10 +14,12 @@ namespace dsp { PolyphaseResampler(stream* in, dsp::filter_window::generic_window* window, float inSampleRate, float outSampleRate) { init(in, window, inSampleRate, outSampleRate); } ~PolyphaseResampler() { + if (!generic_block>::_block_init) { return; } generic_block>::stop(); volk_free(buffer); volk_free(taps); freeTapPhases(); + generic_block>::_block_init = false; } void init(stream* in, dsp::filter_window::generic_window* window, float inSampleRate, float outSampleRate) { @@ -40,9 +42,11 @@ namespace dsp { memset(buffer, 0, STREAM_BUFFER_SIZE * sizeof(T) * 2); generic_block>::registerInput(_in); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); @@ -52,6 +56,7 @@ namespace dsp { } void setInSampleRate(float inSampleRate) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); _inSampleRate = inSampleRate; @@ -63,6 +68,7 @@ namespace dsp { } void setOutSampleRate(float outSampleRate) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); _outSampleRate = outSampleRate; @@ -74,14 +80,17 @@ namespace dsp { } int getInterpolation() { + assert(generic_block>::_block_init); return _interp; } int getDecimation() { + assert(generic_block>::_block_init); return _decim; } void updateWindow(dsp::filter_window::generic_window* window) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); _window = window; @@ -94,6 +103,7 @@ namespace dsp { } int calcOutSize(int in) { + assert(generic_block>::_block_init); return (in * _interp) / _decim; } @@ -187,87 +197,4 @@ namespace dsp { std::vector tapPhases; }; - - class PowerDecimator : public generic_block { - public: - PowerDecimator() {} - - PowerDecimator(stream* in, int power, int tapCount) { init(in, power, tapCount); } - - void init(stream* in, int power, int tapCount) { - _in = in; - _power = power; - _tapCount = tapCount; - - // Allocate buffers - for (int i = 0; i < _power; i++) { - buffers[i] = new complex_t[STREAM_BUFFER_SIZE / (i+1)]; - bufferStart[i] = &buffers[i][_tapCount - 1]; - } - - // Create taps - genHalfbandTaps(); - - generic_block::registerInput(_in); - generic_block::registerOutput(&out); - } - - 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 setPower(unsigned int power) { - std::lock_guard lck(generic_block::ctrlMtx); - generic_block::tempStop(); - _power = power; - generic_block::tempStart(); - } - - int run() { - int count = _in->read(); - if (count < 0) { return -1; } - - if (!out.swap(count)) { return -1; } - return count; - } - - stream out; - - private: - void genHalfbandTaps() { - if (taps != NULL) { delete[] taps; } - taps = new float[_tapCount]; - - // Using Blackman-harris windows - int half = _tapCount / 2; - for (int i = 0; i < _tapCount; i++) { - taps[i] = sinc((FL_M_PI / 2.0f) * (i-half)) * blackmanHarrisWin(i, _tapCount - 1); - printf("%f\n", taps[i]); - } - } - - inline float sinc(float x) { - return ((x == 0) ? 1.0f : (sinf(x)/x)) / FL_M_PI; - } - - inline float blackmanHarrisWin(float n, float N) { - return 0.35875f - (0.48829f*cosf(2.0f*FL_M_PI*(n/N))) + (0.14128f*cosf(4.0f*FL_M_PI*(n/N))) - (0.01168f*cosf(6.0f*FL_M_PI*(n/N))); - } - - int _power = 0; - int _tapCount = 31; - stream* _in; - - // Buffer lists, sets max decimation to 2^32 - complex_t* buffers[32]; - complex_t* bufferStart[32]; - - float* taps = NULL; - - }; } \ No newline at end of file diff --git a/core/src/dsp/routing.h b/core/src/dsp/routing.h index 8d8f5fad..3e57160b 100644 --- a/core/src/dsp/routing.h +++ b/core/src/dsp/routing.h @@ -16,9 +16,11 @@ namespace dsp { void init(stream* in) { _in = in; generic_block::registerInput(_in); + generic_block::_block_init = true; } void setInput(stream* in) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterInput(_in); @@ -28,6 +30,7 @@ namespace dsp { } void bindStream(stream* stream) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); out.push_back(stream); @@ -36,6 +39,7 @@ namespace dsp { } void unbindStream(stream* stream) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); generic_block::unregisterOutput(stream); @@ -71,6 +75,7 @@ namespace dsp { Reshaper(stream* in, int keep, int skip) { init(in, keep, skip); } // NOTE: For some reason, the base class destrcutor doesn't get called.... this is a temporary fix I guess + // I also don't check for _block_init for the exact sample reason, something's weird ~Reshaper() { generic_block>::stop(); } @@ -82,9 +87,11 @@ namespace dsp { ringBuf.init(keep * 2); generic_block>::registerInput(_in); generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); @@ -94,6 +101,7 @@ namespace dsp { } void setKeep(int keep) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); _keep = keep; @@ -102,6 +110,7 @@ namespace dsp { } void setSkip(int skip) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); _skip = skip; @@ -182,7 +191,6 @@ namespace dsp { std::thread bufferWorkerThread; std::thread workThread; int _keep, _skip; - }; } \ No newline at end of file diff --git a/core/src/dsp/sink.h b/core/src/dsp/sink.h index 97299dc6..cb97402a 100644 --- a/core/src/dsp/sink.h +++ b/core/src/dsp/sink.h @@ -16,9 +16,11 @@ namespace dsp { _handler = handler; _ctx = ctx; generic_block>::registerInput(_in); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); @@ -28,6 +30,7 @@ namespace dsp { } void setHandler(void (*handler)(T* data, int count, void* ctx), void* ctx) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); _handler = handler; @@ -61,9 +64,11 @@ namespace dsp { _in = in; data.init(480); // TODO: Use an argument generic_block>::registerInput(_in); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); @@ -107,9 +112,11 @@ namespace dsp { void init(stream* in) { _in = in; generic_block>::registerInput(_in); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); @@ -138,17 +145,21 @@ namespace dsp { FileSink(stream* in, std::string path) { init(in, path); } ~FileSink() { + if (!generic_block>::_block_init) { return; } generic_block>::stop(); if (file.is_open()) { file.close(); } + generic_block>::_block_init = false; } void init(stream* in, std::string path) { _in = in; file = std::ofstream(path, std::ios::binary); generic_block>::registerInput(_in); + generic_block>::_block_init = true; } void setInput(stream* in) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); generic_block>::unregisterInput(_in); @@ -158,6 +169,7 @@ namespace dsp { } bool isOpen() { + assert(generic_block>::_block_init); return file.is_open(); } diff --git a/core/src/dsp/source.h b/core/src/dsp/source.h index 78c2d2ac..535544d3 100644 --- a/core/src/dsp/source.h +++ b/core/src/dsp/source.h @@ -20,9 +20,11 @@ namespace dsp { phase = lv_cmake(1.0f, 0.0f); phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI)); generic_block::registerOutput(&out); + generic_block::_block_init = true; } void setBlockSize(int blockSize) { + assert(generic_block::_block_init); std::lock_guard lck(generic_block::ctrlMtx); generic_block::tempStop(); _blockSize = blockSize; @@ -30,26 +32,29 @@ namespace dsp { } int getBlockSize() { + assert(generic_block::_block_init); return _blockSize; } void setSampleRate(float sampleRate) { - // No need to restart + assert(generic_block::_block_init); _sampleRate = sampleRate; phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI)); } float getSampleRate() { + assert(generic_block::_block_init); return _sampleRate; } void setFrequency(float freq) { - // No need to restart + assert(generic_block::_block_init); _freq = freq; phaseDelta = lv_cmake(std::cos((_freq / _sampleRate) * 2.0f * FL_M_PI), std::sin((_freq / _sampleRate) * 2.0f * FL_M_PI)); } float getFrequency() { + assert(generic_block::_block_init); return _freq; } @@ -82,9 +87,11 @@ namespace dsp { _handler = handler; _ctx = ctx; generic_block>::registerOutput(&out); + generic_block>::_block_init = true; } void setHandler(int (*handler)(T* data, void* ctx), void* ctx) { + assert(generic_block>::_block_init); std::lock_guard lck(generic_block>::ctrlMtx); generic_block>::tempStop(); _handler = handler; diff --git a/core/src/dsp/vfo.h b/core/src/dsp/vfo.h index 251bc3d9..4edb56bd 100644 --- a/core/src/dsp/vfo.h +++ b/core/src/dsp/vfo.h @@ -10,7 +10,11 @@ namespace dsp { public: VFO() {} - ~VFO() { stop(); } + ~VFO() { + if (!_init) { return; } + stop(); + _init = false; + } VFO(stream* in, float offset, float inSampleRate, float outSampleRate, float bandWidth) { init(in, offset, inSampleRate, outSampleRate, bandWidth); @@ -33,21 +37,26 @@ namespace dsp { resamp.updateWindow(&win); out = &resamp.out; + + _init = true; } void start() { + assert(_init); if (running) { return; } xlator.start(); resamp.start(); } void stop() { + assert(_init); if (!running) { return; } xlator.stop(); resamp.stop(); } void setInSampleRate(float inSampleRate) { + assert(_init); _inSampleRate = inSampleRate; if (running) { xlator.stop(); resamp.stop(); } xlator.setSampleRate(_inSampleRate); @@ -61,6 +70,7 @@ namespace dsp { } void setOutSampleRate(float outSampleRate) { + assert(_init); _outSampleRate = outSampleRate; if (running) { resamp.stop(); } resamp.setOutSampleRate(_outSampleRate); @@ -73,6 +83,7 @@ namespace dsp { } void setOutSampleRate(float outSampleRate, float bandWidth) { + assert(_init); _outSampleRate = outSampleRate; _bandWidth = bandWidth; if (running) { resamp.stop(); } @@ -86,11 +97,13 @@ namespace dsp { } void setOffset(float offset) { + assert(_init); _offset = offset; xlator.setFrequency(-_offset); } void setBandwidth(float bandWidth) { + assert(_init); _bandWidth = bandWidth; float realCutoff = std::min(_bandWidth, std::min(_inSampleRate, _outSampleRate)) / 2.0f; win.setCutoff(realCutoff); @@ -101,6 +114,7 @@ namespace dsp { stream* out; private: + bool _init = false; bool running = false; float _offset, _inSampleRate, _outSampleRate, _bandWidth; filter_window::BlackmanWindow win; diff --git a/recorder/src/main.cpp b/recorder/src/main.cpp index ecc55472..873e7c36 100644 --- a/recorder/src/main.cpp +++ b/recorder/src/main.cpp @@ -159,7 +159,7 @@ private: audioInput = sigpath::sinkManager.bindStream(name); if (audioInput == NULL) { return; } selectedStreamName = name; - vol.setInputSize(audioInput); + vol.setInput(audioInput); vol.start(); }