# HG changeset patch # User Mychaela Falconia # Date 1664672991 28800 # Node ID 815e4c59162e33507596dc0daf12e8e34369a29e # Parent 5081f2bc6f1c5b9d873090d1fc6b0c11beb6e78f mgw DTMF: tone definitions and sample array generation diff -r 5081f2bc6f1c -r 815e4c59162e mgw/Makefile --- 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} diff -r 5081f2bc6f1c -r 815e4c59162e mgw/dtmf_defs.h --- /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; +}; diff -r 5081f2bc6f1c -r 815e4c59162e mgw/dtmf_init.c --- /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 +#include +#include +#include +#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; +} diff -r 5081f2bc6f1c -r 815e4c59162e mgw/dtmf_table.c --- /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 +#include +#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} +}; diff -r 5081f2bc6f1c -r 815e4c59162e mgw/main.c --- 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);