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

📄 imaadpcm.c

📁 编解码ADPCM的VC源码
💻 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 + -