From fd3a4d7ac2d0a3759067c08c67cfad406fbcfb3c Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Wed, 15 Nov 2017 18:45:56 +0100 Subject: Restructure: Move dtmf from common code to 'libdtmf' --- .gitignore | 1 + configure.ac | 1 + src/Makefile.am | 2 +- src/common/Makefile.am | 1 - src/common/dtmf.c | 123 ---------------------------------------------- src/common/dtmf.h | 14 ------ src/libdtmf/Makefile.am | 6 +++ src/libdtmf/dtmf_encode.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++ src/libdtmf/dtmf_encode.h | 14 ++++++ src/nmt/Makefile.am | 1 + src/nmt/dsp.c | 6 +-- src/nmt/nmt.c | 4 +- src/nmt/nmt.h | 4 +- 13 files changed, 154 insertions(+), 146 deletions(-) delete mode 100644 src/common/dtmf.c delete mode 100644 src/common/dtmf.h create mode 100644 src/libdtmf/Makefile.am create mode 100644 src/libdtmf/dtmf_encode.c create mode 100644 src/libdtmf/dtmf_encode.h diff --git a/.gitignore b/.gitignore index 1cc062d..f9fe084 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ compile .libs .dirstamp m4 +src/libdtmf/libdtmf.a src/libgermanton/libgermanton.a src/libtimer/libtimer.a src/libsamplerate/libsamplerate.a diff --git a/configure.ac b/configure.ac index 4f44bf8..006acc0 100644 --- a/configure.ac +++ b/configure.ac @@ -75,6 +75,7 @@ AS_IF([test "x$with_soapy" == "xyes"],[AC_MSG_NOTICE( Compiling with SoapySDR su AS_IF([test "x$somethingmagick" == "xyes"],[AC_MSG_NOTICE( Compiling with ImageMagick )],[AC_MSG_NOTICE( ImageMagick not supported )]) AC_OUTPUT( + src/libdtmf/Makefile src/libgermanton/Makefile src/libtimer/Makefile src/libsamplerate/Makefile diff --git a/src/Makefile.am b/src/Makefile.am index c2b8808..187ab9d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,3 @@ AUTOMAKE_OPTIONS = foreign -SUBDIRS = libgermanton libtimer libsamplerate libscrambler libfilter common anetz bnetz cnetz nmt amps tacs jtacs r2000 tv test +SUBDIRS = libdtmf libgermanton libtimer libsamplerate libscrambler libfilter common anetz bnetz cnetz nmt amps tacs jtacs r2000 tv test diff --git a/src/common/Makefile.am b/src/common/Makefile.am index fa686ef..35b1e5f 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -9,7 +9,6 @@ libcommon_a_SOURCES = \ wave.c \ goertzel.c \ jitter.c \ - dtmf.c \ emphasis.c \ compandor.c \ fft.c \ diff --git a/src/common/dtmf.c b/src/common/dtmf.c deleted file mode 100644 index f3d3283..0000000 --- a/src/common/dtmf.c +++ /dev/null @@ -1,123 +0,0 @@ -/* DTMF coder - * - * (C) 2016 by Andreas Eversberg - * 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 . - */ - -#include -#include -#include -#include "sample.h" -#include "dtmf.h" - -#define PI M_PI - -static double tx_peak_dtmf_low = 0.2818 / SPEECH_LEVEL; /* -11 dBm, relative to speech level */ -static double tx_peak_dtmf_high = 0.3548 / SPEECH_LEVEL;/* -9 dBm, relative to speech level */ -#define DTMF_DURATION 0.100 /* duration in seconds */ - -static sample_t dsp_sine_dtmf_low[65536]; -static sample_t dsp_sine_dtmf_high[65536]; - -void dtmf_init(dtmf_t *dtmf, int samplerate) -{ - int i; - - memset(dtmf, 0, sizeof(*dtmf)); - dtmf->samplerate = samplerate; - dtmf->max = (int)((double)samplerate * DTMF_DURATION + 0.5); - - // FIXME: do this globally and not per instance */ - for (i = 0; i < 65536; i++) { - dsp_sine_dtmf_low[i] = sin((double)i / 65536.0 * 2.0 * PI) * tx_peak_dtmf_low; - dsp_sine_dtmf_high[i] = sin((double)i / 65536.0 * 2.0 * PI) * tx_peak_dtmf_high; - } -} - -/* set dtmf tone */ -void dtmf_set_tone(dtmf_t *dtmf, char tone) -{ - double f1, f2; - - switch(tone) { - case '1': f1 = 697.0; f2 = 1209.0; break; - case '2': f1 = 697.0; f2 = 1336.0; break; - case '3': f1 = 697.0; f2 = 1477.0; break; - case'a':case 'A': f1 = 697.0; f2 = 1633.0; break; - case '4': f1 = 770.0; f2 = 1209.0; break; - case '5': f1 = 770.0; f2 = 1336.0; break; - case '6': f1 = 770.0; f2 = 1477.0; break; - case'b':case 'B': f1 = 770.0; f2 = 1633.0; break; - case '7': f1 = 852.0; f2 = 1209.0; break; - case '8': f1 = 852.0; f2 = 1336.0; break; - case '9': f1 = 852.0; f2 = 1477.0; break; - case'c':case 'C': f1 = 852.0; f2 = 1633.0; break; - case '*': f1 = 941.0; f2 = 1209.0; break; - case '0': f1 = 941.0; f2 = 1336.0; break; - case '#': f1 = 941.0; f2 = 1477.0; break; - case'd':case 'D': f1 = 941.0; f2 = 1633.0; break; - default: - dtmf->tone = 0; - return; - } - dtmf->tone = tone; - dtmf->pos = 0; - dtmf->phaseshift65536[0] = 65536.0 / ((double)dtmf->samplerate / f1); - dtmf->phaseshift65536[1] = 65536.0 / ((double)dtmf->samplerate / f2); -} - -/* Generate audio stream from DTMF tone. Keep phase for next call of function. */ -void dtmf_tone(dtmf_t *dtmf, sample_t *samples, int length) -{ - double *phaseshift, *phase; - int i, pos, max; - - /* use silence, if no tone */ - if (!dtmf->tone) { - memset(samples, 0, length * sizeof(*samples)); - return; - } - - phaseshift = dtmf->phaseshift65536; - phase = dtmf->phase65536; - pos = dtmf->pos; - max = dtmf->max; - - for (i = 0; i < length; i++) { - *samples++ = dsp_sine_dtmf_low[(uint16_t)phase[0]] - + dsp_sine_dtmf_high[(uint16_t)phase[1]]; - phase[0] += phaseshift[0]; - if (phase[0] >= 65536) - phase[0] -= 65536; - phase[1] += phaseshift[1]; - if (phase[1] >= 65536) - phase[1] -= 65536; - - /* tone ends */ - if (++pos == max) { - dtmf->tone = 0; - break; - } - } - length -= i; - - dtmf->pos = pos; - - /* if tone ends, fill rest with silence */ - if (length) - memset(samples, 0, length * sizeof(*samples)); -} - diff --git a/src/common/dtmf.h b/src/common/dtmf.h deleted file mode 100644 index d6a5322..0000000 --- a/src/common/dtmf.h +++ /dev/null @@ -1,14 +0,0 @@ - -typedef struct dtmf { - int samplerate; /* samplerate */ - char tone; /* current tone to be played */ - int pos; /* sample counter for tone */ - int max; /* max number of samples for tone duration */ - double phaseshift65536[2]; /* how much the phase of sine wave changes per sample */ - double phase65536[2]; /* current phase */ -} dtmf_t; - -void dtmf_init(dtmf_t *dtmf, int samplerate); -void dtmf_set_tone(dtmf_t *dtmf, char tone); -void dtmf_tone(dtmf_t *dtmf, sample_t *samples, int length); - diff --git a/src/libdtmf/Makefile.am b/src/libdtmf/Makefile.am new file mode 100644 index 0000000..a44d4f6 --- /dev/null +++ b/src/libdtmf/Makefile.am @@ -0,0 +1,6 @@ +AM_CPPFLAGS = -Wall -Wextra -g $(all_includes) + +noinst_LIBRARIES = libdtmf.a + +libdtmf_a_SOURCES = \ + dtmf_encode.c diff --git a/src/libdtmf/dtmf_encode.c b/src/libdtmf/dtmf_encode.c new file mode 100644 index 0000000..6413954 --- /dev/null +++ b/src/libdtmf/dtmf_encode.c @@ -0,0 +1,123 @@ +/* DTMF coder + * + * (C) 2016 by Andreas Eversberg + * 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 . + */ + +#include +#include +#include +#include "../common/sample.h" +#include "dtmf_encode.h" + +#define PI M_PI + +#define PEAK_DTMF_LOW 0.2818 /* -11 dBm, relative to 0 dBm level */ +#define PEAK_DTMF_HIGH 0.3548 /* -9 dBm, relative to 0 dBm level */ +#define DTMF_DURATION 0.100 /* duration in seconds */ + +static sample_t dsp_sine_dtmf_low[65536]; +static sample_t dsp_sine_dtmf_high[65536]; + +void dtmf_encode_init(dtmf_enc_t *dtmf, int samplerate, double dBm_level) +{ + int i; + + memset(dtmf, 0, sizeof(*dtmf)); + dtmf->samplerate = samplerate; + dtmf->max = (int)((double)samplerate * DTMF_DURATION + 0.5); + + // FIXME: do this globally and not per instance */ + for (i = 0; i < 65536; i++) { + dsp_sine_dtmf_low[i] = sin((double)i / 65536.0 * 2.0 * PI) * PEAK_DTMF_LOW * dBm_level; + dsp_sine_dtmf_high[i] = sin((double)i / 65536.0 * 2.0 * PI) * PEAK_DTMF_HIGH * dBm_level; + } +} + +/* set dtmf tone */ +void dtmf_encode_set_tone(dtmf_enc_t *dtmf, char tone) +{ + double f1, f2; + + switch(tone) { + case '1': f1 = 697.0; f2 = 1209.0; break; + case '2': f1 = 697.0; f2 = 1336.0; break; + case '3': f1 = 697.0; f2 = 1477.0; break; + case'a':case 'A': f1 = 697.0; f2 = 1633.0; break; + case '4': f1 = 770.0; f2 = 1209.0; break; + case '5': f1 = 770.0; f2 = 1336.0; break; + case '6': f1 = 770.0; f2 = 1477.0; break; + case'b':case 'B': f1 = 770.0; f2 = 1633.0; break; + case '7': f1 = 852.0; f2 = 1209.0; break; + case '8': f1 = 852.0; f2 = 1336.0; break; + case '9': f1 = 852.0; f2 = 1477.0; break; + case'c':case 'C': f1 = 852.0; f2 = 1633.0; break; + case '*': f1 = 941.0; f2 = 1209.0; break; + case '0': f1 = 941.0; f2 = 1336.0; break; + case '#': f1 = 941.0; f2 = 1477.0; break; + case'd':case 'D': f1 = 941.0; f2 = 1633.0; break; + default: + dtmf->tone = 0; + return; + } + dtmf->tone = tone; + dtmf->pos = 0; + dtmf->phaseshift65536[0] = 65536.0 / ((double)dtmf->samplerate / f1); + dtmf->phaseshift65536[1] = 65536.0 / ((double)dtmf->samplerate / f2); +} + +/* Generate audio stream from DTMF tone. Keep phase for next call of function. */ +void dtmf_encode(dtmf_enc_t *dtmf, sample_t *samples, int length) +{ + double *phaseshift, *phase; + int i, pos, max; + + /* use silence, if no tone */ + if (!dtmf->tone) { + memset(samples, 0, length * sizeof(*samples)); + return; + } + + phaseshift = dtmf->phaseshift65536; + phase = dtmf->phase65536; + pos = dtmf->pos; + max = dtmf->max; + + for (i = 0; i < length; i++) { + *samples++ = dsp_sine_dtmf_low[(uint16_t)phase[0]] + + dsp_sine_dtmf_high[(uint16_t)phase[1]]; + phase[0] += phaseshift[0]; + if (phase[0] >= 65536) + phase[0] -= 65536; + phase[1] += phaseshift[1]; + if (phase[1] >= 65536) + phase[1] -= 65536; + + /* tone ends */ + if (++pos == max) { + dtmf->tone = 0; + break; + } + } + length -= i; + + dtmf->pos = pos; + + /* if tone ends, fill rest with silence */ + if (length) + memset(samples, 0, length * sizeof(*samples)); +} + diff --git a/src/libdtmf/dtmf_encode.h b/src/libdtmf/dtmf_encode.h new file mode 100644 index 0000000..15ac05e --- /dev/null +++ b/src/libdtmf/dtmf_encode.h @@ -0,0 +1,14 @@ + +typedef struct dtmf_enc { + int samplerate; /* samplerate */ + char tone; /* current tone to be played */ + int pos; /* sample counter for tone */ + int max; /* max number of samples for tone duration */ + double phaseshift65536[2]; /* how much the phase of sine wave changes per sample */ + double phase65536[2]; /* current phase */ +} dtmf_enc_t; + +void dtmf_encode_init(dtmf_enc_t *dtmf, int samplerate, double dBm_level); +void dtmf_encode_set_tone(dtmf_enc_t *dtmf, char tone); +void dtmf_encode(dtmf_enc_t *dtmf, sample_t *samples, int length); + diff --git a/src/nmt/Makefile.am b/src/nmt/Makefile.am index e3dd06b..ddaabd9 100644 --- a/src/nmt/Makefile.am +++ b/src/nmt/Makefile.am @@ -22,6 +22,7 @@ nmt_SOURCES = \ nmt_LDADD = \ $(COMMON_LA) \ libdmssms.a \ + $(top_builddir)/src/libdtmf/libdtmf.a \ $(top_builddir)/src/common/libmobile.a \ $(top_builddir)/src/common/libcommon.a \ $(top_builddir)/src/libtimer/libtimer.a \ diff --git a/src/nmt/dsp.c b/src/nmt/dsp.c index 84a33a6..8f58352 100644 --- a/src/nmt/dsp.c +++ b/src/nmt/dsp.c @@ -148,8 +148,8 @@ int dsp_init_sender(nmt_t *nmt, double deviation_factor) nmt->dial_phaseshift65536 = 65536.0 / ((double)nmt->sender.samplerate / DIALTONE_HZ); PDEBUG(DDSP, DEBUG_DEBUG, "dial_phaseshift = %.4f\n", nmt->dial_phaseshift65536); - /* dtmf */ - dtmf_init(&nmt->dtmf, 8000); + /* dtmf, generate tone relative to speech level */ + dtmf_encode_init(&nmt->dtmf, 8000, 1.0 / SPEECH_LEVEL); nmt->dmp_frame_level = display_measurements_add(&nmt->sender, "Frame Level", "%.1f %% (last)", DISPLAY_MEAS_LAST, DISPLAY_MEAS_LEFT, 0.0, 150.0, 100.0); nmt->dmp_frame_quality = display_measurements_add(&nmt->sender, "Frame Quality", "%.1f %% (last)", DISPLAY_MEAS_LAST, DISPLAY_MEAS_LEFT, 0.0, 100.0, 100.0); @@ -342,7 +342,7 @@ void sender_receive(sender_t *sender, sample_t *samples, int length, double __at if (nmt->compandor) expand_audio(&nmt->cstate, samples, count); if (nmt->dsp_mode == DSP_MODE_DTMF) - dtmf_tone(&nmt->dtmf, samples, count); + dtmf_encode(&nmt->dtmf, samples, count); spl = nmt->sender.rxbuf; pos = nmt->sender.rxbuf_pos; for (i = 0; i < count; i++) { diff --git a/src/nmt/nmt.c b/src/nmt/nmt.c index 4fdc195..0287292 100644 --- a/src/nmt/nmt.c +++ b/src/nmt/nmt.c @@ -1454,7 +1454,7 @@ static void rx_active(nmt_t *nmt, frame_t *frame) break; } digit = nmt_value2digit(frame->digit); - dtmf_set_tone(&nmt->dtmf, digit); + dtmf_encode_set_tone(&nmt->dtmf, digit); PDEBUG_CHAN(DNMT, DEBUG_INFO, "Received (odd) digit %c.\n", digit); nmt->mft_num++; break; @@ -1477,7 +1477,7 @@ static void rx_active(nmt_t *nmt, frame_t *frame) break; } digit = nmt_value2digit(frame->digit); - dtmf_set_tone(&nmt->dtmf, digit); + dtmf_encode_set_tone(&nmt->dtmf, digit); PDEBUG_CHAN(DNMT, DEBUG_INFO, "Received (even) digit %c.\n", digit); nmt->mft_num++; break; diff --git a/src/nmt/nmt.h b/src/nmt/nmt.h index c1c83da..aaa61f6 100644 --- a/src/nmt/nmt.h +++ b/src/nmt/nmt.h @@ -1,7 +1,7 @@ #include "../common/sender.h" #include "../libtimer/timer.h" #include "../common/compandor.h" -#include "../common/dtmf.h" +#include "../libdtmf/dtmf_encode.h" #include "../common/call.h" #include "../common/fsk.h" #include "../common/goertzel.h" @@ -77,7 +77,7 @@ typedef struct nmt { sender_t sender; nmt_sysinfo_t sysinfo; compandor_t cstate; - dtmf_t dtmf; + dtmf_enc_t dtmf; struct transaction *trans; /* pointer to transaction, if bound to channel */ /* sender's states */ -- cgit v1.2.3