view tone-detect/g711-tone-detect.c @ 16:4f81b959a5f5

sipout-test-voice: implement PCMU GSM uplink catcher
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 13 May 2024 22:10:25 -0800
parents ff535725e01f
children
line wrap: on
line source

/*
 * 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);
}