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

📄 utilcng2.cpp

📁 G711语音压缩源码
💻 CPP
字号:
//#include "stdafx.h"
#include "LanAudio.h"
#include "Global.h"

/*
**
** File:        "utilcng2.c"
**
** Description:     General Comfort Noise Generation functions
**
**
** Functions:       Calc_Exc_Rand() Computes random excitation
**                                  used both by coder & decoder
**                  Qua_SidGain()   Quantization of SID gain
**                                  used by coder
**                  Dec_SidGain()   Decoding of SID gain
**                                  used both by coder & decoder
**
** Local functions :
**                  random_number()

*/

/* Declaration of local functions */
static short random_number(short np1, short *nRandom);

/*
**
** Function:           Qua_SidGain()
**
** Description:        Quantization of Sid gain
**                     Pseudo-log quantizer in 3 segments
**                     1st segment : length = 16, resolution = 2
**                     2nd segment : length = 16, resolution = 4
**                     3rd segment : length = 32, resolution = 8
**                     quantizes a sum of energies
**
** Links to text:
**
** Arguments:
**
**  float  *Ener        table of the energies
**  short nq           if nq >= 1 : quantization of nq energies
**                      for SID gain calculation in function Cod_Cng()
**                      if nq = 0 : in function Comp_Info(),
**                      quantization of saved estimated excitation energy
**
** Outputs:             None
**
**
** Return value:        index of quantized energy
**
*/
short Qua_SidGain(float *Ener, short nq)
{
    short  temp16, iseg, iseg_p1;
    int  j, j2, k, exp;
    float   temp, x, y, z;
    int     i;

    if (nq == 0) {
        /* Quantize energy saved for frame erasure case                */
        /* x = fact[0] * Ener[0] with fact[0] = 1/(2*SubFrLen)         */
        x = fact[0] * Ener[0];
    }

    else {

        /*
         * Compute weighted average of energies
         * x = fact[nq] x SUM(i=0->nq-1) Ener[i]
         * with fact[nq] =  fact_mul x fact_mul / nq x Frame
         */
        for (i=0, x=(float)0.; i<nq; i++)
            x += Ener[i];
        x *= fact[nq];
    }

    /* Quantize x */
    if (x >= bseg[2])
        return(63);

    /* Compute segment number iseg */
    if (x >= bseg[1]) {
        iseg = 2;
        exp = 4;
    }
    else {
        exp  = 3;
        if (x >= bseg[0])
            iseg = 1;
        else
            iseg = 0;
    }

    iseg_p1 = (short) (iseg + 1);
    j = 1 << exp;
    k = j >> 1;

    /* Binary search in segment iseg */
    for (i=0; i<exp; i++) {
        temp = base[iseg] + (float) (j << iseg_p1);
        y = temp * temp;
        if (x >= y)
            j += k;
        else
            j -= k;
        k = k >> 1;
    }

    temp = base[iseg] + (float) (j << iseg_p1);
    y =  (temp * temp) - x;
    if (y <= (float)0.0) {
        j2 = j + 1;
        temp = base[iseg] + (float) (j2 << iseg_p1);
        z = x - (temp * temp);
        if (y > z)
            temp16 = (short) ((iseg<<4) + j);
        else
            temp16 = (short) ((iseg<<4) + j2);
    }
    else {
        j2 = j - 1;
        temp = base[iseg] + (float) (j2 << iseg_p1);
        z = x - (temp * temp);
        if (y < z)
            temp16 = (short) ((iseg<<4) + j);
        else
            temp16 = (short) ((iseg<<4) + j2);
    }
    return(temp16);
}

/*
**
** Function:           Dec_SidGain()
**
** Description:        Decoding of quantized Sid gain
**                     (corresponding to sqrt of average energy)
**
** Links to text:
**
** Arguments:
**
**  short iGain        index of quantized Sid Gain
**
** Outputs:             None
**
** Return value:        decoded gain value << 5
**
*/
float Dec_SidGain(short iGain)
{
    short i, iseg;
    float  temp;

    iseg = (short) (iGain >> 4);
    if (iseg == 3)
        iseg = 2;
    i = (short) (iGain - (iseg << 4));
    temp = base[iseg] + (float) (i << (iseg + 1));
    return(temp);
}

⌨️ 快捷键说明

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