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

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

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;
}

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值