欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

8Kpcm转16K、32K、44.1K等

程序员文章站 2022-07-14 20:44:46
...

下面函数ChangePcm8KTo16K简单的实现了8K转16K。
8K转44.1K是比较特殊的,因为44.1K不是8K的倍数,是介于40K和48K之间的。40K是8K的5倍,48K是6倍。因此需要进行nSkipByte进行区分。
所以先写5个原始数据,接着再写6个,即5-6–5--6…这样循环,就是下面代码的这几句:
8Kpcm转16K、32K、44.1K等
8Kpcm转16K、32K、44.1K等
还有一个注意点是单声道转双声道问题。
8Kpcm转16K、32K、44.1K等
这是单声道的代码

int ChangePcm8KTo44_1K(char *p8K, int i8KLen, char *p44_1K, int i44_1KLen)
{
	int i = 0;
	int iInx = 0;
	int nSkipByte = 5;

	if (p8K == NULL || p44_1K == NULL || i8KLen == 0 || i44_1KLen < (i8KLen * 10))
	{
		return -1;
	}

	for (i = 1, iInx = 0; i < i8KLen;)
	{
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		if (nSkipByte == 5)
		{
			nSkipByte = 6;
			p44_1K[iInx++] = p8K[i - 1];
			p44_1K[iInx++] = p8K[i];
		}
		else
		{
			nSkipByte = 5;
		}
		i += 2;
	}
	return iInx;
}

这是双声道的代码

int ChangePcm8KTo44_1K(char *p8K, int i8KLen, char *p44_1K, int i44_1KLen)
{
	int i = 0;
	int iInx = 0;
	int nSkipByte = 5;

	if (p8K == NULL || p44_1K == NULL || i8KLen == 0 || i44_1KLen < (i8KLen * 10))
	{
		return -1;
	}

	for (i = 1, iInx = 0; i < i8KLen;)
	{
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		//双声道需要多复制一份,单声道不需要
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		//双声道结束
		if (nSkipByte == 5)
		{
			nSkipByte = 6;
			p44_1K[iInx++] = p8K[i - 1];
			p44_1K[iInx++] = p8K[i];
			//双声道需要多复制一份,单声道不需要
			p44_1K[iInx++] = p8K[i - 1];
			p44_1K[iInx++] = p8K[i];
			//双声道结束
		}
		else
		{
			nSkipByte = 5;
		}	
		i += 2;
	}
	return iInx;
}

总体代码如下:

/*******************************************************************
** 函数名:     		ChangePcm8KTo32K
** 函数描述:   	采样率转换函数
** 参数:       		16k,16k_len,32k,32k_len
** 返回:       		32k_len
********************************************************************/
int ChangePcm8KTo16K(char *p8K, int i8KLen, char *p16K, int i16KLen)
{
	int i = 0;
	int iInx = 0;

	if (p8K == NULL || p16K == NULL || i8KLen == 0 || i16KLen < (i8KLen * 2))
	{
		return -1;
	}

	for (i = 1, iInx = 0; i < i8KLen;)
	{
		p16K[iInx++] = p8K[i - 1];
		p16K[iInx++] = p8K[i];
		p16K[iInx++] = p8K[i - 1];
		p16K[iInx++] = p8K[i];

		i += 2;
	}

	return iInx;
}

/*******************************************************************
** 函数名:     		ChangePcm8KTo44_1K
** 函数描述:   	采样率转换函数8K转44.1K
** 参数:       		16k,16k_len,p44_1K,i44_1KLen
** 返回:       		44_1k_len
********************************************************************/
int ChangePcm8KTo44_1K(char *p8K, int i8KLen, char *p44_1K, int i44_1KLen)
{
	int i = 0;
	int iInx = 0;
	int nSkipByte = 5;

	if (p8K == NULL || p44_1K == NULL || i8KLen == 0 || i44_1KLen < (i8KLen * 10))
	{
		return -1;
	}

	for (i = 1, iInx = 0; i < i8KLen;)
	{
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		//双声道需要多复制一份,单声道不需要
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		p44_1K[iInx++] = p8K[i - 1];
		p44_1K[iInx++] = p8K[i];
		//双声道结束
		if (nSkipByte == 5)
		{
			nSkipByte = 6;
			p44_1K[iInx++] = p8K[i - 1];
			p44_1K[iInx++] = p8K[i];
			//双声道需要多复制一份,单声道不需要
			p44_1K[iInx++] = p8K[i - 1];
			p44_1K[iInx++] = p8K[i];
			//双声道结束
		}
		else
		{
			nSkipByte = 5;
		}
		

		i += 2;
	}

	return iInx;
}

相关标签: 音频编码解码