diff hrutil/rpf2hex.c @ 567:2fcb6b27ee9b

hrutil: new program gsmhr-rpf2hex
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 12 Feb 2025 02:32:21 +0000
parents miscutil/gsmx-to-tw5a.c@5595293e4f29
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hrutil/rpf2hex.c	Wed Feb 12 02:32:21 2025 +0000
@@ -0,0 +1,80 @@
+/*
+ * This program converts HRv1 speech recordings from ETSI TS 101 318 raw
+ * packed format (14 bytes per frame, good frames only) into our preferred
+ * TW-TS-005 Annex B format.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include "../libgsmhr1/tw_gsmhr.h"
+
+static void
+emit_hr1_hex_frame(outf, frame, emit_5993)
+	FILE *outf;
+	uint8_t *frame;
+{
+	unsigned n;
+
+	if (emit_5993) {
+		fprintf(outf, "%02X",
+			gsmhr_ts101318_is_perfect_sid(frame) << 4);
+	}
+	for (n = 0; n < GSMHR_FRAME_LEN_RPF; n++)
+		fprintf(outf, "%02X", frame[n]);
+	putc('\n', outf);
+}
+
+main(argc, argv)
+	char **argv;
+{
+	char *infname, *outfname;
+	FILE *inf, *outf;
+	int opt, cc, emit_5993 = 0;
+	uint8_t frame[GSMHR_FRAME_LEN_RPF];
+	extern int optind;
+
+	while ((opt = getopt(argc, argv, "x")) != EOF) {
+		switch (opt) {
+		case 'x':
+			emit_5993 = 1;
+			continue;
+		default:
+		usage:
+			fprintf(stderr,
+				"usage: %s [-x] input.hrpf output.hex\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+	if (argc != optind + 2)
+		goto usage;
+	infname = argv[optind];
+	outfname = argv[optind+1];
+
+	inf = fopen(infname, "r");
+	if (!inf) {
+		perror(infname);
+		exit(1);
+	}
+	outf = fopen(outfname, "w");
+	if (!outf) {
+		perror(outfname);
+		exit(1);
+	}
+
+	for (;;) {
+		cc = fread(frame, 1, GSMHR_FRAME_LEN_RPF, inf);
+		if (cc == 0)
+			break;
+		if (cc != GSMHR_FRAME_LEN_RPF) {
+			fprintf(stderr, "error: short read from %s\n", infname);
+			exit(1);
+		}
+		emit_hr1_hex_frame(outf, frame, emit_5993);
+	}
+	exit(0);
+}