频道栏目
首页 > 程序开发 > 移动开发 > Android > 正文
Android开发中单独抽取 WebRtc-AGC(音频增益)模块
2017-11-08 11:51:49      个评论    来源:always_and_forever_的博客  
收藏   我要投稿

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

点击复制链接 与好友分享!回本站首页
上一篇:Android开发中实现View滑动的七种方法总结
下一篇:Android系统APP安装流程解析
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站