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

📄 utils.c

📁 GSM中半速率语音编解码源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*___________________________________________________________________________ |                                                                           | |                                                                           | |                 Residual Error Insertion Device                           | |                                                                           | |                                                                           | |      File  :  REID.C                                                      | |                                                                           | |      Date  :  February 03, 1995                                           | |                                                                           | |      Version: 4.1                                                         | |                                                                           | |                                                                           | |   Description:                                                            | |   ------------                                                            | |      This routine transforms the output file format of the GSM Half       | |      Rate Encoder module consisting of:                                   | |           * 18 speech parameters (see GSM TS 06.20)                       | |           *  1 speech flag SP (see GSM TS 06.41)                          | |           *  1 voice activity flag VAD (see GSM TS 06.42)                 | |                                                                           | |      to the input file format of the GSM Half Rate Decoder module         | |      requiring:                                                           | |           * 18 speech parameters (see GSM TS 06.20)                       | |           *  1 channel condition flag BFI (see GSM TS 06.21, 05.05)       | |           *  1 channel condition flag UFI (see GSM TS 06.21, 05.05)       | |           *  1 SID flag (2 bits) (see GSM TS 06.41, 05.05)                | |           *  1 time alignment flag TAF (see GSM TS 06.41)                 | |                                                                           | |      Between SID updates the speech parameters are replaced by random     | |      values simulating an interrupted transmission on the air interface   | |                                                                           | |      The actual implementation only supports error free transmission (EP0)| |                                                                           | |      The shell for the future use of error patterns (residual error       | |      pattern insertion) is already included.  If necessary, byte swapping | |      is performed on the input speech parameters so that they are always  | |      represented internally in PC byte order (assuming that the byte      | |      order of the input file is compatible with the machine on which the  | |      program is run).  However, byte swapping is not done on the flag     | |      words (input: SP and VAD, output: BFI, UFI, SID, and TAF).  Thus,    | |      the residual error pattern insertion code may be written to handle   | |      the speech parameter words on a byte basis, but the flag words must  | |      always be handled on a word basis.                                   | |___________________________________________________________________________|*//*___________________________________________________________________________ |                                                                           | |      Creation: 19.12.94                                                   | |                                                                           | |  Changes:                                                                 | |      22.12.94:  Removal of BCI flag, instead: determination of SID flag   | |      12.01.95:  SID update period = 12 (instead of 24)                    | |      13.01.95:  When in CNI mode, the parameters between SID updates are  | |                 random values. This simulates the interrupted transmission| |      03.02.95:  Longword main( Longword...) replaced by int main(int ...),| |                 initial value of swTAFCnt set to 1                        | |___________________________________________________________________________|*//*___________________________________________________________________________ |                                                                           | |      Include-Files                                                        | |___________________________________________________________________________|*/#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#ifdef VAX#   define OPEN_WI  "wb","mrs=512","rfm=fix","ctx=stm"#   define OPEN_RI  "rb","mrs=512","rfm=fix","ctx=stm"#   define OPEN_WB  "wb","mrs=512","rfm=fix","ctx=stm"#   define OPEN_RB  "rb","mrs=2","rfm=fix","ctx=stm"#   define OPEN_WT  "w","mrs=256","rat=cr","rfm=var"#   define OPEN_RT  "r","mrs=256","rat=cr","rfm=var"#else#   define OPEN_WB  "wb"#   define OPEN_RB  "rb"#   define OPEN_WI  "wb"#   define OPEN_RI  "rb"#   define OPEN_WT  "wt"#   define OPEN_RT  "rt"#endif#define LW_SIGN (long)0x80000000       /* sign bit */#define LW_MIN (long)0x80000000#define LW_MAX (long)0x7fffffff#define SW_MIN (short)0x8000           /* smallest Ram */#define SW_MAX (short)0x7fff           /* largest Ram */typedef char  Byte;typedef long int Longword;             /* 32 bit "accumulator" (L_*) */typedef short int Shortword;           /* 16 bit "register"  (sw*) *//*___________________________________________________________________________ |                                                                           | |      local Functions                                                      | |___________________________________________________________________________|*/static Longword error_free( FILE *infile, FILE *outfile);static void SwapBytes( Shortword buffer[], Longword len );static Longword ByteOrder( void );static size_t ReadInputFile( Shortword buffer[], FILE *fp );static size_t WriteOutputFile( Shortword buffer[], FILE *fp );static Longword EncoderInterface( FILE *infile, Shortword swInPara[] );static Shortword swSidDetection(Shortword pswParameters[],                                                   Shortword pswErrorFlag[]);static void RandomParameters(Shortword pswParameters[]);static Shortword getPnBits(Shortword iBits, Longword *pL_PNSeed);FILE *OpenBinfile( char *name, char *mode );Longword Strincmp( const char *s, const char *t, size_t max );Longword Stricmp( const char *s, const char *t );Longword L_shr(Longword L_var1, Shortword var2);   /* 2 ops */Longword L_shl(Longword L_var1, Shortword var2);   /* 2 ops */Shortword shr(Shortword var1, Shortword var2);     /* 1 ops */Shortword shl(Shortword var1, Shortword var2);     /* 1 ops *//*___________________________________________________________________________ |                                                                           | |      Subroutines                                                          | |___________________________________________________________________________|*/static Longword error_free( FILE *infile, FILE *outfile){#define SPEECH      1#define CNIFIRSTSID 2#define CNICONT     3#define VALIDSID    11#define GOODSPEECH  33    static Shortword swDecoMode = {SPEECH};    static Shortword swTAFCnt = {1};    Shortword swInPara[20], i, swFrameType;    Shortword swOutPara[22],pswErrorFlag[3];     if( EncoderInterface( infile, swInPara )) return( 1 );    /* Copy input parameters to output parameters (error free transmission) */    /* -------------------------------------------------------------------- */    for (i=0;i<18;i++)       swOutPara[i] = swInPara[i];    /* Set channel status flags (error free transmission) */    /* -------------------------------------------------- */    swOutPara[18] = 0;     /* BFI flag */    swOutPara[19] = 0;     /* UFI flag */    /* Evaluate SID flag */    /* ----------------- */    pswErrorFlag[0] = 0;   /* BFI flag */    pswErrorFlag[1] = 0;   /* UFI flag */    pswErrorFlag[2] = 0;   /* BCI flag */    swOutPara[20] = swSidDetection(swOutPara, pswErrorFlag);    /* Evaluate TAF flag */    /* ----------------- */    if (swTAFCnt == 0) swOutPara[21] = 1;    else               swOutPara[21] = 0;    swTAFCnt = (swTAFCnt + 1) % 12;       /* Frame classification:                                                */    /* Since the transmission is error free, the received frames are either */    /* valid speech or valid SID frames                                     */    /* -------------------------------------------------------------------- */    if      ( swOutPara[20] == 2) swFrameType = VALIDSID;    else if ( swOutPara[20] == 0) swFrameType = GOODSPEECH;    else {            printf( "Error in SID detection\n" );            return( 1 );    }    /* Update of decoder state */    /* ----------------------- */    if (swDecoMode == SPEECH) {       if      (swFrameType == VALIDSID)   swDecoMode = CNIFIRSTSID;       else if (swFrameType == GOODSPEECH) swDecoMode = SPEECH;    }    else {  /* comfort noise insertion mode */       if      (swFrameType == VALIDSID)   swDecoMode = CNICONT;       else if (swFrameType == GOODSPEECH) swDecoMode = SPEECH;    }    /* Replace parameters by random data if in CNICONT-mode and TAF=0 */    /* -------------------------------------------------------------- */    if ((swDecoMode == CNICONT) && (swOutPara[21] == 0)){       RandomParameters(swOutPara);       /* Set flags such, that an "unusable frame" is produced */       swOutPara[18] = 1;     /* BFI flag */       swOutPara[19] = 1;     /* UFI flag */       swOutPara[20] = 0;     /* SID flag */    }        if( outfile ) {        if( WriteOutputFile( swOutPara, outfile )) {            printf( "Error writing File\n" );            return( 1 );        }    }    return( 0 );}static Longword EncoderInterface( FILE *infile, Shortword swInPara[] ){    size_t i = 0;        i = ReadInputFile( swInPara, infile );    return(( i == 0 ) ? 1 : 0 );}static size_t ReadInputFile( Shortword buffer[], FILE *fp ){    size_t i;    i = fread( buffer, sizeof( Shortword ), 20, fp );    SwapBytes( buffer, 18 );    return( i );}static size_t WriteOutputFile( Shortword buffer[], FILE *fp ){    size_t i;    SwapBytes( buffer, 18 );    i = fwrite( buffer, sizeof( Shortword ), 22, fp );    return( ( i == 22 ) ? 0 : 1 );}static void SwapBytes( Shortword buffer[], Longword len ){    Byte *pc, tmp;    Longword i;        if( !ByteOrder())        return;    pc = (Byte *)buffer;    for( i = 0; i < len; i++ ) {        tmp   = pc[0];        pc[0] = pc[1];        pc[1] = tmp;        pc += 2;    }}static Longword ByteOrder( void ){    Shortword si;    Byte *pc;    si = 0x1234;    pc = (Byte *)&si;    if (pc[1] == 0x12 && pc[0] == 0x34 )            return( 0 );    if (pc[0] == 0x12 && pc[1] == 0x34 )            return( 1 );    printf( "Error in ByteOrder: %X, %X\n", (int)pc[0], (int)pc[1] );    exit( 1 );    return( 2 );}FILE *OpenBinfile( char *name, char *mode ){    FILE *fp;    if( toupper( *mode ) == 'W' ) { /* Write access */        if(( fp = fopen( name, OPEN_WB )) == NULL ) {             printf( "Can't open output file '%s'\n", name );            exit( 1 );        }    } else { /* Read access */        if(( fp = fopen( name, OPEN_RB )) == NULL ) {             printf( "Can't open file '%s'\n", name );            exit( 1 );        }     }    return( fp );}Longword Strincmp( const char *s, const char *t, size_t max ){    for( ; max > 1; ++s, ++t, --max ) {        if( toupper( *s ) != toupper( *t ))            break;        if( *s == '\0' )            return( 0 );    }    return( toupper( *s ) - toupper( *t ));}Longword Stricmp( const char *s, const char *t ){    for(; toupper( *s ) == toupper( *t ); ++s, ++t ) {        if( *s == '\0' )            return( 0 );    }    return( toupper( *s ) - toupper( *t ));}/************************************************************************* * *   FUNCTION NAME: getPnBits * *   PURPOSE: *      *     Generate iBits pseudo-random bits using *pL_PNSeed as the *     pn-generators seed. * *   INPUTS: * *     iBits - integer indicating how many random bits to return. *     range [0,15], 0 yields 1 bit output *      *     *pL_PNSeed - 32 bit seed (changed by function) *      *   OUTPUTS: *      *     *pL_PNSeed - 32 bit seed, modified. *      *   RETURN VALUE: *      *    random bits in iBits LSB's.   *      *      *   IMPLEMENTATION: *      *    implementation of x**31 + x**3 + 1 == PN_XOR_REG | PN_XOR_ADD a *    PN sequence generator using Longwords generating a 2**31 -1 *    length pn-sequence. * *************************************************************************/static Shortword getPnBits(Shortword iBits, Longword *pL_PNSeed){#define PN_XOR_REG (Longword)0x00000005L#define PN_XOR_ADD (Longword)0x40000000L  Shortword swPnBits=0;  Longword L_Taps,L_FeedBack;  Shortword i;  for (i=0; i < iBits; i++){        /* update the state */                         /********************/        L_Taps = *pL_PNSeed & PN_XOR_REG;      L_FeedBack = L_Taps; /* Xor tap bits to yield feedback bit */    L_Taps = L_shr(L_Taps,1);    while(L_Taps){      L_FeedBack = L_FeedBack ^ L_Taps;      L_Taps = L_shr(L_Taps,1);    }    /* LSB of L_FeedBack is next MSB of PN register */    *pL_PNSeed = L_shr(*pL_PNSeed,1);    if (L_FeedBack & 1)       *pL_PNSeed = *pL_PNSeed | PN_XOR_ADD;

⌨️ 快捷键说明

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