annotate libtwamr/ex_ctrl.c @ 395:8c7d5eec544c

libtwamr: integrate weight_a.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 06 May 2024 18:52:57 +0000
parents 3f27ca24c620
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
364
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 *
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * R99 Version 3.3.0
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * REL-4 Version 4.1.0
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 *
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 ********************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 *
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 * File : ex_ctrl.c
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 * Purpose : Excitation Control module in background noise
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 *
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 ********************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 /*
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 ********************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 * MODULE INCLUDE FILE AND VERSION ID
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 ********************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 #include "namespace.h"
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 #include "ex_ctrl.h"
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 #include "typedef.h"
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 #include "basic_op.h"
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 #include "no_count.h"
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 #include "cnst.h"
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 #include "memops.h"
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 #include "gmed_n.h"
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 #include "sqrt_l.h"
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 /*
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 ********************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 * LOCAL VARIABLES AND TABLES
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 ********************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 /*-----------------------------------------------------------------*
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 * Decoder constant parameters (defined in "cnst.h") *
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 *-----------------------------------------------------------------*
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 * L_FRAME : Frame size. *
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 * L_SUBFR : Sub-frame size. *
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 *-----------------------------------------------------------------*/
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 /*
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 ********************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 * PUBLIC PROGRAM CODE
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 ********************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 /*
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 **************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 *
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 * Function : Ex_ctrl
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 * Purpose : Charaterice synthesis speech and detect background noise
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 * Returns : background noise decision; 0 = no bgn, 1 = bgn
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 *
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 **************************************************************************
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 Word16 Ex_ctrl (Word16 excitation[], /*i/o: Current subframe excitation */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 Word16 excEnergy, /* i : Exc. Energy, sqrt(totEx*totEx)*/
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 Word16 exEnergyHist[], /* i : History of subframe energies */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 Word16 voicedHangover, /* i : # of fr. after last voiced fr.*/
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 Word16 prevBFI, /* i : Set i previous BFI */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 Word16 carefulFlag /* i : Restrict dymamic in scaling */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 )
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 {
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 Word16 i, exp;
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 Word16 testEnergy, scaleFactor, avgEnergy, prevEnergy;
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 Word32 t0;
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 /* get target level */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 avgEnergy = gmed_n(exEnergyHist, 9); move16();
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 prevEnergy = shr( add (exEnergyHist[7], exEnergyHist[8]) ,1);
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 test ();
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 if ( sub (exEnergyHist[8], prevEnergy) < 0)
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 {
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 prevEnergy = exEnergyHist[8]; move16 ();
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 /* upscaling to avoid too rapid energy rises for some cases */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 test (); test ();
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if ( sub (excEnergy, avgEnergy) < 0 && sub (excEnergy, 5) > 0)
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 {
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 testEnergy = shl(prevEnergy, 2); /* testEnergy = 4*prevEnergy; */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 test (); test ();
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 if ( sub (voicedHangover, 7) < 0 || prevBFI != 0 )
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 {
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 /* testEnergy = 3*prevEnergy */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 testEnergy = sub (testEnergy, prevEnergy);
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 }
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 test ();
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 if ( sub (avgEnergy, testEnergy) > 0)
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 {
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 avgEnergy = testEnergy; move16 ();
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 }
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 /* scaleFactor=avgEnergy/excEnergy in Q0 (const 29 below)*/
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 exp = norm_s (excEnergy);
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 excEnergy = shl (excEnergy, exp);
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 excEnergy = div_s ((Word16) 16383, excEnergy);
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 t0 = L_mult (avgEnergy, excEnergy);
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 t0 = L_shr (t0, sub (20, exp)); /* const=30 for t0 in Q0, 20 for Q10 */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 if ( L_sub(t0, 32767) > 0 )
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 {
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 t0 = 32767; move32 (); /* saturate */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 scaleFactor = extract_l (t0);
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 /* test if scaleFactor > 3.0 */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 test (); test ();
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 if ( carefulFlag != 0 && sub(scaleFactor, 3072) > 0 )
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 {
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 scaleFactor = 3072; move16 ();
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 }
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 /* scale the excitation by scaleFactor */
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 for (i = 0; i < L_SUBFR; i++)
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 {
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 t0 = L_mult (scaleFactor, excitation[i]);
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 t0 = L_shr (t0, 11);
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 excitation[i] = extract_l (t0);
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 }
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 }
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 return 0;
3f27ca24c620 libtwamr: integrate ex_ctrl.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 }