概述
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模块位于 srcmodulesaudio_processingagc,另外和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命令无法识别
原创:望希望
最后
以上就是香蕉板凳为你收集整理的Android 单独抽取 WebRtc-AGC(音频增益) 模块的全部内容,希望文章能够帮你解决Android 单独抽取 WebRtc-AGC(音频增益) 模块所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复