The NS module code of WebRTC can be called directly

For the NS module of WebRTC, no suitable code was found on github. As a result, I found the code of Great God, praise! ! ! ! ! ! ! ! ! ! It is the code under windows, you can use it directly! ! ! ! ! ! ! ! ! ! People who share code selflessly are the cutest hahaha

https://www.cnblogs.com/mod109/p/5767867.html

 

On this basis, I changed it a bit because the pcm file at hand is from Baidu. Baidu only supports a single-channel sampling rate of 160000. So it is necessary to modify a sampling rate of 16k to use. The code is as follows, the calling method is the same as above, the above example is 32k sampling:

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

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

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

	printf("sizeof(short) = %d\n", sizeof(short));

	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, shBufferIn, NULL, shBufferOut, 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);
		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);
}

https://blog.csdn.net/neustar1/article/details/19344503  This is a summary article introducing webrtc, which may be useful to me. After the follow-up project is completed, write a summary report. 

https://blog.csdn.net/boywgw/article/details/46790955

Guess you like

Origin blog.csdn.net/gbz3300255/article/details/109021628