⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ilbc_common.java

📁 java的ilbc语音编码器,实现了其全部功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.media.codec.audio.ilbc;

import java.lang.*;

/**
 * @author Jean Lorchat
 */
class ilbc_common {

   /*----------------------------------------------------------------*
    *  check for stability of lsf coefficients
    *---------------------------------------------------------------*/

    public static int LSF_check(    /* (o) 1 for stable lsf vectors and 0 for
                              nonstable ones */
			 float lsf[],     /* (i) a table of lsf vectors */
			 int dim,    /* (i) the dimension of each lsf vector */
			 int NoAn)    /* (i) the number of lsf vectors in the
                              table */
    {
       int k,n,m, Nit=2, change=0,pos;
       float tmp;
       float eps=(float)0.039; /* 50 Hz */
       float eps2=(float)0.0195;
       float maxlsf=(float)3.14; /* 4000 Hz */
       float minlsf=(float)0.01; /* 0 Hz */

       /* LSF separation check*/

       for (n=0; n<Nit; n++) { /* Run through a couple of times */
           for (m=0; m<NoAn; m++) { /* Number of analyses per frame */
               for (k=0; k<(dim-1); k++) {
                   pos=m*dim+k;

                   if ((lsf[pos+1]-lsf[pos])<eps) {

                       if (lsf[pos+1]<lsf[pos]) {
                           tmp=lsf[pos+1];
                           lsf[pos+1]= lsf[pos]+eps2;
                           lsf[pos]= lsf[pos+1]-eps2;
                       } else {
                           lsf[pos]-=eps2;
                           lsf[pos+1]+=eps2;
                       }
                       change=1;
                   }

                   if (lsf[pos]<minlsf) {
                       lsf[pos]=minlsf;
                       change=1;
                   }

                   if (lsf[pos]>maxlsf) {
                       lsf[pos]=maxlsf;
                       change=1;
                   }
               }
           }
       }

       return change;
   }

   /*----------------------------------------------------------------*
    *  decoding of the start state
    *---------------------------------------------------------------*/

    public static void StateConstructW(
       int idxForMax,      /* (i) 6-bit index for the quantization of
                                  max amplitude */
       int idxVec[],    /* (i) vector of quantization indexes */
       float syntDenum[],   /* (i) synthesis filter denumerator */
       int syntDenum_idx,
       float out[],         /* (o) the decoded state vector */
       int out_idx,
       int len             /* (i) length of a state vector */
   ){
       float maxVal;
       float [] tmpbuf = new float[ilbc_constants.LPC_FILTERORDER+2*ilbc_constants.STATE_LEN];
       //, *tmp,
       int tmp;
       float [] numerator = new float[ilbc_constants.LPC_FILTERORDER+1];
       float [] foutbuf = new float[ilbc_constants.LPC_FILTERORDER+2*ilbc_constants.STATE_LEN];
       //, *fout;
       int fout;
       int k,tmpi;

       /* decoding of the maximum value */

       maxVal = ilbc_constants.state_frgqTbl[idxForMax];
       //System.out.println("idxForMax : " + idxForMax + "maxVal : " + maxVal);
       maxVal = (float)Math.pow(10,maxVal) / 4.5f;
       //System.out.println("maxVal : " + maxVal);

       /* initialization of buffers and coefficients */

       for (int li = 0; li < ilbc_constants.LPC_FILTERORDER; li++) {
	   tmpbuf[li] = 0.0f;
	   foutbuf[li] = 0.0f;
       }
       //       memset(tmpbuf, 0, LPC_FILTERORDER*sizeof(float));
       //       memset(foutbuf, 0, LPC_FILTERORDER*sizeof(float));

       for (k=0; k < ilbc_constants.LPC_FILTERORDER; k++) {
           numerator[k]=syntDenum[syntDenum_idx + ilbc_constants.LPC_FILTERORDER - k];
	   //System.out.println("numerator-" + k + " = " + numerator[k] + " (( " + syntDenum[syntDenum_idx + ilbc_constants.LPC_FILTERORDER - k]);
       }

       numerator[ilbc_constants.LPC_FILTERORDER]=syntDenum[syntDenum_idx];
       //       tmp = &tmpbuf[LPC_FILTERORDER];
       tmp = ilbc_constants.LPC_FILTERORDER;
       //       fout = &foutbuf[LPC_FILTERORDER];
       fout = ilbc_constants.LPC_FILTERORDER;

       /* decoding of the sample values */

       //       for (int li = 0; li < idxVec.length; li++)
	 //System.out.println("idxVec["+li+"] = " + idxVec[li]);

       for (k=0; k<len; k++) {
           tmpi = len-1-k;
           /* maxVal = 1/scal */
           tmpbuf[tmp+k] = maxVal*ilbc_constants.state_sq3Tbl[idxVec[tmpi]];
	   //System.out.println("index " + k + ", valeur " + tmpbuf[tmp+k]);
       }

       /* circular convolution with all-pass filter */

       for (int li = 0; li < len; li++)
	   tmpbuf[tmp+len+li] = 0.0f;
       //       memset(tmp+len, 0, len*sizeof(float));
       ilbc_common.ZeroPoleFilter(tmpbuf, tmp, numerator, syntDenum, syntDenum_idx,
				  2*len, ilbc_constants.LPC_FILTERORDER,
				  foutbuf, fout);
       for (k=0;k<len;k++) {
           out[out_idx+k] = foutbuf[fout+len-1-k]+foutbuf[fout+2*len-1-k];
	   //System.out.println("MEM -- index " + out_idx + " + " + k + " initialise a " + out[out_idx+k]);
	   //System.out.println("       calcul : " + foutbuf[fout+len-1-k] + " + " + foutbuf[fout+2*len-1-k]);
       }
   }


    /*----------------------------------------------------------------*
    *  all-pole filter
    *---------------------------------------------------------------*/

    public static void AllPoleFilter(
       float InOut[],   /* (i/o) on entrance InOut[-orderCoef] to
                              InOut[-1] contain the state of the
                              filter (delayed samples). InOut[0] to
                              InOut[lengthInOut-1] contain the filter
                              input, on en exit InOut[-orderCoef] to
                              InOut[-1] is unchanged and InOut[0] to
                              InOut[lengthInOut-1] contain filtered
                              samples */
       int InOut_idx,
       float Coef[],/* (i) filter coefficients, Coef[0] is assumed
                              to be 1.0f */
       int Coef_idx,
       int lengthInOut,/* (i) number of input/output samples */
       int orderCoef)   /* (i) number of filter coefficients */
    {
	int n, k;

	for(n = 0; n < lengthInOut; n++) {
	    for(k = 1; k <= orderCoef; k++) {
		InOut[n+InOut_idx] -= Coef[Coef_idx + k] * InOut[n-k+InOut_idx];
	    }
	}
    }

   /*----------------------------------------------------------------*
    *  all-zero filter
    *---------------------------------------------------------------*/

    public static void AllZeroFilter(
       float In[],      /* (i) In[0] to In[lengthInOut-1] contain
                              filter input samples */
       int In_idx,
       float Coef[],/* (i) filter coefficients (Coef[0] is assumed
                              to be 1.0f) */
       int lengthInOut,/* (i) number of input/output samples */
       int orderCoef,  /* (i) number of filter coefficients */
       float Out[],      /* (i/o) on entrance Out[-orderCoef] to Out[-1]
                              contain the filter state, on exit Out[0]
                              to Out[lengthInOut-1] contain filtered
                              samples */
       int Out_idx)
    {
       int n, k;

       for(n = 0; n < lengthInOut; n++) {
           Out[Out_idx] = Coef[0]*In[In_idx];
           for(k=1;k<=orderCoef;k++){
               Out[Out_idx] += Coef[k]*In[In_idx-k];
           }
            Out_idx++;
            In_idx++;
       }
   }

   /*----------------------------------------------------------------*
    *  pole-zero filter
    *---------------------------------------------------------------*/

    public static void ZeroPoleFilter(
       float In[],      /* (i) In[0] to In[lengthInOut-1] contain
                              filter input samples In[-orderCoef] to
                              In[-1] contain state of all-zero
                              section */
       int In_idx,
       float ZeroCoef[],/* (i) filter coefficients for all-zero
                              section (ZeroCoef[0] is assumed to
                              be 1.0f) */
       float PoleCoef[],/* (i) filter coefficients for all-pole section
                              (ZeroCoef[0] is assumed to be 1.0f) */
       int PoleCoef_idx,
       int lengthInOut,/* (i) number of input/output samples */
       int orderCoef,  /* (i) number of filter coefficients */
       float Out[],      /* (i/o) on entrance Out[-orderCoef] to Out[-1]
                              contain state of all-pole section. On
                              exit Out[0] to Out[lengthInOut-1]
                              contain filtered samples */
       int Out_idx)
    {
	AllZeroFilter(In, In_idx, ZeroCoef, lengthInOut, orderCoef, Out, Out_idx);
	AllPoleFilter(Out, Out_idx, PoleCoef, PoleCoef_idx, lengthInOut, orderCoef);
    }

   /*----------------------------------------------------------------*
    *  conversion from lsf coefficients to lpc coefficients
    *---------------------------------------------------------------*/

    public static void lsf2a(float a_coef[], float freq[])
    {
	int i, j;
	float hlp;
	float [] p = new float[ilbc_constants.LPC_HALFORDER];
	float [] q = new float[ilbc_constants.LPC_HALFORDER];
	float [] a = new float[ilbc_constants.LPC_HALFORDER + 1];
	float [] a1 = new float[ilbc_constants.LPC_HALFORDER];
	float [] a2 = new float[ilbc_constants.LPC_HALFORDER];
	float [] b = new float[ilbc_constants.LPC_HALFORDER + 1];
	float [] b1 = new float[ilbc_constants.LPC_HALFORDER];
	float [] b2 = new float[ilbc_constants.LPC_HALFORDER];

	//System.out.println("debut de lsf2a");

	for (i=0; i < ilbc_constants.LPC_FILTERORDER; i++) {
	    freq[i] = freq[i] * ilbc_constants.PI2;
	}

       /* Check input for ill-conditioned cases.  This part is not
       found in the TIA standard.  It involves the following 2 IF
       blocks.  If "freq" is judged ill-conditioned, then we first
       modify freq[0] and freq[LPC_HALFORDER-1] (normally
       LPC_HALFORDER = 10 for LPC applications), then we adjust
       the other "freq" values slightly */

       if ((freq[0] <= 0.0f) || (freq[ilbc_constants.LPC_FILTERORDER - 1] >= 0.5)){


           if (freq[0] <= 0.0f) {
               freq[0] = (float)0.022;
           }


           if (freq[ilbc_constants.LPC_FILTERORDER - 1] >= 0.5) {
               freq[ilbc_constants.LPC_FILTERORDER - 1] = (float)0.499;
           }

           hlp = (freq[ilbc_constants.LPC_FILTERORDER - 1] - freq[0]) /
               (float) (ilbc_constants.LPC_FILTERORDER - 1);

           for (i=1; i < ilbc_constants.LPC_FILTERORDER; i++) {
               freq[i] = freq[i - 1] + hlp;
           }
       }

       for (int li = 0; li < ilbc_constants.LPC_HALFORDER; li++) {
	   a1[li] = 0.0f;
	   a2[li] = 0.0f;
	   b1[li] = 0.0f;
	   b2[li] = 0.0f;
       }
//        memset(a1, 0, LPC_HALFORDER*sizeof(float));
//        memset(a2, 0, LPC_HALFORDER*sizeof(float));
//        memset(b1, 0, LPC_HALFORDER*sizeof(float));
//        memset(b2, 0, LPC_HALFORDER*sizeof(float));
       for (int li = 0; li < ilbc_constants.LPC_HALFORDER + 1; li++) {
	   a[li] = 0.0f;
	   b[li] = 0.0f;
       }
//        memset(a, 0, (LPC_HALFORDER+1)*sizeof(float));
//        memset(b, 0, (LPC_HALFORDER+1)*sizeof(float));

       /* p[i] and q[i] compute cos(2*pi*omega_{2j}) and
       cos(2*pi*omega_{2j-1} in eqs. 4.2.2.2-1 and 4.2.2.2-2.
       Note that for this code p[i] specifies the coefficients
       used in .Q_A(z) while q[i] specifies the coefficients used
       in .P_A(z) */

       for (i = 0; i < ilbc_constants.LPC_HALFORDER; i++) {
           p[i] = (float)Math.cos(ilbc_constants.TWO_PI * freq[2 * i]);
           q[i] = (float)Math.cos(ilbc_constants.TWO_PI * freq[2 * i + 1]);
       }

       a[0] = 0.25f;
       b[0] = 0.25f;

       for (i= 0; i < ilbc_constants.LPC_HALFORDER; i++) {
           a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i];
           b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i];
           a2[i] = a1[i];
           a1[i] = a[i];
           b2[i] = b1[i];
           b1[i] = b[i];
       }

       for (j=0; j < ilbc_constants.LPC_FILTERORDER; j++) {

           if (j == 0) {
               a[0] = 0.25f;
               b[0] = -0.25f;
           } else {
               a[0] = b[0] = 0.0f;
           }

           for (i=0; i < ilbc_constants.LPC_HALFORDER; i++) {
               a[i + 1] = a[i] - 2 * p[i] * a1[i] + a2[i];
               b[i + 1] = b[i] - 2 * q[i] * b1[i] + b2[i];
               a2[i] = a1[i];
               a1[i] = a[i];
               b2[i] = b1[i];
               b1[i] = b[i];
           }

           a_coef[j + 1] = 2 * (a[ilbc_constants.LPC_HALFORDER] +
				b[ilbc_constants.LPC_HALFORDER]);
       }

       a_coef[0] = 1.0f;
   }

   /*----------------------------------------------------------------*
    *  Construct decoded vector from codebook and gains.
    *---------------------------------------------------------------*/

   /*----------------------------------------------------------------*
    *  interpolation between vectors
    *---------------------------------------------------------------*/

    public static void interpolate(
       float out[],      /* (o) the interpolated vector */
       float in1[],     /* (i) the first vector for the
                              interpolation */
       float in2[],     /* (i) the second vector for the
                              interpolation */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -