Android 单独抽取 WebRtc-AGC(音频增益) 模块

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

先看下效果图:

AGC前:

这里写图片描述

AGC后:

这里写图片描述

其实也可以用来衰减:

这里写图片描述

Android层调用(部分代码):

 try{
            AgcUtils agcUtils = new AgcUtils();
            agcUtils.setAgcConfig(3,1,20).prepare();

            FileInputStream fInt = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/agc-input-test.pcm");
            FileOutputStream fOut = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() +"/agc-out-test.pcm");
            byte[] buffer = new byte[160];
            int bytes;

            while((bytes = fInt.read(buffer)) != -1){
                short[] data = new short[80];
                short[] outData = new short[80];
                short[] processData = new short[80];
                ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(data);
                 
                int agcProcessStatus = agcUtils.agcProcess(data,0,80,outData,0,micOutLevel,0,0);
                Log.e(TAG, "return value" + agcProcessStatus);

                fOut.write(shortArrayToByteArry(outData));

            }

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

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

agcUtils 是对native方法的一个封装,其核心处理方法agcProcess(short[] inNear,int num_bands,int samples,short[] out,int inMicLevel,int outMicLevel,int echo,int saturationWarning),在webRtc源码的头文件中如下:

/*
 * This function processes a 10/20ms frame and adjusts (normalizes) the gain
 * both analog and digitally. The gain adjustments are done only during
 * active periods of speech. The input speech length can be either 10ms or
 * 20ms and the output is of the same length. The length of the speech
 * vectors must be given in samples (80/160 when FS=8000, and 160/320 when
 * FS=16000 or FS=32000). The echo parameter can be used to ensure the AGC will
 * not adjust upward in the presence of echo.
 *
 * This function should be called after processing the near-end microphone
 * signal, in any case after any echo cancellation.
 *
 * Input:
 *      - agcInst           : AGC instance
 *      - inNear            : Near-end input speech vector (10 or 20 ms) for
 *                            L band
 *      - inNear_H          : Near-end input speech vector (10 or 20 ms) for
 *                            H band
 *      - samples           : Number of samples in input/output vector
 *      - inMicLevel        : Current microphone volume level
 *      - echo              : Set to 0 if the signal passed to add_mic is
 *                            almost certainly free of echo; otherwise set
 *                            to 1. If you have no information regarding echo
 *                            set to 0.
 *
 * Output:
 *      - outMicLevel       : Adjusted microphone volume level
 *      - out               : Gain-adjusted near-end speech vector (L band)
 *                          : May be the same vector as the input.
 *      - out_H             : Gain-adjusted near-end speech vector (H band)
 *      - saturationWarning : A returned value of 1 indicates a saturation event
 *                            has occurred and the volume cannot be further
 *                            reduced. Otherwise will be set to 0.
 *
 * Return value:
 *                          :  0 - Normal operation.
 *                          : -1 - Error
 */
int WebRtcAgc_Process(void* agcInst,
                      const int16_t* inNear,
                      const int16_t* inNear_H,
                      int16_t samples,
                      int16_t* out,
                      int16_t* out_H,
                      int32_t inMicLevel,
                      int32_t* outMicLevel,
                      int16_t echo,
                      uint8_t* saturationWarning);

参数比较多,具体传参可参考上面的android层代码。

**agcUtils.setAgcConfig(3,1,20)**此方法在头文件中定义如下:

/*
 * This function sets the config parameters (targetLevelDbfs,
 * compressionGaindB and limiterEnable).
 *
 * Input:
 *      - agcInst           : AGC instance
 *      - config            : config struct
 *
 * Output:
 *
 * Return value:
 *                          :  0 - Normal operation.
 *                          : -1 - Error
 */
int WebRtcAgc_set_config(void* agcInst, WebRtcAgc_config_t config);


**解释下WebRtcAgc_config_t中的变量含义**
typedef struct
{
    int16_t targetLevelDbfs;   // default 3 (-3 dBOv)
    int16_t compressionGaindB; // default 9 dB
    uint8_t limiterEnable;     // default kAgcTrue (on)
} WebRtcAgc_config_t;

上面的效果图,有增益有衰减,其实现就是通过WebRtcAgc_set_config方法实现的

compressionGaindB (上面设置的是20) ,在kAgcModeFixedDigital模式下,越大声音越大;
targetLevelDbfs (上面设置的是3),0表示full scale,越小声音越大 ,越大声音越小,(笔者测试发现最大只能设置到30)
limiterEnable(上面设置的是1),默认设置1就行,是一个开关。

WebRtc源码中的NS模块位于 src\modules\audio_processing\agc,另外和agc同级目录下,还有一个agc2目录,没有做深层研究。AGC最外层的头文件是gain_control.h

因经济压力,现改为付费下载模式,不再免费开源:

付费下载链接:https://download.csdn.net/download/always_and_forever_/71992243

使用说明
1.下载源码,直接运行即可
工程解读
1.根目录下的jni目录,是从webrtc源码中抽取出来的agc模块核心代码文件.
2.libs目录下,为编译jni生成的so文件,您可以直接使用.
3.webrtc-agc.apk 可直接安装到真机上快速体验.
4.test_input.pcm为测试文件,启动app将自动执行agc,agc后文件路径为手机根目录/agc_out.pcm
5.如您需要体验生成so这一步骤,可cd到jni目录下,执行ndk-build命令,前提是您下载了ndk且配置了环境变量,否则ndk命令无法识别

原创:望希望

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code王工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值