annotate miscutil/amr2efr.c @ 101:d86f866489e9

gsm-amr2efr utility written
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 27 Nov 2022 05:15:39 +0000
parents
children dd88f9312170
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
101
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Our gsm-amr2efr utility reads in a speech recording in .amr format
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * (the mode must be MR122 for every frame, no DTX) and converts it into
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * our ad hoc .gsmx format for EFR. The conversion is a bit reshuffling
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * only, no transcoding takes place.
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdint.h>
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <string.h>
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <strings.h>
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern const uint8_t amr_122_bit_order[244];
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 main(argc, argv)
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 char **argv;
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 FILE *inf, *outf;
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 int hdrb;
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 uint8_t frm_in[31], frm_out[31];
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 unsigned bit_a, bit_n;
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 if (argc != 3) {
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 fprintf(stderr, "usage: %s input.amr output.gsmx\n", argv[0]);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 exit(1);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 inf = fopen(argv[1], "r");
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (!inf) {
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 perror(argv[1]);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 exit(1);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 outf = fopen(argv[2], "w");
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 if (!outf) {
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 perror(argv[2]);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 exit(1);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 for (;;) {
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 hdrb = getc(inf);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (hdrb < 0)
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 break;
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 if ((hdrb & 0x78) != 0x38) {
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 fprintf(stderr, "error: unexpected content in %s\n",
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 argv[1]);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 exit(1);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (fread(frm_in, 1, 31, inf) != 31) {
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 fprintf(stderr, "error: short read from %s\n", argv[1]);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 exit(1);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 bzero(frm_out, 31);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 frm_out[0] = 0xC0;
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 for (bit_a = 0; bit_a < 244; bit_a++) {
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 bit_n = amr_122_bit_order[bit_a];
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 msb_set_bit(frm_out, 4 + bit_n,
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 msb_get_bit(frm_in, bit_a));
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 fwrite(frm_out, 1, 31, outf);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 fclose(outf);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 exit(0);
d86f866489e9 gsm-amr2efr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 }