使用CMAKE编译webRTC中的AGC、NS、AEC代码

首先,了解一下CMAKE的基本语法及概念

学习cmake,参考cmake practice文档。


首先.1 基本概念及命令

PROJECT(projectname [CXX] [C] [Java])
你可以用这个指令定义工程名称,并可指定工程支持的语言,支持的语言列表是可以忽略的,默认情况表示支持所有语言。 
这个指令隐式的定义了两个 cmake 变量,变量名称是: 
<projectname>_BINARY_DIR 
<projectname>_SOURCE_DIR 
引用格式如下:
${HELLO_BINARY_DIR} 
SET(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]])
调用格式
SET(SRC_LIST main.c)
如果有多个源文件,也可以定义成
SET(SRC_LIST main.c t1.c t2.c)
MESSAGE([SEND_ERROR | STATUS | FATAL_ERROR] "message to display"...)
指令用于向终端输出用户定义的信息,包含了三种类型: 
SEND_ERROR,产生错误,生成过程被跳过 
SATUS,输出前缀为—的信息
FATAL_ERROR,立即终止所有 cmake 过程
ADD_EXECUTABLE(hello ${SRC_LIST})
定义了这个工程会生成一个文件名为 hello 的可执行文件,相关的源文件是SRC_LIST 中定义的源文件列表
ADD_SUBDIRECTORY(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
向当前工程添加存放源文件的子目录,并可以制定输出binary的路径,第三个参数用于排除不想包含在内的文件,可以不用
如果不进行 bin 目录的指定,那么编译结果(包括中间结果)都将存放在src 目录(这个目录跟原有的 src 目录对应)
基本语法规则
1,变量使用${}方式取值,但是在 IF 控制语句中是直接使用变量名
2,指令(参数 1 参数 2...),参数使用括弧括起,参数之间使用空格或分号分开。 
指令是大小写无关的,参数和变量是大小写相关的。 
可以忽略掉 source 列表中的源文件后缀 ,cmake 会自动的在本目录查找 ,对应文件名的文件
运行: make clean 即可对构建结果进行清理


首先.2 外部编译方法

新建一个build目录,进入 build 目录,运行 cmake .. (..代表父目录,因为父目录存在我们需要的CMakeLists.txt,如果你在其他地方建立了 build 目录,需要运行 cmake <工程的全路径>),查看一下 build 目录,就会发现了生成了编译需要的 Makefile 以及其他的中间文件 ,运行 make 构建工程,就会在当前目录(build 目录)中获得目标文件

外部编译时候,HELLO_BINARY_DIR 应指向编译路径,即/backup/cmake/t1/build


首先.3 静态库与动态库

对于执行代码需要用到的库文件函数,有两种调用方法:

1】将需要调用的方法,全部调用集成到执行程序中,有点include的意思,这种方法称之为静态库调用

2】将库中的函数列一个目录表给执行程序,用到哪个调用哪个,这种方法称之为动态调用库



其次,了解一下代码的基本结构


通过分析文件,我们基本上可以将文件分为三类:

1】.pcm 音频文件,这个是用来测试用的数据素材,编译时候暂时不用管他

2】测试的主体程序,放在了module文件夹的外面

3】主体需要用的函数都写在了moudule文件夹中,里面存放了功能函数的源程序以及头文件

至此,半截木头就准备好了,下一步我们就要做的就是,把这半截木头扔到河里,抱着这半截木头过河



然后,写两个CMAKE文件用


然后.1 我们这样分解后面的任务:

1】将module中的程序编译成库文件,并安装到linux系统中

2】链接库文件,编译主体程序


然后.2 实现上面分解的任务

然后.2.1    安装库文件

1】在webRtcModule文件夹下,创建CMakeLists.txt文件,在文件中添加以下代码

cmake_minimum_required(VERSION 2.8)
PROJECT(webRTCLIB)

AUX_SOURCE_DIRECTORY(. DIR_SRCS)
SET(DIR_HEADERS analog_agc.h complex_fft_tables.h cpu_features_wrapper.h defines.h digital_agc.h 
		fft4g.h gain_control.h noise_suppression.h noise_suppression_x.h ns_core.h 			nsx_core.h nsx_defines.h real_fft.h resample_by_2_internal.h ring_buffer.h 			signal_processing_library.h spl_inl.h typedefs.h windows_private.h)
# SET(LIBWebRTC_SRC ${DIR_SRCS})
ADD_LIBRARY(webRTC SHARED ${DIR_SRCS})
ADD_LIBRARY(webRTC_static STATIC ${DIR_SRCS})
ADD_DEFINITIONS(-DWEBRTC_POSIX)
SET_TARGET_PROPERTIES(webRTC_static PROPERTIES OUTPUT_NAME "webRTC")
GET_TARGET_PROPERTY(OUTPUT_VALUE webRTC_static OUTPUT_NAME)
MESSAGE(STATUS "this is the webRTC_static OUTPUT_NAME:"${OUTPUT_VALUE})

SET_TARGET_PROPERTIES(webRTC PROPERTIES CLEAN_DIRECT_OUTPUT 1)
SET_TARGET_PROPERTIES(webRTC_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)

#INCLUDE_DIRECTORIES(.)

INSTALL(TARGETS webRTC webRTC_static 
	LIBRARY DESTINATION lib 
	ARCHIVE DESTINATION lib
	RUNTIME DESTINATION lib
	COMPONENT library)

INSTALL(FILES ${DIR_HEADERS} DESTINATION include/webRtcModule)

#AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR}/WebRtcModule module_srs)

2】在webRtcModule文件夹下创建build文件夹,并在终端中执行cmake .. 命令

lijianglong@lijianglong-Lenovo-M495:~/C++ 编程学习/cmake/webRTC/nsagc/WebRtcMouduild$ cmake ..
CMake Warning (dev) at CMakeLists.txt:13:
  Syntax Warning in cmake code at column 56

  Argument not separated from preceding token by whitespace.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- this is the webRTC_static OUTPUT_NAME:webRTC
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lijianglong/C++ 编程学习/cmake/webRTC/nsagc/WebRtcMoudle/build


3】执行make命令

lijianglong@lijianglong-Lenovo-M495:~/C++ 编程学习/cmake/webRTC/nsagc/WebRtcMouduild$ make
Scanning dependencies of target webRTC_static
[  1%] Building C object CMakeFiles/webRTC_static.dir/analog_agc.c.o
[  3%] Building C object CMakeFiles/webRTC_static.dir/complex_bit_reverse.c.o
[  4%] Building C object CMakeFiles/webRTC_static.dir/complex_fft.c.o
[  6%] Building C object CMakeFiles/webRTC_static.dir/copy_set_operations.c.o
[  7%] Building C object CMakeFiles/webRTC_static.dir/cross_correlation.c.o
[  9%] Building C object CMakeFiles/webRTC_static.dir/digital_agc.c.o
[ 10%] Building C object CMakeFiles/webRTC_static.dir/division_operations.c.o
[ 12%] Building C object CMakeFiles/webRTC_static.dir/dot_product_with_scale.c.o
[ 13%] Building C object CMakeFiles/webRTC_static.dir/downsample_fast.c.o
[ 15%] Building C object CMakeFiles/webRTC_static.dir/energy.c.o
[ 16%] Building C object CMakeFiles/webRTC_static.dir/fft4g.c.o
[ 18%] Building C object CMakeFiles/webRTC_static.dir/get_scaling_square.c.o
[ 19%] Building C object CMakeFiles/webRTC_static.dir/min_max_operations.c.o
[ 21%] Building C object CMakeFiles/webRTC_static.dir/noise_suppression.c.o
[ 22%] Building C object CMakeFiles/webRTC_static.dir/noise_suppression_x.c.o
[ 24%] Building C object CMakeFiles/webRTC_static.dir/ns_core.c.o
[ 25%] Building C object CMakeFiles/webRTC_static.dir/nsx_core.c.o
[ 27%] Building C object CMakeFiles/webRTC_static.dir/nsx_core_c.c.o
[ 28%] Building C object CMakeFiles/webRTC_static.dir/nsx_core_neon_offsets.c.o
[ 30%] Building C object CMakeFiles/webRTC_static.dir/real_fft.c.o
[ 31%] Building C object CMakeFiles/webRTC_static.dir/resample.c.o
[ 33%] Building C object CMakeFiles/webRTC_static.dir/resample_48khz.c.o
[ 34%] Building C object CMakeFiles/webRTC_static.dir/resample_by_2.c.o
[ 36%] Building C object CMakeFiles/webRTC_static.dir/resample_by_2_internal.c.o
[ 37%] Building C object CMakeFiles/webRTC_static.dir/resample_by_2_mips.c.o
[ 39%] Building C object CMakeFiles/webRTC_static.dir/resample_fractional.c.o
[ 40%] Building C object CMakeFiles/webRTC_static.dir/ring_buffer.c.o
[ 42%] Building C object CMakeFiles/webRTC_static.dir/spl_init.c.o
[ 43%] Building C object CMakeFiles/webRTC_static.dir/spl_sqrt.c.o
[ 45%] Building C object CMakeFiles/webRTC_static.dir/spl_sqrt_floor.c.o
[ 46%] Building C object CMakeFiles/webRTC_static.dir/splitting_filter.c.o
[ 48%] Building C object CMakeFiles/webRTC_static.dir/vector_scaling_operations.c.o
[ 50%] Linking C static library libwebRTC.a
[ 50%] Built target webRTC_static
Scanning dependencies of target webRTC
[ 51%] Building C object CMakeFiles/webRTC.dir/analog_agc.c.o
[ 53%] Building C object CMakeFiles/webRTC.dir/complex_bit_reverse.c.o
[ 54%] Building C object CMakeFiles/webRTC.dir/complex_fft.c.o
[ 56%] Building C object CMakeFiles/webRTC.dir/copy_set_operations.c.o
[ 57%] Building C object CMakeFiles/webRTC.dir/cross_correlation.c.o
[ 59%] Building C object CMakeFiles/webRTC.dir/digital_agc.c.o
[ 60%] Building C object CMakeFiles/webRTC.dir/division_operations.c.o
[ 62%] Building C object CMakeFiles/webRTC.dir/dot_product_with_scale.c.o
[ 63%] Building C object CMakeFiles/webRTC.dir/downsample_fast.c.o
[ 65%] Building C object CMakeFiles/webRTC.dir/energy.c.o
[ 66%] Building C object CMakeFiles/webRTC.dir/fft4g.c.o
[ 68%] Building C object CMakeFiles/webRTC.dir/get_scaling_square.c.o
[ 69%] Building C object CMakeFiles/webRTC.dir/min_max_operations.c.o
[ 71%] Building C object CMakeFiles/webRTC.dir/noise_suppression.c.o
[ 72%] Building C object CMakeFiles/webRTC.dir/noise_suppression_x.c.o
[ 74%] Building C object CMakeFiles/webRTC.dir/ns_core.c.o
[ 75%] Building C object CMakeFiles/webRTC.dir/nsx_core.c.o
[ 77%] Building C object CMakeFiles/webRTC.dir/nsx_core_c.c.o
[ 78%] Building C object CMakeFiles/webRTC.dir/nsx_core_neon_offsets.c.o
[ 80%] Building C object CMakeFiles/webRTC.dir/real_fft.c.o
[ 81%] Building C object CMakeFiles/webRTC.dir/resample.c.o
[ 83%] Building C object CMakeFiles/webRTC.dir/resample_48khz.c.o
[ 84%] Building C object CMakeFiles/webRTC.dir/resample_by_2.c.o
[ 86%] Building C object CMakeFiles/webRTC.dir/resample_by_2_internal.c.o
[ 87%] Building C object CMakeFiles/webRTC.dir/resample_by_2_mips.c.o
[ 89%] Building C object CMakeFiles/webRTC.dir/resample_fractional.c.o
[ 90%] Building C object CMakeFiles/webRTC.dir/ring_buffer.c.o
[ 92%] Building C object CMakeFiles/webRTC.dir/spl_init.c.o
[ 93%] Building C object CMakeFiles/webRTC.dir/spl_sqrt.c.o
[ 95%] Building C object CMakeFiles/webRTC.dir/spl_sqrt_floor.c.o
[ 96%] Building C object CMakeFiles/webRTC.dir/splitting_filter.c.o
[ 98%] Building C object CMakeFiles/webRTC.dir/vector_scaling_operations.c.o
[100%] Linking C shared library libwebRTC.so
[100%] Built target webRTC

4】安装库文件,注意权限

lijianglong@lijianglong-Lenovo-M495:~/C++ 编程学习/cmake/webRTC/nsagc/WebRtcMouduild$ sudo make install
[sudo] lijianglong 的密码: 
[ 50%] Built target webRTC_static
[100%] Built target webRTC
Install the project...
-- Install configuration: ""
-- Installing: /usr/lib/libwebRTC.so
-- Installing: /usr/lib/libwebRTC.a
-- Up-to-date: /usr/include/webRtcModule/analog_agc.h
-- Up-to-date: /usr/include/webRtcModule/complex_fft_tables.h
-- Up-to-date: /usr/include/webRtcModule/cpu_features_wrapper.h
-- Up-to-date: /usr/include/webRtcModule/defines.h
-- Up-to-date: /usr/include/webRtcModule/digital_agc.h
-- Up-to-date: /usr/include/webRtcModule/fft4g.h
-- Up-to-date: /usr/include/webRtcModule/gain_control.h
-- Up-to-date: /usr/include/webRtcModule/noise_suppression.h
-- Up-to-date: /usr/include/webRtcModule/noise_suppression_x.h
-- Up-to-date: /usr/include/webRtcModule/ns_core.h
-- Up-to-date: /usr/include/webRtcModule/nsx_core.h
-- Up-to-date: /usr/include/webRtcModule/nsx_defines.h
-- Up-to-date: /usr/include/webRtcModule/real_fft.h
-- Up-to-date: /usr/include/webRtcModule/resample_by_2_internal.h
-- Up-to-date: /usr/include/webRtcModule/ring_buffer.h
-- Up-to-date: /usr/include/webRtcModule/signal_processing_library.h
-- Up-to-date: /usr/include/webRtcModule/spl_inl.h
-- Up-to-date: /usr/include/webRtcModule/typedefs.h
-- Up-to-date: /usr/include/webRtcModule/windows_private.h

至此,库文件安装完毕,效果如下


然后.2.2 编译主体程序

因为主程序本来是针对win系统下写的,所以移植到linux中时候,需要做些许小小的改动,改动后代码如下:

// WebRtcAudioTest.cpp : ¶¨Òå¿ØÖÆ̨ӦÓóÌÐòµÄÈë¿Úµã¡£
//

// #include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include <Windows.h>

#include "./WebRtcMoudle/signal_processing_library.h"
#include "./WebRtcMoudle/noise_suppression_x.h"
#include "./WebRtcMoudle/noise_suppression.h"
#include "./WebRtcMoudle/gain_control.h"

void NoiseSuppression32(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;

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

		int  filter_state1[6],filter_state12[6];
		int  Synthesis_state1[6],Synthesis_state12[6];

		memset(filter_state1,0,sizeof(filter_state1));
		memset(filter_state12,0,sizeof(filter_state12));
		memset(Synthesis_state1,0,sizeof(Synthesis_state1));
		memset(Synthesis_state12,0,sizeof(Synthesis_state12));

		// nTime = GetTickCount();
		nTime = 0;
		for (i = 0;i < nFileSize;i+=640)
		{
			if (nFileSize - i >= 640)
			{
				short shBufferIn[320] = {0};

				short shInL[160],shInH[160];
				short shOutL[160] = {0},shOutH[160] = {0};

				memcpy(shBufferIn,(char*)(pInBuffer+i),320*sizeof(short));
				//Ê×ÏÈÐèҪʹÓÃÂ˲¨º¯Êý½«ÒôƵÊý¾Ý·Ö¸ßµÍƵ£¬ÒÔ¸ßƵºÍµÍƵµÄ·½Ê½´«Èë½µÔ뺯ÊýÄÚ²¿
				WebRtcSpl_AnalysisQMF(shBufferIn,320,shInL,shInH,filter_state1,filter_state12);

				//½«ÐèÒª½µÔëµÄÊý¾ÝÒÔ¸ßƵºÍµÍƵ´«Èë¶ÔÓ¦½Ó¿Ú£¬Í¬Ê±ÐèҪעÒâ·µ»ØÊý¾ÝÒ²ÊÇ·Ö¸ßƵºÍµÍƵ
				if (0 == WebRtcNs_Process(pNS_inst ,shInL  ,shInH ,shOutL , shOutH))
				{
					short shBufferOut[320];
					//Èç¹û½µÔë³É¹¦£¬Ôò¸ù¾Ý½µÔëºó¸ßƵºÍµÍƵÊý¾Ý´«ÈëÂ˲¨½Ó¿Ú£¬È»ºóÓý«·µ»ØµÄÊý¾ÝдÈëÎļþ
					WebRtcSpl_SynthesisQMF(shOutL,shOutH,160,shBufferOut,Synthesis_state1,Synthesis_state12);
					memcpy(pOutBuffer+i,shBufferOut,320*sizeof(short));
				}
			}	
		}

		// nTime = GetTickCount() - nTime;
		nTime = 0;
		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);
}

void NoiseSuppressionX32(char *szFileIn,char *szFileOut,int nSample,int nMode)
{
	int nRet = 0;
	NsxHandle *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 != WebRtcNsx_Create(&pNS_inst))
		{
			printf("Noise_Suppression WebRtcNs_Create err! \n");
			break;
		}

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

		if (0 !=  WebRtcNsx_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);

		int  filter_state1[6],filter_state12[6];
		int  Synthesis_state1[6],Synthesis_state12[6];

		memset(filter_state1,0,sizeof(filter_state1));
		memset(filter_state12,0,sizeof(filter_state12));
		memset(Synthesis_state1,0,sizeof(Synthesis_state1));
		memset(Synthesis_state12,0,sizeof(Synthesis_state12));

		// nTime = GetTickCount();
		nTime = 0;
		for (i = 0;i < nFileSize;i+=640)
		{
			if (nFileSize - i >= 640)
			{
				short shBufferIn[320] = {0};

				short shInL[160],shInH[160];
				short shOutL[160] = {0},shOutH[160] = {0};

				memcpy(shBufferIn,(char*)(pInBuffer+i),320*sizeof(short));
				//Ê×ÏÈÐèҪʹÓÃÂ˲¨º¯Êý½«ÒôƵÊý¾Ý·Ö¸ßµÍƵ£¬ÒÔ¸ßƵºÍµÍƵµÄ·½Ê½´«Èë½µÔ뺯ÊýÄÚ²¿
				WebRtcSpl_AnalysisQMF(shBufferIn,320,shInL,shInH,filter_state1,filter_state12);

				//½«ÐèÒª½µÔëµÄÊý¾ÝÒÔ¸ßƵºÍµÍƵ´«Èë¶ÔÓ¦½Ó¿Ú£¬Í¬Ê±ÐèҪעÒâ·µ»ØÊý¾ÝÒ²ÊÇ·Ö¸ßƵºÍµÍƵ
				if (0 == WebRtcNsx_Process(pNS_inst ,shInL  ,shInH ,shOutL , shOutH))
				{
					short shBufferOut[320];
					//Èç¹û½µÔë³É¹¦£¬Ôò¸ù¾Ý½µÔëºó¸ßƵºÍµÍƵÊý¾Ý´«ÈëÂ˲¨½Ó¿Ú£¬È»ºóÓý«·µ»ØµÄÊý¾ÝдÈëÎļþ
					WebRtcSpl_SynthesisQMF(shOutL,shOutH,160,shBufferOut,Synthesis_state1,Synthesis_state12);
					memcpy(pOutBuffer+i,shBufferOut,320*sizeof(short));
				}
			}	
		}

		// nTime = GetTickCount() - nTime;
		nTime = 0;
		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);

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

void WebRtcAgcTest(char *filename, char *outfilename,int fs)
{
	FILE *infp      = NULL;
	FILE *outfp     = NULL;

	short *pData    = NULL;
	short *pOutData = NULL;
	void *agcHandle = NULL;	

	do 
	{
		WebRtcAgc_Create(&agcHandle);

		int minLevel = 0;
		int maxLevel = 255;
		int agcMode  = kAgcModeFixedDigital;
		WebRtcAgc_Init(agcHandle, minLevel, maxLevel, agcMode, fs);

		WebRtcAgc_config_t agcConfig;
		agcConfig.compressionGaindB = 20;
		agcConfig.limiterEnable     = 1;
		agcConfig.targetLevelDbfs   = 3;
		WebRtcAgc_set_config(agcHandle, agcConfig);

		infp = fopen(filename,"rb");
		int frameSize = 80;
		pData    = (short*)malloc(frameSize*sizeof(short));
		pOutData = (short*)malloc(frameSize*sizeof(short));

		outfp = fopen(outfilename,"wb");
		int len = frameSize*sizeof(short);
		int micLevelIn = 0;
		int micLevelOut = 0;
		while(1)
		{
			memset(pData, 0, len);
			len = fread(pData, 1, len, infp);
			if (len > 0)
			{
				int inMicLevel  = micLevelOut;
				int outMicLevel = 0;
				uint8_t saturationWarning;
				int nAgcRet = WebRtcAgc_Process(agcHandle, pData, NULL, frameSize, pOutData,NULL, inMicLevel, &outMicLevel, 0, &saturationWarning);
				if (nAgcRet != 0)
				{
					printf("failed in WebRtcAgc_Process\n");
					break;
				}
				micLevelIn = outMicLevel;
				fwrite(pOutData, 1, len, outfp);
			}
			else
			{
				break;
			}
		}
	} while (0);

	fclose(infp);
	fclose(outfp);
	free(pData);
	free(pOutData);
	WebRtcAgc_Free(agcHandle);
}

int main(int argc, char* argv[])
{
	WebRtcAgcTest("byby_8K_1C_16bit.pcm","byby_8K_1C_16bit_agc.pcm",8000);

	NoiseSuppression32("lhydd_1C_16bit_32K.pcm","lhydd_1C_16bit_32K_ns.pcm",32000,1);

	NoiseSuppressionX32("lhydd_1C_16bit_32K.pcm","lhydd_1C_16bit_32K_nsx.pcm",32000,1);

	printf("ÉùÒôÔöÒ棬½µÔë½áÊø...\n");
	return 0;
}

待上面的代码改完了之后,开始链接库文件,编译主体程序

lijianglong@lijianglong-Lenovo-M495:~/C++ 编程学习/cmake/webRTC/nsagc$ g++ -DWEBRTC_POSIX WebRtcAudioTest.cpp stdafx.cpp  -lwebRTC -lpthread -o test
WebRtcAudioTest.cpp: In function ‘int main(int, char**)’:
WebRtcAudioTest.cpp:290:70: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
  WebRtcAgcTest("byby_8K_1C_16bit.pcm","byby_8K_1C_16bit_agc.pcm",8000);
                                                                      ^
WebRtcAudioTest.cpp:290:70: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
WebRtcAudioTest.cpp:292:81: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
 seSuppression32("lhydd_1C_16bit_32K.pcm","lhydd_1C_16bit_32K_ns.pcm",32000,1);
                                                                             ^
WebRtcAudioTest.cpp:292:81: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
WebRtcAudioTest.cpp:294:83: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
 SuppressionX32("lhydd_1C_16bit_32K.pcm","lhydd_1C_16bit_32K_nsx.pcm",32000,1);
                                                                             ^
WebRtcAudioTest.cpp:294:83: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]

可以暂时无视这些警告,一切正常的话,现在你应该能看到在nsagc文件夹下面生成了一个test可执行文件。

PS:编译主体程序也可以使用cmake方法,在项目根目录下建立build文件夹,并在文件夹之内编写CMakeLists.txt文件,将以下内容添加到文件中,通过上面提到的外部编译的方法依次执行cmake .. ->make生成可执行程序webRTCtest,效果是一样的。

cmake_minimum_required(VERSION 2.8)
PROJECT(webRTC)

SET(CMAKE_CXX_FLAGS_DEBUG "")

AUX_SOURCE_DIRECTORY(${PROJECT_SOURCE_DIR} main_srs)

include_directories(/usr/include/webRtcModule)
include_directories(${PROJECT_SOURCE_DIR})

add_executable(webRTCtest  ${main_srs})

TARGET_LINK_LIBRARIES(webRTCtest libwebRTC.so)
TARGET_LINK_LIBRARIES(webRTCtest libpthread.so)




测试一下这个编译出来的结果

因为音频文件本身就在nsagc文件夹下,直接执行./test,在webRtcTest文件中最后一行代码是getchar(),所以执行完毕后需要手动推出,可以直接删调他。

lijianglong@lijianglong-Lenovo-M495:~/C++ 编程学习/cmake/webRTC/nsagc$ ./test
n_s user time=0ms
n_s user time=0ms
�������棬��������...

执行完上面的命令后,在nsagc文件夹下可见处理之后的结果,导入到audacity中查看处理效果



我把代码上传到了github上(本博文全部代码



至此,经过一番痛苦的摸索,终于可以宣告活着上岸了,今天晚上开一瓶香槟庆祝一下。


猜你喜欢

转载自blog.csdn.net/ljl86400/article/details/80649515
今日推荐