WebRTC

单独抽取webRtc的AGC(增益)模块

2020-01-15  本文已影响0人  INode

注意:本文抽取的AGC源码基于webRtc源码2020年1月7日的提交
本文只提取源码中 legacy 版本的AGC模块
本文代码最终使用在Android设备上(其它设备根据情况自行调整)

抽取NS&NSX(降噪)模块文章链接:https://www.jianshu.com/p/72ae0ca2c0a7

本文简述步骤:下载源码抽取文件修改源码文件编写jni文件编写CMakeLists.txt文件正常使用特别注意GitHub链接
(提示: 非代码编写顺序 )

1.webRtc源码

使用Git工具Clone源码地址:https://webrtc.googlesource.com/src (需要翻墙)

2.AGC模块抽取

主函数入口为 gain_control.h
以下为主要源码文件:

modules/audio_processing/agc/legacy/
                                   analog_agc.c
                                   analog_agc.h
                                   digital_agc.c
                                   digital_agc.h
                                   gain_control.h

以下为依赖的源码文件:

rtc_base/
        numerics/
                safe_conversions_impl.h
                safe_conversions.h
        checks.cc
        checks.h
        compile_assert_c.h
        sanitizer.h

common_audio/
            signal_processing/
                              include/
                                     signal_processing_library.h
                                     spl_inl.h
                              copy_set_operations.c
                              division_operations.c
                              dot_product_with_scale.cc
                              dot_product_with_scale.h
                              resample_by_2.c
                              spl_sqrt.c
            third_party/spl_sqrt_floor/
                                      spl_sqrt_floor.c
                                      spl_sqrt_floor.h

3.修改源码文件

主要修改check.h与check.cc文件,因为这两个文件引用了absl库
主要修改是移除log打印,检查数据则修改使用基础库文件assert.h代替,效果无差别
下面贴出主要代码,详情查看GitHub中源码(因为全贴出来代码太长了)

check.h修改如下

//zhonghua code------------------------------------
#include <assert.h>

#define RTC_CHECK(condition) assert(condition)
#define RTC_CHECK_EQ(val1, val2) assert(val1 == val2)
#define RTC_CHECK_NE(val1, val2) assert(val1 != val2)
#define RTC_CHECK_LE(val1, val2) assert(val1 <= val2)
#define RTC_CHECK_LT(val1, val2) assert(val1 < val2)
#define RTC_CHECK_GE(val1, val2) assert(val1 >= val2)
#define RTC_CHECK_GT(val1, val2) assert(val1 > val2)

#define RTC_DCHECK(condition) RTC_CHECK(condition)
#define RTC_DCHECK_EQ(v1, v2) RTC_CHECK_EQ(v1, v2)
#define RTC_DCHECK_NE(v1, v2) RTC_CHECK_NE(v1, v2)
#define RTC_DCHECK_LE(v1, v2) RTC_CHECK_LE(v1, v2)
#define RTC_DCHECK_LT(v1, v2) RTC_CHECK_LT(v1, v2)
#define RTC_DCHECK_GE(v1, v2) RTC_CHECK_GE(v1, v2)
#define RTC_DCHECK_GT(v1, v2) RTC_CHECK_GT(v1, v2)

#define FATAL()

//zhonghua code------------------------------------
//为了缩减lib库大小,这样处理后可缩减200K大小

#ifdef USE_WEBRTC_CODE

#ifdef __cplusplus
// C++ version.
···
···[此处省略N行]
···
#endif  // __cplusplus
#endif

check.cc文件主要是去掉执行代码,理论上是可以去掉这个文件

//zhonghua code---------------------------------------
//为了缩减lib库大小,这样处理后可缩减200K大小
#ifdef USE_WEBRTC_CODE
namespace {
#if defined(__GNUC__)
__attribute__((__format__(__printf__, 2, 3)))
#endif
void AppendFormat(std::string* s, const char* fmt, ...) {
···
···[此处省略N行]
···
#endif
//zhonghua code---------------------------------------

4.编写jni文件

这里的JNI文件指的是我们调用webRtc模块的C++文件

#include <jni.h>
#include <string>
#include <cstdlib>

#include "modules/audio_processing/agc/legacy/gain_control.h"

#if defined(__cplusplus)
extern "C" {
#endif

JNIEXPORT jlong JNICALL
Java_vip_inode_demo_webrtc_AutomaticGainControlUtils_agcCreate(JNIEnv *env, jobject obj) {
    return (long) WebRtcAgc_Create();
}


JNIEXPORT jint JNICALL
Java_vip_inode_demo_webrtc_AutomaticGainControlUtils_agcFree(JNIEnv *env, jobject obj,
                                                             jlong agcInst) {
    void *_agcInst = (void *) agcInst;
    if (_agcInst == nullptr)
        return -3;
    WebRtcAgc_Free(_agcInst);
    return 0;
}

JNIEXPORT jint JNICALL
Java_vip_inode_demo_webrtc_AutomaticGainControlUtils_agcInit(JNIEnv *env,
                                                             jobject obj, jlong agcInst,
                                                             jint minLevel, jint maxLevel,
                                                             jint agcMode, jint fs) {
    void *_agcInst = (void *) agcInst;
    if (_agcInst == nullptr)
        return -3;
    return WebRtcAgc_Init(_agcInst, minLevel, maxLevel, agcMode, fs);
}

JNIEXPORT jint JNICALL
Java_vip_inode_demo_webrtc_AutomaticGainControlUtils_agcSetConfig(JNIEnv *env, jobject obj,
                                                                  jlong agcInst,
                                                                  jshort targetLevelDbfs,
                                                                  jshort compressionGaindB,
                                                                  jboolean limiterEnable
) {
    void *_agcInst = (void *) agcInst;
    if (_agcInst == nullptr)
        return -3;
    WebRtcAgcConfig setConfig;
    setConfig.targetLevelDbfs = targetLevelDbfs;
    setConfig.compressionGaindB = compressionGaindB;
    setConfig.limiterEnable = limiterEnable;
    return WebRtcAgc_set_config(_agcInst, setConfig);
}

JNIEXPORT jint JNICALL
Java_vip_inode_demo_webrtc_AutomaticGainControlUtils_agcProcess(JNIEnv *env, jobject obj,
                                                                jlong agcInst,
                                                                jshortArray inNear,
                                                                jint num_bands,
                                                                jint samples, jshortArray out,
                                                                jint inMicLevel,
                                                                jint outMicLevel,
                                                                jint echo,
                                                                jboolean saturationWarning) {
    void *_agcInst = (void *) agcInst;
    if (_agcInst == nullptr)
        return -3;
    jshort *cinNear = env->GetShortArrayElements(inNear, nullptr);
    jshort *cout = env->GetShortArrayElements(out, nullptr);

    int32_t gains[11] = {};
    jint ret = WebRtcAgc_Analyze(_agcInst, &cinNear, num_bands, samples, inMicLevel, &outMicLevel,
                                 echo, &saturationWarning, gains);
    if (ret == 0)
        ret = WebRtcAgc_Process(_agcInst, gains, &cinNear, num_bands, &cout);
    env->ReleaseShortArrayElements(inNear, cinNear, 0);
    env->ReleaseShortArrayElements(out, cout, 0);
    return ret;
}


JNIEXPORT jint JNICALL
Java_vip_inode_demo_webrtc_AutomaticGainControlUtils_agcAddFarend(JNIEnv *env, jobject obj,
                                                                  jlong agcInst,
                                                                  jshortArray inFar,
                                                                  jint samples) {
    void *_agcInst = (void *) agcInst;
    if (_agcInst == nullptr)
        return -3;
    short *cinFar = env->GetShortArrayElements(inFar, nullptr);
    jint ret = WebRtcAgc_AddFarend(_agcInst, cinFar, samples);
    env->ReleaseShortArrayElements(inFar, cinFar, 0);
    return ret;
}


JNIEXPORT jint JNICALL
Java_vip_inode_demo_webrtc_AutomaticGainControlUtils_agcAddMic(JNIEnv *env, jobject obj,
                                                               jlong agcInst,
                                                               jshortArray inMic,
                                                               jint num_bands, jint samples
) {
    void *_agcInst = (void *) agcInst;
    if (_agcInst == nullptr)
        return -3;
    short *cinMic = env->GetShortArrayElements(inMic, nullptr);
    jint ret = WebRtcAgc_AddMic(_agcInst, &cinMic, num_bands, samples);
    env->ReleaseShortArrayElements(inMic, cinMic, 0);
    return ret;
}


JNIEXPORT jint JNICALL
Java_vip_inode_demo_webrtc_AutomaticGainControlUtils_agcVirtualMic(JNIEnv *env, jobject obj,
                                                                   jlong agcInst,
                                                                   jshortArray inMic,
                                                                   jint num_bands,
                                                                   jint samples,
                                                                   jint micLevelIn,
                                                                   jint micLevelOut
) {
    void *_agcInst = (void *) agcInst;
    if (_agcInst == nullptr)
        return -3;
    jshort *cinMic = env->GetShortArrayElements(inMic, nullptr);
    jint ret = WebRtcAgc_VirtualMic(_agcInst, &cinMic, num_bands, samples, micLevelIn,
                                    &micLevelOut);
    env->ReleaseShortArrayElements(inMic, cinMic, 0);
    return ret;
}


#if defined(__cplusplus)
}
#endif


5.编写CMakeLists.txt文件

Android中使用需要定义宏WEBRTC_ANDROIDWEBRTC_POSIX

CMakeLists.txt内容如下:

cmake_minimum_required(VERSION 3.4.1)

file(GLOB SRC_FILES
        */*.cc
        */*/*.c
        */*/*.cc
        */*/*/*/*.c
        agc-lib.cpp
        )

add_library(legacy_agc-lib SHARED ${SRC_FILES})
include_directories(./)
add_definitions(
        -DWEBRTC_ANDROID
        -DWEBRTC_POSIX
)

find_library(log-lib log)

target_link_libraries(legacy_agc-lib ${log-lib})

6.正常使用

在Java中使用需要先初始化并配置音频参数

主要使用代码:

val agcUtils = AutomaticGainControlUtils()
val agcId = agcUtils.agcCreate()
val agcInitResult = agcUtils.agcInit(agcId, 0, 255, 3, 16000)
val agcSetConfigResult = agcUtils.agcSetConfig(agcId, 9, 9, true)
Log.e(tag, "agcId : $agcId  agcInit: $agcInitResult agcSetConfig: $agcSetConfigResult")
···
val inputData = ShortArray(160)
val outAgcData = ShortArray(160)
agcUtils.agcProcess(agcId, inputData, 1, 160, outAgcData, 0, 0, 0, false)
···
agcUtils.agcFree(agcId)

特别注意:每次处理的音频数据需始终为 10ms (否则会失败)

以下为官方注解:

/*·
 * This function processes a 10 ms frame by applying precomputed digital gains.
 *
 * Input:
 *      - agcInst           : AGC instance
 *      - gains             : Vector of gains to apply for digital normalization
 *      - in_near           : Near-end input speech vector for each band
 *      - num_bands         : Number of bands in input/output vector
 *
 * Output:
 *      - out               : Gain-adjusted near-end speech vector
 *                          : May be the same vector as the input.
 *
 * Return value:
 *                          :  0 - Normal operation.
 *                          : -1 - Error
 */
int WebRtcAgc_Process(const void *agcInst,
                      const int32_t gains[11],
                      const int16_t *const *in_near,
                      size_t num_bands,
                      int16_t *const *out);

GitHub链接:https://github.com/inodevip/WebRtcNsAgcModel

抽取NS&NSX(降噪)模块文章链接:https://www.jianshu.com/p/72ae0ca2c0a7

上一篇下一篇

猜你喜欢

热点阅读