view tchtools/fc-vm2gsmx.c @ 964:a96cb97b66a2

ringtools/imy: fix duplicate definition of tdma_durations[] The bug was reported by Vadim Yanitskiy <fixeria@osmocom.org>, although the present fix is slightly different from the contributed patch: because main.c doesn't need this tdma_durations[] array at all, let's simply remove the reference to this array from main.c rather than turn it into an extern. I no longer remember my original thought flow that resulted (by mistake) in tdma_durations[] being multiply defined in main.c and durations.c. My intent might have been to define all globals in main.c and have the reference in durations.c be an extern - and I missed that extern - but without clear memory, I have no certainty. In any case, having this data array defined in the same module that fills it (durations.c) is sensible, so let's make it the new way.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 31 Aug 2023 19:38:18 +0000
parents 546bf873ccc8
children
line wrap: on
line source

/*
 * This utility converts old-fashioned (non-AMR) TCS211 voice memo files
 * read out of FFS into Themyscira Wireless gsmx (extended-libgsm) format,
 * allowing further decoding into playable speech with gsmfr-decode.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>

static char *infname;
static FILE *inf, *outf;
static u_long file_offset;

static const u_char bfi_marker[2] = {0xBF, 0x00};

static unsigned
get_word()
{
	u_char b[2];
	int i, c;

	for (i = 0; i < 2; i++) {
		c = getc(inf);
		if (c < 0) {
			fprintf(stderr, "error: premature EOF in %s\n",
				infname);
			exit(1);
		}
		b[i] = c;
		file_offset++;
	}
	return((b[1] << 8) | b[0]);
}

static void
read_dsp_bytes(bytes)
	u_char *bytes;
{
	int i, dp;
	unsigned word;

	dp = 0;
	for (i = 0; i < 17; i++) {
		word = get_word();
		bytes[dp++] = word >> 8;
		bytes[dp++] = word;
	}
}

static void
convert_speech_sample()
{
	u_char tidsp_bytes[34], libgsm_bytes[33];

	read_dsp_bytes(tidsp_bytes);
	gsm0610_tidsp_to_libgsm(tidsp_bytes, libgsm_bytes);
	fwrite(libgsm_bytes, 1, 33, outf);
}

main(argc, argv)
	char **argv;
{
	unsigned first_word;

	if (argc != 3) {
		fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
		exit(1);
	}
	infname = argv[1];
	inf = fopen(infname, "r");
	if (!inf) {
		perror(infname);
		exit(1);
	}
	outf = fopen(argv[2], "w");
	if (!outf) {
		perror(argv[2]);
		exit(1);
	}
	for (;;) {
		first_word = get_word();
		if (first_word == 0xFBFF)	/* SC_VM_END_MASK */
			break;
		if ((first_word & 0xB7FF) == 0x8400) {
			/* skip dummy header words 1 and 2 */
			get_word();
			get_word();
			/* process the actual speech or SID frame */
			convert_speech_sample();
		} else if (first_word == 0x0400) {
			/* it's a skipped frame, aka BFI */
			fwrite(bfi_marker, 1, 2, outf);
		} else {
			fprintf(stderr,
	"error in %s at offset 0x%lx: invalid frame header word 0x%04X\n",
				infname, file_offset, first_word);
			exit(1);
		}
	}
	fclose(outf);
	exit(0);
}