Android移植Speex回声消除

来源:互联网 发布:剑灵灵男好看捏脸数据 编辑:程序博客网 时间:2024/04/29 07:59

最近弄了很久的Android回声消除,今天终于有电成效,顺便拿来分享分享,希望对大家有帮助


Speex不用介绍了,这里介绍了如何使用Speex进行回声消除,下面直接上代码

speex_jni.cpp

//// Created by jiaokx on 2015/11/30.//#include "com_example_speex1_Speex.h"#include <string.h>#include <unistd.h>//#include <speex/speex.h>//#include <speex/speex_echo.h>/*start*/#include <fcntl.h>#include <speex/speex.h>#include <speex/speex_echo.h>#include <speex/speex_preprocess.h>#include <speex/speex_bits.h>#include <speex/speex_buffer.h>#include <speex/speex_header.h>#include <speex/speex_types.h>// the header length of the RTP frame (must skip when en/decoding)static const int rtp_header = 0;int codec_status = 0;const int CODEC_OPENED = 1;const int CODEC_CLOSED = 0;int aec_status = 0;const int AEC_OPENED = 1;const int AEC_CLOSED = 0;SpeexEchoState *echoState;SpeexPreprocessState *den;int sampleRate = 8000;/*end*/static int codec_open = 0;static int dec_frame_size;static int enc_frame_size;static SpeexBits ebits, dbits;void *enc_state;void *dec_state;static JavaVM *gJavaVM;extern "C" jint  Java_com_example_speex1_Speex_open(        JNIEnv *env, jobject obj, jint compression) {    int tmp = 0;    if (codec_open++ != 0)        return (jint) 0;    speex_bits_init(&ebits);    speex_bits_init(&dbits);    enc_state = speex_encoder_init(&speex_nb_mode);    dec_state = speex_decoder_init(&speex_nb_mode);    tmp = compression;    speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);    speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &enc_frame_size);    speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &dec_frame_size);    SpeexPreprocessState * m_st;    m_st = speex_preprocess_state_init(enc_frame_size, sampleRate);    int denoise = 1;    int noiseSuppress = -25;    speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_DENOISE, &denoise);  speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS,&noiseSuppress); int agc = 1;        float q=24000;        //actually default is 8000(0,32768),here make it louder for voice is not loudy enough by default. 8000        speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_AGC, &agc);//增益        speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_AGC_LEVEL,&q);        int vad = 1;        int vadProbStart = 80;        int vadProbContinue = 65;        speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_VAD, &vad); //静音检测        speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_PROB_START , &vadProbStart); //Set probability required for the VAD to go from silence to voice        speex_preprocess_ctl(m_st, SPEEX_PREPROCESS_SET_PROB_CONTINUE, &vadProbContinue); //Set probability required for the VAD to stay in the voice state (integer percent)    return (jint) 0;}extern "C"  jint  Java_com_example_speex1_Speex_encode(        JNIEnv *env, jobject obj, jshortArray lin, jint offset,        jbyteArray encoded, jint size) {    jshort buffer[enc_frame_size];    jbyte output_buffer[enc_frame_size];    int nsamples = (size - 1) / enc_frame_size + 1;    int i, tot_bytes = 0;    if (!codec_open)        return 0;    speex_bits_reset(&ebits);    for (i = 0; i < nsamples; i++) {        env->GetShortArrayRegion(lin, offset + i * enc_frame_size,                                 enc_frame_size, buffer);        speex_encode_int(enc_state, buffer, &ebits);    }    //env->GetShortArrayRegion(lin, offset, enc_frame_size, buffer);    //speex_encode_int(enc_state, buffer, &ebits);    tot_bytes = speex_bits_write(&ebits, (char *) output_buffer,                                 enc_frame_size);    env->SetByteArrayRegion(encoded, 0, tot_bytes, output_buffer);    return (jint) tot_bytes;}extern "C"  jint Java_com_example_speex1_Speex_decode(        JNIEnv *env, jobject obj, jbyteArray encoded, jshortArray lin,        jint size) {    jbyte buffer[dec_frame_size];    jshort output_buffer[dec_frame_size];    jsize encoded_length = size;    if (!codec_open)    return 0;    env->GetByteArrayRegion(encoded, 0, encoded_length, buffer);    speex_bits_read_from(&dbits, (char *) buffer, encoded_length);    speex_decode_int(dec_state, &dbits, output_buffer);    env->SetShortArrayRegion(lin, 0, dec_frame_size, output_buffer);    return (jint) dec_frame_size;}extern "C"  jint  Java_com_example_speex1_Speex_getFrameSize(        JNIEnv *env, jobject obj) {    if (!codec_open)        return 0;    return (jint) enc_frame_size;}extern "C"  void  Java_com_example_speex1_Speex_close(        JNIEnv *env, jobject obj) {    if (--codec_open != 0)    return;    speex_bits_destroy(&ebits);    speex_bits_destroy(&dbits);    speex_decoder_destroy(dec_state);    speex_encoder_destroy(enc_state);}extern "C"  void Java_com_example_speex1_Speex_initEcho(        JNIEnv *env, jobject jobj, jint frame_size, jint filter_length) {    if (aec_status == AEC_OPENED)        return;    aec_status = AEC_OPENED;    int frm_size;    int f_length;    frm_size = frame_size;    f_length = filter_length;    echoState = speex_echo_state_init(frame_size, filter_length);    den = speex_preprocess_state_init(frame_size, sampleRate);    speex_echo_ctl(echoState, SPEEX_ECHO_SET_SAMPLING_RATE, &sampleRate);    speex_preprocess_ctl(den, SPEEX_PREPROCESS_SET_ECHO_STATE, echoState);}extern "C"  void Java_com_example_speex1_Speex_echoCancellation(        JNIEnv *env, jobject obj,jshortArray rec, jshortArray play, jshortArray out) {    jshort echo_buf[enc_frame_size];    jshort ref_buf[enc_frame_size];    jshort e_buf[enc_frame_size];    env->GetShortArrayRegion(rec, 0, enc_frame_size, echo_buf);    env->GetShortArrayRegion(play, 0, enc_frame_size, ref_buf);    speex_echo_cancellation(echoState, echo_buf, ref_buf, e_buf);    speex_preprocess_run(den, e_buf);    env->SetShortArrayRegion(out, 0, enc_frame_size, e_buf);}extern "C"  int Java_com_example_speex1_Speex_echoCancellationEncode(        JNIEnv *env, jobject obj,jshortArray rec, jshortArray play, jbyteArray encoded) {    jshort echo_buf[enc_frame_size];    jshort ref_buf[enc_frame_size];    jshort e_buf[enc_frame_size];    jbyte output_buffer[enc_frame_size];    env->GetShortArrayRegion(rec, 0, enc_frame_size, echo_buf);    env->GetShortArrayRegion(play, 0, enc_frame_size, ref_buf);    speex_echo_cancellation(echoState, echo_buf, ref_buf, e_buf);    speex_preprocess_run(den, e_buf);    speex_bits_reset(&ebits);    speex_encode_int(enc_state, e_buf, &ebits);    jint tot_bytes = speex_bits_write(&ebits, (char *) output_buffer,                                      enc_frame_size);    env->SetByteArrayRegion(encoded, 0, tot_bytes, output_buffer);    return (jint) tot_bytes;}extern "C"  jint  Java_com_example_speex1_Speex_getAecStatus(        JNIEnv * env, jobject jobj) {    return (jint) aec_status;}extern "C"  void Java_com_example_speex1_Speex_destroyEcho(        JNIEnv * env, jobject jobj) {    if (aec_status == AEC_CLOSED)        return;    aec_status = AEC_CLOSED;    speex_echo_state_destroy(echoState);    speex_preprocess_state_destroy(den);}

Android.mk

LOCAL_PATH := $(call my-dir)  include $(CLEAR_VARS)  LOCAL_MODULE:= libspeex  LOCAL_CFLAGS = -DFIXED_POINT -DUSE_KISS_FFT -DEXPORT="" -UHAVE_CONFIG_H  LOCAL_C_INCLUDES := $(LOCAL_PATH)/include  LOCAL_SRC_FILES :=\    libspeex/bits.c \    libspeex/buffer.c \    libspeex/cb_search.c \    libspeex/exc_10_16_table.c \    libspeex/exc_10_32_table.c \    libspeex/exc_20_32_table.c \    libspeex/exc_5_256_table.c \    libspeex/exc_5_64_table.c \    libspeex/exc_8_128_table.c \    libspeex/fftwrap.c \    libspeex/filterbank.c \    libspeex/filters.c \    libspeex/gain_table.c \    libspeex/gain_table_lbr.c \    libspeex/hexc_10_32_table.c \    libspeex/hexc_table.c \    libspeex/high_lsp_tables.c \    libspeex/jitter.c \    libspeex/kiss_fft.c \    libspeex/kiss_fftr.c \    libspeex/lpc.c \    libspeex/lsp.c \    libspeex/lsp_tables_nb.c \    libspeex/ltp.c \    libspeex/mdf.c \    libspeex/modes.c \    libspeex/modes_wb.c \    libspeex/nb_celp.c \    libspeex/preprocess.c \    libspeex/quant_lsp.c \    libspeex/resample.c \    libspeex/sb_celp.c \    libspeex/scal.c \    libspeex/smallft.c \    libspeex/speex.c \    libspeex/speex_callbacks.c \    libspeex/speex_header.c \    libspeex/stereo.c \    libspeex/vbr.c \    libspeex/vq.c \    libspeex/window.c \    speex_jni.cpp \include $(BUILD_SHARED_LIBRARY) 

Application.mk

APP_ABI := all

Speex.java

public class Speex {    private static final int DEFAULT_COMPRESSION = 8;    private static final Speex speex = new Speex();    private Speex() {    }    public static Speex getInstance() {        return speex;    }    public void init() {        load();        open(DEFAULT_COMPRESSION);        initEcho(160,filter_length);//80,160,320    }    private void load() {        try {            System.loadLibrary("speex");        } catch (Throwable e) {            e.printStackTrace();        }    }    public native int open(int compression);    public native int getFrameSize();    /**     * Decode     * @param encoded input     * @param lin output     * @param size output size     */    public native int decode(byte encoded[], short lin[], int size);    /**     * Eecode     * @param lin input     * @param offset input start location     * @param encoded output     * @param size input lin buffer size     */    public native int encode(short lin[], int offset, byte encoded[], int size);    public native void close();    public native void initEcho(int frameSize, int filterLength);    public native void echoCancellation(short[] rec, short[] play, short[] out);    public native int echoCancellationEncode(short[] rec, short[] play, byte[] encoded);    public native void destroyEcho();    public native int getAecStatus();}

先cd到java文件夹javah -jni xxx.xxx.xxx.Speex 生成xxx.xxx.xxx.Speex .h复制到jni目录下,然后cd到jni
ndk-build编译就好,最后so文件在libs文件夹下
最后在build.gradle添加如下代码

   sourceSets {        main {            jniLibs.srcDirs = ['libs']        }    }

使用: Speex.getInstance().init();//初始化
Speex.getInstance().echoCancellation(rec, palyer, outdata);//回声消除

注意

AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT);new AudioTrack(AudioManager.STREAM_MUSIC,8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,AudioRecord.getMinBufferSize(AudioConfig.SAMPLERATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioConfig.AUDIO_FORMAT),AudioTrack.MODE_STREAM);

上述代码是初始化录音器和播放器的,这里的参数频率和声道等配置是和Speex初始化AEC匹配的,不能用别的,否则在初始化Speex处参数也要相应的修改;其次,在录音和放音最后放在一个线程里,调用 audioTrack.write(dada[], 0, size);之后将播放的数据传个录音器,录音完毕将录音的数据和播放的数据一并传给Speex最回声消除处理,以此保证同步对齐,才能真正实现回声消除。下面给出部分关键代码。

   run() {        while (isPlaying) {            while (dataList.size() > 0) {                playData = dataList.remove(0);                audioTrack.write(playData.getRealData(), 0, playData.getSize());                timewerte = System.currentTimeMillis();                if(speex){                    AudioRecorder.getAudioRe().Recorder(playData.getRealData());                }else {                    AudioRecorder.getAudioRe().Recorder(null);                }                Log.e("录音", " size:" + playData.size + "耗时" + (System.currentTimeMillis() - timewerte) + "ms");                continue;            }            AudioRecorder.getAudioRe().Recorder(null);        }    }
    public void Recorder(short[] playerdata) {        short[] samples = new short[size];        int bufferRead = audioRecord.read(samples, 0, size);        if (bufferRead > 0) {            if (playerdata != null) {                short[] outdata = new short[size];                Speex.getInstance().echoCancellation(samples, playerdata, outdata);                addPlayerData(outdata,size);            } else {                addPlayerData(samples,size);            }        }    }

jni下载地址