FreeCalypso > hg > gsm-codec-lib
annotate miscutil/efr2amr.c @ 103:0123ca1f1402
gsm-efr2amr utility written
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 27 Nov 2022 05:41:13 +0000 |
parents | |
children | 7f43089e9a7a |
rev | line source |
---|---|
103
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * Our gsm-efr2amr utility reads in an EFR speech recording in our |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * extended-libgsm file format and converts it to AMR, inserting |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * an AMR NO_DATA frame in the place of every BFI in the input |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * but NOT performing any special handling or even detection of |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 * EFR SID frames. |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 */ |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdint.h> |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdlib.h> |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <string.h> |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <strings.h> |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include "../libtest/binreader.h" |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 extern const uint8_t amr_122_bit_order[244]; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 static const char amr_file_hdr[] = "#!AMR\n"; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 main(argc, argv) |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 char **argv; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 { |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 FILE *inf, *outf; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 uint8_t frm_in[BINFILE_MAX_FRAME], frm_out[32]; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 unsigned bit_a, bit_n, outlen; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 int rc, bfi; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 if (argc != 3) { |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 fprintf(stderr, "usage: %s input.gsmx output.amr\n", argv[0]); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 exit(1); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 inf = fopen(argv[1], "r"); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if (!inf) { |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 perror(argv[1]); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 exit(1); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 outf = fopen(argv[2], "w"); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 if (!outf) { |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 perror(argv[2]); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 exit(1); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 } |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 for (;;) { |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 rc = binfile_read_frame(inf, frm_in); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 if (rc < 0) { |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 fprintf(stderr, "error: garbage in %s\n", argv[1]); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 exit(1); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 if (!rc) |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 break; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 if (frm_in[0] == 0xBF) |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 bfi = 1; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 else if ((frm_in[0] & 0xF0) == 0xC0) |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 bfi = 0; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 else { |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 fprintf(stderr, |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 "error: %s is not in EFR codec format\n", |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 argv[1]); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 exit(1); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 } |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 if (bfi) { |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 frm_out[0] = 0x78; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 outlen = 1; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 } else { |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 frm_out[0] = 0x3C; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 bzero(frm_out + 1, 31); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 for (bit_a = 0; bit_a < 244; bit_a++) { |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 bit_n = amr_122_bit_order[bit_a]; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 msb_set_bit(frm_out + 1, bit_a, |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 msb_get_bit(frm_in, 4 + bit_n)); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 } |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 outlen = 32; |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 } |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 fwrite(frm_out, 1, outlen, outf); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 } |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 fclose(outf); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 exit(0); |
0123ca1f1402
gsm-efr2amr utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 } |