blob: bc9a27a7d3a3835ebf2e57c224629e80b5afe187 (
plain)
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
76
77
78
79
80
81
82
83
84
85
|
/**
* @file src/aulevel.c Audio level
*
* Copyright (C) 2017 Creytiv.com
*/
#include <math.h>
#include <re.h>
#include <baresip.h>
#include "core.h"
/**
* Generic routine to calculate RMS (Root-Mean-Square) from
* a set of signed 16-bit values
*
* \verbatim
.---------------
| N-1
| ----.
| \
| \ 2
| | s[n]
| /
| /
_ | ----'
\ | n=0
\ | ------------
\| N
\endverbatim
*
* @param data Array of signed 16-bit values
* @param len Number of values
*
* @return RMS value from 0 to 32768
*/
static double calc_rms(const int16_t *data, size_t len)
{
double sum = 0;
size_t i;
if (!data || !len)
return .0;
for (i = 0; i < len; i++) {
const double sample = data[i];
sum += sample * sample;
}
return sqrt(sum / (double)len);
}
/**
* Calculate the audio level in dBov from a set of audio samples.
* dBov is the level, in decibels, relative to the overload point
* of the system
*
* @param sampv Audio samples
* @param sampc Number of audio samples
*
* @return Audio level expressed in dBov
*/
double aulevel_calc_dbov(const int16_t *sampv, size_t sampc)
{
static const double peak = 32767.0;
double rms, dbov;
if (!sampv || !sampc)
return AULEVEL_MIN;
rms = calc_rms(sampv, sampc) / peak;
dbov = 20 * log10(rms);
if (dbov < AULEVEL_MIN)
dbov = AULEVEL_MIN;
else if (dbov > AULEVEL_MAX)
dbov = AULEVEL_MAX;
return dbov;
}
|