C++ 双声道PCM音频分成单声道音频

记录一下自己写的双声道音频切分成单声道音频的代码

原始音频的格式为:采样率16KHZ 采样点精度16位 双声道

#include   
#include 

using namespace std;

#define PCMTEST

int main()
{
#ifdef PCMTEST
	FILE *fp = fopen(".\\srpcm\\test.pcm", "rb");
#else
	FILE *fp = fopen(".\\srpcm\\txttxt.txt", "rb");
#endif

	if (fp == NULL)
	{
		printf("Open File Failed!\n");
		return 0;
	}

	fseek(fp, 0L, SEEK_END);
	long fileLength = ftell(fp);
	fseek(fp,0L, SEEK_SET);
	printf("获取文件的长度\n", fileLength);
	printf("File Length %ld byte\n", fileLength);

	char* pBuff = new char[fileLength];
	memset(pBuff, 0, fileLength);
	int readLength = fread(pBuff, sizeof(char), fileLength, fp);
	printf("readLength = %d\n", readLength);
    
    //采样精度16bit = 2 Byte,所以这里用了short
	short *pBuffLeft = new short[fileLength / 4];
	short *pBuffRight = new short[fileLength / 4];
	memset(pBuffLeft, 0, fileLength / 2);
	memset(pBuffLeft, 0, fileLength / 2);

	for (int i = 0; i < (fileLength / 4); i++)
	{
		pBuffLeft[i] = ((short*)pBuff)[i * 2];
		pBuffRight[i] = ((short*)pBuff)[(i * 2) + 1];
		
	}

#ifdef PCMTEST
	FILE *fpLeft = fopen(".\\srpcm\\testLeft.pcm", "wb");
	FILE *fpRight = fopen(".\\srpcm\\testRight.pcm", "wb");
	FILE *fpAll = fopen(".\\srpcm\\testAll.pcm", "wb");
#else
	FILE *fpLeft = fopen(".\\srpcm\\txtLeft.txt", "wb");
	FILE *fpRight = fopen(".\\srpcm\\txtRight.txt", "wb");
	FILE *fpAll = fopen(".\\srpcm\\txtAll.txt", "wb");
#endif


	fwrite(pBuffLeft, sizeof(char), fileLength / 2, fpLeft);
	fwrite(pBuffRight, sizeof(char), fileLength / 2, fpRight);
	fwrite(pBuff, sizeof(char), fileLength, fpAll);
	fclose(fpLeft);
	fclose(fpRight);
	fclose(fpAll);
	return 0;
}

 

 

你可能感兴趣的:(C++)