FreeCalypso > hg > gsm-codec-lib
annotate libgsmfrp/prng.c @ 118:cc08498ed21b
libgsmefr: factor out q_plsf_5 tables into their own C module
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 10 Dec 2022 01:28:05 +0000 |
parents | 3b64f255689a |
children |
rev | line source |
---|---|
4
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
108
3b64f255689a
libgsmfrp: factor out PRNG into its own module,
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
2 * We use a pseudorandom sequence generator function in two places: |
3b64f255689a
libgsmfrp: factor out PRNG into its own module,
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
3 * when generating comfort noise (GSM 06.12 section 6.1), and when |
3b64f255689a
libgsmfrp: factor out PRNG into its own module,
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
4 * randomizing grid position parameters as part of speech muting |
3b64f255689a
libgsmfrp: factor out PRNG into its own module,
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
5 * during error handling (GSM 06.11 section 6). |
3b64f255689a
libgsmfrp: factor out PRNG into its own module,
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
6 * |
3b64f255689a
libgsmfrp: factor out PRNG into its own module,
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
7 * Our PRNG is a copy of the pseudonoise() function from ETSI EFR code, |
3b64f255689a
libgsmfrp: factor out PRNG into its own module,
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
8 * where it is used for similar purposes. |
4
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 */ |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdint.h> |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include "gsm_fr_preproc.h" |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include "internal.h" |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
108
3b64f255689a
libgsmfrp: factor out PRNG into its own module,
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
15 uint16_t gsmfr_preproc_prng(struct gsmfr_preproc_state *st, uint16_t no_bits) |
4
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 uint16_t noise_bits, Sn, i; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 noise_bits = 0; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 for (i = 0; i < no_bits; i++) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 /* State n == 31 */ |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 if ((st->cn_random_lfsr & 0x00000001L) != 0) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 Sn = 1; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 else |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 Sn = 0; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 /* State n == 3 */ |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if ((st->cn_random_lfsr & 0x10000000L) != 0) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 Sn = Sn ^ 1; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 else |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 Sn = Sn ^ 0; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 noise_bits = noise_bits << 1; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 noise_bits = noise_bits | st->cn_random_lfsr & 1; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 st->cn_random_lfsr >>= 1; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 if (Sn & 1) |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 { |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 st->cn_random_lfsr |= 0x40000000L; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 } |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 return noise_bits; |
286d5f097eb4
libgsmfrp: implement comfort noise generation
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |