使用webRtc进行音频降噪(NS)和VAD检测

webRtc整个项目在windows下编译还是很难搞定的一件事,本人是下载别人已经编译好的工程进行开发的,整个工程有200多个项目,音频降噪和VAD检测只是其中的2个项目。


一、音频降噪

void TestNs(char *szFileIn, int nSample, int nMode)
{
	int nRet = 0;
	NsHandle *pNS_inst = NULL;

	FILE *fpIn = NULL;
	FILE *fpOut = NULL;

	char *pInBuffer = NULL;
	char *pOutBuffer = NULL;

	do
	{
		int i = 0;
		int nFileSize = 0;
		int nTime = 0;
		if (0 != WebRtcNs_Create(&pNS_inst))
		{
			printf("Noise_Suppression WebRtcNs_Create err! \n");
			break;
		}

		if (0 != WebRtcNs_Init(pNS_inst, nSample))
		{
			printf("Noise_Suppression WebRtcNs_Init err! \n");
			break;
		}

		if (0 != WebRtcNs_set_policy(pNS_inst, nMode))
		{
			printf("Noise_Suppression WebRtcNs_set_policy err! \n");
			break;
		}

		fpIn = fopen(szFileIn, "rb");
		if (NULL == fpIn)
		{
			printf("open src file err \n");
			break;
		}
		fseek(fpIn, 0, SEEK_END);
		nFileSize = ftell(fpIn);
		fseek(fpIn, 0, SEEK_SET);

		pInBuffer = (char*)malloc(nFileSize);
		memset(pInBuffer, 0, nFileSize);
		fread(pInBuffer, sizeof(char), nFileSize, fpIn);

		pOutBuffer = (char*)malloc(nFileSize);
		memset(pOutBuffer, 0, nFileSize);

		nTime = GetTickCount();
		for (i = 0; i < nFileSize; i += 320)
		{
			if (nFileSize - i >= 320)
			{
				short shBufferIn[160] = { 0 };
				short shBufferOut[160] = { 0 };
				float fBufferIn[160] = { 0 };
				float fBufferOut[160] = { 0 };

				memcpy(shBufferIn, (char*)(pInBuffer + i), 160 * sizeof(short));
				for (int k = 0; k < 160; ++k)
				{
					fBufferIn[k] = (float)shBufferIn[k];
				}

				if (0 == WebRtcNs_Process(pNS_inst, fBufferIn, NULL, fBufferOut, NULL))
				{
					for (int k = 0; k < 160; ++k)
					{
						shBufferOut[k] = (short)fBufferOut[k];
					}
					memcpy(pOutBuffer + i, shBufferOut, 160 * sizeof(short));
				}
			}
		}

		nTime = GetTickCount() - nTime;
		printf("n_s user time=%dms\n", nTime);
		char szFileOut[20] = "NS_FILE";
		char* pLine = strchr(szFileIn, '\\');
		strcat(szFileOut, pLine);
		fpOut = fopen(szFileOut, "wb");
		if (NULL == fpOut)
		{
			printf("open out file err! \n");
			break;
		}
		fwrite(pOutBuffer, sizeof(char), nFileSize, fpOut);
	} while (0);

	WebRtcNs_Free(pNS_inst);
	fclose(fpIn);
	fclose(fpOut);
	free(pInBuffer);
	free(pOutBuffer);
}

需要注意的是,如果传入的音频采样率大于32000,要将音频分为高低音频传入。


二、VAD检测

void TestVAD(char* pAudioFile,char* pResFile,int nSample,int nMode)
{
	VadInst* pVad = NULL;
	if (WebRtcVad_Create(&pVad))
	{
		perror("WebRtcVad_Create failed!");
		return;
	}

	if (WebRtcVad_Init(pVad))
	{
		perror("WebRtcVad_Init failed!");
		return;
	}

	if (WebRtcVad_set_mode(pVad, nMode))
	{
		perror("WebRtcVad_set_mode failed!");
		return;
	}

	FILE* fp = NULL;
	FILE* fpR = NULL;
	fp = fopen(pAudioFile, "rb");
	fpR = fopen(pResFile, "wb");
	fseek(fp, 0, SEEK_END);
	unsigned int nLen = ftell(fp);
	fseek(fp, 0, SEEK_SET);
	short shBufferIn[160] = { 0 };
	while (1)
	{
		if (160 != fread(shBufferIn, 2, 160, fp))
			break;
		int nRet = WebRtcVad_Process(pVad, 16000, shBufferIn, 160);
		printf("%d", nRet);
		if (1 == nRet)
		{
			fwrite(shBufferIn, 2, 160, fpR);
		}
	}

	fclose(fpR);
	fclose(fp);
	WebRtcVad_Free(pVad);
}

VAD检测有4种模式,0,1,2,3可以自己体验一下。

你可能感兴趣的:(webrtc)