android噪音抑制--Webrtc NS模块编译
-
工具
1.cygwin
2.ndk
环境配置请看Win7下Android native code的编译和调试 -
获取Webrtc ns模块源码
文件结构.jpg 文件可在https://github.com/rillian/webrtc 获取
可根据网上的教程获取webrtc整个工程的源码,但是很大,这里只给出ns模块所需文件。参考单独编译使用WebRTC的音频处理模块 - android
-
新建android 工程WebrtcNS,根目录下新建jni目录,将源码复制进jni目录。结构可以按照webrtc源码的结构来,但此处由于源码比较少,所以所有的头文件都放在include文件夹下,同时也要修改源码中的
文件结构1.jpg#include
,避免引用错误。
在src下新建com.nativetools包,新建WebrtcNS类。
package com.nativetools;
public class WebrtcNS {
static{
System.loadLibrary("WebrtcNS"); //加载native code的动态库
}
public native int createNSInstance();
public native int initNSInstance(int nsHandler,long sf);
public native int setNSPolicy(int nsHandler,int mode);
public native void nsProcess(int nsHandler,short bufferIn[],short bufferIn_H[],short bufferOut[],short bufferOut_H[]);
public native void nsFree(int nsHandler);
}
然后打开cmd,进入此工程的bin->classes目录,输入javah com.nativetools.WebrtcNS
就可以在bin->classes文件夹下得到头文件com_nativetools_WebrtcNS.h
:
<pre>/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_nativetools_WebrtcNS */
#ifndef _Included_com_nativetools_WebrtcNS
#define _Included_com_nativetools_WebrtcNS
#ifdef __cplusplus
extern "C" {
#endif</br>
/*
* Class: com_nativetools_WebrtcNS
* Method: createNSInstance
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_nativetools_WebrtcNS_createNSInstance
(JNIEnv *, jobject);</br>
/*
* Class: com_nativetools_WebrtcNS
* Method: initNSInstance
* Signature: (IJ)I
*/
JNIEXPORT jint JNICALL Java_com_nativetools_WebrtcNS_initNSInstance
(JNIEnv *, jobject, jint, jlong);</br>
/*
* Class: com_nativetools_WebrtcNS
* Method: setNSPolicy
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_nativetools_WebrtcNS_setNSPolicy
(JNIEnv *, jobject, jint, jint);</br>
/*
* Class: com_nativetools_WebrtcNS
* Method: nsProcess
* Signature: (I[S[S[S[S)V
*/
JNIEXPORT void JNICALL Java_com_nativetools_WebrtcNS_nsProcess
(JNIEnv *, jobject, jint, jshortArray, jshortArray, jshortArray, jshortArray);</br>
/*
* Class: com_nativetools_WebrtcNS
* Method: nsFree
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_nativetools_WebrtcNS_nsFree
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
</pre>
将此头文件复制到jni中备用。
在jni下新建demo.cpp
,引入com_nativetools_WebrtcNS.h
,编写jni:
<pre>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include "ns/include/com_nativetools_WebrtcNS.h"
#include "ns/include/noise_suppression.h"
#include "ns/include/ns_core.h"
extern "C" {
JNIEXPORT jint JNICALL
Java_com_nativetools_WebrtcNS_createNSInstance(JNIEnv *env, jobject jobj) {
NsHandle *hNS = NULL; //create a pointer to NsHandle on native stack.
if (WebRtcNs_Create(&hNS) == -1) { //allocate dynamic memory on native heap for NS instance pointed by hNS.
return -1;//error occurs
} else {
return ((int) (NSinst_t *) hNS); //returns the address of NS instance on native heap.
}
}
JNIEXPORT jint JNICALL
Java_com_nativetools_WebrtcNS_initNSInstance(JNIEnv \*env, jobject jobj, jint nsHandler, jlong sf) {
NsHandle \*hNS = (NsHandle\*) nsHandler;
return WebRtcNs_Init(hNS, sf);
}
JNIEXPORT jint JNICALL
Java_com_nativetools_WebrtcNS_setNSPolicy(JNIEnv \*env, jobject jobj, jint nsHandler, jint mode){
NsHandle \*hNS = (NsHandle\*) nsHandler;
return WebRtcNs_set_policy(hNS,mode);
}
JNIEXPORT void JNICALL
Java_com_nativetools_WebrtcNS_nsProcess(JNIEnv \*env, jobject jobj, jint nsHandler, jshortArray bufferIn, jshortArray bufferIn_H, jshortArray bufferOut, jshortArray bufferOut_H){
NsHandle \*hNS = (NsHandle*) nsHandler;
jshort *spFrameInData = env->GetShortArrayElements(bufferIn,0);
jshort \*spFrameOutData = env->GetShortArrayElements(bufferOut,0);
short \* spFrame_In = (short \*)spFrameInData;
short \* spFrame_Out = (short \*)spFrameOutData;
WebRtcNs_Process(hNS,spFrame_In,NULL,spFrame_Out,NULL);
(env)->ReleaseShortArrayElements(bufferIn, spFrameInData, 0);
(env)->ReleaseShortArrayElements(bufferOut, spFrameOutData, 0);
}
JNIEXPORT void JNICALL
Java_com_nativetools_WebrtcNS_nsFree(JNIEnv \*env, jobject jobj, jint nsHandler){
NsHandle \*hNS = (NsHandle*) nsHandler;
WebRtcNs_Free(hNS);
}
}
</pre>
-
编写Andorid.mk
在jni下新建Android.mk,若是从其他地方复制过来需要注意末尾的空格。
<pre>
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := WebrtcNS
LOCAL_SRC_FILES :=
demo.cpp
./ns/fft4g.c
./ns/noise_suppression.c
./ns/ns_core.c
include $(BUILD_SHARED_LIBRARY)
</pre>
工程根目录添加Application.mk
<pre>
APP_PROJECT_PATH := $(call my-dir)
APP_MODULES := WebrtcNS
</pre> -
编译
进入cygwin,进到工程jni文件夹,输入指令:$NDK/ndk-build
,编译成功则libs下会出现libWebrtcNS.so文件