yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Sanechips Technology Co., Ltd. |
| 3 | * |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license |
| 6 | * that can be found in the LICENSE file in the root of the source |
| 7 | * tree. An additional intellectual property rights grant can be found |
| 8 | * in the file PATENTS. All contributing project authors may |
| 9 | * be found in the AUTHORS file in the root of the source tree. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | |
| 14 | /**************************************************************************** |
| 15 | * Include files |
| 16 | ****************************************************************************/ |
| 17 | #include <stdio.h> |
| 18 | #include <unistd.h> |
| 19 | #include <string.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <errno.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <ctype.h> |
| 26 | #include <math.h> |
| 27 | /**************************************************************************** |
| 28 | * Local Macros |
| 29 | ****************************************************************************/ |
| 30 | #define DB_COFF_ARRAY_SIZE 15 |
| 31 | #define DRV_SUCCESS 0 |
| 32 | #define DRV_ERROR -1 |
| 33 | |
| 34 | typedef signed char SINT8; |
| 35 | typedef unsigned char UINT8; |
| 36 | typedef short SINT16; |
| 37 | typedef unsigned short UINT16; |
| 38 | typedef long SINT32; |
| 39 | typedef void VOID; |
| 40 | /**************************************************************************** |
| 41 | * Global Constants |
| 42 | ****************************************************************************/ |
| 43 | |
| 44 | |
| 45 | /**************************************************************************** |
| 46 | * Global Variables |
| 47 | ****************************************************************************/ |
| 48 | |
| 49 | |
| 50 | typedef enum { |
| 51 | VOL_PATH_RX = 0, |
| 52 | VOL_PATH_TX = 1, |
| 53 | MAX_VOL_PATH = VOL_PATH_TX |
| 54 | } T_Vol_Path; |
| 55 | |
| 56 | typedef struct { |
| 57 | |
| 58 | float gain; |
| 59 | float coff; |
| 60 | |
| 61 | } T_Db_Coff; |
| 62 | |
| 63 | typedef struct { |
| 64 | |
| 65 | int rx_vol; //0~5 |
| 66 | float rx_coff; |
| 67 | int rx_upflag; //rx vol update flag , 1 update, else not |
| 68 | |
| 69 | int tx_vol; //0~5 |
| 70 | float tx_coff; |
| 71 | int tx_upflag; //tx vol update flag , 1 update, else not |
| 72 | |
| 73 | } T_Vol_Inst; |
| 74 | |
| 75 | typedef struct { |
| 76 | int vol_on;// 0 off, 1 on |
| 77 | float vol[6]; |
| 78 | } T_Wrtc_Vol_Para; |
| 79 | |
| 80 | T_Wrtc_Vol_Para vol_para = { |
| 81 | 1, |
| 82 | // tx_vol vol[6]; |
| 83 | { |
| 84 | -6,//vol0 |
| 85 | -3,//vol1 |
| 86 | 0,//vol2 |
| 87 | 3,//vol3 |
| 88 | 6,//vol4 |
| 89 | 9,//vol5 |
| 90 | }, |
| 91 | }; |
| 92 | |
| 93 | #if 1 |
| 94 | T_Db_Coff coff_table[] = { |
| 95 | {-96.000000,0.000016}, |
| 96 | {-15.000000,0.177828}, |
| 97 | {-14.000000,0.199526}, |
| 98 | {-13.000000,0.223872}, |
| 99 | {-12.000000,0.251189}, |
| 100 | {-11.000000,0.281838}, |
| 101 | {-10.000000,0.316228}, |
| 102 | {-9.000000,0.354813}, |
| 103 | {-8.000000,0.398107}, |
| 104 | {-7.000000,0.446684}, |
| 105 | {-6.000000,0.501187}, |
| 106 | {-5.000000,0.562341}, |
| 107 | {-4.000000,0.630957}, |
| 108 | {-3.000000,0.707946}, |
| 109 | {-2.000000,0.794328}, |
| 110 | {-1.000000,0.891251}, |
| 111 | {0.000000,1.000000}, |
| 112 | {1.000000,1.122018}, |
| 113 | {2.000000,1.258925}, |
| 114 | {3.000000,1.412538}, |
| 115 | {4.000000,1.584893}, |
| 116 | {5.000000,1.778279}, |
| 117 | {6.000000,1.995262}, |
| 118 | {7.000000,2.238721}, |
| 119 | {8.000000,2.511886}, |
| 120 | {9.000000,2.818383}, |
| 121 | {10.000000,3.162278}, |
| 122 | {11.000000,3.548134}, |
| 123 | {12.000000,3.981072}, |
| 124 | {13.000000,4.466835}, |
| 125 | {14.000000,5.011872}, |
| 126 | {15.000000,5.623413}, |
| 127 | {16.000000,6.309574}, |
| 128 | {17.000000,7.079458}, |
| 129 | {18.000000,7.943282}, |
| 130 | {19.000000,8.912509}, |
| 131 | {20.000000,10.000000}, |
| 132 | {21.000000,11.220183}, |
| 133 | {22.000000,12.589254}, |
| 134 | {23.000000,14.125375}, |
| 135 | {24.000000,15.848933}, |
| 136 | |
| 137 | }; |
| 138 | #else |
| 139 | T_Db_Coff coff_table[] = { |
| 140 | { -96, 0.000016}, |
| 141 | { -15, 0.177828}, |
| 142 | { -14, 0.199526}, |
| 143 | { -13, 0.223872}, |
| 144 | { -12, 0.251189}, |
| 145 | { -11, 0.281838}, |
| 146 | { -10, 0.316228}, |
| 147 | { -9, 0.354813}, |
| 148 | { -8, 0.398107}, |
| 149 | { -7, 0.446684}, |
| 150 | { -6, 0.501187}, |
| 151 | { -5, 0.562341}, |
| 152 | { -4, 0.630957}, |
| 153 | { -3, 0.707946}, |
| 154 | { -2, 0.794328}, |
| 155 | { -1, 0.891251}, |
| 156 | {0, 1.000000}, |
| 157 | {1, 1.122018}, |
| 158 | {2, 1.258925}, |
| 159 | {3, 1.412538}, |
| 160 | {4, 1.584893}, |
| 161 | {5, 1.778279}, |
| 162 | {6, 1.995262}, |
| 163 | {7, 2.238721}, |
| 164 | {8, 2.511886}, |
| 165 | {9, 2.818383}, |
| 166 | {10, 0}, |
| 167 | {11, 0}, |
| 168 | {12, 3.981072}, |
| 169 | {13, 0}, |
| 170 | {14, 0}, |
| 171 | {15, 5.623413}, |
| 172 | {16, 0}, |
| 173 | {17, 0}, |
| 174 | {18, 7.943282}, |
| 175 | {19, 0}, |
| 176 | {20, 0}, |
| 177 | {21, 0}, |
| 178 | {22, 0}, |
| 179 | {23, 0}, |
| 180 | {24, 0}, |
| 181 | }; |
| 182 | #endif |
| 183 | |
| 184 | T_Vol_Inst vol_inst = { |
| 185 | |
| 186 | |
| 187 | 3,//int rx_vol; |
| 188 | 0,//int rx_coff; |
| 189 | 1,//int rx_upflag; //rx vol update flag |
| 190 | |
| 191 | 3,//int tx_vol; |
| 192 | 0,//int tx_coff; |
| 193 | 1,//int tx_upflag; //tx vol update flag |
| 194 | }; |
| 195 | |
| 196 | int mute_flag = 0; |
| 197 | |
| 198 | /**************************************************************************** |
| 199 | * Global Function Prototypes |
| 200 | ****************************************************************************/ |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | /**************************************************************************** |
| 206 | * Function Definitions |
| 207 | ****************************************************************************/ |
| 208 | |
| 209 | float Cal_DbCoff(float db) |
| 210 | { |
| 211 | |
| 212 | float coff = 0.0f; |
| 213 | |
| 214 | float tmp = (float)(db / 20); |
| 215 | coff = (float) pow(10, tmp); |
| 216 | printf("Cal_DbCoff db=%f,tmp=%f,coff=%f!\r\n", db , tmp , coff ); |
| 217 | |
| 218 | return coff; |
| 219 | |
| 220 | } |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | VOID Cal_ArrayDbCoff(VOID) |
| 226 | { |
| 227 | int i = 0; |
| 228 | int size = sizeof(coff_table) / sizeof(coff_table[0]); |
| 229 | printf("update array, Db covert into Coff size=%d!\r\n", size); |
| 230 | |
| 231 | for (i = 0; i < size; i++) { |
| 232 | coff_table[i].coff = Cal_DbCoff(coff_table[i].gain); |
| 233 | printf("update array, Db covert into Coff, db=%f,coff=%f!\r\n", coff_table[i].gain , coff_table[i].coff); |
| 234 | |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | |
| 239 | float Get_ArrayDbCoff(float db) |
| 240 | { |
| 241 | int i = 0; |
| 242 | float coff = 0; |
| 243 | int size = sizeof(coff_table) / sizeof(coff_table[0]); |
| 244 | for (i = 0; i < size; i++) { |
| 245 | if (coff_table[i].gain == db) { |
| 246 | coff = coff_table[i].coff; |
| 247 | printf("Get_ArrayDbCoff, find db=%d,coff=%d!\r\n", db * 1000, coff * 1000); |
| 248 | |
| 249 | return coff; |
| 250 | } |
| 251 | } |
| 252 | if (i >= DB_COFF_ARRAY_SIZE) { |
| 253 | coff = Cal_DbCoff(db); |
| 254 | |
| 255 | } |
| 256 | |
| 257 | printf("Get_ArrayDbCoff, db=%d,size=%d,i=%d,coff=%d!\r\n", db * 1000, DB_COFF_ARRAY_SIZE, i, coff * 1000); |
| 258 | |
| 259 | return coff; |
| 260 | |
| 261 | } |
| 262 | |
| 263 | |
| 264 | float Get_VolCoff(T_Vol_Path volpath) |
| 265 | { |
| 266 | float coff = 0.0f; |
| 267 | float gain = 0.0f; |
| 268 | |
| 269 | |
| 270 | T_Wrtc_Vol_Para *volpara; |
| 271 | |
| 272 | if (volpath == VOL_PATH_TX) { |
| 273 | |
| 274 | if (vol_inst.tx_upflag == 0) { |
| 275 | return vol_inst.tx_coff; |
| 276 | } |
| 277 | volpara = & vol_para; |
| 278 | gain = volpara->vol[vol_inst.tx_vol]; |
| 279 | coff = Get_ArrayDbCoff(gain); |
| 280 | vol_inst.tx_coff = coff; |
| 281 | vol_inst.tx_upflag = 0; |
| 282 | } else { |
| 283 | |
| 284 | if (vol_inst.rx_upflag == 0) { |
| 285 | return vol_inst.rx_coff; |
| 286 | } |
| 287 | |
| 288 | volpara = & vol_para; |
| 289 | gain = volpara->vol[vol_inst.rx_vol]; |
| 290 | coff = Get_ArrayDbCoff(gain); |
| 291 | vol_inst.rx_coff = coff; |
| 292 | vol_inst.rx_upflag = 0; |
| 293 | |
| 294 | } |
| 295 | |
| 296 | |
| 297 | |
| 298 | return coff; |
| 299 | |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| 304 | SINT32 Adjust_DataGain(UINT8 *pinBuffer, UINT8 *poutBuffer, UINT16 sampleNums, int mute, float gain) |
| 305 | { |
| 306 | |
| 307 | SINT32 ret = DRV_SUCCESS; |
| 308 | |
| 309 | //float coff_f = 0; |
| 310 | float coff = 0; |
| 311 | int i = 0; |
| 312 | short tmp = 0; |
| 313 | |
| 314 | short *sinBuffer; |
| 315 | short *soutBuffer; |
| 316 | |
| 317 | coff = Get_ArrayDbCoff(gain); |
| 318 | //coff = (int)coff_f; |
| 319 | |
| 320 | |
| 321 | sinBuffer = (short *)(pinBuffer); |
| 322 | soutBuffer = (short *)(poutBuffer); |
| 323 | |
| 324 | if (mute_flag == 0) { |
| 325 | for (i = 0; i < sampleNums; i++) { |
| 326 | tmp = (short)coff * sinBuffer[i]; |
| 327 | if ((tmp > -32768) && (tmp < 32768)) { |
| 328 | soutBuffer[i] = tmp; |
| 329 | } else if (tmp > 32767) { |
| 330 | soutBuffer[i] = 32767; |
| 331 | } else if (tmp < -32768) { |
| 332 | soutBuffer[i] = -32767; |
| 333 | } |
| 334 | } |
| 335 | } else if (mute_flag == 1) { |
| 336 | for (i = 0; i < sampleNums; i++) { |
| 337 | |
| 338 | soutBuffer[i] = 0; |
| 339 | } |
| 340 | } else { |
| 341 | printf("Adjust_DataGain mute val=%d invalid!\r\n", mute); |
| 342 | |
| 343 | } |
| 344 | |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | |
| 349 | SINT32 Adjust_TxDataVol(UINT8 *pinBuffer, UINT8 *poutBuffer, UINT16 sampleNums) |
| 350 | { |
| 351 | |
| 352 | SINT32 ret = DRV_SUCCESS; |
| 353 | |
| 354 | //float coff_f = 0; |
| 355 | float coff = 0.0f; |
| 356 | int i = 0; |
| 357 | short tmp = 0; |
| 358 | |
| 359 | short *sinBuffer; |
| 360 | short *soutBuffer; |
| 361 | #if 0 |
| 362 | float gain; |
| 363 | T_Webrtc_Para *para = &webrtcPara; |
| 364 | //T_Webrtc_TxProcPara *procpara =&tx_procpara; |
| 365 | T_Wrtc_Vol_Para *volpara = & tx_procpara.tx_vol; |
| 366 | gain = volpara->vol[para->txvol]; |
| 367 | |
| 368 | coff = Get_ArrayDbCoff(gain); |
| 369 | //coff = (int)coff_f; |
| 370 | #else |
| 371 | coff = Get_VolCoff(VOL_PATH_TX); |
| 372 | |
| 373 | #endif |
| 374 | |
| 375 | sinBuffer = (short *)(pinBuffer); |
| 376 | soutBuffer = (short *)(poutBuffer); |
| 377 | |
| 378 | if (mute_flag == 0) { |
| 379 | for (i = 0; i < sampleNums; i++) { |
| 380 | tmp = (short)coff * sinBuffer[i]; |
| 381 | if ((tmp > -32768) && (tmp < 32768)) { |
| 382 | soutBuffer[i] = tmp; |
| 383 | } else if (tmp > 32767) { |
| 384 | soutBuffer[i] = 32767; |
| 385 | } else if (tmp < -32768) { |
| 386 | soutBuffer[i] = -32767; |
| 387 | } |
| 388 | } |
| 389 | } else if (mute_flag == 1) { |
| 390 | for (i = 0; i < sampleNums; i++) { |
| 391 | |
| 392 | soutBuffer[i] = 0; |
| 393 | } |
| 394 | } else { |
| 395 | printf("Adjust_DataGain mute val=%d invalid!\r\n", mute_flag); |
| 396 | |
| 397 | } |
| 398 | |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | SINT32 Adjust_RxDataVol(UINT8 *pinBuffer, UINT8 *poutBuffer, UINT16 sampleNums) |
| 403 | { |
| 404 | |
| 405 | SINT32 ret = DRV_SUCCESS; |
| 406 | |
| 407 | float coff = 0.0; |
| 408 | int i = 0; |
| 409 | short tmp = 0; |
| 410 | |
| 411 | short *sinBuffer; |
| 412 | short *soutBuffer; |
| 413 | #if 0 |
| 414 | float gain; |
| 415 | |
| 416 | T_Webrtc_Para *para = &webrtcPara; |
| 417 | //T_Webrtc_RxProcPara *procpara = &rx_procpara; |
| 418 | T_Wrtc_Vol_Para *volpara = & rx_procpara.rx_vol; |
| 419 | gain = volpara->vol[para->rxvol]; |
| 420 | |
| 421 | |
| 422 | coff = Get_ArrayDbCoff(gain); |
| 423 | #else |
| 424 | coff = Get_VolCoff(VOL_PATH_RX); |
| 425 | #endif |
| 426 | sinBuffer = (short *)(pinBuffer); |
| 427 | soutBuffer = (short *)(poutBuffer); |
| 428 | |
| 429 | |
| 430 | for (i = 0; i < sampleNums; i++) { |
| 431 | tmp = (short)coff * sinBuffer[i]; |
| 432 | if ((tmp > -32768) && (tmp < 32768)) { |
| 433 | soutBuffer[i] = tmp; |
| 434 | } else if (tmp > 32767) { |
| 435 | soutBuffer[i] = 32767; |
| 436 | } else if (tmp < -32768) { |
| 437 | soutBuffer[i] = -32767; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | SINT32 Vol_SetPara(int volpath, int vol, int upflag) |
| 446 | { |
| 447 | SINT32 ret = DRV_SUCCESS; |
| 448 | if (volpath == VOL_PATH_TX) { |
| 449 | vol_inst.tx_vol = vol; |
| 450 | vol_inst.tx_upflag = upflag; |
| 451 | printf(" Vol_SetPara tx volpath=%d,upflag=%d.\n", volpath, upflag); |
| 452 | |
| 453 | } else if (volpath == VOL_PATH_RX) { |
| 454 | vol_inst.rx_vol = vol; |
| 455 | vol_inst.rx_upflag = upflag; |
| 456 | printf(" Vol_SetPara rx volpath=%d,upflag=%d.\n", volpath, upflag); |
| 457 | |
| 458 | } else { |
| 459 | printf(" Vol_SetPara err volpath=%d.\n", volpath); |
| 460 | return DRV_ERROR; |
| 461 | } |
| 462 | |
| 463 | |
| 464 | |
| 465 | return ret; |
| 466 | } |
| 467 | |
| 468 | SINT32 vol_SetRxVol(int vol) |
| 469 | { |
| 470 | SINT32 ret = DRV_SUCCESS; |
| 471 | float coff = 0.0f; |
| 472 | vol_inst.rx_vol = vol; |
| 473 | ret = Vol_SetPara(0, vol, 1); |
| 474 | printf(" vol_SetRxVol vol=%d,ret=%d.\n", vol, ret); |
| 475 | coff = Get_VolCoff(0); |
| 476 | printf(" vol_SetRxVol Get_VolCoff coff=%f.\n", coff); |
| 477 | |
| 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | int main(int argc, char **argv) |
| 483 | { |
| 484 | |
| 485 | printf("voltest start!\n"); |
| 486 | |
| 487 | int vol = 0; |
| 488 | int ret = 0; |
| 489 | |
| 490 | |
| 491 | if (strcmp(argv[1], "caldb") == 0) { |
| 492 | |
| 493 | Cal_ArrayDbCoff(); |
| 494 | |
| 495 | } else if (strcmp(argv[1], "setvol") == 0) { |
| 496 | |
| 497 | |
| 498 | |
| 499 | vol = atoi(argv[2]); |
| 500 | ret = vol_SetRxVol(vol); |
| 501 | |
| 502 | } else { |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |