ex_ctrl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*************************************************************************
  2. *
  3. * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
  4. * R99 Version 3.3.0
  5. * REL-4 Version 4.1.0
  6. *
  7. ********************************************************************************
  8. *
  9. * File : ex_ctrl.c
  10. * Purpose : Excitation Control module in background noise
  11. *
  12. ********************************************************************************
  13. */
  14. /*
  15. ********************************************************************************
  16. * MODULE INCLUDE FILE AND VERSION ID
  17. ********************************************************************************
  18. */
  19. #include "ex_ctrl.h"
  20. const char ex_ctrl_id[] = "@(#)$Id $" ex_ctrl_h;
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "typedef.h"
  24. #include "basic_op.h"
  25. #include "count.h"
  26. #include "cnst.h"
  27. #include "copy.h"
  28. #include "set_zero.h"
  29. #include "gmed_n.h"
  30. #include "sqrt_l.h"
  31. /*
  32. ********************************************************************************
  33. * LOCAL VARIABLES AND TABLES
  34. ********************************************************************************
  35. */
  36. /*-----------------------------------------------------------------*
  37. * Decoder constant parameters (defined in "cnst.h") *
  38. *-----------------------------------------------------------------*
  39. * L_FRAME : Frame size. *
  40. * L_SUBFR : Sub-frame size. *
  41. *-----------------------------------------------------------------*/
  42. /*
  43. ********************************************************************************
  44. * PUBLIC PROGRAM CODE
  45. ********************************************************************************
  46. */
  47. /*
  48. **************************************************************************
  49. *
  50. * Function : Ex_ctrl
  51. * Purpose : Charaterice synthesis speech and detect background noise
  52. * Returns : background noise decision; 0 = no bgn, 1 = bgn
  53. *
  54. **************************************************************************
  55. */
  56. Word16 Ex_ctrl (Word16 excitation[], /*i/o: Current subframe excitation */
  57. Word16 excEnergy, /* i : Exc. Energy, sqrt(totEx*totEx)*/
  58. Word16 exEnergyHist[], /* i : History of subframe energies */
  59. Word16 voicedHangover, /* i : # of fr. after last voiced fr.*/
  60. Word16 prevBFI, /* i : Set i previous BFI */
  61. Word16 carefulFlag /* i : Restrict dymamic in scaling */
  62. )
  63. {
  64. Word16 i, exp;
  65. Word16 testEnergy, scaleFactor, avgEnergy, prevEnergy;
  66. Word32 t0;
  67. /* get target level */
  68. avgEnergy = gmed_n(exEnergyHist, 9); move16();
  69. prevEnergy = shr_ex( add_ex (exEnergyHist[7], exEnergyHist[8]) ,1);
  70. test ();
  71. if ( sub_ex (exEnergyHist[8], prevEnergy) < 0)
  72. {
  73. prevEnergy = exEnergyHist[8]; move16 ();
  74. }
  75. /* upscaling to avoid too rapid energy rises for some cases */
  76. test (); test ();
  77. if ( sub_ex (excEnergy, avgEnergy) < 0 && sub_ex (excEnergy, 5) > 0)
  78. {
  79. testEnergy = shl_ex(prevEnergy, 2); /* testEnergy = 4*prevEnergy; */
  80. test (); test ();
  81. if ( sub_ex (voicedHangover, 7) < 0 || prevBFI != 0 )
  82. {
  83. /* testEnergy = 3*prevEnergy */
  84. testEnergy = sub_ex (testEnergy, prevEnergy);
  85. }
  86. test ();
  87. if ( sub_ex (avgEnergy, testEnergy) > 0)
  88. {
  89. avgEnergy = testEnergy; move16 ();
  90. }
  91. /* scaleFactor=avgEnergy/excEnergy in Q0 (const 29 below)*/
  92. exp = norm_s_ex (excEnergy);
  93. excEnergy = shl_ex (excEnergy, exp);
  94. excEnergy = div_s ((Word16) 16383, excEnergy);
  95. t0 = L_mult_ex (avgEnergy, excEnergy);
  96. t0 = L_shr_ex (t0, sub_ex (20, exp)); /* const=30 for t0 in Q0, 20 for Q10 */
  97. if ( L_sub_ex(t0, 32767) > 0 )
  98. {
  99. t0 = 32767; move32 (); /* saturate */
  100. }
  101. scaleFactor = extract_l_ex (t0);
  102. /* test if scaleFactor > 3.0 */
  103. test (); test ();
  104. if ( carefulFlag != 0 && sub_ex(scaleFactor, 3072) > 0 )
  105. {
  106. scaleFactor = 3072; move16 ();
  107. }
  108. /* scale the excitation by scaleFactor */
  109. for (i = 0; i < L_SUBFR; i++)
  110. {
  111. t0 = L_mult_ex (scaleFactor, excitation[i]);
  112. t0 = L_shr_ex (t0, 11);
  113. excitation[i] = extract_l_ex (t0);
  114. }
  115. }
  116. return 0;
  117. }