Android 单独抽取 WebRtc-NS/NSX(音频降噪) 模块

Android 单独抽取 WebRtc-NS/NSX 模块,封装好JNI层,并且ndk-build出so库。

项目地址:https://github.com/wangzhengcheng1994/android-webrtc-ns
(欢迎star,谢谢)

先看下效果图:

NS前:

Android 单独抽取 WebRtc-NS/NSX(音频降噪) 模块_第1张图片

NS后:

Android 单独抽取 WebRtc-NS/NSX(音频降噪) 模块_第2张图片

Android层调用(部分代码):

try{
        NsUtils nsUtils = new NsUtils();
        int createStatus = nsUtils.nsCreate();
        int initStatus = nsUtils.nsInit(createStatus,8000);
        int setStatus = nsUtils.nsSetPolicy(createStatus,2);

        Log.e(TAG,"createStatus = " + createStatus + "initStatus = " + initStatus  + "setStatus = " + setStatus);

        FileInputStream fInt = new FileInputStream(INPUT_FILE_PATH);
        FileOutputStream fOut = new FileOutputStream(OUT_FILE_PATH);
        byte[] buffer = new byte[160];
        int bytes ;

        while(fInt.read(buffer) != -1){
            short[] inputData = new short[80];
            short[] outData = new short[80];
            ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(inputData);
            int ret = nsUtils.nsProcess(createStatus,inputData,null,outData,null);

            Log.e(TAG,"ret = " + ret);

            fOut.write(shortArrayToByteArry(outData));
        }

        fInt.close();
        fOut.close();

    }catch (Exception e){
        e.printStackTrace();
    }

nsUtils是native方法的一个封装类,核心方法nsProcess(int nsHandler,short[] sample,short[] sample_H,short[] outData, short[] outData_H),源码中头文件说明如下:

/*  
 * This functions does Noise Suppression for the inserted speech frame. The
 * input and output signals should always be 10ms (80 or 160 samples).
 *
 * Input
 *      - NS_inst       : Noise suppression instance.
 *      - spframe       : Pointer to speech frame buffer for L band
 *      - spframe_H     : Pointer to speech frame buffer for H band
 *      - fs            : sampling frequency
 *
 * Output:
 *      - NS_inst       : Updated NS instance
 *      - outframe      : Pointer to output frame for L band
 *      - outframe_H    : Pointer to output frame for H band
 *
 * Return value         :  0 - OK
 *                        -1 - Error
 */
int WebRtcNs_Process(NsHandle* NS_inst,
                     short* spframe,
                     short* spframe_H,
                     short* outframe,
                     short* outframe_H);

WebRtc源码中的NS模块位于 src\modules\audio_processing\ns,其中包含NS和NSX,这两种前者是定点数运算,后者是浮点数运算,从处理结果来看,好像没什么区别,没有深究。

WebRtc的NS模块还是比较简单的,希望对大家有所帮助。有需要看JNI层实现和完整demo的,请加我V:15092216090

原创:VampireHunter

你可能感兴趣的:(Android)