#pragma once #include #include #include #include #include #include namespace dsp { class LevelMeter : public generic_block { public: LevelMeter() {} LevelMeter(stream* in) { init(in); } 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() { int count = _in->read(); if (count < 0) { return -1; } float maxL = 0, maxR = 0; float absL, absR; for (int i = 0; i < count; i++) { absL = fabs(_in->readBuf[i].l); absR = fabs(_in->readBuf[i].r); if (absL > maxL) { maxL = absL; } if (absR > maxR) { maxR = absR; } } _in->flush(); float _lvlL = 10.0f * logf(maxL); float _lvlR = 10.0f * logf(maxR); // Update max values { std::lock_guard lck(lvlMtx); if (_lvlL > lvlL) { lvlL = _lvlL; } if (_lvlR > lvlR) { lvlR = _lvlR; } } return count; } float getLeftLevel() { std::lock_guard lck(lvlMtx); float ret = lvlL; lvlL = -90.0f; return ret; } float getRightLevel() { std::lock_guard lck(lvlMtx); float ret = lvlR; lvlR = -90.0f; return ret; } private: float lvlL = -90.0f; float lvlR = -90.0f; stream* _in; std::mutex lvlMtx; }; }