aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Eversberg <jolly@eversberg.eu>2017-11-18 12:54:12 +0100
committerAndreas Eversberg <jolly@eversberg.eu>2017-12-03 08:46:01 +0100
commit06092e90ed698ab9c1abd7e97b73369bd65bb3c7 (patch)
treecaa3a14a9b41e26499d02bf7dc9773b1baa6b707
parent021c6958837dd99ee24dc9c711718fc57e694a33 (diff)
Add library to normalize speech level (Sendevolumenregler SVoR)
Note: This lib has no user yet.
-rw-r--r--.gitignore2
-rw-r--r--configure.ac1
-rw-r--r--src/Makefile.am1
-rw-r--r--src/libsendevolumenregler/Makefile.am6
-rw-r--r--src/libsendevolumenregler/sendevolumenregler.c115
-rw-r--r--src/libsendevolumenregler/sendevolumenregler.h14
-rw-r--r--src/test/Makefile.am8
-rw-r--r--src/test/test_sendevolumenregler.c105
8 files changed, 252 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 75bf814..494a229 100644
--- a/.gitignore
+++ b/.gitignore
@@ -24,6 +24,7 @@ src/libdebug/libdebug.a
src/libmobile/libmobile.a
src/libdisplay/libdisplay.a
src/libimage/libimage.a
+src/libsendevolumenregler/libsendevolumenregler.a
src/libcompandor/libcompandor.a
src/libgoertzel/libgoertzel.a
src/libjitter/libjitter.a
@@ -57,6 +58,7 @@ src/r2000/radiocom2000
src/tv/osmotv
sim/cnetz_sim
src/test/test_filter
+src/test/test_sendevolumenregler
src/test/test_compandor
src/test/test_emphasis
src/test/test_dms
diff --git a/configure.ac b/configure.ac
index 51ca055..97c40d2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -79,6 +79,7 @@ AC_OUTPUT(
src/libmobile/Makefile
src/libdisplay/Makefile
src/libimage/Makefile
+ src/libsendevolumenregler/Makefile
src/libcompandor/Makefile
src/libgoertzel/Makefile
src/libjitter/Makefile
diff --git a/src/Makefile.am b/src/Makefile.am
index 4aae1df..28b61b0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -6,6 +6,7 @@ SUBDIRS = \
libdisplay \
libsample \
libimage \
+ libsendevolumenregler \
libcompandor \
libgoertzel \
libjitter \
diff --git a/src/libsendevolumenregler/Makefile.am b/src/libsendevolumenregler/Makefile.am
new file mode 100644
index 0000000..3acd414
--- /dev/null
+++ b/src/libsendevolumenregler/Makefile.am
@@ -0,0 +1,6 @@
+AM_CPPFLAGS = -Wall -Wextra -g $(all_includes)
+
+noinst_LIBRARIES = libsendevolumenregler.a
+
+libsendevolumenregler_a_SOURCES = \
+ sendevolumenregler.c
diff --git a/src/libsendevolumenregler/sendevolumenregler.c b/src/libsendevolumenregler/sendevolumenregler.c
new file mode 100644
index 0000000..30b3285
--- /dev/null
+++ b/src/libsendevolumenregler/sendevolumenregler.c
@@ -0,0 +1,115 @@
+/* Sendevolumenregler to be used by B-Netz
+ *
+ * (C) 2017 by Andreas Eversberg <jolly@eversberg.eu>
+ * All Rights Reserved
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+/* This function keeps the speech level for the transmitter constant.
+ *
+ * B-Netz specs say that levels are amplified to speech level. The maximum gain
+ * is 16 dB. The level may overshoot by 2.6 dB and must lowered to normal level
+ * within 20 ms. The level should raise 4.3 dB per second until target level is
+ * reached.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <math.h>
+#include "../libsample/sample.h"
+#include "sendevolumenregler.h"
+
+#define db2level(db) pow(10, (double)db / 20.0)
+
+/*
+ * Init function
+ *
+ * abwaerts_dbs = how many dB per second to lower the amplification, if input signal is above dBm0
+ * aufwaerts_dbs = how many dB per second to raise the amplification, if input signal is below dBm0
+ * maximum_db = limit of the output level above dBm0
+ * minimum_db = below this input level, the amplification is not raised, so it stays constant.
+ * dbm0_level = target level to be treated as 0 dB
+ *
+ * Hopefully this is correct
+ *
+ */
+void init_sendevolumenregler(sendevolumenregler_t *state, double samplerate, double abwaerts_dbs, double aufwaerts_dbs, double maximum_db, double minimum_db, double dbm0_level)
+{
+ memset(state, 0, sizeof(*state));
+
+ state->peak = 1.0;
+ state->envelope = 1.0;
+ state->dbm0_level = dbm0_level;
+ state->step_down = pow(db2level(abwaerts_dbs), 1.0 / samplerate);
+ state->step_up = pow(db2level(aufwaerts_dbs), 1.0 / samplerate);
+ state->maximum_level = db2level(maximum_db);
+ state->minimum_level = db2level(minimum_db);
+}
+
+/*
+ * how to works:
+ *
+ * if value is above 'peak', raise 'peak' to value instantaniously
+ * if value is below 'peak', lower 'peak' by 'step_up' (increase level)
+ * if 'peak' is above 'envelope', raise 'envelope' by 'step_down' (decrease level)
+ * if 'peak' is below 'envelope', lower 'envelope' to 'peak' (increase level)
+ * 'envelope' will not fall below 'minimum_level' (maximum amplification)
+ * if 'peak' is 'maximum_level' above 'envelope', raise 'envelope' to 'maximum_level' below 'peak'
+ */
+void sendevolumenregler(sendevolumenregler_t *state, sample_t *samples, int num)
+{
+ double value, peak, envelope, step_up, step_down, maximum_level, minimum_level, dbm0_level;
+ int i;
+
+ dbm0_level = state->dbm0_level;
+ step_up = state->step_up;
+ step_down = state->step_down;
+ maximum_level = state->maximum_level;
+ minimum_level = state->minimum_level;
+ peak = state->peak;
+ envelope = state->envelope;
+
+ for (i = 0; i < num; i++) {
+ /* normalize sample value to dbm0_level level */
+ value = *samples / dbm0_level;
+
+ /* 'peak' is the level that raises directly with the value
+ * level, but falls as specified by step_up. */
+ if (fabs(value) > peak)
+ peak = fabs(value);
+ else
+ peak /= step_up;
+ /* 'evelope' is the level that raises with the specified step_down
+ * to 'peak', but falls with 'peak'. */
+ if (peak > envelope)
+ envelope *= step_down;
+ else
+ envelope = peak;
+ /* no envelope below minimum level */
+ if (envelope < minimum_level)
+ envelope = minimum_level;
+ /* raise envelope, if 'peak' exceeds maximum level */
+ if (peak / envelope > maximum_level)
+ envelope = peak / maximum_level;
+
+ *samples++ = value / envelope * dbm0_level;
+ }
+
+ state->envelope = envelope;
+ state->peak = peak;
+}
+
diff --git a/src/libsendevolumenregler/sendevolumenregler.h b/src/libsendevolumenregler/sendevolumenregler.h
new file mode 100644
index 0000000..2bc916b
--- /dev/null
+++ b/src/libsendevolumenregler/sendevolumenregler.h
@@ -0,0 +1,14 @@
+
+typedef struct sendevolumenregler {
+ double peak;
+ double envelope;
+ double step_up;
+ double step_down;
+ double minimum_level;
+ double maximum_level;
+ double dbm0_level;
+} sendevolumenregler_t;
+
+void init_sendevolumenregler(sendevolumenregler_t *state, double samplerate, double abwaerts_dbs, double aufwaerts_dbs, double maximum_db, double minimum_db, double dbm0_level);
+void sendevolumenregler(sendevolumenregler_t *state, sample_t *samples, int num);
+
diff --git a/src/test/Makefile.am b/src/test/Makefile.am
index 3acba91..d15256c 100644
--- a/src/test/Makefile.am
+++ b/src/test/Makefile.am
@@ -2,6 +2,7 @@ AM_CPPFLAGS = -Wall -g $(all_includes)
noinst_PROGRAMS = \
test_filter \
+ test_sendevolumenregler \
test_compandor \
test_emphasis \
test_dms \
@@ -37,6 +38,13 @@ test_filter_LDADD += \
$(SOAPY_LIBS)
endif
+test_sendevolumenregler_SOURCES = test_sendevolumenregler.c
+
+test_sendevolumenregler_LDADD = \
+ $(COMMON_LA) \
+ $(top_builddir)/src/libsendevolumenregler/libsendevolumenregler.a \
+ -lm
+
test_compandor_SOURCES = dummy.x test_compandor.c
test_compandor_LDADD = \
diff --git a/src/test/test_sendevolumenregler.c b/src/test/test_sendevolumenregler.c
new file mode 100644
index 0000000..29ec813
--- /dev/null
+++ b/src/test/test_sendevolumenregler.c
@@ -0,0 +1,105 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <math.h>
+#include <string.h>
+#include "../libsample/sample.h"
+#include "../libsendevolumenregler/sendevolumenregler.h"
+
+#define level2db(level) (20 * log10(level))
+#define db2level(db) pow(10, (double)db / 20.0)
+
+#define SAMPLERATE 48000
+#define ABWAERTS_DBS (2.6 / 0.020) /* 2.6 dB in 20 ms */
+#define AUFWAERTS_DBS 4.3
+#define MINIMUM_DB -16.0
+#define MAXIMUM_DB 2.6
+
+static double test_frequency[3] = { 1000.0, 4000.0, 300.0 };
+
+static sample_t samples_plus4db[SAMPLERATE];
+static sample_t samples_minus20db[SAMPLERATE];
+static sample_t samples_0db[SAMPLERATE];
+
+/* generate 2 samples: one with -4 dB, the other with -16 dB */
+static void generate_test_sample(double test_frequency)
+{
+ int i;
+ double value;
+
+ for (i = 0; i < SAMPLERATE; i++) {
+ value = cos(2.0 * M_PI * test_frequency / (double)SAMPLERATE * i);
+ samples_plus4db[i] = value * db2level(4);
+ samples_minus20db[i] = value * db2level(-20);
+ samples_0db[i] = value;
+ }
+}
+
+static void check_level(sample_t *samples, const char *desc, double target_db, int when)
+{
+ int i;
+ double last = 0.0, envelope = 0.0;
+ int up = 0;
+ double factor;
+
+ for (i = 0; i < when; i++) {
+//puts(debug_amplitude(samples[i]));
+ if (last < samples[i]) {
+ up = 1;
+ } else if (last > samples[i]) {
+ if (up) {
+ envelope = last;
+ }
+ up = 0;
+ }
+#if 0
+ if ((i % (SAMPLERATE/(SAMPLERATE/100))) == 0)
+ printf("%s: envelope = %.4f (val=%.4f) (when=%d ms)\n", desc, level2db(envelope), envelope, i * 1000 / SAMPLERATE);
+#endif
+ last = samples[i];
+ }
+ factor = envelope / db2level(target_db);
+ printf("%s: envelope after %d ms is %.4f db (factor %.4f)\n", desc, i * 1000 / SAMPLERATE, level2db(envelope), factor);
+ if (factor > 1.1 || factor < 0.9) {
+ printf("**** ERROR: we expected a factor close to 1.0\n");
+ }
+}
+
+int main(void)
+{
+ sendevolumenregler_t state;
+ sample_t samples[SAMPLERATE * 2];
+ int f;
+
+ for (f = 0; f < 3; f++) {
+ printf("Testing frequency %.0f Hz:\n", test_frequency[f]);
+ generate_test_sample(test_frequency[f]);
+
+ /* increment low level */
+ memcpy(samples, samples_minus20db, SAMPLERATE * sizeof(sample_t));
+
+ init_sendevolumenregler(&state, SAMPLERATE, ABWAERTS_DBS, AUFWAERTS_DBS, MAXIMUM_DB, MINIMUM_DB, 1.0);
+ sendevolumenregler(&state, samples, SAMPLERATE);
+
+ check_level(samples, "aufwaerts (-20 dB)", AUFWAERTS_DBS - 20.0, 1000 * SAMPLERATE / 1000);
+
+ /* decrement high level */
+ memcpy(samples, samples_plus4db, SAMPLERATE * sizeof(sample_t));
+
+ init_sendevolumenregler(&state, SAMPLERATE, ABWAERTS_DBS, AUFWAERTS_DBS, MAXIMUM_DB, MINIMUM_DB, 1.0);
+ sendevolumenregler(&state, samples, SAMPLERATE);
+
+ check_level(samples, "abwaerts (+4 dB)", MAXIMUM_DB / 2.0, 10 * SAMPLERATE / 1000);
+ check_level(samples, "abwaerts (+4 dB)", 0.0, 20 * SAMPLERATE / 1000);
+
+ /* 0 DB */
+ memcpy(samples, samples_0db, SAMPLERATE * sizeof(sample_t));
+
+ init_sendevolumenregler(&state, SAMPLERATE, ABWAERTS_DBS, AUFWAERTS_DBS, MAXIMUM_DB, MINIMUM_DB, 1.0);
+ check_level(samples, "unaffected level", 0.0, 1000 * SAMPLERATE / 1000);
+
+ puts("");
+ }
+
+ return 0;
+}
+