summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlfred E. Heggestad <alfred.heggestad@gmail.com>2017-04-27 20:30:31 +0200
committerAlfred E. Heggestad <alfred.heggestad@gmail.com>2017-04-27 20:30:31 +0200
commitea12293e0a6764fb4655420de8e906bd42d90efd (patch)
tree70629399c3cf07a520d40a075705c37e3d55a1d0 /src
parent036030c23b1fac6bda68bdd9406ffca5c7539759 (diff)
aulevel: calculate audio levels in dBov
Diffstat (limited to 'src')
-rw-r--r--src/aulevel.c85
-rw-r--r--src/srcs.mk1
2 files changed, 86 insertions, 0 deletions
diff --git a/src/aulevel.c b/src/aulevel.c
new file mode 100644
index 0000000..bc9a27a
--- /dev/null
+++ b/src/aulevel.c
@@ -0,0 +1,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;
+}
diff --git a/src/srcs.mk b/src/srcs.mk
index c538600..cdb2a26 100644
--- a/src/srcs.mk
+++ b/src/srcs.mk
@@ -8,6 +8,7 @@ SRCS += account.c
SRCS += aucodec.c
SRCS += audio.c
SRCS += aufilt.c
+SRCS += aulevel.c
SRCS += auplay.c
SRCS += ausrc.c
SRCS += baresip.c