-
Notifications
You must be signed in to change notification settings - Fork 1
/
avhistogram.cpp
76 lines (61 loc) · 1.84 KB
/
avhistogram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "avhistogram.h"
#include "avfile.h"
#include "math.h"
#include <iostream>
AVHistogram::AVHistogram(size_t window_size, float threshold):
dataCallback(nullptr),
_window_size(window_size), _threshold(threshold),
_cnt_in(0), _pos_cnt(0), _neg_cnt(0),
_pos_peak(0), _neg_peak(0), _pos_rms(0), _neg_rms(0)
{
}
AVHistogram::~AVHistogram() {
}
size_t AVHistogram::pull(av_sample_t */*buffer_ptr*/, size_t /*buffer_size*/)
{
return 0;
}
size_t AVHistogram::push(float *buffer_ptr, size_t buffer_size) {
size_t to_consume = buffer_size;
while (to_consume-- > 0) {
float v = *buffer_ptr++;
if (v > 0) {
if (v > _pos_peak) _pos_peak = v;
_pos_rms += v*v;
_pos_cnt++;
} else {
if ((-v) > _neg_peak) _neg_peak = -v;
_neg_rms += v*v;
_neg_cnt++;
}
if (++_cnt_in == _window_size) {
if (_threshold != 0.0f) {
_processDb();
} else {
_processLinear();
}
if (dataCallback) dataCallback(_pos_peak, _neg_peak, _pos_rms, _neg_rms);
_pos_peak = _neg_peak = 0;
_pos_rms = _neg_rms = 0;
_pos_cnt = _neg_cnt = 0;
_cnt_in = 0;
}
}
return buffer_size;
}
void AVHistogram::_processDb() {
// Peak value
_pos_peak = 10 * log10f(_pos_peak);
_neg_peak = 10 * log10f(_neg_peak);
// RMS value
_pos_rms = 10 * log10f(sqrtf(_pos_rms / _pos_cnt));
_neg_rms = 10 * log10f(sqrtf(_neg_rms / _neg_cnt));
}
void AVHistogram::_processLinear() {
// Block peak value
_pos_peak = sqrtf(_pos_peak);
_neg_peak = sqrtf(_neg_peak);
// Block RMS value
_pos_rms = _pos_cnt ? sqrtf(sqrtf(_pos_rms/_pos_cnt)) : 0;
_neg_rms = _neg_cnt ? sqrtf(sqrtf(_neg_rms/_neg_cnt)) : 0;
}