1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 /* encode.c  - Encoding function for the iSAC coder */
12 
13 #include "structs.h"
14 #include "codec.h"
15 #include "pitch_estimator.h"
16 
17 #include <math.h>
18 
WebRtcIsac_InitMasking(MaskFiltstr * maskdata)19 void WebRtcIsac_InitMasking(MaskFiltstr *maskdata) {
20 
21   int k;
22 
23   for (k = 0; k < WINLEN; k++) {
24     maskdata->DataBufferLo[k] = 0.0;
25     maskdata->DataBufferHi[k] = 0.0;
26   }
27   for (k = 0; k < ORDERLO+1; k++) {
28     maskdata->CorrBufLo[k] = 0.0;
29     maskdata->PreStateLoF[k] = 0.0;
30     maskdata->PreStateLoG[k] = 0.0;
31     maskdata->PostStateLoF[k] = 0.0;
32     maskdata->PostStateLoG[k] = 0.0;
33   }
34   for (k = 0; k < ORDERHI+1; k++) {
35     maskdata->CorrBufHi[k] = 0.0;
36     maskdata->PreStateHiF[k] = 0.0;
37     maskdata->PreStateHiG[k] = 0.0;
38     maskdata->PostStateHiF[k] = 0.0;
39     maskdata->PostStateHiG[k] = 0.0;
40   }
41 
42   maskdata->OldEnergy = 10.0;
43 
44   /* fill tables for transforms */
45   WebRtcIsac_InitTransform();
46 
47   return;
48 }
49 
WebRtcIsac_InitPreFilterbank(PreFiltBankstr * prefiltdata)50 void WebRtcIsac_InitPreFilterbank(PreFiltBankstr *prefiltdata)
51 {
52   int k;
53 
54   for (k = 0; k < QLOOKAHEAD; k++) {
55     prefiltdata->INLABUF1[k] = 0;
56     prefiltdata->INLABUF2[k] = 0;
57 
58     prefiltdata->INLABUF1_float[k] = 0;
59     prefiltdata->INLABUF2_float[k] = 0;
60   }
61   for (k = 0; k < 2*(QORDER-1); k++) {
62     prefiltdata->INSTAT1[k] = 0;
63     prefiltdata->INSTAT2[k] = 0;
64     prefiltdata->INSTATLA1[k] = 0;
65     prefiltdata->INSTATLA2[k] = 0;
66 
67     prefiltdata->INSTAT1_float[k] = 0;
68     prefiltdata->INSTAT2_float[k] = 0;
69     prefiltdata->INSTATLA1_float[k] = 0;
70     prefiltdata->INSTATLA2_float[k] = 0;
71   }
72 
73   /* High pass filter states */
74   prefiltdata->HPstates[0] = 0.0;
75   prefiltdata->HPstates[1] = 0.0;
76 
77   prefiltdata->HPstates_float[0] = 0.0f;
78   prefiltdata->HPstates_float[1] = 0.0f;
79 
80   return;
81 }
82 
WebRtcIsac_InitPostFilterbank(PostFiltBankstr * postfiltdata)83 void WebRtcIsac_InitPostFilterbank(PostFiltBankstr *postfiltdata)
84 {
85   int k;
86 
87   for (k = 0; k < 2*POSTQORDER; k++) {
88     postfiltdata->STATE_0_LOWER[k] = 0;
89     postfiltdata->STATE_0_UPPER[k] = 0;
90 
91     postfiltdata->STATE_0_LOWER_float[k] = 0;
92     postfiltdata->STATE_0_UPPER_float[k] = 0;
93   }
94 
95   /* High pass filter states */
96   postfiltdata->HPstates1[0] = 0.0;
97   postfiltdata->HPstates1[1] = 0.0;
98 
99   postfiltdata->HPstates2[0] = 0.0;
100   postfiltdata->HPstates2[1] = 0.0;
101 
102   postfiltdata->HPstates1_float[0] = 0.0f;
103   postfiltdata->HPstates1_float[1] = 0.0f;
104 
105   postfiltdata->HPstates2_float[0] = 0.0f;
106   postfiltdata->HPstates2_float[1] = 0.0f;
107 
108   return;
109 }
110 
111 
WebRtcIsac_InitPitchFilter(PitchFiltstr * pitchfiltdata)112 void WebRtcIsac_InitPitchFilter(PitchFiltstr *pitchfiltdata)
113 {
114   int k;
115 
116   for (k = 0; k < PITCH_BUFFSIZE; k++) {
117     pitchfiltdata->ubuf[k] = 0.0;
118   }
119   pitchfiltdata->ystate[0] = 0.0;
120   for (k = 1; k < (PITCH_DAMPORDER); k++) {
121     pitchfiltdata->ystate[k] = 0.0;
122   }
123   pitchfiltdata->oldlagp[0] = 50.0;
124   pitchfiltdata->oldgainp[0] = 0.0;
125 }
126 
WebRtcIsac_InitWeightingFilter(WeightFiltstr * wfdata)127 void WebRtcIsac_InitWeightingFilter(WeightFiltstr *wfdata)
128 {
129   int k;
130   double t, dtmp, dtmp2, denum, denum2;
131 
132   for (k=0;k<PITCH_WLPCBUFLEN;k++)
133     wfdata->buffer[k]=0.0;
134 
135   for (k=0;k<PITCH_WLPCORDER;k++) {
136     wfdata->istate[k]=0.0;
137     wfdata->weostate[k]=0.0;
138     wfdata->whostate[k]=0.0;
139   }
140 
141   /* next part should be in Matlab, writing to a global table */
142   t = 0.5;
143   denum = 1.0 / ((double) PITCH_WLPCWINLEN);
144   denum2 = denum * denum;
145   for (k=0;k<PITCH_WLPCWINLEN;k++) {
146     dtmp = PITCH_WLPCASYM * t * denum + (1-PITCH_WLPCASYM) * t * t * denum2;
147     dtmp *= 3.14159265;
148     dtmp2 = sin(dtmp);
149     wfdata->window[k] = dtmp2 * dtmp2;
150     t++;
151   }
152 }
153 
154 /* clear all buffers */
WebRtcIsac_InitPitchAnalysis(PitchAnalysisStruct * State)155 void WebRtcIsac_InitPitchAnalysis(PitchAnalysisStruct *State)
156 {
157   int k;
158 
159   for (k = 0; k < PITCH_CORR_LEN2+PITCH_CORR_STEP2+PITCH_MAX_LAG/2-PITCH_FRAME_LEN/2+2; k++)
160     State->dec_buffer[k] = 0.0;
161   for (k = 0; k < 2*ALLPASSSECTIONS+1; k++)
162     State->decimator_state[k] = 0.0;
163   for (k = 0; k < 2; k++)
164     State->hp_state[k] = 0.0;
165   for (k = 0; k < QLOOKAHEAD; k++)
166     State->whitened_buf[k] = 0.0;
167   for (k = 0; k < QLOOKAHEAD; k++)
168     State->inbuf[k] = 0.0;
169 
170   WebRtcIsac_InitPitchFilter(&(State->PFstr_wght));
171 
172   WebRtcIsac_InitPitchFilter(&(State->PFstr));
173 
174   WebRtcIsac_InitWeightingFilter(&(State->Wghtstr));
175 }
176