changeset 19:4ab8762be333

trau-ul-prep: starting with gsmx2tsrc program
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 23 Jun 2024 21:55:03 +0000
parents b36419bbc2c4
children 7cd046ffefe7
files .hgignore trau-ul-prep/Makefile trau-ul-prep/binreader.c trau-ul-prep/binreader.h trau-ul-prep/gsmx2tsrc.c
diffstat 5 files changed, 148 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed May 29 23:11:28 2024 +0000
+++ b/.hgignore	Sun Jun 23 21:55:03 2024 +0000
@@ -10,3 +10,5 @@
 ^trau-files/.*\.dump$
 ^trau-files/.*\.gsmx$
 ^trau-files/.*\.wav$
+
+^trau-ul-prep/gsmx2tsrc$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trau-ul-prep/Makefile	Sun Jun 23 21:55:03 2024 +0000
@@ -0,0 +1,11 @@
+CC=	gcc
+CFLAGS=	-O2
+PROGS=	gsmx2tsrc
+
+all:	${PROGS}
+
+gsmx2tsrc:	binreader.o gsmx2tsrc.o
+	${CC} ${CFLAGS} -o $@ $^ -lgsmfr2 -lgsmefr
+
+clean:
+	rm -f *.o ${PROGS}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trau-ul-prep/binreader.c	Sun Jun 23 21:55:03 2024 +0000
@@ -0,0 +1,29 @@
+/*
+ * Here we implement our binfile_read_frame() function.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include "binreader.h"
+
+int binfile_read_frame(FILE *binf, uint8_t *frame)
+{
+	int cc, morelen;
+
+	cc = fread(frame, 1, 1, binf);
+	if (cc != 1)
+		return 0;
+	if (frame[0] == 0xBF)
+		morelen = 1;
+	else if ((frame[0] & 0xF0) == 0xC0)
+		morelen = 30;
+	else if ((frame[0] & 0xF0) == 0xD0)
+		morelen = 32;
+	else
+		return -1;
+	cc = fread(frame+1, 1, morelen, binf);
+	if (cc == morelen)
+		return 1;
+	else
+		return -2;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trau-ul-prep/binreader.h	Sun Jun 23 21:55:03 2024 +0000
@@ -0,0 +1,14 @@
+/*
+ * This header file defines the interface to our reader function for binary
+ * files recording GSM FR or EFR streams, see ../doc/Binary-file-format.
+ *
+ * binfile_read_frame() return values are:
+ *  1 = successfully read valid frame
+ *  0 = normal EOF
+ * -1 = unrecognized header byte
+ * -2 = EOF in the middle of a frame
+ */
+
+#define	BINFILE_MAX_FRAME	33
+
+extern int binfile_read_frame(FILE *binf, uint8_t *frame);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trau-ul-prep/gsmx2tsrc.c	Sun Jun 23 21:55:03 2024 +0000
@@ -0,0 +1,92 @@
+/*
+ * This program reads a binary file in ThemWi gsmx format
+ * and converts it into ASCII source for TRAU-UL construction.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <tw_gsmfr.h>
+#include <gsm_efr.h>
+#include "binreader.h"
+
+main(argc, argv)
+	char **argv;
+{
+	FILE *binf, *outf;
+	unsigned frame_index;
+	uint8_t frame[BINFILE_MAX_FRAME];
+	int16_t params[GSMFR_NUM_PARAMS];
+	int rc, i, j, n;
+
+	if (argc < 2 || argc > 3) {
+		fprintf(stderr, "usage: %s input.gsmx [output.tsrc]\n",
+			argv[0]);
+		exit(1);
+	}
+	binf = fopen(argv[1], "r");
+	if (!binf) {
+		perror(argv[1]);
+		exit(1);
+	}
+	if (argc > 2) {
+		outf = fopen(argv[2], "w");
+		if (!outf) {
+			perror(argv[2]);
+			exit(1);
+		}
+	} else
+		outf = stdout;
+	for (frame_index = 0; ; frame_index++) {
+		rc = binfile_read_frame(binf, frame);
+		if (rc < 0) {
+			fprintf(stderr, "error: garbage in %s\n", argv[1]);
+			exit(1);
+		}
+		if (!rc)
+			break;
+		switch (frame[0] & 0xF0) {
+		case 0xB0:
+			fprintf(outf, "# input frame %u is BFI\n\n",
+				frame_index);
+			break;
+		case 0xC0:
+			fprintf(outf, "Frame_EFR {\t# input frame %u\n",
+				frame_index);
+			EFR_frame2params(frame, params);
+			n = 0;
+			fputs("\tLPC", outf);
+			for (i = 0; i < 5; i++)
+				fprintf(outf, " %d", params[n++]);
+			putc('\n', outf);
+			for (i = 0; i < 4; i++) {
+				fputs("\tsf", outf);
+				for (j = 0; j < 13; j++)
+					fprintf(outf, " %d", params[n++]);
+				putc('\n', outf);
+			}
+			fputs("}\n\n", outf);
+			break;
+		case 0xD0:
+			fprintf(outf, "Frame_FR {\t# input frame %u\n",
+				frame_index);
+			gsmfr_unpack_to_array(frame, params);
+			n = 0;
+			fputs("\tLARc", outf);
+			for (i = 0; i < 8; i++)
+				fprintf(outf, " %d", params[n++]);
+			putc('\n', outf);
+			for (i = 0; i < 4; i++) {
+				fputs("\tsf", outf);
+				for (j = 0; j < 17; j++)
+					fprintf(outf, " %d", params[n++]);
+				putc('\n', outf);
+			}
+			fputs("}\n\n", outf);
+			break;
+		}
+	}
+	exit(0);
+}