changeset 126:815e4c59162e

mgw DTMF: tone definitions and sample array generation
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 01 Oct 2022 17:09:51 -0800
parents 5081f2bc6f1c
children f062c32a5116
files mgw/Makefile mgw/dtmf_defs.h mgw/dtmf_init.c mgw/dtmf_table.c mgw/main.c
diffstat 5 files changed, 110 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mgw/Makefile	Sat Oct 01 16:07:59 2022 -0800
+++ b/mgw/Makefile	Sat Oct 01 17:09:51 2022 -0800
@@ -1,15 +1,16 @@
 CC=	gcc
 CFLAGS=	-O2
 PROG=	themwi-mgw
-OBJS=	crcx.o ctrl_prot.o ctrl_sock.o dlcx.o g711_decode.o g711_encode.o \
-	gsm2pstn.o main.o mdcx.o pstn2gsm.o readconf.o udpsink.o
+OBJS=	crcx.o ctrl_prot.o ctrl_sock.o dlcx.o dtmf_init.o dtmf_table.o \
+	g711_decode.o g711_encode.o gsm2pstn.o main.o mdcx.o pstn2gsm.o \
+	readconf.o udpsink.o
 LIBS=	../libutil/libutil.a
 INSTBIN=/usr/local/bin
 
 all:	${PROG}
 
 ${PROG}: ${OBJS} ${LIBS}
-	${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS} -lgsm
+	${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS} -lgsm -lm
 
 install:
 	install -c -o bin -g bin -m 755 ${PROG} ${INSTBIN}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mgw/dtmf_defs.h	Sat Oct 01 17:09:51 2022 -0800
@@ -0,0 +1,14 @@
+/*
+ * This header file holds various internal definitions for DTMF generation,
+ * including key tunable settings of min and max DTMF duration.
+ */
+
+#define	DTMF_MIN_FRAMES		10	/* 200 ms */
+#define	DTMF_MAX_FRAMES		60	/* 1200 ms */
+
+struct dtmf_desc {
+	int	digit;
+	float	freq_low;
+	float	freq_high;
+	int16_t	*samples;
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mgw/dtmf_init.c	Sat Oct 01 17:09:51 2022 -0800
@@ -0,0 +1,54 @@
+/*
+ * The code in this module executes once upon themwi-mgw startup;
+ * it initializes the arrays of linear PCM samples for DTMF output.
+ *
+ * The amplitudes of the two sine waves comprising each DTMF tone
+ * are also defined here.
+ */
+
+#include <math.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include "dtmf_defs.h"
+#include "int_defs.h"
+
+#define	DTMF_AMPL_LOW_TONE	5000
+#define	DTMF_AMPL_HIGH_TONE	6000
+
+extern struct dtmf_desc dtmf_table[];
+
+static void
+init_one_dtmf(desc)
+	struct dtmf_desc *desc;
+{
+	float angle_low, angle_high;
+	unsigned n;
+	int16_t *samp;
+
+	angle_low = 0;
+	angle_high = 0;
+	samp = desc->samples;
+	for (n = 0; n < DTMF_MAX_FRAMES * SAMPLES_PER_FRAME; n++) {
+		*samp++ = sinf(angle_low) * DTMF_AMPL_LOW_TONE +
+			  sinf(angle_high) * DTMF_AMPL_HIGH_TONE;
+		angle_low += desc->freq_low;
+		angle_high += desc->freq_high;
+	}
+}
+
+dtmf_init_sample_arrays()
+{
+	struct dtmf_desc *desc;
+
+	for (desc = dtmf_table; desc->digit; desc++) {
+		desc->samples =
+		  malloc(DTMF_MAX_FRAMES * SAMPLES_PER_FRAME * sizeof(int16_t));
+		if (!desc->samples) {
+			perror("malloc for DTMF samples");
+			exit(1);
+		}
+		init_one_dtmf(desc);
+	}
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mgw/dtmf_table.c	Sat Oct 01 17:09:51 2022 -0800
@@ -0,0 +1,37 @@
+/*
+ * This module holds the table of all possible DTMF tones we can generate.
+ */
+
+#include <math.h>
+#include <stdint.h>
+#include "dtmf_defs.h"
+
+#define	FREQ_697	(M_PI * 697.0 / 4000.0)
+#define	FREQ_770	(M_PI * 770.0 / 4000.0)
+#define	FREQ_852	(M_PI * 852.0 / 4000.0)
+#define	FREQ_941	(M_PI * 941.0 / 4000.0)
+#define	FREQ_1209	(M_PI * 1209.0 / 4000.0)
+#define	FREQ_1336	(M_PI * 1336.0 / 4000.0)
+#define	FREQ_1477	(M_PI * 1477.0 / 4000.0)
+#define	FREQ_1633	(M_PI * 1633.0 / 4000.0)
+
+struct dtmf_desc dtmf_table[] = {
+	{'1', FREQ_697, FREQ_1209},
+	{'2', FREQ_697, FREQ_1336},
+	{'3', FREQ_697, FREQ_1477},
+	{'A', FREQ_697, FREQ_1633},
+	{'4', FREQ_770, FREQ_1209},
+	{'5', FREQ_770, FREQ_1336},
+	{'6', FREQ_770, FREQ_1477},
+	{'B', FREQ_770, FREQ_1633},
+	{'7', FREQ_852, FREQ_1209},
+	{'8', FREQ_852, FREQ_1336},
+	{'9', FREQ_852, FREQ_1477},
+	{'C', FREQ_852, FREQ_1633},
+	{'*', FREQ_941, FREQ_1209},
+	{'0', FREQ_941, FREQ_1336},
+	{'#', FREQ_941, FREQ_1477},
+	{'D', FREQ_941, FREQ_1633},
+	/* table search terminator */
+	{0, 0, 0}
+};
--- a/mgw/main.c	Sat Oct 01 16:07:59 2022 -0800
+++ b/mgw/main.c	Sat Oct 01 17:09:51 2022 -0800
@@ -38,6 +38,7 @@
 
 	openlog("themwi-mgw", 0, LOG_LOCAL5);
 	read_config_file();
+	dtmf_init_sample_arrays();
 	if (create_ctrl_socket() < 0) {
 		fprintf(stderr, "error creating TMGW control socket\n");
 		exit(1);