?? imaadpcm.c
字號:
#include "stdio.h"
typedef signed char S8;
typedef unsigned char U8;
typedef unsigned short U16;
typedef signed short S16;
typedef signed int S32;
typedef unsigned int U32;
S32 tabIndexAdjust[8] = {-1,-1,-1,-1,2,4,6,8};
S32 tabStep[89] =
{
7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31,
34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130,
143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411,
1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026,
4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487,
12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
}; //this is a constant ratio rising number list, ratio=1.1
U8 ImaAdpcmEnc(S16 *pIn, S8 *pOut, U32 inLen, U32 *pOutLen)
{
U32 index,signVal, smpCnt;
S32 prevSmp,thisSmp,dSmp, code,tmp;
index=0;
prevSmp=0;
smpCnt=0;
while (inLen--)
{
thisSmp=*pIn++;
dSmp=thisSmp-prevSmp;
if (dSmp<0)
{
dSmp=-dSmp;
signVal=8;
}
else
{
signVal=0;
}
code=(dSmp<<2)/tabStep[index];
index+=tabIndexAdjust[code&7];
if (index<0) index=0;
if (index>88) index=88;
prevSmp=thisSmp;
++smpCnt;
if (smpCnt & 1) *pOut= (code|signVal) & 0x0f;
else
{
tmp=*pOut;
tmp|= ((code|signVal)&7)<<4;
*pOut++=tmp;
}
}
*pOutLen=smpCnt;
return 0;
}
U8 ImaAdpcmDec(S8 *pIn, S16 *pOut, U32 inLen, U32 outLen)
{
U32 index,signVal,smpCnt;
S32 thisSmp,dSmp, code;
index=0;
thisSmp=0;
smpCnt=0;
inLen<<=1;
while (inLen--)
{
++smpCnt;
if (smpCnt &1 ) code=(*pOut) & 0x0f;
if ((code & 8) != 0) signVal=1;
else signVal=0;
code&=7; // 將 code 分離為數據和符號
dSmp=((tabStep[index]*code)>>2) + (tabStep[index] >>3); // 后面加的一項是為了減少誤差
if (signVal==1) dSmp=-dSmp;
thisSmp+=dSmp; // 計算出當前的波形數據
if (thisSmp>32767) thisSmp=32767;
else if (thisSmp<-32768) thisSmp=-32768;
*pOut++=(S16)(thisSmp);
index+=tabIndexAdjust[code];
index+=tabIndexAdjust[code];
if (index<0) index=0;
if (index>88) index=88;
}
return 0;
}
char buf[65536],bufCpr[32768];
int TestAdpcm(const char *pInFile, const char *pOutFile)
{
unsigned long iLen,cprLen;
FILE *fpi,*fpo;
fpi=fopen(pInFile,"rb");
if (!fpi) return -1;
fpo=fopen(pOutFile,"wb");
if (!fpo) return -2;
fseek(fpi,0,2);
iLen=ftell(fpi);
fseek(fpi,0,0);
while ( iLen>=65536)
{
fread(buf,1,65536,fpi);
ImaAdpcmEnc((S16 *)buf,bufCpr,65536,&cprLen);
ImaAdpcmDec(bufCpr,(S16 *) buf,cprLen,65536);
fwrite(buf,1,65536,fpo);
}
fclose(fpi);
fclose(fpo);
return 0;
}
int main()
{
TestAdpcm("f:\\test.pcm","f:\\dec.adpcm");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -