2020-12-28 14:39:30 +01:00
|
|
|
#pragma once
|
|
|
|
#include <dsp/block.h>
|
|
|
|
#include <fftw3.h>
|
|
|
|
#include <volk/volk.h>
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <dsp/types.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
namespace dsp {
|
2021-02-06 21:28:27 +01:00
|
|
|
class LevelMeter : public generic_block<LevelMeter> {
|
2020-12-28 14:39:30 +01:00
|
|
|
public:
|
2021-02-06 21:28:27 +01:00
|
|
|
LevelMeter() {}
|
2020-12-28 14:39:30 +01:00
|
|
|
|
2021-02-06 21:28:27 +01:00
|
|
|
LevelMeter(stream<stereo_t>* in) { init(in); }
|
2020-12-28 14:39:30 +01:00
|
|
|
|
|
|
|
void init(stream<stereo_t>* in) {
|
|
|
|
_in = in;
|
2021-02-06 21:28:27 +01:00
|
|
|
generic_block<LevelMeter>::registerInput(_in);
|
2021-07-12 05:03:51 +02:00
|
|
|
generic_block<LevelMeter>::_block_init = true;
|
2020-12-28 14:39:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setInput(stream<stereo_t>* in) {
|
2021-07-12 05:03:51 +02:00
|
|
|
assert(generic_block<LevelMeter>::_block_init);
|
2021-02-06 21:28:27 +01:00
|
|
|
std::lock_guard<std::mutex> lck(generic_block<LevelMeter>::ctrlMtx);
|
|
|
|
generic_block<LevelMeter>::tempStop();
|
|
|
|
generic_block<LevelMeter>::unregisterInput(_in);
|
2020-12-28 14:39:30 +01:00
|
|
|
_in = in;
|
2021-02-06 21:28:27 +01:00
|
|
|
generic_block<LevelMeter>::registerInput(_in);
|
|
|
|
generic_block<LevelMeter>::tempStart();
|
2020-12-28 14:39:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int run() {
|
2021-03-29 21:53:43 +02:00
|
|
|
int count = _in->read();
|
2020-12-28 14:39:30 +01:00
|
|
|
if (count < 0) { return -1; }
|
|
|
|
|
2021-02-06 21:28:27 +01:00
|
|
|
float maxL = 0, maxR = 0;
|
2021-03-29 21:53:43 +02:00
|
|
|
float absL, absR;
|
2021-02-06 21:28:27 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2021-03-29 21:53:43 +02:00
|
|
|
absL = fabs(_in->readBuf[i].l);
|
|
|
|
absR = fabs(_in->readBuf[i].r);
|
2021-02-06 21:28:27 +01:00
|
|
|
if (absL > maxL) { maxL = absL; }
|
|
|
|
if (absR > maxR) { maxR = absR; }
|
|
|
|
}
|
2020-12-28 14:39:30 +01:00
|
|
|
|
|
|
|
_in->flush();
|
|
|
|
|
2021-02-06 21:28:27 +01:00
|
|
|
float _lvlL = 10.0f * logf(maxL);
|
2021-02-20 15:27:43 +01:00
|
|
|
float _lvlR = 10.0f * logf(maxR);
|
2021-02-06 21:28:27 +01:00
|
|
|
|
|
|
|
// Update max values
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lck(lvlMtx);
|
|
|
|
if (_lvlL > lvlL) { lvlL = _lvlL; }
|
|
|
|
if (_lvlR > lvlR) { lvlR = _lvlR; }
|
2020-12-28 14:39:30 +01:00
|
|
|
}
|
2021-02-06 21:28:27 +01:00
|
|
|
|
2020-12-28 14:39:30 +01:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2021-02-06 21:28:27 +01:00
|
|
|
float getLeftLevel() {
|
|
|
|
std::lock_guard<std::mutex> lck(lvlMtx);
|
|
|
|
float ret = lvlL;
|
|
|
|
lvlL = -90.0f;
|
|
|
|
return ret;
|
|
|
|
}
|
2020-12-28 14:39:30 +01:00
|
|
|
|
2021-02-06 21:28:27 +01:00
|
|
|
float getRightLevel() {
|
|
|
|
std::lock_guard<std::mutex> lck(lvlMtx);
|
|
|
|
float ret = lvlR;
|
|
|
|
lvlR = -90.0f;
|
|
|
|
return ret;
|
|
|
|
}
|
2020-12-28 14:39:30 +01:00
|
|
|
|
|
|
|
private:
|
2021-02-06 21:28:27 +01:00
|
|
|
float lvlL = -90.0f;
|
|
|
|
float lvlR = -90.0f;
|
2020-12-28 14:39:30 +01:00
|
|
|
stream<stereo_t>* _in;
|
2021-02-06 21:28:27 +01:00
|
|
|
std::mutex lvlMtx;
|
2020-12-28 14:39:30 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
}
|