xref: /OK3568_Linux_fs/external/common_algorithm/audio/rkaudio_algorithms/include/rkaudio_preprocess.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #ifndef _RKAUDIO_PREPROCESS_H_
2 #define _RKAUDIO_PREPROCESS_H_
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 #define NUM_CHANNEL					 8
10 #define NUM_REF_CHANNEL              1
11 #define NUM_DROP_CHANNEL             0
12 #define REF_POSITION				 1
13 //static short int Array[NUM_SRC_CHANNEL] = { 9,7,5,3,2,4,6,8 };
14 //static short int Array[NUM_CHANNEL] = {2, 3, 0, 1}; //src first, ref second
15 static short int Array[NUM_CHANNEL] = { 1, 0, 3, 2, 5, 4, 6, 7 };
16 //Array[NUM_SRC_CHANNEL] = { 2,3,4,5,6,7};
17 /**********************EQ Parameter**********************/
18 static short EqPara_16k[5][13] =
19 {
20 	//filter_bank 1
21 	{-1 ,-1 ,-1 ,-1 ,-2 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,-2 ,-3 },
22 	//filter_bank 2
23 	{-1 ,-1 ,-1 ,-1 ,-2 ,-2 ,-3 ,-5 ,-3 ,-2 ,-1 ,-1 ,-2 },
24 	//filter_bank 3
25 	{-2 ,-5 ,-9 ,-4 ,-2 ,-2 ,-1 ,-5 ,-5 ,-11 ,-20 ,-11 ,-5 },
26 	//filter_bank 4
27 	{-5 ,-1 ,-7 ,-7 ,-19 ,-40 ,-20 ,-9 ,-10 ,-1 ,-20 ,-24 ,-60 },
28 	//filter_bank 5
29 	{-128 ,-76 ,-40 ,-44 ,-1 ,-82 ,-111 ,-383 ,-1161 ,-1040 ,-989 ,-3811 ,32764 },
30 };
31 
32 /**********************NLP Parameter**********************/
33 static short int ashwAecBandNlpPara_16k[8][2] =
34 {
35 	/* BandPassThd      SuperEstFactor*/
36 	{      10,                 6      },                                         /* Hz: 0    - 300  */
37 	{      10,                 10      },                                         /* Hz: 300  - 575  */
38 	{      10,                 10      },                                         /* Hz: 575  - 950  */
39 	{      5,                  10      },                                        /* Hz: 950  - 1425 */
40 	{      5,                  10      },                                        /* Hz: 1425 - 2150 */
41 	{      5,                  10      },                                        /* Hz: 2150 - 3350 */
42 	{      0,                  6      },                                        /* Hz: 3350 - 5450 */
43 	{      0,                  6      },                                        /* Hz: 5450 - 8000 */
44 };
45 /*************************************************/
46 /*The Main Enable which used to control the AEC,BF and RX*/
47 typedef enum RKAUDIOEnable_
48 {
49 	RKAUDIO_EN_AEC = 1 << 0,
50 	RKAUDIO_EN_BF = 1 << 1,
51 	RKAUDIO_EN_RX = 1 << 2,
52 	RKAUDIO_EN_CMD = 1 << 3,
53 } RKAUDIOEnable;
54 
55 /* The Sub-Enable which used to control the AEC,BF and RX*/
56 typedef enum RKAecEnable_
57 {
58 	EN_DELAY = 1 << 0,
59 	EN_ARRAY_RESET = 1<<1,
60 	EN_HOWLING = 1 << 2,
61 } RKAecEnable;
62 typedef enum RKPreprocessEnable_
63 {
64 	EN_Fastaec = 1 << 0,
65 	EN_Wakeup = 1 << 1,
66 	EN_Dereverberation = 1 << 2,
67 	EN_Nlp = 1 << 3,
68 	EN_AES = 1<<4,
69 	EN_Agc = 1 << 5,
70 	EN_Anr = 1 << 6,
71 	EN_GSC = 1 << 7,
72 	GSC_Method = 1 << 8,
73 	EN_Fix = 1 << 9,
74 	EN_STDT = 1 << 10,
75 	EN_CNG = 1<<11,
76 	EN_EQ = 1<<12,
77 	EN_CHN_SELECT = 1<<13,
78 } RKPreprocessEnable;
79 typedef enum RkaudioRxEnable_
80 {
81 	EN_RX_Anr = 1 << 0,
82 } RkaudioRxEnable;
83 /*****************************************/
84 
85 /* Set the three Main Para which used to initialize the AEC,BF and RX*/
86 typedef struct SKVAECParameter_ {
87 	int pos;
88 	int drop_ref_channel;
89 	int model_aec_en;
90 	int delay_len;
91 	int look_ahead;
92 	short int *Array_list;
93 } SKVAECParameter;
94 typedef struct SKVPreprocessParam_
95 {
96 	/* Parameters of agc */
97 	int model_bf_en;
98 	int ref_pos;
99 	int Targ;
100 	int num_ref_channel;
101 	int drop_ref_channel;
102 	void* dereverb_para;
103 	void* aes_para;
104 	void* nlp_para;
105 	void* anr_para;
106 	void* agc_para;
107 	void* cng_para;
108 	void* dtd_para;
109 	void* eq_para;
110 }SKVPreprocessParam;
111 typedef struct RkaudioRxParam_
112 {
113 	/* Parameters of agc */
114 	int model_rx_en;
115 	void* anr_para;
116 }RkaudioRxParam;
117 /****************************************/
118 /*The param struct of sub-mudule of AEC,BF and RX*/
119 typedef struct RKAudioDereverbParam_
120 {
121 	int				   rlsLg;
122 	int                curveLg;
123 	int				   delay;
124 	float              forgetting;
125 	float              T60;
126 	float			   coCoeff;
127 } RKAudioDereverbParam;
128 
129 typedef struct RKAudioAESParameter_ {
130 	float Beta_Up;
131 	float Beta_Down;
132 } RKAudioAESParameter;
133 
134 typedef struct SKVNLPParameter_ {
135 	short int g_ashwAecBandNlpPara_16k[8][2];
136 } SKVNLPParameter;
137 typedef struct SKVANRParam_ {
138 	float noiseFactor;
139 	int swU;
140 	float PsiMin;
141 	float PsiMax;
142 	float fGmin;
143 } SKVANRParam;
144 
145 typedef struct RKAGCParam_ {
146 	/* 新版AGC参数 */
147 	float              attack_time;  /* 触发时间,即AGC增益下降所需要的时间 */
148 	float			   release_time; /* 施放时间,即AGC增益上升所需要的时间 */
149 	float              max_gain; /* 最大增益,同时也是线性段增益,单位:dB */
150 	float 			   max_peak; /* 经AGC处理后,输出语音的最大能量,范围:单位:dB */
151 	float              fRth0;    /* 扩张段结束能量dB阈值,同时也是线性段开始阈值 */
152 	float              fRk0;     /* 扩张段斜率 */
153 	float              fRth1;    /* 压缩段起始能量dB阈值,同时也是线性段结束阈值 */
154 
155 	/* 无效参数 */
156 	int            fs;                       /* 数据采样率 */
157 	int            frmlen;                   /* 处理帧长 */
158 	float          attenuate_time; /* 噪声衰减时间,即噪声段增益衰减到1所需的时间 */
159 	float          fRth2;                     /* 压缩段起始能量dB阈值 */
160 	float          fRk1;                      /* 扩张段斜率 */
161 	float          fRk2;                      /* 扩张段斜率 */
162 	float          fLineGainDb;               /* 线性段提升dB数 */
163 	int            swSmL0;                    /* 扩张段时域平滑点数 */
164 	int            swSmL1;                    /* 线性段时域平滑点数 */
165 	int            swSmL2;                    /* 压缩段时域平滑点数 */
166 
167 } RKAGCParam;
168 
169 typedef struct RKCNGParam_
170 {
171 	/*CNG Parameter*/
172 	float              fGain;                     /* INT16 Q0 施加舒适噪声幅度比例 */
173 	float              fMpy;						/* INT16 Q0 白噪随机数生成幅度 */
174 	float              fSmoothAlpha;              /* 舒适噪声平滑系数 */
175 	float              fSpeechGain;               /* 根据语音能量额外施加舒适噪声比例增益 */
176 } RKCNGParam;
177 
178 typedef struct RKDTDParam_
179 {
180 	float ksiThd_high;			 /* 单双讲判决阈值 */
181 	float ksiThd_low;			 /* 单双讲判决阈值 */
182 
183 }RKDTDParam;
184 
185 typedef struct RKaudioEqParam_ {
186     int shwParaLen;           // 滤波器系数个数
187     short pfCoeff[5][13];          // 滤波器系数
188 } RKaudioEqParam;
189 
190 
191 /* Set the Sub-Para which used to initialize the Dereverb*/
rkaudio_dereverb_param_init()192 inline static void * rkaudio_dereverb_param_init(){
193 	RKAudioDereverbParam* param = (RKAudioDereverbParam*)malloc(sizeof(RKAudioDereverbParam));
194 	param->rlsLg = 4; /* RLS滤波器阶数 */
195 	param->curveLg = 10; /* 分布曲线阶数 */
196 	param->delay = 2; /* RLS滤波器延时 */
197 	param->forgetting = 0.98; /* RLS滤波器遗忘因子 */
198 	param->T60 = 0.5;//1.5; /* 混响时间估计值(单位:s),越大,去混响能力越强,但是越容易过消除 */
199 	param->coCoeff = 1.0; /* 互相干性调整系数,防止过消除,越大能力越强,建议取值:0.5到2之间 */
200 	return (void*)param;
201 }
rkaudio_aes_param_init()202 inline static void* rkaudio_aes_param_init() {
203 	RKAudioAESParameter* param = (RKAudioAESParameter*)malloc(sizeof(RKAudioAESParameter));
204 	//param->Beta_Up = 0.001f; /* 上升速度 */
205 	//param->Beta_Down = 0.005f; /* 下降速度 */
206 	param->Beta_Up = 0.005f; /* 上升速度 */
207 	param->Beta_Down = 0.001f; /* 下降速度 */
208 	//param->Beta_Up = 0.0005f; /* 上升速度 */
209 	//param->Beta_Down = 0.01f; /* 下降速度 */
210 	return (void*)param;
211 }
212 /* Set the Sub-Para which used to initialize the NLP*/
rkaudio_nlp_param_init()213 inline static void* rkaudio_nlp_param_init()
214 {
215 	SKVNLPParameter* param = (SKVNLPParameter*)malloc(sizeof(SKVNLPParameter));
216 	for (int i = 0; i < 8; i++)
217 	{
218 		for (int j = 0; j < 2; j++)
219 		{
220 			param->g_ashwAecBandNlpPara_16k[i][j] = ashwAecBandNlpPara_16k[i][j];
221 		}
222 	}
223 	return (void*)param;
224 }
225 /* Set the Sub-Para which used to initialize the ANR*/
rkaudio_anr_param_init()226 inline static void *rkaudio_anr_param_init() {
227 	SKVANRParam* param = (SKVANRParam*)malloc(sizeof(SKVANRParam));
228 	/* anr parameters */
229 	param->noiseFactor = 0.88;
230 	param->swU = 10;					  //2
231 	//param->PsiMin = 0.2;				//0.05
232 	//param->PsiMax = 0.316;			  //0.516
233 	//param->fGmin = 0.1;					 //0.1
234 	param->PsiMin = 0.01;				//0.05
235 	param->PsiMax = 0.516;			  //0.516
236 	param->fGmin = 0.05;					 //0.1
237 	return (void*)param;
238 }
239 /* Set the Sub-Para which used to initialize the AGC*/
rkaudio_agc_param_init()240 inline static void* rkaudio_agc_param_init()
241 {
242 	RKAGCParam* param = (RKAGCParam*)malloc(sizeof(RKAGCParam));
243 
244 	/* 新版AGC参数 */
245 	param->attack_time = 200.0;		/* 触发时间,即AGC增益上升所需要的时间 */
246 	param->release_time = 200.0;	/* 施放时间,即AGC增益下降所需要的时间 */
247 	//param->max_gain = 35.0;		/* 最大增益,同时也是线性段增益,单位:dB */
248 	param->max_gain = 30;			/* 最大增益,同时也是线性段增益,单位:dB */
249 	param->max_peak = -1.0;			/* 经AGC处理后,输出语音的最大能量,范围:单位:dB */
250 	param->fRk0 = 2;				/* 扩张段斜率 */
251 	param->fRth2 = -35;				/* 压缩段起始能量dB阈值,同时也是线性段结束阈值,增益逐渐降低,注意 fRth2 + max_gain < max_peak */
252 	param->fRth1 = -80;				/* 扩张段结束能量dB阈值,同时也是线性段开始阈值,能量高于改区域以max_gain增益 */
253 	param->fRth0 = -85;				/* 噪声门阈值 */
254 
255 	/* 无效参数 */
256 	param->fs = 16000;                       /* 数据采样率 */
257 	param->frmlen = 256;                   /* 处理帧长 */
258 	param->attenuate_time = 1000; /* 噪声衰减时间,即噪声段增益衰减到1所需的时间 */
259 	param->fRk1 = 0.8;                      /* 扩张段斜率 */
260 	param->fRk2 = 0.4;                      /* 扩张段斜率 */
261 	param->fLineGainDb = -25.0f;               /* 低于该值,起始的attenuate_time(ms)内不做增益 */
262 	param->swSmL0 = 40;                    /* 扩张段时域平滑点数 */
263 	param->swSmL1 = 80;                    /* 线性段时域平滑点数 */
264 	param->swSmL2 = 80;                    /* 压缩段时域平滑点数 */
265 
266 	return (void*)param;
267 }
268 /* Set the Sub-Para which used to initialize the CNG*/
rkaudio_cng_param_init()269 inline static void* rkaudio_cng_param_init()
270 {
271 	RKCNGParam* param = (RKCNGParam*)malloc(sizeof(RKCNGParam));
272 	/* cng paremeters */
273 	param->fSmoothAlpha = 0.99f;										            /* INT16 Q15 施加舒适噪声平滑度 */
274 	param->fSpeechGain = 0;										                /* INT16 Q15 施加舒适噪声语音纹理模拟程度 */
275 	param->fGain = 20.0;                                           /* INT16 Q0 施加舒适噪声幅度比例 */
276 	param->fMpy = 20;                                            /* INT16 Q0 白噪随机数生成幅度 */
277 	return (void*)param;
278 }
279 /* Set the Sub-Para which used to initialize the DTD*/
rkaudio_dtd_param_init()280 inline static void* rkaudio_dtd_param_init()
281 {
282 	RKDTDParam* param = (RKDTDParam*)malloc(sizeof(RKDTDParam));
283 	/* dtd paremeters*/
284 	param->ksiThd_high = 0.60f;										            /* 单双讲判决阈值 */
285 	param->ksiThd_low = 0.30f;
286 	return (void*)param;
287 }
288 /* Set the Sub-Para which used to initialize the AEC*/
289 
rkaudio_aec_param_init()290 inline static void* rkaudio_aec_param_init()
291 {
292 	SKVAECParameter* param = (SKVAECParameter*)malloc(sizeof(SKVAECParameter));
293 	param->pos = REF_POSITION;
294 	param->drop_ref_channel = NUM_DROP_CHANNEL;
295 	param->model_aec_en = 0;//param->model_aec_en = EN_DELAY;
296 	param->delay_len = 0;
297 	param->look_ahead = 0;
298 	param->Array_list = Array;
299 	return (void*)param;
300 }
301 
rkaudio_eq_param_init()302 inline static void * rkaudio_eq_param_init(){
303 	RKaudioEqParam* param = (RKaudioEqParam*)malloc(sizeof(RKaudioEqParam));
304 	param->shwParaLen = 65;
305 	int i, j;
306 	for (i = 0; i < 5; i++){
307 		for (j = 0; j < 13; j++) {
308 			param->pfCoeff[i][j] = EqPara_16k[i][j];
309 		}
310 	}
311 	return (void*)param;
312 }
313 
314 /* Set the Sub-Para which used to initialize the BF*/
rkaudio_preprocess_param_init()315 inline static void* rkaudio_preprocess_param_init()
316 {
317 	SKVPreprocessParam* param = (SKVPreprocessParam*)malloc(sizeof(SKVPreprocessParam));
318 	//param->model_bf_en = EN_Fastaec |EN_AES |EN_Wakeup;
319 	//param->model_bf_en = EN_Agc | EN_Anr | EN_Dereverberation;
320 	param->model_bf_en = EN_Fastaec | EN_AES | EN_Anr | EN_Agc | EN_GSC | EN_Dereverberation;
321 	//param->model_bf_en = EN_Wakeup ;
322 	//param->model_bf_en = EN_Fix;
323 	//param->model_bf_en = EN_Anr | EN_GSC |EN_Agc;
324 	//param->model_bf_en = EN_Fastaec | EN_AES | EN_Anr | EN_Agc;
325 	param->Targ = 4;
326 	param->ref_pos = REF_POSITION;
327 	param->num_ref_channel = NUM_REF_CHANNEL;
328 	param->drop_ref_channel = NUM_DROP_CHANNEL;
329 	param->dereverb_para = rkaudio_dereverb_param_init();
330 	param->aes_para = rkaudio_aes_param_init();
331 	param->nlp_para = rkaudio_nlp_param_init();
332 	param->anr_para = rkaudio_anr_param_init();
333 	param->agc_para = rkaudio_agc_param_init();
334 	param->cng_para = rkaudio_cng_param_init();
335 	param->dtd_para = rkaudio_dtd_param_init();
336 	param->eq_para = rkaudio_eq_param_init();
337 	return (void*)param;
338 }
339 /* Set the Sub-Para which used to initialize the RX*/
rkaudio_rx_param_init()340 inline static void* rkaudio_rx_param_init()
341 {
342 	RkaudioRxParam* param = (RkaudioRxParam*)malloc(sizeof(RkaudioRxParam));
343 	param->anr_para = rkaudio_anr_param_init();
344 	param->model_rx_en = EN_RX_Anr;
345 	return (void*)param;
346 }
347 typedef struct RKAUDIOParam_
348 {
349 	int model_en;
350 	void *aec_param;
351 	void *bf_param;
352 	void* rx_param;
353 } RKAUDIOParam;
354 
rkaudio_aec_param_destory(void * param_)355 inline static void rkaudio_aec_param_destory(void* param_)
356 {
357 	SKVAECParameter* param = (SKVAECParameter*)param_;
358 	free(param); param = NULL;
359 }
360 
rkaudio_preprocess_param_destory(void * param_)361 inline static void rkaudio_preprocess_param_destory(void* param_)
362 {
363 	SKVPreprocessParam* param = (SKVPreprocessParam*)param_;
364 	free(param->dereverb_para); param->dereverb_para = NULL;
365 	free(param->aes_para); param->aes_para = NULL;
366 	free(param->anr_para); param->anr_para = NULL;
367 	free(param->agc_para); param->agc_para = NULL;
368 	free(param->nlp_para); param->nlp_para = NULL;
369 	free(param->cng_para); param->cng_para = NULL;
370 	free(param->dtd_para); param->dtd_para = NULL;
371 	free(param->eq_para); param->eq_para = NULL;
372 	free(param); param = NULL;
373 }
374 
rkaudio_rx_param_destory(void * param_)375 inline static void rkaudio_rx_param_destory(void* param_)
376 {
377 	RkaudioRxParam* param = (RkaudioRxParam*)param_;
378 	free(param->anr_para); param->anr_para = NULL;
379 	free(param); param = NULL;
380 }
381 
rkaudio_param_deinit(void * param_)382 inline static void rkaudio_param_deinit(void* param_)
383 {
384 	RKAUDIOParam* param = (RKAUDIOParam*)param_;
385 	rkaudio_aec_param_destory(param->aec_param);
386 	rkaudio_preprocess_param_destory(param->bf_param);
387 	rkaudio_rx_param_destory(param->rx_param);
388 }
389 
390 void *rkaudio_preprocess_init(int rate, int bits, int src_chan, int ref_chan, RKAUDIOParam *param);
391 void rkaudio_param_printf(int src_chan, int ref_chan, RKAUDIOParam* param);
392 int rkaudio_Doa_invoke(void* st_ptr);
393 int rkaudio_Cir_Doa_invoke(void* st_ptr, int* ang_doa, int* pth_doa);
394 int rkaudio_preprocess_get_cmd_id(void* st_ptr, float* asr_score, int* cmd_id);
395 
396 void rkaudio_preprocess_destory(void *st_ptr);
397 
398 int rkaudio_preprocess_short(void *st_ptr, short *in, short *out, int in_size, int *is_wakeup);
399 int rkaudio_rx_short(void* st_ptr, short* in, short* out);
400 int rkaudio_preprocess_key_short(void *st_ptr, short *in, short *out, int in_size);
401 int rkaudio_preprocess_command_short(void *st_ptr, short *in, int in_size, float *asr_scor);
402 void rkaudio_asr_set_param(float min, float max, float keep);
403 
404 void rkaudio_param_deinit(void* param_);
405 #ifdef __cplusplus
406 }
407 #endif
408 
409 #endif    // _RKAUDIO_PREPROCESS_H_
410