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

📄 utils.c

📁 GSM中半速率语音编解码源码
💻 C
📖 第 1 页 / 共 3 页
字号:
 * *   KEYWORDS: shift, arithmetic shift right, * *************************************************************************/Shortword shr(Shortword var1, Shortword var2){  Shortword swMask,         swOut;  if (var2 == 0 || var1 == 0)    swOut = var1;  else if (var2 < 0)  {    /* perform an arithmetic left shift */    /*----------------------------------*/    if (var2 <= -15)    {      /* saturate */      if (var1 > 0)        swOut = SW_MAX;      else        swOut = SW_MIN;    }    else      swOut = shl(var1, -var2);  }  else  {    /* positive shift count */    /*----------------------*/    if (var2 >= 15)    {      if (var1 < 0)        swOut = (Shortword) 0xffff;      else        swOut = 0x0;    }    else    {      /* take care of sign extension */      /*-----------------------------*/      swMask = 0;      if (var1 < 0)      {        swMask = ~swMask << (16 - var2);      }      var1 >>= var2;      swOut = swMask | var1;    }  }  return (swOut);}/*___________________________________________________________________________ |                                                                           | |     This subroutine calculates the 'SID flag'                             | |                                                                           | |     Input:     pswParameters[18]                                          | |                           input parameters of the speech decoder          | |                                                                           | |                pswErrorFlag[3]                                            | |                           error flags, generated by channel decoder       | |                                                                           | |     Return Value:                                                         | |                0:         speech frame detected                           | |                1:         most likely SID frame received                  | |                2:         SID frame detected                              | |                                                                           | |___________________________________________________________________________| |                                                                           | |     History:                                                              | |                                                                           | |     12-Oct-1994: Bug removed: error corrected in case of a mode (unvoiced/| |                  voiced) mismatch, if a SID frame was received as an      | |                  unvoiced frame                                           | |___________________________________________________________________________|*/static Shortword swSidDetection(Shortword pswParameters[],                                Shortword pswErrorFlag[]){  static Shortword ppswIBit[2][18] = {        5, 11,9,8, 1, 2, 7,7,5, 7,7,5, 7,7,5, 7,7,5,  /* unvoiced */        5, 11,9,8, 1, 2, 8,9,5, 4,9,5, 4,9,5, 4,9,5}; /* voiced */  static Shortword ppswCL1pCL2[2][18] = {                              0x0001, /* R0      */  /* unvoiced */                              0x00ef, /* LPC1    */                              0x003e, /* LPC2    */                              0x007f, /* LPC3    */                              0x0001, /* INT LPC */                              0x0003, /* Mode    */                              0x001f, /* Code1_1 */                              0x0072, /* Code2_1 */                              0x0012, /* GSP0_1  */                              0x003f, /* Code1_2 */                              0x007f, /* Code2_2 */                              0x0008, /* GSP0_2  */                              0x007f, /* Code1_3 */                              0x007f, /* Code2_3 */                              0x0008, /* GSP0_3  */                              0x007f, /* Code1_4 */                              0x007f, /* Code2_4 */                              0x000c, /* GSP0_4  */                                                            0x0000, /* R0      */  /* voiced */                              0x0000, /* LPC1    */                              0x0000, /* LPC2    */                              0x0000, /* LPC3    */                              0x0001, /* INT LPC */                              0x0003, /* Mode    */                              0x00ff, /* Lag_1   */                              0x01ff, /* Code_1  */                              0x001f, /* GSP0_1  */                              0x000f, /* Lag_2   */                              0x01ff, /* Code_2  */                              0x001f, /* GSP0_2  */                              0x000f, /* Lag_3   */                              0x01ff, /* Code_3  */                              0x001f, /* GSP0_3  */                              0x000f, /* Lag_4   */                              0x01ff, /* Code_4  */                              0x001f}; /* GSP0_4 */  static Shortword ppswCL2[2][18] = {                              0x0000, /* R0      */ /* unvoiced */                              0x0000, /* LPC1    */                              0x0000, /* LPC2    */                              0x0000, /* LPC3    */                              0x0000, /* INT LPC */                              0x0000, /* Mode    */                               0x0000, /* Code1_1 */                              0x0000, /* Code2_1 */                              0x0000, /* GSP0_1  */                              0x0000, /* Code1_2 */                              0x0000, /* Code2_2 */                              0x0000, /* GSP0_2  */                              0x0000, /* Code1_3 */                              0x0007, /* Code2_3 */  /* 3 bits */                              0x0000, /* GSP0_3  */                              0x007f, /* Code1_4 */  /* 7 bits */                              0x007f, /* Code2_4 */  /* 7 bits */                              0x0000, /* GSP0_4  */                              0x0000, /* R0      */  /* voiced */                              0x0000, /* LPC1    */                              0x0000, /* LPC2    */                              0x0000, /* LPC3    */                              0x0000, /* INT LPC */                              0x0000, /* Mode    */                              0x0000, /* Lag_1   */                              0x0000, /* Code_1  */                              0x0000, /* GSP0_1  */                              0x0000, /* Lag_2   */                              0x0000, /* Code_2  */                              0x0000, /* GSP0_2  */                              0x0000, /* Lag_3   */                              0x00ff, /* Code_3  */  /* 8 bits */                              0x0000, /* GSP0_3  */                              0x0000, /* Lag_4   */                              0x01ff, /* Code_4  */  /* 9 bits */                              0x0000}; /* GSP0_4 */  static int first = 1;  Shortword swMode, swBitMask;  Shortword swSidN1, swSidN2, swSidN1pN2;  Shortword swSid ;  short siI, siII;  if (first)  {    /* Force Sid codewords to be represented */    /* internally in PC byte order           */    /* ------------------------------------- */    SwapBytes(ppswCL1pCL2[0], 18);    SwapBytes(ppswCL1pCL2[1], 18);    SwapBytes(ppswCL2[0], 18);    SwapBytes(ppswCL2[1], 18);    first = 0;  }  /* count transmission errors within the SID codeword      */  /* count number of bits equal '0' within the SID codeword */  /* ------------------------------------------------------ */  if (pswParameters[5] == 0)    swMode = 0;  else     swMode = 1;    swSidN1pN2 = 0;         /* N1 + N2 */  swSidN2    = 0;  swSidN1    = 0;    for (siI = 0; siI < 18; siI++) {      swBitMask = 0x0001;      SwapBytes(&swBitMask, 1);  /* force swBitMask to PC byte order */      for (siII = 0; siII < ppswIBit[swMode][siI]; siII++) {        if ( (pswParameters[siI] & swBitMask) == 0 ) {          if ( (ppswCL1pCL2[swMode][siI] & swBitMask) != 0 ) swSidN1pN2++;          if ( (ppswCL2[swMode][siI] & swBitMask)     != 0 ) swSidN2++;        }        SwapBytes(&swBitMask, 1);  /* return swBitMask to native byte order */        swBitMask = swBitMask << 1;        SwapBytes(&swBitMask, 1);  /* force swBitMask to PC byte order */      }  }  swSidN1 = swSidN1pN2 - swSidN2;  /* frame classification */  /* -------------------- */  if (pswErrorFlag[2]) {    if (swSidN1 < 3)       swSid = 2;    else if (swSidN1pN2 < 16)       swSid = 1;    else       swSid = 0;         if ( (swSidN1pN2 >= 16) && (swSidN1pN2 <= 25) ) {      pswErrorFlag[0] = 1;         }  }  else {    if (swSidN1 < 3)       swSid = 2;    else if (swSidN1pN2 < 11)       swSid = 1;    else       swSid = 0;  }  /* in case of a mode mismatch */  /*----------------------------*/    if ( (swSid == 2) && (swMode == 0) ) swSid = 1;  return(swSid);}/*___________________________________________________________________________ |                                                                           | |     This subroutine sets the 18 speech parameters to random values        | |                                                                           | |     Input:     pswParameters[18]                                          | |                           input parameters of the speech decoder          | |                                                                           | |___________________________________________________________________________|*/static void RandomParameters(Shortword pswParameters[]){  static Shortword ppswIBit[2][18] = {             5, 11,9,8, 1, 2, 7,7,5, 7,7,5, 7,7,5, 7,7,5,  /* unvoiced */             5, 11,9,8, 1, 2, 8,9,5, 4,9,5, 4,9,5, 4,9,5}; /* voiced */  static Longword L_PNSeed=(Longword)0x1091988L;  Shortword  i,ind;  /* Determine mode bit */  /* ------------------ */  pswParameters[5] = getPnBits(2, &L_PNSeed);  /* Switch bit allocation accordingly */  /* --------------------------------- */  ind = 0;  if (pswParameters[5] > 0) ind = 1;  for (i=0; i < 5; i++){     pswParameters[i] = getPnBits(ppswIBit[ind][i], &L_PNSeed);  }  for (i=6; i < 18; i++){     pswParameters[i] = getPnBits(ppswIBit[ind][i], &L_PNSeed);  }  /* force random parameters to PC byte order */  /* ---------------------------------------- */  SwapBytes(pswParameters, 18);  }/*___________________________________________________________________________ |                                                                           | |     Main - Program                                                        | |                                                                           | |___________________________________________________________________________|*/int main( int argc, char *argv[] ){    FILE *infile, *outfile;    Shortword errpat, i = 0;    if( argc < 4 || argc > 4 ) {        fprintf( stderr, "\tUsage: REID input output EPx \n" );        fprintf( stderr, "\tEPx: EP0\n" );        fprintf( stderr, "\t     EP1 (not implemented)\n" );        fprintf( stderr, "\t     EP2 (not implemented)\n" );        fprintf( stderr, "\t     EP3 (not implemented)\n" );        return( 1 );    }    if( !Strincmp( argv[3], "ep", 2 ))         errpat  = atoi( &argv[3][2] );    printf( "  _____________________________________________\n" );    printf( " |                                             |\n" );    printf( " |       Residual Error Insertion Device       |\n" );    printf( " |                 for                         |\n" );    printf( " |       GSM Half-Rate Codec Simulation        |\n" );    printf( " |                                             |\n" );    printf( " |_____________________________________________|\n\n" );    printf( "    Input File       : %s\n", argv[1] );    printf( "    Output File      : %s\n", argv[2] );    if( errpat ){        printf( "    Error Pattern    : EP%d (not implemented)\n", errpat);        return (1);    }    else        printf( "    Error Pattern    : EP%d (error free)\n", errpat );    printf( "\n" );    infile  = OpenBinfile( argv[1], "r" );    outfile = OpenBinfile( argv[2], "w" );    if (errpat == 0) {       for (i=0;i<6000;i++)           if( error_free( infile, outfile)) break;    }    /*else       for (i=0;i<6000;i++)           if( residual_error_pattern( infile, outfile)) break;        EP1-3 not implemented */    fclose( infile );    fclose( outfile );    printf( " %d Frame%s processed      \n\n", i,( i != 1 ) ? "s" : "" );     return( 0 );}

⌨️ 快捷键说明

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