view hrutil/rpf2hex.c @ 579:1dc5d9320e96

libgsmhr1: implement RxFE block
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 13 Feb 2025 09:10:12 +0000
parents 2fcb6b27ee9b
children
line wrap: on
line source

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