melp_sub.c
上传用户:luckfish
上传日期:2021-12-16
资源大小:77k
文件大小:14k
源码类别:

语音压缩

开发平台:

Visual C++

  1. /*
  2. 2.4 kbps MELP Proposed Federal Standard speech coder
  3. version 1.2
  4. Copyright (c) 1996, Texas Instruments, Inc.  
  5. Texas Instruments has intellectual property rights on the MELP
  6. algorithm.  The Texas Instruments contact for licensing issues for
  7. commercial and non-government use is William Gordon, Director,
  8. Government Contracts, Texas Instruments Incorporated, Semiconductor
  9. Group (phone 972 480 7442).
  10. */
  11. /*
  12.   melp_sub.c: MELP-specific subroutines
  13. */
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include "spbstd.h"
  17. #include "mat.h"
  18. #include "melp_sub.h"
  19. #include "dsp_sub.h"
  20. #include "pit.h"
  21. /*
  22.     Name: bpvc_ana.c
  23.     Description: Bandpass voicing analysis
  24.     Inputs:
  25.       speech[] - input speech signal
  26.       fpitch[] - initial (floating point) pitch estimates
  27.     Outputs: 
  28.       bpvc[] - bandpass voicing decisions
  29.       pitch[] - frame pitch estimates
  30.     Returns: void 
  31.     Copyright (c) 1995 by Texas Instruments, Inc.  All rights reserved.
  32. */
  33. /* Filter orders */
  34. #define ENV_ORD 2
  35. #define BPF_ORD 6
  36. /* Constants */
  37. static int PITCHMAX;
  38. static int PITCHMIN;
  39. static int FRAME;
  40. static int NUM_BANDS;
  41. static int PIT_BEG;
  42. static int PIT_P_FR;
  43. static int PIT_FR_BEG;
  44. static int FIRST_CNTR;
  45. static int BPF_BEG;
  46. static int NUM_PITCHES;
  47. static int LMIN;
  48. /* Static memory */
  49. static float **bpfdel;
  50. static float **envdel;
  51. static float *envdel2;
  52. static float *sigbuf;
  53. /* External variables */
  54. extern float bpf_num[], bpf_den[];
  55. void bpvc_ana(float speech[], float fpitch[], float bpvc[], float pitch[])
  56. {
  57.     float pcorr, temp;
  58.     int j;
  59.     /* Filter lowest band and estimate pitch */
  60.     v_equ(&sigbuf[PIT_BEG-BPF_ORD],&bpfdel[0][0],BPF_ORD);
  61.     polflt(&speech[PIT_FR_BEG],&bpf_den[0],&sigbuf[PIT_BEG],
  62.    BPF_ORD,PIT_P_FR);
  63.     v_equ(&bpfdel[0][0],&sigbuf[PIT_BEG+FRAME-BPF_ORD],BPF_ORD);
  64.     zerflt(&sigbuf[PIT_BEG],&bpf_num[0],&sigbuf[PIT_BEG],
  65.    BPF_ORD,PIT_P_FR);
  66.     
  67.     *pitch = frac_pch(&sigbuf[FIRST_CNTR],
  68. &bpvc[0],fpitch[0],5,PITCHMIN,PITCHMAX,LMIN);
  69.     
  70.     for (j = 1; j < NUM_PITCHES; j++) {
  71. temp = frac_pch(&sigbuf[FIRST_CNTR],
  72. &pcorr,fpitch[j],5,PITCHMIN,PITCHMAX,LMIN);
  73. /* choose largest correlation value */
  74. if (pcorr > bpvc[0]) {
  75.     *pitch = temp;
  76.     bpvc[0] = pcorr; 
  77. }
  78.     }
  79.     
  80.     /* Calculate bandpass voicing for frames */
  81.     
  82.     for (j = 1; j < NUM_BANDS; j++) {
  83. /* Bandpass filter input speech */
  84. v_equ(&sigbuf[PIT_BEG-BPF_ORD],&bpfdel[j][0],BPF_ORD);
  85. polflt(&speech[PIT_FR_BEG],&bpf_den[j*(BPF_ORD+1)],&sigbuf[PIT_BEG],
  86.        BPF_ORD,PIT_P_FR);
  87. v_equ(&bpfdel[j][0],&sigbuf[PIT_BEG+FRAME-BPF_ORD],BPF_ORD);
  88. zerflt(&sigbuf[PIT_BEG],&bpf_num[j*(BPF_ORD+1)],&sigbuf[PIT_BEG],
  89.        BPF_ORD,PIT_P_FR);
  90. /* Check correlations for each frame */
  91. temp = frac_pch(&sigbuf[FIRST_CNTR],
  92. &bpvc[j],*pitch,0,PITCHMIN,PITCHMAX,LMIN);
  93. /* Calculate envelope of bandpass filtered input speech */
  94. temp = envdel2[j];
  95. envdel2[j] = sigbuf[PIT_BEG+FRAME-1];
  96. v_equ(&sigbuf[PIT_BEG-ENV_ORD],&envdel[j][0],ENV_ORD);
  97. envelope(&sigbuf[PIT_BEG],temp,&sigbuf[PIT_BEG],PIT_P_FR);
  98. v_equ(&envdel[j][0],&sigbuf[PIT_BEG+FRAME-ENV_ORD],ENV_ORD);
  99. /* Check correlations for each frame */
  100. temp = frac_pch(&sigbuf[FIRST_CNTR],&pcorr,
  101. *pitch,0,PITCHMIN,PITCHMAX,LMIN);
  102. /* reduce envelope correlation */
  103. pcorr -= 0.1;
  104. if (pcorr > bpvc[j])
  105.     bpvc[j] = pcorr;
  106.     }
  107. }
  108. void bpvc_ana_init(int fr, int pmin, int pmax, int nbands, int num_p, int lmin)
  109. {
  110.     /* Initialize constants */
  111.     FRAME = fr;
  112.     PITCHMIN = pmin;
  113.     PITCHMAX = pmax;
  114.     NUM_BANDS = nbands;
  115.     NUM_PITCHES = num_p;
  116.     LMIN = lmin;
  117.     PIT_BEG = BPF_ORD;
  118.     PIT_P_FR = ((2*PITCHMAX)+1);
  119.     PIT_FR_BEG = (-PITCHMAX);
  120.     FIRST_CNTR = (PIT_BEG+PITCHMAX);
  121.     BPF_BEG = (PIT_BEG+PIT_P_FR-FRAME);
  122.     /* Allocate memory */
  123.     MEM_2ALLOC(malloc,bpfdel,NUM_BANDS,BPF_ORD,float);
  124.     v_zap(&bpfdel[0][0],NUM_BANDS*BPF_ORD);
  125.     MEM_2ALLOC(malloc,envdel,NUM_BANDS,ENV_ORD,float);
  126.     v_zap(&envdel[0][0],NUM_BANDS*ENV_ORD);
  127.     MEM_ALLOC(malloc,envdel2,NUM_BANDS,float);
  128.     v_zap(envdel2,NUM_BANDS);
  129.     /* Allocate scratch buffer */
  130.     MEM_ALLOC(malloc,sigbuf,PIT_BEG+PIT_P_FR,float);
  131. }
  132. /*
  133.     Name: dc_rmv.c
  134.     Description: remove DC from input signal
  135.     Inputs: 
  136.       sigin[] - input signal
  137.       dcdel[] - filter delay history (size DC_ORD)
  138.       frame - number of samples to filter
  139.     Outputs: 
  140.       sigout[] - output signal
  141.       dcdel[] - updated filter delay history
  142.     Returns: void 
  143.     See_Also:
  144.     Copyright (c) 1995 by Texas Instruments, Inc.  All rights reserved.
  145. */
  146. /* Filter order */
  147. #define DC_ORD 4
  148. /* DC removal filter */
  149. /* 4th order Chebychev Type II 60 Hz removal filter */
  150. /* cutoff=60 Hz, stop=-30 dB */
  151. static float dc_num[DC_ORD+1] = {    
  152.       0.92692416,
  153.      -3.70563834,
  154.       5.55742893,
  155.      -3.70563834,
  156.       0.92692416};
  157. static float dc_den[DC_ORD+1] = {
  158.        1.00000000,
  159.      -3.84610723,
  160.       5.55209760,
  161.      -3.56516069,
  162.       0.85918839};
  163. void dc_rmv(float sigin[], float sigout[], float dcdel[], int frame)
  164. {
  165.     float *sigbuf;
  166.     /* Allocate scratch buffer */
  167.     MEM_ALLOC(malloc,sigbuf,frame+DC_ORD,float);
  168.     /* Remove DC from input speech */
  169.     v_equ(sigbuf,dcdel,DC_ORD);
  170.     polflt(sigin,dc_den,&sigbuf[DC_ORD],DC_ORD,frame);
  171.     v_equ(dcdel,&sigbuf[frame],DC_ORD);
  172.     zerflt(&sigbuf[DC_ORD],dc_num,sigout,DC_ORD,frame);
  173.     /* Free scratch buffer */
  174.     MEM_FREE(free,sigbuf);
  175. }
  176. /*
  177.     Name: gain_ana.c
  178.     Description: analyze gain level for input signal
  179.     Inputs: 
  180.       sigin[] - input signal
  181.       pitch - pitch value (for pitch synchronous window)
  182.       minlength - minimum window length
  183.       maxlength - maximum window length
  184.     Outputs: 
  185.     Returns: log gain in dB
  186.     See_Also:
  187.     Copyright (c) 1995 by Texas Instruments, Inc.  All rights reserved.
  188. */
  189. #define MINGAIN 0.0
  190. float gain_ana(float sigin[], float pitch, int minlength, int maxlength)
  191. {
  192.     int length;
  193.     float flength, gain;
  194.     /* Find shortest pitch multiple window length (floating point) */
  195.     flength = pitch;
  196.     while (flength < minlength)
  197.       flength += pitch;
  198.     /* Convert window length to integer and check against maximum */
  199.     length = flength + 0.5;
  200.     if (length > maxlength)
  201.       length = (length/2);
  202.     
  203.     /* Calculate RMS gain in dB */
  204.     gain = 10.0*log10(0.01 + (v_magsq(&sigin[-(length/2)],length) / length));
  205.     if (gain < MINGAIN)
  206.       gain = MINGAIN;
  207.     return(gain);
  208. }
  209. /*
  210.     Name: lin_int_bnd.c
  211.     Description: Linear interpolation within bounds
  212.     Inputs:
  213.       x - input X value
  214.       xmin - minimum X value
  215.       xmax - maximum X value
  216.       ymin - minimum Y value
  217.       ymax - maximum Y value
  218.     Returns: y - interpolated and bounded y value
  219.     Copyright (c) 1995 by Texas Instruments, Inc.  All rights reserved.
  220. */
  221. float lin_int_bnd(float x,float xmin,float xmax,float ymin,float ymax)
  222. {
  223.     float y;
  224.     if (x <= xmin)
  225.       y = ymin;
  226.     else if (x >= xmax)
  227.       y = ymax;
  228.     else 
  229.       y = ymin + (x-xmin)*(ymax-ymin)/(xmax-xmin);
  230.     return(y);
  231. }
  232. /*
  233.     Name: noise_est.c
  234.     Description: Estimate long-term noise floor
  235.     Inputs:
  236.       gain - input gain (in dB)
  237.       noise_gain - current noise gain estimate
  238.       up - maximum up stepsize
  239.       down - maximum down stepsize
  240.       min - minimum allowed gain
  241.       max - maximum allowed gain
  242.     Outputs:
  243.       noise_gain - updated noise gain estimate
  244.     Returns: void
  245.     Copyright (c) 1995 by Texas Instruments, Inc.  All rights reserved.
  246. */
  247. void noise_est(float gain,float *noise_gain,float up,float down,float min,float max)
  248. {
  249.     /* Update noise_gain */
  250.     if (gain > *noise_gain+up)
  251.       *noise_gain = *noise_gain+up;
  252.     else if (gain < *noise_gain+down)
  253.       *noise_gain = *noise_gain+down;
  254.     else
  255.       *noise_gain = gain;
  256.     /* Constrain total range of noise_gain */
  257.     if (*noise_gain < min)
  258.       *noise_gain = min;
  259.     if (*noise_gain > max)
  260.       *noise_gain = max;
  261. }
  262. /*
  263.     Name: noise_sup.c
  264.     Description: Perform noise suppression on speech gain
  265.     Inputs: (all in dB)
  266.       gain - input gain (in dB)
  267.       noise_gain - current noise gain estimate (in dB)
  268.       max_noise - maximum allowed noise gain 
  269.       max_atten - maximum allowed attenuation
  270.       nfact - noise floor boost
  271.     Outputs:
  272.       gain - updated gain 
  273.     Returns: void
  274.     Copyright (c) 1995 by Texas Instruments, Inc.  All rights reserved.
  275. */
  276. void noise_sup(float *gain,float noise_gain,float max_noise,float max_atten,float nfact)
  277. {
  278.     float gain_lev,suppress;
  279.     /* Reduce effect for louder background noise */
  280.     if (noise_gain > max_noise)
  281.       noise_gain = max_noise;
  282.     /* Calculate suppression factor */
  283.     gain_lev = *gain - (noise_gain + nfact);
  284.     if (gain_lev > 0.001) {
  285. suppress = -10.0*log10(1.0 - pow(10.0,-0.1*gain_lev));
  286. if (suppress > max_atten)
  287.   suppress = max_atten;
  288.     }
  289.     else
  290.       suppress = max_atten;
  291.     /* Apply suppression to input gain */
  292.     *gain -= suppress;
  293. }
  294. /*
  295.     Name: q_bpvc.c, q_bpvc_dec.c
  296.     Description: Quantize/decode bandpass voicing
  297.     Inputs:
  298.       bpvc, bpvc_index
  299.       bpthresh - threshold
  300.       NUM_BANDS - number of bands
  301.     Outputs: 
  302.       bpvc, bpvc_index
  303.     Returns: uv_flag - flag if unvoiced
  304.     Copyright (c) 1995 by Texas Instruments, Inc.  All rights reserved.
  305. */
  306. /* Compile constants */
  307. #define INVALID_BPVC 0001
  308. int q_bpvc(float *bpvc,int *bpvc_index,float bpthresh,int NUM_BANDS)
  309. {
  310.     int j, uv_flag;
  311.     uv_flag = 1;
  312.     if (bpvc[0] > bpthresh) {
  313. /* Voiced: pack bandpass voicing */
  314. uv_flag = 0;
  315. *bpvc_index = 0;
  316. bpvc[0] = 1.0;
  317. for (j = 1; j < NUM_BANDS; j++) {
  318.     *bpvc_index <<= 1; /* left shift */
  319.     if (bpvc[j] > bpthresh) {
  320. bpvc[j] = 1.0;
  321. *bpvc_index |= 1;
  322.     }
  323.     else {
  324. bpvc[j] = 0.0;
  325. *bpvc_index |= 0;
  326.     }
  327. }
  328. /* Don't use invalid code (only top band voiced) */
  329. if (*bpvc_index == INVALID_BPVC) {
  330.     bpvc[(NUM_BANDS-1)] = 0.0;
  331.     *bpvc_index = 0;
  332. }
  333.     }
  334.     
  335.     else {
  336. /* Unvoiced: force all bands unvoiced */
  337. *bpvc_index = 0;
  338. for (j = 0; j < NUM_BANDS; j++)
  339.     bpvc[j] = 0.0;
  340.     }
  341.     return(uv_flag);
  342. }
  343. void q_bpvc_dec(float *bpvc,int *bpvc_index,int uv_flag,int NUM_BANDS)
  344. {
  345.     int j;
  346.     if (uv_flag) {
  347. /* Unvoiced: set all bpvc to 0 */
  348. *bpvc_index = 0;
  349. bpvc[0] = 0.0;
  350.     }
  351.     
  352.     else {
  353. /* Voiced: set bpvc[0] to 1.0 */
  354. bpvc[0] = 1.0;
  355.     }
  356.     
  357.     if (*bpvc_index == INVALID_BPVC) {
  358. /* Invalid code received: set higher band voicing to zero */
  359. *bpvc_index = 0;
  360.     }
  361.     /* Decode remaining bands */
  362.     for (j = NUM_BANDS-1; j > 0; j--) {
  363. if ((*bpvc_index & 1) == 1)
  364.     bpvc[j] = 1.0;
  365. else
  366.     bpvc[j] = 0.0;
  367. *bpvc_index >>= 1;
  368.     }
  369. }
  370. /*
  371.     Name: q_gain.c, q_gain_dec.c
  372.     Description: Quantize/decode two gain terms using quasi-differential coding
  373.     Inputs:
  374.       gain[2],gain_index[2]
  375.       GN_QLO,GN_QUP,GN_QLEV for second gain term
  376.     Outputs: 
  377.       gain[2],gain_index[2]
  378.     Returns: void
  379.     Copyright (c) 1995 by Texas Instruments, Inc.  All rights reserved.
  380. */
  381. /* Compile constants */
  382. #define GAIN_INT_DB 5.0
  383. void q_gain(float *gain,int *gain_index,float GN_QLO,float GN_QUP,float GN_QLEV)
  384. {
  385.     static float prev_gain = 0.0;
  386.     float temp,temp2;
  387.     /* Quantize second gain term with uniform quantizer */
  388.     quant_u(&gain[1],&gain_index[1],GN_QLO,GN_QUP,GN_QLEV);
  389.     
  390.     /* Check for intermediate gain interpolation */
  391.     if (gain[0] < GN_QLO)
  392.       gain[0] = GN_QLO;
  393.     if (gain[0] > GN_QUP)
  394.       gain[0] = GN_QUP;
  395.     if (fabs(gain[1] - prev_gain) < GAIN_INT_DB && 
  396. fabs(gain[0] - 0.5*(gain[1]+prev_gain)) < 3.0) {
  397. /* interpolate and set special code */
  398. gain[0] = 0.5*(gain[1]+prev_gain);
  399. gain_index[0] = 0;
  400.     }
  401.     else {
  402. /* Code intermediate gain with 7-levels */
  403. if (prev_gain < gain[1]) {
  404.     temp = prev_gain;
  405.     temp2 = gain[1];
  406. }
  407. else {
  408.     temp = gain[1];
  409.     temp2 = prev_gain;
  410. }
  411. temp -= 6.0;
  412. temp2 += 6.0;
  413. if (temp < GN_QLO)
  414.   temp = GN_QLO;
  415. if (temp2 > GN_QUP)
  416.   temp2 = GN_QUP;
  417. quant_u(&gain[0],&gain_index[0],temp,temp2,7);
  418. /* Skip all-zero code */
  419. gain_index[0]++;
  420.     }
  421.     /* Update previous gain for next time */
  422.     prev_gain = gain[1];
  423.     
  424. }
  425. void q_gain_dec(float *gain,int *gain_index,float GN_QLO,float GN_QUP,float GN_QLEV)
  426. {
  427.     static float prev_gain = 0.0;
  428.     static int prev_gain_err = 0;
  429.     float temp,temp2;
  430.     /* Decode second gain term */
  431.     quant_u_dec(gain_index[1],&gain[1],GN_QLO,GN_QUP,GN_QLEV);
  432.     
  433.     if (gain_index[0] == 0) {
  434. /* interpolation bit code for intermediate gain */
  435. if (fabs(gain[1] - prev_gain) > GAIN_INT_DB) {
  436.     /* Invalid received data (bit error) */
  437.     if (prev_gain_err == 0) {
  438. /* First time: don't allow gain excursion */
  439. gain[1] = prev_gain;
  440.     }
  441.     prev_gain_err = 1;
  442. }
  443. else 
  444.   prev_gain_err = 0;
  445. /* Use interpolated gain value */
  446. gain[0] = 0.5*(gain[1]+prev_gain);
  447.     }
  448.     else {
  449. /* Decode 7-bit quantizer for first gain term */
  450. prev_gain_err = 0;
  451. gain_index[0]--;
  452. if (prev_gain < gain[1]) {
  453.     temp = prev_gain;
  454.     temp2 = gain[1];
  455. }
  456. else {
  457.     temp = gain[1];
  458.     temp2 = prev_gain;
  459. }
  460. temp -= 6.0;
  461. temp2 += 6.0;
  462. if (temp < GN_QLO)
  463.   temp = GN_QLO;
  464. if (temp2 > GN_QUP)
  465.   temp2 = GN_QUP;
  466. quant_u_dec(gain_index[0],&gain[0],temp,temp2,7);
  467.     }
  468.     /* Update previous gain for next time */
  469.     prev_gain = gain[1];
  470.     
  471. }
  472. /*
  473.     Name: scale_adj.c
  474.     Description: Adjust scaling of output speech signal.
  475.     Inputs:
  476.       speech - speech signal
  477.       gain - desired RMS gain
  478.       prev_scale - previous scale factor
  479.       length - number of samples in signal
  480.       SCALEOVER - number of points to interpolate scale factor
  481.     Warning: SCALEOVER is assumed to be less than length.
  482.     Outputs: 
  483.       speech - scaled speech signal
  484.       prev_scale - updated previous scale factor
  485.     Returns: void
  486.     Copyright (c) 1995 by Texas Instruments, Inc.  All rights reserved.
  487. */
  488. void scale_adj(float *speech, float gain, float *prev_scale, int length, int SCALEOVER)
  489. {
  490.     int i;
  491.     float scale;
  492.     /* Calculate desired scaling factor to match gain level */
  493.     scale = gain / (sqrt(v_magsq(&speech[0],length) / length) + .01);
  494.     /* interpolate scale factors for first SCALEOVER points */
  495.     for (i = 1; i < SCALEOVER; i++) {
  496. speech[i-1] *= ((scale*i + *prev_scale*(SCALEOVER-i))
  497.       * (1.0/SCALEOVER) );
  498.     }
  499.     
  500.     /* Scale rest of signal */
  501.     v_scale(&speech[SCALEOVER-1],scale,length-SCALEOVER+1);
  502.     /* Update previous scale factor for next call */
  503.     *prev_scale = scale;
  504. }