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

希望對大家有所幫助,不喜勿噴。有需要看JNI層實現和完整demo的,請加我 V:15092216090

原創:VampireHunter

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章