FreeCalypso > hg > gsm-codec-lib
annotate miscutil/wav2raw.c @ 242:f081a6850fb5
libgsmfrp: new refined implementation
The previous implementation exhibited the following defects,
which are now fixed:
1) The last received valid SID was cached forever for the purpose of
handling future invalid SIDs - we could have received some valid
SID ages ago, then lots of speech or NO_DATA, and if we then get
an invalid SID, we would resurrect the last valid SID from ancient
history - a bad design. In our new design, we handle invalid SID
based on the current state, much like BFI.
2) GSM 06.11 spec says clearly that after the second lost SID
(received BFI=1 && TAF=1 in CN state) we need to gradually decrease
the output level, rather than jump directly to emitting silence
frames - we previously failed to implement such logic.
3) Per GSM 06.12 section 5.2, Xmaxc should be the same in all 4 subframes
in a SID frame. What should we do if we receive an otherwise valid
SID frame with different Xmaxc? Our previous approach would
replicate this Xmaxc oddity in every subsequent generated CN frame,
which is rather bad. In our new design, the very first CN frame
(which can be seen as a transformation of the SID frame itself)
retains the original 4 distinct Xmaxc, but all subsequent CN frames
are based on the Xmaxc from the last subframe of the most recent SID.
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 09 May 2023 05:16:31 +0000 |
parents | 578fdedf4327 |
children |
rev | line source |
---|---|
142
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This program reads a 16-bit linear PCM speech recording in WAV format |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * and converts it to raw format, either BE or LE. |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdio.h> |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdint.h> |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdlib.h> |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <string.h> |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <strings.h> |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include "../libtest/wavreader.h" |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include "../libtest/wavrdhelp.h" |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 main(argc, argv) |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 char **argv; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 { |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 int big_endian; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 void *wav; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 FILE *outf; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 int16_t pcm[160], samp; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 uint8_t bytes[320], *dp; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 int cc, rc, i; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 if (argc != 4) { |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 usage: fprintf(stderr, "usage: %s input.wav output.raw be|le\n", |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 argv[0]); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 exit(1); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 } |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 if (!strcmp(argv[3], "be")) |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 big_endian = 1; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 else if (!strcmp(argv[3], "le")) |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 big_endian = 0; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 else |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 goto usage; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 wav = wav_read_open(argv[1]); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 if (!wav) { |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 perror(argv[1]); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 exit(1); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 } |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 rc = wavrd_check_header(wav, argv[1]); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 if (rc < 0) |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 exit(1); /* error msg already printed */ |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 outf = fopen(argv[2], "w"); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 if (!outf) { |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 perror(argv[2]); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 exit(1); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 for (;;) { |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 cc = wavrd_get_pcm_block(wav, pcm); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 if (!cc) |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 break; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 dp = bytes; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 for (i = 0; i < cc; i++) { |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 samp = pcm[i]; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (big_endian) { |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 *dp++ = (samp >> 8) & 0xFF; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 *dp++ = samp & 0xFF; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 } else { |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 *dp++ = samp & 0xFF; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 *dp++ = (samp >> 8) & 0xFF; |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 } |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 fwrite(bytes, 2, cc, outf); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 } |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 fclose(outf); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 exit(0); |
578fdedf4327
pcm16-wav2raw utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 } |