annotate libtwamr/vad2.c @ 468:4104b0390fab

efrtest: new program gsmefr-dlcap-sync
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 13 May 2024 07:21:09 +0000
parents 0152c069d01f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
410
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 *****************************************************************************
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * R99 Version 3.3.0
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * REL-4 Version 4.1.0
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 *****************************************************************************
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 * File : vad2.c
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 * Purpose : Voice Activity Detection (VAD) for AMR (option 2)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 *****************************************************************************
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 /***************************************************************************
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 * FUNCTION NAME: vad2()
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 * PURPOSE:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 * This function provides the Voice Activity Detection function option 2
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 * for the Adaptive Multi-rate (AMR) codec.
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 * INPUTS:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 * farray_ptr
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 * pointer to Word16[80] input array
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 * vadState2
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 * pointer to vadState2 state structure
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 * OUTPUTS:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 * state variables are updated
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 * RETURN VALUE:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 * Word16
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 * VAD(m) - two successive calls to vad2() yield
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 * the VAD decision for the 20 ms frame:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 * VAD_flag = VAD(m-1) || VAD(m)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 *************************************************************************/
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 /* Includes */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 #include <stdint.h>
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 #include <string.h>
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 #include "tw_amr.h"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 #include "namespace.h"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 #include "typedef.h"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 #include "cnst.h"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 #include "basic_op.h"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 #include "oper_32b.h"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 #include "no_count.h"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 #include "log2.h"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 #include "pow2.h"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 #include "vad2.h"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 /* Local functions */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 /***************************************************************************
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 * FUNCTION NAME: fn10Log10
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 * PURPOSE:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 * The purpose of this function is to take the 10*log base 10 of input and
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 * divide by 128 and return; i.e. output = 10*log10(input)/128 (scaled as 7,8)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 * INPUTS:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 * L_Input
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 * input (scaled as 31-fbits,fbits)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 * fbits
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 * number of fractional bits on input
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 * OUTPUTS:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 * none
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 * RETURN VALUE:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 * Word16
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 * output (scaled as 7,8)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 * DESCRIPTION:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 * 10*log10(x)/128 = 10*(log10(2) * (log2(x<<fbits)-log2(1<<fbits)) >> 7
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 * = 3.0103 * (log2(x<<fbits) - fbits) >> 7
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 * = ((3.0103/4.0 * (log2(x<<fbits) - fbits) << 2) >> 7
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 * = (3.0103/4.0 * (log2(x<<fbits) - fbits) >> 5
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 *************************************************************************/
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 static Word16 fn10Log10 (Word32 L_Input, Word16 fbits)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 Word16 integer; /* Integer part of Log2. (range: 0<=val<=30) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 Word16 fraction; /* Fractional part of Log2. (range: 0<=val<1) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 Word32 Ltmp;
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 Word16 tmp;
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 Log2(L_Input, &integer, &fraction);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 integer = sub(integer, fbits);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 Ltmp = Mpy_32_16 (integer, fraction, 24660); /* 24660 = 10*log10(2)/4 scaled 0,15 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 Ltmp = L_shr_r(Ltmp, 5+1); /* extra shift for 30,1 => 15,0 extract correction */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 tmp = extract_l(Ltmp);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 return (tmp);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 /***************************************************************************
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 * FUNCTION NAME: block_norm
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 * PURPOSE:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 * The purpose of this function is block normalise the input data sequence
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 * INPUTS:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 * &in[0]
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 * pointer to data sequence to be normalised
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 * length
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 * number of elements in data sequence
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 * headroom
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 * number of headroom bits (i.e.,
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 * OUTPUTS:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 * &out[0]
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 * normalised output data sequence pointed to by &out[0]
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 * RETURN VALUE:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 * Word16
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 * number of bits sequence was left shifted
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 * DESCRIPTION:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 * 1) Search for maximum absolute valued data element
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 * 2) Normalise the max element with "headroom"
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 * 3) Transfer/shift the input sequence to the output buffer
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 * 4) Return the number of left shifts
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 * CAVEATS:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 * An input sequence of all zeros will return the maximum
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 * number of left shifts allowed, NOT the value returned
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 * by a norm_s(0) call, since it desired to associate an
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 * all zeros sequence with low energy.
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 *************************************************************************/
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 static
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 Word16 block_norm (Word16 * in, Word16 * out, Word16 length, Word16 headroom)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 Word16 i, max, scnt, adata;
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 max = abs_s(in[0]);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 for (i = 1; i < length; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 adata = abs_s(in[i]); test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 if (sub(adata, max) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 max = adata; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173 if (max != 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 scnt = sub(norm_s(max), headroom);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 for (i = 0; i < length; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 out[i] = shl(in[i], scnt); move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 scnt = sub(16, headroom);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 for (i = 0; i < length; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 out[i] = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 return (scnt);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 /********************************************* The VAD function ***************************************************/
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 Word16 vad2 (Word16 * farray_ptr, vadState2 * st)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 /*
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 * The channel table is defined below. In this table, the
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 * lower and higher frequency coefficients for each of the 16
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 * channels are specified. The table excludes the coefficients
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 * with numbers 0 (DC), 1, and 64 (Foldover frequency).
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 static const Word16 ch_tbl[NUM_CHAN][2] =
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 {2, 3},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 {4, 5},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 {6, 7},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 {8, 9},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 {10, 11},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 {12, 13},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 {14, 16},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 {17, 19},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 {20, 22},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 {23, 26},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 {27, 30},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 {31, 35},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 {36, 41},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 {42, 48},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 {49, 55},
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 {56, 63}
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 };
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 /* channel energy scaling table - allows efficient division by number
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 * of DFT bins in the channel: 1/2, 1/3, 1/4, etc.
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
229 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231 static const Word16 ch_tbl_sh[NUM_CHAN] =
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
232 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
233 16384, 16384, 16384, 16384, 16384, 16384, 10923, 10923,
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
234 10923, 8192, 8192, 6554, 5461, 4681, 4681, 4096
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 };
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 /*
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
238 * The voice metric table is defined below. It is a non-
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239 * linear table with a deadband near zero. It maps the SNR
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 * index (quantized SNR value) to a number that is a measure
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
241 * of voice quality.
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
242 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
243
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
244 static const Word16 vm_tbl[90] =
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
245 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
246 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
247 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7,
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
248 8, 8, 9, 9, 10, 10, 11, 12, 12, 13, 13, 14, 15,
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
249 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 23, 24,
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
250 24, 25, 26, 27, 28, 28, 29, 30, 31, 32, 33, 34,
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
251 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45,
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
252 46, 47, 48, 49, 50, 50, 50, 50, 50, 50, 50, 50,
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
253 50, 50
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
254 };
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
255
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
256 /* hangover as a function of peak SNR (3 dB steps) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
257 static const Word16 hangover_table[20] =
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
258 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
259 30, 30, 30, 30, 30, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 8, 8, 8
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
260 };
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
261
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
262 /* burst sensitivity as a function of peak SNR (3 dB steps) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
263 static const Word16 burstcount_table[20] =
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
264 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
265 8, 8, 8, 8, 8, 8, 8, 8, 7, 6, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
266 };
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
267
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
268 /* voice metric sensitivity as a function of peak SNR (3 dB steps) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
269 static const Word16 vm_threshold_table[20] =
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
270 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
271 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 40, 51, 71, 100, 139, 191, 257, 337, 432
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
272 };
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
273
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
274
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
275 /* State tables that use 22,9 or 27,4 scaling for ch_enrg[] */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
276
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
277 static const Word16 noise_floor_chan[2] = {NOISE_FLOOR_CHAN_0, NOISE_FLOOR_CHAN_1};
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
278 static const Word16 min_chan_enrg[2] = {MIN_CHAN_ENRG_0, MIN_CHAN_ENRG_1};
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
279 static const Word16 ine_noise[2] = {INE_NOISE_0, INE_NOISE_1};
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
280 static const Word16 fbits[2] = {FRACTIONAL_BITS_0, FRACTIONAL_BITS_1};
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
281 static const Word16 state_change_shift_r[2] = {STATE_1_TO_0_SHIFT_R, STATE_0_TO_1_SHIFT_R};
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
282
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
283 /* Energy scale table given 30,1 input scaling (also account for -6 dB shift on input) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
284 static const Word16 enrg_norm_shift[2] = {(FRACTIONAL_BITS_0-1+2), (FRACTIONAL_BITS_1-1+2)};
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
285
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
286
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
287 /* Automatic variables */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
288
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
289 Word32 Lenrg; /* scaled as 30,1 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
290 Word32 Ltne; /* scaled as 22,9 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
291 Word32 Ltce; /* scaled as 22,9 or 27,4 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
292
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
293 Word16 tne_db; /* scaled as 7,8 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
294 Word16 tce_db; /* scaled as 7,8 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
295
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
296 Word16 input_buffer[FRM_LEN]; /* used for block normalising input data */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
297 Word16 data_buffer[FFT_LEN]; /* used for in-place FFT */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
298
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
299 Word16 ch_snr[NUM_CHAN]; /* scaled as 7,8 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
300 Word16 ch_snrq; /* scaled as 15,0 (in 0.375 dB steps) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
301 Word16 vm_sum; /* scaled as 15,0 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
302 Word16 ch_enrg_dev; /* scaled as 7,8 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
303
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
304 Word32 Lpeak; /* maximum channel energy */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
305 Word16 p2a_flag; /* flag to indicate spectral peak-to-average ratio > 10 dB */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
306
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
307 Word16 ch_enrg_db[NUM_CHAN]; /* scaled as 7,8 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
308 Word16 ch_noise_db; /* scaled as 7,8 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
309
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
310 Word16 alpha; /* scaled as 0,15 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
311 Word16 one_m_alpha; /* scaled as 0,15 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
312 Word16 update_flag; /* set to indicate a background noise estimate update */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
313
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
314 Word16 i, j, j1, j2; /* Scratch variables */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
315 Word16 hi1, lo1;
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
316
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
317 Word32 Ltmp, Ltmp1, Ltmp2;
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
318 Word16 tmp;
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
319
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
320 Word16 normb_shift; /* block norm shift count */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
321
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
322 Word16 ivad; /* intermediate VAD decision (return value) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
323 Word16 tsnrq; /* total signal-to-noise ratio (quantized 3 dB steps) scaled as 15,0 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
324 Word16 xt; /* instantaneous frame SNR in dB, scaled as 7,8 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
325
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
326 Word16 state_change;
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
327
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
328
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
329 /* Increment frame counter */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
330 st->Lframe_cnt = L_add(st->Lframe_cnt, 1);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
331
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
332 /* Block normalize the input */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
333 normb_shift = block_norm(farray_ptr, input_buffer, FRM_LEN, FFT_HEADROOM);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
334
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
335 /* Pre-emphasize the input data and store in the data buffer with the appropriate offset */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
336 for (i = 0; i < DELAY; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
337 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
338 data_buffer[i] = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
339 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
340
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
341 st->pre_emp_mem = shr_r(st->pre_emp_mem, sub(st->last_normb_shift, normb_shift));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
342 st->last_normb_shift = normb_shift; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
343
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
344 data_buffer[DELAY] = add(input_buffer[0], mult(PRE_EMP_FAC, st->pre_emp_mem)); move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
345
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
346 for (i = DELAY + 1, j = 1; i < DELAY + FRM_LEN; i++, j++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
347 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
348 data_buffer[i] = add(input_buffer[j], mult(PRE_EMP_FAC, input_buffer[j-1])); move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
349 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
350 st->pre_emp_mem = input_buffer[FRM_LEN-1]; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
351
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
352 for (i = DELAY + FRM_LEN; i < FFT_LEN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
353 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
354 data_buffer[i] = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
355 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
356
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
357
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
358 /* Perform FFT on the data buffer */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
359 r_fft(data_buffer);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
360
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
361
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
362 /* Use normb_shift factor to determine the scaling of the energy estimates */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
363 state_change = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
364 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
365 if (st->shift_state == 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
366 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
367 if (sub(normb_shift, -FFT_HEADROOM+2) <= 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
368 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
369 state_change = 1; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
370 st->shift_state = 1; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
371 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
372 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
373 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
374 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
375 if (sub(normb_shift, -FFT_HEADROOM+5) >= 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
376 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
377 state_change = 1; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
378 st->shift_state = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
379 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
380 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
381
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
382 /* Scale channel energy estimate */ test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
383 if (state_change)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
384 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
385 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
386 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
387 st->Lch_enrg[i] = L_shr(st->Lch_enrg[i], state_change_shift_r[st->shift_state]); move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
388 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
389 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
390
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
391
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
392 /* Estimate the energy in each channel */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
393 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
394 if (L_sub(st->Lframe_cnt, 1) == 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
395 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
396 alpha = 32767; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
397 one_m_alpha = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
398 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
399 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
400 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
401 alpha = CEE_SM_FAC; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
402 one_m_alpha = ONE_MINUS_CEE_SM_FAC; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
403 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
404
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
405 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
406 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
407 Lenrg = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
408 j1 = ch_tbl[i][0]; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
409 j2 = ch_tbl[i][1]; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
410
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
411 for (j = j1; j <= j2; j++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
412 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
413 Lenrg = L_mac(Lenrg, data_buffer[2 * j], data_buffer[2 * j]);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
414 Lenrg = L_mac(Lenrg, data_buffer[2 * j + 1], data_buffer[2 * j + 1]);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
415 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
416
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
417 /* Denorm energy & scale 30,1 according to the state */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
418 Lenrg = L_shr_r(Lenrg, sub(shl(normb_shift, 1), enrg_norm_shift[st->shift_state]));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
419
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
420 /* integrate over time: e[i] = (1-alpha)*e[i] + alpha*enrg/num_bins_in_chan */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
421 tmp = mult(alpha, ch_tbl_sh[i]);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
422 L_Extract (Lenrg, &hi1, &lo1);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
423 Ltmp = Mpy_32_16(hi1, lo1, tmp);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
424
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
425 L_Extract (st->Lch_enrg[i], &hi1, &lo1);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
426 st->Lch_enrg[i] = L_add(Ltmp, Mpy_32_16(hi1, lo1, one_m_alpha)); move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
427 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
428 if (L_sub(st->Lch_enrg[i], min_chan_enrg[st->shift_state]) < 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
429 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
430 st->Lch_enrg[i] = min_chan_enrg[st->shift_state]; move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
431 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
432
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
433 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
434
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
435
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
436 /* Compute the total channel energy estimate (Ltce) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
437 Ltce = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
438 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
439 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
440 Ltce = L_add(Ltce, st->Lch_enrg[i]);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
441 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
442
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
443
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
444 /* Calculate spectral peak-to-average ratio, set flag if p2a > 10 dB */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
445 Lpeak = 0; move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
446 for (i = LO_CHAN+2; i <= HI_CHAN; i++) /* Sine waves not valid for low frequencies */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
447 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
448 if (L_sub(st->Lch_enrg [i], Lpeak) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
449 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
450 Lpeak = st->Lch_enrg [i]; move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
451 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
452 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
453
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
454 /* Set p2a_flag if peak (dB) > average channel energy (dB) + 10 dB */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
455 /* Lpeak > Ltce/num_channels * 10^(10/10) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
456 /* Lpeak > (10/16)*Ltce */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
457
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
458 L_Extract (Ltce, &hi1, &lo1);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
459 Ltmp = Mpy_32_16(hi1, lo1, 20480);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
460 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
461 if (L_sub(Lpeak, Ltmp) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
462 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
463 p2a_flag = TRUE; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
464 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
465 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
466 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
467 p2a_flag = FALSE; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
468 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
469
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
470
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
471 /* Initialize channel noise estimate to either the channel energy or fixed level */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
472 /* Scale the energy appropriately to yield state 0 (22,9) scaling for noise */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
473 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
474 if (L_sub(st->Lframe_cnt, 4) <= 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
475 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
476 if (p2a_flag == TRUE)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
477 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
478 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
479 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
480 st->Lch_noise[i] = INE_NOISE_0; move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
481 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
482 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
483 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
484 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
485 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
486 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
487 if (L_sub(st->Lch_enrg[i], ine_noise[st->shift_state]) < 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
488 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
489 st->Lch_noise[i] = INE_NOISE_0; move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
490 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
491 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
492 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
493 if (st->shift_state == 1)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
494 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
495 st->Lch_noise[i] = L_shr(st->Lch_enrg[i], state_change_shift_r[0]);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
496 move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
497 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
498 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
499 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
500 st->Lch_noise[i] = st->Lch_enrg[i]; move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
501 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
502 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
503 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
504 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
505 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
506
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
507
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
508 /* Compute the channel energy (in dB), the channel SNRs, and the sum of voice metrics */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
509 vm_sum = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
510 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
511 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
512 ch_enrg_db[i] = fn10Log10(st->Lch_enrg[i], fbits[st->shift_state]); move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
513 ch_noise_db = fn10Log10(st->Lch_noise[i], FRACTIONAL_BITS_0);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
514
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
515 ch_snr[i] = sub(ch_enrg_db[i], ch_noise_db); move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
516
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
517 /* quantize channel SNR in 3/8 dB steps (scaled 7,8 => 15,0) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
518 /* ch_snr = round((snr/(3/8))>>8) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
519 /* = round(((0.6667*snr)<<2)>>8) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
520 /* = round((0.6667*snr)>>6) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
521
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
522 ch_snrq = shr_r(mult(21845, ch_snr[i]), 6);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
523
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
524 /* Accumulate the sum of voice metrics */ test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
525 if (sub(ch_snrq, 89) < 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
526 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
527 if (ch_snrq > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
528 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
529 j = ch_snrq; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
530 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
531 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
532 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
533 j = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
534 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
535 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
536 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
537 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
538 j = 89; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
539 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
540 vm_sum = add(vm_sum, vm_tbl[j]);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
541 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
542
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
543
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
544 /* Initialize NOMINAL peak voice energy and average noise energy, calculate instantaneous SNR */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
545 test(),test(),logic16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
546 if (L_sub(st->Lframe_cnt, 4) <= 0 || st->fupdate_flag == TRUE)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
547 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
548 /* tce_db = (96 - 22 - 10*log10(64) (due to FFT)) scaled as 7,8 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
549 tce_db = 14320; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
550 st->negSNRvar = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
551 st->negSNRbias = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
552
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
553 /* Compute the total noise estimate (Ltne) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
554 Ltne = 0; move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
555 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
556 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
557 Ltne = L_add(Ltne, st->Lch_noise[i]);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
558 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
559
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
560 /* Get total noise in dB */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
561 tne_db = fn10Log10(Ltne, FRACTIONAL_BITS_0);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
562
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
563 /* Initialise instantaneous and long-term peak signal-to-noise ratios */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
564 xt = sub(tce_db, tne_db);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
565 st->tsnr = xt; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
566 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
567 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
568 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
569 /* Calculate instantaneous frame signal-to-noise ratio */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
570 /* xt = 10*log10( sum(2.^(ch_snr*0.1*log2(10)))/length(ch_snr) ) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
571 Ltmp1 = 0; move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
572 for (i=LO_CHAN; i<=HI_CHAN; i++) {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
573 /* Ltmp2 = ch_snr[i] * 0.1 * log2(10); (ch_snr scaled as 7,8) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
574 Ltmp2 = L_shr(L_mult(ch_snr[i], 10885), 8);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
575 L_Extract(Ltmp2, &hi1, &lo1);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
576 hi1 = add(hi1, 3); /* 2^3 to compensate for negative SNR */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
577 Ltmp1 = L_add(Ltmp1, Pow2(hi1, lo1));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
578 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
579 xt = fn10Log10(Ltmp1, 4+3); /* average by 16, inverse compensation 2^3 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
580
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
581 /* Estimate long-term "peak" SNR */ test(),test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
582 if (sub(xt, st->tsnr) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
583 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
584 /* tsnr = 0.9*tsnr + 0.1*xt; */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
585 st->tsnr = round(L_add(L_mult(29491, st->tsnr), L_mult(3277, xt)));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
586 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
587 /* else if (xt > 0.625*tsnr) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
588 else if (sub(xt, mult(20480, st->tsnr)) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
589 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
590 /* tsnr = 0.998*tsnr + 0.002*xt; */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
591 st->tsnr = round(L_add(L_mult(32702, st->tsnr), L_mult(66, xt)));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
592 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
593 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
594
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
595 /* Quantize the long-term SNR in 3 dB steps, limit to 0 <= tsnrq <= 19 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
596 tsnrq = shr(mult(st->tsnr, 10923), 8);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
597
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
598 /* tsnrq = min(19, max(0, tsnrq)); */ test(),test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
599 if (sub(tsnrq, 19) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
600 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
601 tsnrq = 19; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
602 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
603 else if (tsnrq < 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
604 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
605 tsnrq = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
606 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
607
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
608 /* Calculate the negative SNR sensitivity bias */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
609 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
610 if (xt < 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
611 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
612 /* negSNRvar = 0.99*negSNRvar + 0.01*xt*xt; */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
613 /* xt scaled as 7,8 => xt*xt scaled as 14,17, shift to 7,8 and round */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
614 tmp = round(L_shl(L_mult(xt, xt), 7));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
615 st->negSNRvar = round(L_add(L_mult(32440, st->negSNRvar), L_mult(328, tmp)));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
616
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
617 /* if (negSNRvar > 4.0) negSNRvar = 4.0; */ test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
618 if (sub(st->negSNRvar, 1024) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
619 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
620 st->negSNRvar = 1024; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
621 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
622
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
623 /* negSNRbias = max(12.0*(negSNRvar - 0.65), 0.0); */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
624 tmp = mult_r(shl(sub(st->negSNRvar, 166), 4), 24576); test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
625
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
626 if (tmp < 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
627 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
628 st->negSNRbias = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
629 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
630 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
631 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
632 st->negSNRbias = shr(tmp, 8);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
633 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
634 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
635
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
636
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
637 /* Determine VAD as a function of the voice metric sum and quantized SNR */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
638
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
639 tmp = add(vm_threshold_table[tsnrq], st->negSNRbias); test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
640 if (sub(vm_sum, tmp) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
641 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
642 ivad = 1; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
643 st->burstcount = add(st->burstcount, 1); test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
644 if (sub(st->burstcount, burstcount_table[tsnrq]) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
645 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
646 st->hangover = hangover_table[tsnrq]; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
647 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
648 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
649 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
650 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
651 st->burstcount = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
652 st->hangover = sub(st->hangover, 1); test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
653 if (st->hangover <= 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
654 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
655 ivad = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
656 st->hangover = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
657 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
658 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
659 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
660 ivad = 1; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
661 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
662 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
663
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
664
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
665 /* Calculate log spectral deviation */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
666 ch_enrg_dev = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
667 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
668 if (L_sub(st->Lframe_cnt, 1) == 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
669 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
670 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
671 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
672 st->ch_enrg_long_db[i] = ch_enrg_db[i]; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
673 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
674 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
675 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
676 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
677 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
678 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
679 tmp = abs_s(sub(st->ch_enrg_long_db[i], ch_enrg_db[i]));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
680 ch_enrg_dev = add(ch_enrg_dev, tmp);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
681 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
682 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
683
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
684 /*
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
685 * Calculate long term integration constant as a function of instantaneous SNR
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
686 * (i.e., high SNR (tsnr dB) -> slower integration (alpha = HIGH_ALPHA),
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
687 * low SNR (0 dB) -> faster integration (alpha = LOW_ALPHA)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
688 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
689
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
690 /* alpha = HIGH_ALPHA - ALPHA_RANGE * (tsnr - xt) / tsnr, low <= alpha <= high */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
691 tmp = sub(st->tsnr, xt); test(),logic16(),test(),test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
692 if (tmp <= 0 || st->tsnr <= 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
693 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
694 alpha = HIGH_ALPHA; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
695 one_m_alpha = 32768L-HIGH_ALPHA; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
696 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
697 else if (sub(tmp, st->tsnr) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
698 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
699 alpha = LOW_ALPHA; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
700 one_m_alpha = 32768L-LOW_ALPHA; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
701 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
702 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
703 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
704 tmp = div_s(tmp, st->tsnr);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
705 alpha = sub(HIGH_ALPHA, mult(ALPHA_RANGE, tmp));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
706 one_m_alpha = sub(32767, alpha);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
707 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
708
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
709 /* Calc long term log spectral energy */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
710 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
711 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
712 Ltmp1 = L_mult(one_m_alpha, ch_enrg_db[i]);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
713 Ltmp2 = L_mult(alpha, st->ch_enrg_long_db[i]);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
714 st->ch_enrg_long_db[i] = round(L_add(Ltmp1, Ltmp2));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
715 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
716
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
717
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
718 /* Set or clear the noise update flags */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
719 update_flag = FALSE; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
720 st->fupdate_flag = FALSE; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
721 test(),test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
722 if (sub(vm_sum, UPDATE_THLD) <= 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
723 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
724 if (st->burstcount == 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
725 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
726 update_flag = TRUE; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
727 st->update_cnt = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
728 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
729 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
730 else if (L_sub(Ltce, noise_floor_chan[st->shift_state]) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
731 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
732 if (sub(ch_enrg_dev, DEV_THLD) < 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
733 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
734 if (p2a_flag == FALSE)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
735 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
736 if (st->LTP_flag == FALSE)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
737 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
738 st->update_cnt = add(st->update_cnt, 1); test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
739 if (sub(st->update_cnt, UPDATE_CNT_THLD) >= 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
740 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
741 update_flag = TRUE; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
742 st->fupdate_flag = TRUE; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
743 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
744 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
745 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
746 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
747 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
748 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
749 if (sub(st->update_cnt, st->last_update_cnt) == 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
750 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
751 st->hyster_cnt = add(st->hyster_cnt, 1);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
752 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
753 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
754 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
755 st->hyster_cnt = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
756 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
757
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
758 st->last_update_cnt = st->update_cnt; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
759 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
760 if (sub(st->hyster_cnt, HYSTER_CNT_THLD) > 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
761 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
762 st->update_cnt = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
763 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
764
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
765
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
766 /* Conditionally update the channel noise estimates */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
767 test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
768 if (update_flag == TRUE)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
769 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
770 /* Check shift state */ test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
771 if (st->shift_state == 1)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
772 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
773 /* get factor to shift ch_enrg[] from state 1 to 0 (noise always state 0) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
774 tmp = state_change_shift_r[0]; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
775 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
776 else
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
777 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
778 /* No shift if already state 0 */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
779 tmp = 0; move16();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
780 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
781
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
782 /* Update noise energy estimate */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
783 for (i = LO_CHAN; i <= HI_CHAN; i++)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
784 { test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
785 /* integrate over time: en[i] = (1-alpha)*en[i] + alpha*e[n] */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
786 /* (extract with shift compensation for state 1) */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
787 L_Extract (L_shr(st->Lch_enrg[i], tmp), &hi1, &lo1);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
788 Ltmp = Mpy_32_16(hi1, lo1, CNE_SM_FAC);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
789
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
790 L_Extract (st->Lch_noise[i], &hi1, &lo1);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
791 st->Lch_noise[i] = L_add(Ltmp, Mpy_32_16(hi1, lo1, ONE_MINUS_CNE_SM_FAC)); move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
792
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
793 /* Limit low level noise */ test();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
794 if (L_sub(st->Lch_noise[i], MIN_NOISE_ENRG_0) < 0)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
795 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
796 st->Lch_noise[i] = MIN_NOISE_ENRG_0; move32();
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
797 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
798 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
799 }
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
800
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
801 return(ivad);
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
802 } /* end of vad2 () */
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
803
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
804
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
805 /**** Other related functions *****/
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
806
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
807 /***************************************************************************
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
808 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
809 * FUNCTION NAME: vad2_reset()
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
810 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
811 * PURPOSE:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
812 * The purpose of this function is to initialise the vad2() state
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
813 * variables.
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
814 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
815 * INPUTS:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
816 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
817 * &st
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
818 * pointer to data structure of vad2 state variables
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
819 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
820 * OUTPUTS:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
821 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
822 * none
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
823 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
824 * RETURN VALUE:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
825 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
826 * none
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
827 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
828 * DESCRIPTION:
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
829 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
830 * Set all values in vad2 state to zero. Since it is
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
831 * known that all elements in the structure contain
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
832 * 16 and 32 bit fixed point elements, the initialisation
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
833 * is performed by zeroing out the number of bytes in the
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
834 * structure divided by two.
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
835 *
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
836 *************************************************************************/
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
837
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
838 void vad2_reset (vadState2 * st)
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
839 {
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
840 memset(st, 0, sizeof(vadState2));
0152c069d01f libtwamr: integrate VAD2 main body
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
841 } /* end of vad2_reset () */