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

📄 demo.cpp

📁 The line echo canceller (LEC) is designed to provide the maximum attainable transparent voice qualit
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			}
		} /* end of switch */
	} /* state chandge */

	/* periodic */
	switch(_Gen.iState)
	{
		case 0: /* silence */
		{
			Rx = _gen_rand(_Gen.fRxNoise);
			Tx = _gen_rand(_Gen.fTxNoise);
			break;
		}
		case 1: /* tx */
		{
			Rx = _gen_rand(_Gen.fRxNoise);
			Tx = _gen_rand(_Gen.fTxLevel);
			break;
		}
		case 2: /* rx */
		{
			Rx = _gen_rand(_Gen.fRxLevel);
			Tx = _gen_rand(_Gen.fTxNoise);
			break;
		}
		case 3: /* double talk */
		{
			Rx = _gen_rand(_Gen.fRxLevel);
			Tx = _gen_rand(_Gen.fTxLevel);
			break;
		}
	}
	*pRx = Rx;
	*pTx = Tx;

}

/*-------------------------------------------------------------------------*/
void                            _init_generator
/*-------------------------------------------------------------------------*/
(
void
)
{
	_Gen.iDirection  = 1;
	_Gen.liCurrWord  = 3;
	switch(_Gen.iState)
	{
		case 0:
		{
			_Gen.liStateCntr = long(_Gen.fPauseLen * 8000);
			break;
		}
		case 1:
		case 2:
		{
			_Gen.liStateCntr = long(_Gen.fWordLen * 8000);
			_Gen.liCurrWord = _Gen.liSentenceLen;
			break;
		}
		default:
		{
			_Gen.iState = 0;
			break;
		}
	}
//	randomize();
	srand(105);
}

/*-------------------------------------------------------------------------*/
void                _make_input
/*-------------------------------------------------------------------------*/
(
)
{

#define FR1     50
#define FR2     (FR1 + 80)
#define FR3     (FR2 + 40)
#define FR4     (FR3 + 40)
#define FR5     (FR4 + 30)

#define F1 (350/8000.)
#define F2 (440/8000.)
#define F3 (697/8000.)
#define F4 (1633/8000.)
#define F5 (697/8000.)
#define F6 (1633/8000.)
#define ADT (400)
#define ADTMF (3000)

    int k;
	if (_Frame < FR1)
	{
		for (k = 0; k < ILEC_FR_SZ; k++)
		{
			double ph = 2*M_PI*(k+ILEC_FR_SZ*_Frame);
			_asRx[k] = S16(ADT*(sin(ph*F1)+sin(ph*F2)));
			_asTx[k] = 0;
		}
	}
	else if (_Frame < FR2 && _Frame >= FR1 )
	{
		for (k = 0; k < ILEC_FR_SZ; k++)
		{
			double ph = 2*M_PI*(k+ILEC_FR_SZ*_Frame);
			_asRx[k] = S16(ADT*(sin(ph*F1)+sin(ph*F2)));
			_asTx[k] = S16(ADTMF*(sin(ph*F3)+sin(ph*F4)));
		}
	}
	else if (_Frame < FR3 && _Frame >=FR2 )
	{
		for (k = 0; k < ILEC_FR_SZ; k++)
		{
			double ph = 2*M_PI*(k+ILEC_FR_SZ*_Frame);
			_asRx[k] = S16(ADT*(sin(ph*F1)+sin(ph*F2)));
			_asTx[k] = 0;
		}
	}
	else if (_Frame < FR4 && _Frame >= FR3 )
	{
		for (k = 0; k < ILEC_FR_SZ; k++)
		{
			_asRx[k] = 0;
			_asTx[k] = 0;
		}
	}
#if 1
	else if (_Frame < FR5 && _Frame >= FR4 )
	{
		for (k = 0; k < ILEC_FR_SZ; k++)
		{
			double ph = 2*M_PI*(k+ILEC_FR_SZ*_Frame);
			_asRx[k] = 0;
			_asTx[k] = S16(ADTMF*(sin(ph*F5)+sin(ph*F6)));
		}
	}
#endif
	else
    {
		for (k = 0; k < ILEC_FR_SZ; k++)
		{
            S16 sRx, sTx;
			_generate_input(&sRx, &sTx);
			_asRx[k] = sRx;
			_asTx[k] = sTx;
//			aRx[k] = 0;
//			if(xxx == FILTER_LEN){ aTx[k] = 700; xxx = 1;}
//			else {aTx[k] = (rand()-0x4000)/0x1000;xxx++;}
		}
    }
}

/*-------------------------------------------------------------------------*/
void                        _make_singular
/*-------------------------------------------------------------------------*/
(
)
{
	int Rx = 0;
	double dTx = 0;
	int Tx;
	int k;
	static float f = 247.0;
	static double sph = 0;

	for (k = 0; k < ILEC_FR_SZ; k++)
	{
#if (IN_MODE == 1)
		dTx = 0.5*5810.0*sin(2*M_PI*sph*375.0/8000.0)+

			  0.1*5810.0*sin(2*M_PI*sph*300.0/8000.0);
		Rx = 0;
#elif (IN_MODE == 2)
		dTx = (long(sph)%20-10)*300.0;
		Rx = 0;
#elif (IN_MODE == 3)
		int xxx = 10.0+sph/1e4;
		dTx = (long(sph)%xxx-xxx/2 < 0)? 1000:-1000;
		Rx = 0;
#elif (IN_MODE == 4)

		if(k== 0)
		{
			f+= 0.1;
		}
		dTx = 0.5*5810.0*sin(2*M_PI*sph*f/8000.0);
		Rx = 0;
#endif
		sph += 1;
		Tx = int(dTx);
		if (dTx > 8159) Tx = 8159;
		if (dTx < -8159) Tx = -8159;
		_asTx[k] = Tx;
		_asRx[k] = Rx;
	}

}

/*-------------------------------------------------------------------------*/
void                            _fill_input
/*-------------------------------------------------------------------------*/
(
)
{
    int k;

    for (k = 0; k < ILEC_FR_SZ; k++)
    {
        _asRx[k] = 0;
        _asTx[k] = 0;
    }

#if (IN_MODE != 0)

    _make_singular();

#else /* IN_MODE == 0*/

    if (_Control.pInTrxFile)
    {
        _read_intrx_file();
    }
    else if (_Control.pInTxFile)
	{
        _read_intx_file();
    }
    else if (_Control.pInTxBin)
	{
        _read_tx_bin();
        if (_Control.pInRxBin)
	    {
            _read_rx_bin();
        }
    }
    else
    {
        _make_input();
    }

#endif
}

/*-------------------------------------------------------------------------*/
void                        _add_reflections
/*-------------------------------------------------------------------------*/
(
)
{
	int k;

	for (k = 0; k < ECHO_SZ + ILEC_FR_SZ; k++)
	{
		_asEchoTx[k] = _asEchoTx[k + ILEC_FR_SZ];
	}

	for (k = 0; k < ILEC_FR_SZ; k++)
	{
		_asEchoTx[k + ECHO_SZ + ILEC_FR_SZ] = _asTx[k];
	}

	for (k = 0; k < ILEC_FR_SZ; k++)
	{
		double Sum = 0;
		int n;
		int DataIndex = ECHO_SZ + k - ILEC_FR_SZ;

		for (n = 0; n < ECHO_SZ; n++, DataIndex--)
//		for (n = 0; n < 2*ILEC_FR_SZ; n++, DataIndex--)
		{
			Sum += _asEchoTx[DataIndex] * _afEcho[n];
		}
#if 0
//#if PREEMP
#define KK 2
#define CF1 0.75
#define CF2 0.25
        {
	        float t;
	        static float LastSample1 = 0;
	        static float LastSample2 = 0;
	        t = (-LastSample2*CF2 -LastSample1*CF1 +Sum)*KK;
	        LastSample2 = LastSample1;
	        LastSample1 = Sum;
	        Sum = t;
        }
#endif
		_asRx[k] += S16(Sum);
	}
}


/*-------------------------------------------------------------------------*/
void                    add_noise
/*-------------------------------------------------------------------------*/
(
double fNorma, 
S16 *pIn
)
{
    S16 k;
    for(k=0; k<ILEC_FR_SZ; k++)
    {
        pIn[k] += _gen_rand(fNorma);
    }
}


/*-------------------------------------------------------------------------*/
S16                     dbs
/*-------------------------------------------------------------------------*/
(
double dB
)
{
    return (S16)(dB*170.1);
}

/*-------------------------------------------------------------------------*/
void                     _set_wav
/*-------------------------------------------------------------------------*/
(
)
{
    _WavHdr.acRiff[0] = 'R';
    _WavHdr.acRiff[1] = 'I';
    _WavHdr.acRiff[2] = 'F';
    _WavHdr.acRiff[3] = 'F';

    _WavHdr.ulLen1 = 36;
    _WavHdr.acWave[0] = 'W';
    _WavHdr.acWave[1] = 'A';
    _WavHdr.acWave[2] = 'V';
    _WavHdr.acWave[3] = 'E';

    _WavHdr.acFmt[0] = 'f';
    _WavHdr.acFmt[1] = 'm';
    _WavHdr.acFmt[2] = 't';
    _WavHdr.acFmt[3] = ' ';

    _WavHdr.ulLen2 = 16;
    _WavHdr.uMode  = 1;
    _WavHdr.uNumber = 1;
    _WavHdr.ulFs = 8000;
    _WavHdr.ulBps = 16000;
    _WavHdr.uBytes = 2;
    _WavHdr.uBits = 16;

    _WavHdr.acData[0] = 'd';
    _WavHdr.acData[1] = 'a';
    _WavHdr.acData[2] = 't';
    _WavHdr.acData[3] = 'a';
    _WavHdr.ulLen3 = 0;
    
    fwrite(&_WavHdr, 1, sizeof(_WavHdr), _Control.pNlpWavFile);
    fwrite(&_WavHdr, 1, sizeof(_WavHdr), _Control.pCnlWavFile);
    fwrite(&_WavHdr, 1, sizeof(_WavHdr), _Control.pTxWavFile);
    fwrite(&_WavHdr, 1, sizeof(_WavHdr), _Control.pRxWavFile);
}

/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/

/*                  C O N F I G U R A T I O N                              */

/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------*/
int                     str_cmp
/*-------------------------------------------------------------------------*/
(
const char *pBasis, 
char **ppArg
)
{
    if (_strnicmp(pBasis, *ppArg, strlen(pBasis)) == 0)
    {
        *ppArg += strlen(pBasis);
        return 0;
    }
    else
    {
        return -1;
    }
}

/*-------------------------------------------------------------------------*/
int                     get_cfg_line
/*-------------------------------------------------------------------------*/
(
char *pArg
)
/* returns :
        0 - no errs
        1 - file err
        2 - arg scanf err
        3 - switch err
        */
{
    int Rc = 0;
    float fTmp;
    int iTmp;
    

    if ( *pArg == '-' || *pArg == '/')
    {
        pArg++;

        if (str_cmp("ADF", &pArg) == 0)
	    {
		    _Control.pAdfFile = fopen("adf.f", "w");
		    if ( _Control.pAdfFile == NULL )
		    {
			    Rc = 1;
		    } /* end-of-if */
	    }
	    else if (str_cmp("PCM", &pArg) == 0)
	    {
		    _Control.pPcmFile = fopen("out.pcm", "wb");
		    if ( _Control.pPcmFile == NULL )
		    {
			    Rc = 1;
		    } /* end-of-if */
	    }
	    else if (str_cmp("WAV", &pArg) == 0)
	    {
		    _Control.pNlpWavFile = fopen("nlp.wav", "wb");
		    _Control.pCnlWavFile = fopen("cnl.wav", "wb");
		    _Control.pTxWavFile = fopen("tx.wav", "wb");
		    _Control.pRxWavFile = fopen("rx.wav", "wb");
		    if ((_Control.pNlpWavFile == NULL ) ||
		        (_Control.pCnlWavFile == NULL ) ||
		        (_Control.pTxWavFile == NULL ) ||
		        (_Control.pRxWavFile == NULL ))
		    {
			    Rc = 1;
		    }
            else
            {
                _set_wav();
            }/* end-of-if */
	    }
	    else if (str_cmp("TMP", &pArg) == 0)
	    {
		    _Control.pTmpFile = fopen("tmp.txt", "w");
		    if ( _Control.pTmpFile == NULL )
		    {
			    Rc = 1;
		    } /* end-of-if */
	    }
	    else if (str_cmp("BYPASS", &pArg) == 0)
	    {
		    if (sscanf(pArg, "%f", &fTmp) != 1)
		    {
                Rc = 2;
		    }
            else
            {
                _Control.fBypass = fTmp;
            }
	    }
	    else if (str_cmp("GRMODE", &pArg) == 0)
	    {
		    if (sscanf(pArg, "%d", &_Control.iGmode) != 1)
            {
                Rc = 2;
            }
	    }
        else if (str_cmp("STEPMODE", &pArg) == 0)
	    {
		    if (sscanf(pArg, "%d", &iTmp) != 1)
		    {
                Rc = 2;
		    }
            else
            {
    		    DEMO_StepMode = iTmp;
            }
	    }
        else if (str_cmp("TRX_FILE", &pArg) == 0)
	    {
		    _Control.pInTrxFile = fopen(pArg+1, "r");
		    if ( _Control.pInTrxFile == NULL )
		    {
			    Rc  = 1;
		    } /* end-of-if */
            else
            {
				if (_Control.fBypass > 0)
				{
					for (long i = 0; i < _Control.fBypass * 8000; i++)
					{
						S16 x, y;
                        fscanf(_Control.pInTrxFile, "%d %d", &x, &y);
						if (i%8000 == 0)
						{
							val_out(0,0, WHITE, "second %ld   ", (i+1)/8000);
						}
					}
				}

            }
	    }
        else if (str_cmp("TX_FILE", &pArg) == 0)
	    {
		    _Control.pInTxFile = fopen(pArg+1, "r");
		    if ( _Control.pInTxFile == NULL )
		    {

⌨️ 快捷键说明

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