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

📄 adpcmcompiler.cpp

📁 ADPCM,源代码.
💻 CPP
字号:
// ADPCMCompiler.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"



/* Intel ADPCM step variation table */
static long indexTable[16] = {
	-1, -1, -1, -1, 2, 4, 6, 8,
		-1, -1, -1, -1, 2, 4, 6, 8,
};

static long stepsizeTable[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
};
int*	g_pPCMBuf;
int     g_PCMSize;


int Load(char* filename)
{

	FILE* fp = fopen(filename,"rb");
	if(fp == NULL)
	{
		printf("open %s error..............\n",filename);
		system("pause");
		return 0;
	}
	fseek(fp,0,SEEK_END);

	g_PCMSize = ftell(fp);


	g_pPCMBuf = new int[g_PCMSize];

	fseek(fp,0,SEEK_SET);


	for(int i=0;i<g_PCMSize;++i)
	{
		char temp = 0;
		fread(&temp,1,2,fp);
		g_pPCMBuf[i] = temp;
	}

	fclose(fp);
	return 1;
}


int CoderAndOut(FILE* fp)
{
	long val;                   /* Current input sample value */
	long sign;                  /* Current adpcm sign bit */
	long delta;                 /* Current adpcm output value */
	long step;                  /* Stepsize */
	long valprev;               /* virtual previous output value */
	long vpdiff;                /* Current change to valprev */
	long index;                 /* Current step change index */
	long outputbuffer;          /* place to keep previous 4-bit value */
	long bufferstep;            /* toggle between outputbuffer/output */

	int* pbuf = g_pPCMBuf;
	int* pEnd = g_pPCMBuf + g_PCMSize;

	valprev = 0;
	index = 0;
	step = stepsizeTable[index];

	bufferstep = 1;

	while (pbuf < pEnd){
		val = *pbuf++;

		/* Step 1 - compute difference with previous value */
		delta = val - valprev;
		sign = (delta < 0) ? 8 : 0;
		if ( sign ) delta = (-delta);

		/* Step 2 - Divide and clamp */
		delta = (delta<<2) / step;
		if ( delta > 7 ) delta = 7;

		vpdiff = (delta*step) >> 2;

		/* Step 3 - Update previous value */
		if ( sign )
			valprev -= vpdiff;
		else
			valprev += vpdiff;

		/* Step 4 - Clamp previous value to 16 bits */
		if ( valprev > 32767 )
			valprev = 32767;
		else if ( valprev < -32768 )
			valprev = -32768;

		/* Step 5 - Assemble value, update index and step values */
		delta |= sign;

		index += indexTable[delta];
		if ( index < 0 ) index = 0;
		if ( index > 88 ) index = 88;
		step = stepsizeTable[index];

		/* Step 6 - Output value */
		if ( bufferstep ) {
			outputbuffer = (delta << 4) & 0xf0;
		} else {
			char out = (char)((delta & 0x0f) | outputbuffer);
			fwrite(&out,1,1,fp);
		}
		bufferstep = !bufferstep;
	}
	return 0;
}


int Save(char* filename)
{
	FILE* fp = fopen(filename,"wb");
	if(fp == NULL)
	{
		printf("write %s error..............\n",filename);
		system("pause");
		return 0;
	}
	CoderAndOut(fp);
	fclose(fp);
	return 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int len;
	g_pPCMBuf = NULL;
	g_PCMSize = 0;
	if(argc<=1){
		printf("please drag pcm file on exe\n");
		system("pause");
		return 0;
	}
	printf("start coder................\n");
	for(int i = 1;i<argc;i++)
	{
		char buf[256];
		sprintf(buf,argv[i]);
		printf("coder %s.......................\n",buf);
		Load(buf);

		len = strlen(buf);


		buf[len-1] = 'r';
		buf[len-2] = 'a';
		buf[len-3] = 'w';
		Save(buf);
	
		printf("coder %s complete..............\n",buf);
		if(g_pPCMBuf!=NULL){
			delete[] g_pPCMBuf;
			g_PCMSize = 0;
		}
	}
	printf("all coder complete..............\n");
	system("pause");
	return 0;
}

⌨️ 快捷键说明

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