changeset 9:ff535725e01f

g711-tone-detect program written
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 07 Mar 2024 00:05:44 -0800
parents eaf0e8f81a22
children 3c5734b88c20
files .hgignore Makefile tone-detect/Makefile tone-detect/g711-tone-detect.c
diffstat 4 files changed, 94 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed Mar 06 21:33:49 2024 -0800
+++ b/.hgignore	Thu Mar 07 00:05:44 2024 -0800
@@ -4,3 +4,4 @@
 
 ^test-fsk/sipout-test-fsk$
 ^test-voice/sipout-test-voice$
+^tone-detect/g711-tone-detect$
--- a/Makefile	Wed Mar 06 21:33:49 2024 -0800
+++ b/Makefile	Thu Mar 07 00:05:44 2024 -0800
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2
 
-PROGDIR=test-fsk test-voice
+PROGDIR=test-fsk test-voice tone-detect
 LIBDIR=	librtpalloc libsip libutil
 SUBDIR=	${PROGDIR} ${LIBDIR}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tone-detect/Makefile	Thu Mar 07 00:05:44 2024 -0800
@@ -0,0 +1,13 @@
+CC=	gcc
+CFLAGS=	-O2
+PROG=	g711-tone-detect
+
+all:	${PROG}
+
+${PROG}:	${PROG}.c
+	${CC} ${CFLAGS} -o $@ $@.c -lspandsp -lm
+
+install:
+
+clean:
+	rm -f *.o ${PROG} errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tone-detect/g711-tone-detect.c	Thu Mar 07 00:05:44 2024 -0800
@@ -0,0 +1,79 @@
+/*
+ * This program reads a G.711 sample stream recording (typically extracted
+ * from an RTP stream pcap with rtp-g711-extr by the same author) and feeds
+ * it to SpanDSP modem connection tone detector.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <spandsp.h>
+#include "../include/pstn_defs.h"
+
+static char *data_filename;
+static int use_alaw, tone_type_sought;
+static g711_state_t *g711_dec_state;
+static modem_connect_tones_rx_state_t *tones_rx_state;
+static unsigned frame_count;
+
+static void report_func(void *user_data, int code, int level, int delay)
+{
+	printf("@%u.%03us: %s\n", frame_count / 50, (frame_count % 50) * 20,
+		modem_connect_tone_to_str(code));
+}
+
+main(argc, argv)
+	char **argv;
+{
+	FILE *inf;
+	uint8_t g711_buf[FRAME_20MS];
+	int16_t linear[FRAME_20MS];
+	int cc;
+
+	switch (argc) {
+	case 3:
+		data_filename = argv[1];
+		tone_type_sought = atoi(argv[2]);
+		break;
+	case 4:
+		data_filename = argv[1];
+		if (strcmp(argv[2], "alaw"))
+			goto usage;
+		use_alaw = 1;
+		tone_type_sought = atoi(argv[3]);
+		break;
+	default:
+	usage:
+		fprintf(stderr,
+			"usage: %s data-filename [alaw] tone-type-code\n",
+			argv[0]);
+		exit(1);
+	}
+	inf = fopen(data_filename, "r");
+	if (!inf) {
+		perror(data_filename);
+		exit(1);
+	}
+	g711_dec_state = g711_init(NULL, use_alaw ? G711_ALAW : G711_ULAW);
+	if (!g711_dec_state) {
+		fprintf(stderr, "error: g711_init() failed!\n");
+		exit(1);
+	}
+	tones_rx_state = modem_connect_tones_rx_init(NULL, tone_type_sought,
+						     report_func, NULL);
+	if (!tones_rx_state) {
+		fprintf(stderr,
+			"error: modem_connect_tones_rx_init() failed!\n");
+		exit(1);
+	}
+	for (frame_count = 0; ; frame_count++) {
+		cc = fread(g711_buf, 1, FRAME_20MS, inf);
+		if (cc == 0)
+			break;
+		g711_decode(g711_dec_state, linear, g711_buf, cc);
+		modem_connect_tones_rx(tones_rx_state, linear, cc);
+	}
+	exit(0);
+}