pcm数据采样率8k转为16k

#include "stdafx.h"
#include  
#include 
using namespace std;

void Resample16K(short* pInAudioData, int nInAudioLen, short* pOutAudioData, int& nOutAudioLen)
{
	short* sSampleIn = pInAudioData;
	int nFrequency = 0;
	while (sSampleIn - pInAudioData < nInAudioLen)
	{
		memcpy((char*)(pOutAudioData + nFrequency), (char*)sSampleIn, 2);
		nFrequency++;
		memcpy((char*)(pOutAudioData + nFrequency), (char*)sSampleIn, 2);
		nFrequency++;
		sSampleIn++;
	}
	
	nOutAudioLen = nFrequency*2;
}

int main()
{
	char* filename = "sh.pcm";
	FILE* fp = fopen(filename, "ab+");
	FILE *fp_down = fopen("out.pcm", "wb+");

	fseek(fp, 0, SEEK_END);
	int buff_len = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	unsigned char* pData = (unsigned char*)malloc(buff_len);
	memset(pData, 0, buff_len);
	fread(pData, sizeof(unsigned char), buff_len, fp);

	char* pOutData = new char [buff_len*2];
	memset(pOutData, 0, buff_len * 2);
	int nLOutLen = 0;
	Resample16K((short*)pData ,buff_len/2, (short*)pOutData, nLOutLen);


	fwrite(pOutData, 1, nLOutLen, fp_down);

	fclose(fp);
	fclose(fp_down);

	
    return 0;
}

ps:1.采样率8K转为16k,关键在于8k采样率每一份short类型数据,在l6k采样率中对应为双份。

2.还有一点,关于memcpy()函数,传入数据类型应该为char类型,而不是short类型,这一点应该注意。

你可能感兴趣的:(window编程)