changeset 445:30cff6b60890

amrtest: implement twamr-encode-r
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 10 May 2024 00:07:07 +0000
parents fe4983b05230
children e2f824ffd08a
files .hgignore amrtest/Makefile amrtest/encode-r.c
diffstat 3 files changed, 95 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu May 09 23:35:25 2024 +0000
+++ b/.hgignore	Fri May 10 00:07:07 2024 +0000
@@ -16,6 +16,7 @@
 
 ^amrtest/twamr-decode$
 ^amrtest/twamr-encode$
+^amrtest/twamr-encode-r$
 ^amrtest/twamr-tseq-dec$
 ^amrtest/twamr-tseq-enc$
 
--- a/amrtest/Makefile	Thu May 09 23:35:25 2024 +0000
+++ b/amrtest/Makefile	Fri May 10 00:07:07 2024 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	twamr-decode twamr-encode twamr-tseq-dec twamr-tseq-enc
+PROGS=	twamr-decode twamr-encode twamr-encode-r twamr-tseq-dec twamr-tseq-enc
 LIBAMR=	../libtwamr/libtwamr.a
 LIBTEST=../libtest/libtest.a
 INSTBIN=/opt/freecalypso/bin
@@ -13,6 +13,9 @@
 twamr-encode:	encode.o mode_file.o mode_kw.o ${LIBAMR} ${LIBTEST}
 	${CC} ${CFLAGS} -o $@ $^
 
+twamr-encode-r:	encode-r.o mode_file.o mode_kw.o ${LIBAMR} ${LIBTEST}
+	${CC} ${CFLAGS} -o $@ $^
+
 twamr-tseq-dec:	tseq-dec.o ${LIBAMR}
 	${CC} ${CFLAGS} -o $@ $^
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/amrtest/encode-r.c	Fri May 10 00:07:07 2024 +0000
@@ -0,0 +1,90 @@
+/*
+ * twamr-encode-r is a speech encoder utility going from "robe" (raw BE)
+ * input to RFC 4867 storage format (.amr) output.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include "../libtwamr/tw_amr.h"
+#include "../libtest/roberead.h"
+
+main(argc, argv)
+	char **argv;
+{
+	char *infname, *outfname, *mode_arg;
+	FILE *inf, *outf;
+	struct amr_encoder_state *state;
+	enum Mode mode_val;
+	int16_t pcm[160];
+	struct amr_param_frame frame;
+	uint8_t out_bytes[AMR_IETF_MAX_PL];
+	int opt, dtx = 0, vad2 = 0, use_mode_file = 0, rc;
+	unsigned nbytes;
+	extern int optind;
+
+	while ((opt = getopt(argc, argv, "d2")) != EOF) {
+		switch (opt) {
+		case 'd':
+			dtx = 1;
+			continue;
+		case '2':
+			vad2 = 1;
+			continue;
+		default:
+		usage:
+			fprintf(stderr,
+			"usage: %s [-d] [-2] input.robe mode output.amr\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+	if (argc != optind + 3)
+		goto usage;
+	infname = argv[optind];
+	mode_arg = argv[optind+1];
+	outfname = argv[optind+2];
+
+	inf = fopen(infname, "r");
+	if (!inf) {
+		perror(infname);
+		exit(1);
+	}
+	if (!strncmp(mode_arg, "file:", 5)) {
+		open_mode_file(mode_arg + 5);
+		use_mode_file = 1;
+	} else {
+		rc = grok_mode_name(mode_arg, &mode_val);
+		if (rc < 0) {
+			fprintf(stderr, "error: invalid mode argument \"%s\"\n",
+				mode_arg);
+			exit(1);
+		}
+	}
+	outf = fopen(outfname, "w");
+	if (!outf) {
+		perror(outfname);
+		exit(1);
+	}
+	state = amr_encoder_create(dtx, vad2);
+	if (!state) {
+		perror("amr_encoder_create()");
+		exit(1);
+	}
+	fwrite(amr_file_header_magic, 1, AMR_IETF_HDR_LEN, outf);
+	for (;;) {
+		rc = robe_get_pcm_block(inf, pcm);
+		if (!rc)
+			break;
+		if (use_mode_file)
+			read_mode_file_line(&mode_val);
+		amr_encode_frame(state, mode_val, pcm, &frame);
+		nbytes = amr_frame_to_ietf(&frame, out_bytes);
+		fwrite(out_bytes, 1, nbytes, outf);
+	}
+	fclose(outf);
+	exit(0);
+}