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

📄 celp_encode.c

📁 this the source code of addio compression standard CELP. Also, it is optimizied for the execution sp
💻 C
字号:
/**************************************************************************
*                                                                         *
*	CELP Voice Encoder --- A fast version                             *
*	Base on the original Version 3.2c.                                *
*									  *
*                                                                         *
**************************************************************************/
#define TRUE		1
#define FALSE		0
#define STREAMBITS	144
#define CODELENGTH1	15
#define CODELENGTH2	11
#define PARITYLENGTH	(CODELENGTH1 - CODELENGTH2)
#define SYNDRUN		100
#define OMEGA		0.994127  /* Bandwidth expansion for LPC analysis (15 Hz) */
#define ALPHA		0.8  /* Bandwidth expansion for postfilter */
#define BETA		0.5  /* Bandwidth expansion for postfilter */
#define I2		1  /* symbolic disk_io data type */
#define I4		2  /* symbolic disk_io data type */
#define R4		3  /* symbolic disk_io data type */
#define mmax(A,B)        ((A)>(B)?(A):(B))
#define mmin(A,B)        ((A)<(B)?(A):(B))
/* #define nint(F)          (((F)>0)?(int)(F+0.5):(int)(F-0.5))*/
#include <stdio.h>
#include <math.h>
#include <strings.h>
#include "ccsub.h"

int cbgbits = 5, cbindex = 0, gindex = 0, idb = 0, ncsize = 512, no = 10;
int nseg = 0, pindex = 0, frame = 0, tauptr = 0, minptr = 0, plevel1 = 0;
int plevel2 = 0, pbits[MAXNP + 2] = {8, 6, 5, 0, 0};

float bb[MAXNP + 1], e0[MAXLP];
float fc[MAXNO + 1], fcn[MAXNO + 1];
float gamma2 = 0.8, prewt = 0.0;

  /* *read adaptive code book index (pitch delay) file		*/
float pdelay[MAXPD] = 
{
#include "pdelay.h"
};


  /* *load stochastic code book vector	file			*/
float x[MAXCODE] = 
{
#include "codebook.h"
};

/* *load the ENG(=YxY) of every filtered stochastic code book vector file */ 
float eng_cb[MAXNCSIZE] = 
{
#include "ENG_YXY.h"
};

    /* 3rd order predictor  */
float fc_3[11]={1.0, -1.10, 0.28, 0.08, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};  

     /* Impuls response of a time-ivariable 3rd order predictor    */
float h[MAXLP]={1.0, 0.88, 0.5952, 0.32512, 0.143401, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};  
float hl[MAXLP];  


main(argc, argv)
int argc;
char *argv[];

{

  register int i, k, ntmp;
  int l = 60;
  int ll = 240, lp = 60, nn, np = 1;
  int findex[MAXNO];
  int lspflag;
  int sync = 1;

  short iarf[MAXLL];

  float sold[MAXLL], snew[MAXLL], ssub[MAXLL], v[MAXLL];
  float rcn[MAXNO], hamw[MAXLL], hamws[MAXL];
  float newfreq[MAXNO], lsp[MAXLL / MAXL][MAXNO];

  /* *load pitch delay coding tables for bit assignment		*/
  /* *pdencode.h for encoding 		*/
  static int pdencode[MAXPD] = 
  {
#include "pdencode.h"
  };
  /* *filter coefficients for 2nd order 100 Hz HPF with 60 Hz notch:	*/
  /* static float ahpf[3] = {1.0, -1.99778, 1.0};  */
  /* static float bhpf[3] = {1.0, -1.88 0.89};  */

  /* *filter coefficients for 2nd order Butterworth 100 Hz HPF:		*/
  static float ahpf[3] = {0.946, -1.892, 0.946};
  static float bhpf[3] = {1.0, -1.889033, 0.8948743};

  /* *filter coefficients for 2nd order Butterworth 275 Hz HPF:		*/
  /* static float ahpfo[3] = {0.858, -1.716, 0.858}; */
  /* static float bhpfo[3] = {1.0, -1.696452, 0.7368054}; */


/*
		*bit stream 
*/


  int cbbits = 9, pointer;
  int  pstream[STREAMBITS];
  static int sbits[MAXNO] = {3, 4, 4, 4, 4, 3, 3, 3, 3, 3};
  short stream[STREAMBITS];
  char line[38];

/*
		*filter memories (should be maxno+1)
*/

  static float dhpf1[3], dhpf2[3];
/*
		*error control coding parameters:
*/
  int codeword[CODELENGTH1], hmatrix[CODELENGTH1];
  int syndrometable[CODELENGTH1], paritybit, protect;

  /* *load bit protection vector 				*/
  static int bitprotect[CODELENGTH2] = 
  {
#include "bitprot.h"
  };

  /* *load bit permutation vector				 */
  static int bitpermute[STREAMBITS] = 
  {
#include "bitperm.h"
  };


  /*  set path for reading *.adc files   */

     if (argc != 1) {
        fprintf(stderr, "Usage: %s   < *.spd   >  *.chan\n", argv[0]);
        exit(1);
     }

  /* ********************* initialize********************	 */

  /* *number of codewords/LPC frame				 */

  nn = ll / l;

  /* *dimension of d1a and d1b???				 */

  idb = MMAX + MAXNP - 1 + l;
  plevel1 = 1 << pbits[0];

  /* *levels of delta tau					 */

  plevel2 = 1 << pbits[1];

  /* *enable/disable error control coding			 */

  protect = TRUE;

  /* *intialize arrays						*/

  for (i = 0; i < MAXLP; i++)  e0[i] = 0.0;
  for (i = 0; i < MAXLL; i++) sold[i] = 0.0;
  for (i = 0; i < STREAMBITS; i++) stream[i] = 0;

  /* *start nseg at 0 to do pitch on odd segments 		*/
  /*   (nseg is incremented before csub) 			*/

  nseg = 0;

  /* *generate matrix for error control coding			 */

  matrixgen(CODELENGTH1, CODELENGTH2, hmatrix, syndrometable);

  /* *generate Hamming windows					 */

  ham(hamw, ll);

  /* *** open and define files					 */


  /* *bit stream channel file                                   */

  /* ......................... m a i n  l o o p ........................ */

  /* *** ANALYSIS ...................................................... */

  /* *** read speech segment s of size ll, until end of file		 */
  while(1)
  {
    if( i=fread(iarf, sizeof(short), ll, stdin) < ll ) break;
    frame++;
    pointer = 0;

    /* *display a propeller (rotating bar) once per frame		 */

/*    mark(0);          */
    /* fprint(stderr,"frame =  ",frame);				 */


    /* *The ssub buffer used for subframe CELP analysis is 1/2 a	 */
    /* *frame behind the snew buffer and 1/2 a frame ahead of the 	 */
    /* *sold buffer.							 */

    for (i = 0; i < ll; i++)
    {
      snew[i] = mmax(-32768., mmin(iarf[i], 32767.));
    } 

      
    /* *high pass filter snew						 */
/*
    zerofilt_S(ahpf, 2, dhpf1, snew, ll);
    polefilt_S(bhpf, 2, dhpf2, snew, ll);
*/
    /* *make ssub vector from snew and sold			 	 */
    ntmp = ll/2;
    for (i = 0; i < ntmp; i++)
    {
      ssub[i] = sold[i + ntmp];
      ssub[i + ntmp] = snew[i];
    }


    autohf(snew, hamw, ll, no, OMEGA, fcn, rcn);


    /* *pc -> lsp (new)							 */

    pctolsp2(fcn, no, newfreq, &lspflag);

    /* *quantize lsp's		 					 */

    lsp34(newfreq, no, sbits, findex);


    /* *pack lsp indices in bit stream array				 */

    for (i = 0; i < no; i++)
      pack(findex[i], sbits[i], stream, &pointer);

    /* *linearly interpolate LSP's for each subframe			 */

    intanaly(newfreq, nn, lsp);

    /* *** for each subframe, search stochastic & adaptive code books    */

    k = 0;
    for (i = 0; i < nn; i++)
    {

      lsptopc(&lsp[i][0], fc);
      nseg++;

      /* *** code book & pitch searches					 */

      csub(&ssub[k], &v[k], l, lp);


      /* *pitch quantization tau					 */

      /* *pack parameter indices in bit stream array			 */

      if (((i+1) % 2) != 0)
	packtau(tauptr-minptr, pbits[0], pdencode, stream, &pointer);
      else
	pack(tauptr-minptr, pbits[1], stream, &pointer);

      pack(pindex, pbits[2], stream, &pointer);
      cbindex--;
      pack(cbindex, cbbits, stream, &pointer);
      pack(gindex, cbgbits, stream, &pointer);

      cbindex++;
      k += l;
    }

    /* *** bit error protection						 */
    /* *extract bits to protect from stream array		 	 */
    if (protect)
    {
      for (i = 0; i < CODELENGTH2; i++)
	codeword[i] = stream[bitprotect[i] - 1];

      /* *hamming encode						 */

      encodeham(CODELENGTH1, CODELENGTH2, hmatrix, &paritybit, codeword);

      /* *pack future bit					 */

      pack(0, 1, stream, &pointer);

      /* *pack parity bits						 */

      for (i = 0; i < PARITYLENGTH; i++)
	pack(codeword[CODELENGTH2 + i], 1, stream, &pointer);

      /* *toggle and pack the sync bit					*/

      sync = sync ^ 1;
      pack(sync, 1, stream, &pointer);

    }

    /* *permute bitstream 					 	 */

    for (i = 0; i < STREAMBITS; i++)
      pstream[i] = stream[bitpermute[i] - 1];

    /* *save stream in Dave's format					 */

    puthex(STREAMBITS, pstream, line);
    fprintf(stdout, "%s\n", line);

    /* .......................end block................................. */


    /* *** shift new speech buffer into old speech buffer	 	 */

    /*          	sold		   snew				 */
    /*         |-------------------|-------------------| snew		 */
    /*		          |-------------------|				 */
    /* 				  ssub					 */

    for (i = 0; i < ll; i++)
      sold[i] = snew[i];

    /* *** frame finished, end loop					 */

  } /* end main while loop     */

  /* ...........e n d  m a i n  l o o p ...............................  */

  /* ***  and close files					 */
   
   exit(0);
}

⌨️ 快捷键说明

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