G711编解码
http://blog.csdn.net/rightorwrong/article/details/4209467
搞語音對講幾天了,播放時聲音干擾太大了。拖得時間久有兩個原因:
1.每次采樣的位數這個值設置的問題。本來是用的采樣位數為16,但是服務端那邊說用8。導致編解碼時8位的始終有雜音。
1.G711編解碼的問題:用了一個錯誤的編解碼程序。目前用的編解碼代碼對于8為始終有雜音
下面把編解碼的代碼放在這里,16為采集效果很好
[cpp] view plaincopyprint?
//編碼
intCG711Decoder::G711_EnCode(unsignedchar*pCodecBits,constchar*pBuffer,intnBufferSize)
{
short*buffer=(short*)pBuffer;
for(inti=0;i<nBufferSize/2;i++)
{
pCodecBits[i]=encode(buffer[i]);
}
returnnBufferSize/2;
}
//解碼
intCG711Decoder::G711_Decode(char*pRawData,constunsignedchar*pBuffer,intnBufferSize)
{
short*out_data=(short*)pRawData;
for(inti=0;i<nBufferSize;i++)
{
out_data[i]=decode(pBuffer[i]);
}
returnnBufferSize*2;
}
#defineMAX(32635)
unsignedcharCG711Decoder::encode(shortpcm)
{
intsign=(pcm&0x8000)>>8;
if(sign!=0)
pcm=-pcm;
if(pcm>MAX)pcm=MAX;
intexponent=7;
intexpMask;
for(expMask=0x4000;(pcm&expMask)==0
&&exponent>0;exponent--,expMask>>=1){}
intmantissa=(pcm>>((exponent==0)?4:(exponent+3)))&0x0f;
unsignedcharalaw=(unsignedchar)(sign|exponent<<4|mantissa);
return(unsignedchar)(alaw^0xD5);
}
shortCG711Decoder::decode(unsignedcharalaw)
{
alaw^=0xD5;
intsign=alaw&0x80;
intexponent=(alaw&0x70)>>4;
intdata=alaw&0x0f;
data<<=4;
data+=8;
if(exponent!=0)
data+=0x100;
if(exponent>1)
data<<=(exponent-1);
return(short)(sign==0?data:-data);
}
總結
- 上一篇: php mysql修复_MySQL数据表
- 下一篇: 树莓派(Linux)添加USB外接硬盘