l最近在调speex接口参数,将speex算法的一些特性给添加进去,比如:降噪,静音检测,白噪声添加,增益等等。下面我们就先简单介绍一些spexx算

法。speex语音算法主要是针对VOIP应用的一个开源算法,他集合了多种功能,除了如上所述的,还增加了回声消除(ACE)等功能,能够在多种平台

进行应用。下面我主要介绍一下speex在android平台上的应用。

首先我们来介绍一下speex算法的模块划分。在介绍之前,我们最好去speex官网  http://www.speex.org/downloads/ 去下载他的相关文档以及源码。其

中有一个包speex-api-reference.tar.gz 就有speex模块的相关介绍。其API介绍在1.2为止,speex总共分为以下9大模块:

--Speex encoder and decoder。——编码和解码模块。

--SpeexBits:Bit-stream mainpulations。——比特流操作模块,也就是数据的读写模块。

--Various definitions for Speex callbacks supported by the decoder。——解码回调模块。

--SpeexEchoState:Acoustic echo caceller。——回声消除模块。

--SpeexHeader:Makes it easy to writ/parse an Ogg/Speex header。——ogg格式相关的处理模块。

--JitterBuffer:Adaptive jitter buffer。——语音抖动缓冲模块。

--SpeexJitter:Adaptive jitter buffer specifically for Speex。——针对speex算法特点优化的语言抖动处理模块。

--SpeexPreprocessState:The Speex preprocessor。——Speex其他相关特点的处理模块,如:降噪,静音检测等。

--SpeexStereoState:Handing Speex stereo files。——立体声处理的相关模块。

.以上就是Speex算法的主要模块,每个模块都有相关功能的函数接口,具体我们可以去查看其api的相关介绍。

好了现在我们来介绍其在android平台的使用。由于其使用的是C实现的,所以要想在android进行调用其相关方法就必须通过JNI的方法进行调用,所以

我们首先就必须获得speex算法的一个.so文件,因此我们先使用cygwin编译获取.so文件。

一、将speex相关源码复制进项目

下载speex源码,在项目中新建文件夹,命名为jni。将speex源码下的include,libspeex两个文件的源码复制进jni文件夹中。将include文件夹下的

speex_config_types.h.in文件改为speex_config_types.h文件,并且将其中的内容改为以下内容:

#ifndef _SPEEX_CONFIG_TYPES_H#define _SPEEX_CONFIG_TYPES_H   typedef signed short spx_int16_t;   typedef unsigned short spx_uint16_t;   typedef signed int spx_int32_t;   typedef unsigned int spx_uint32_t;#endif  /* _SPEEX_CONFIG_TYPES_H */

二、编写Android.mk以及Application.mk相关文件。

Android.mk文件内容如下:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := libspeexLOCAL_CFLAGS = -DFIXED_POINT -DUSE_KISS_FFT -DEXPORT="" -UHAVE_CONFIG_HLOCAL_C_INCLUDES := $(LOCAL_PATH)/includeLOCAL_SRC_FILES := speex_jni.cpp \./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.cinclude $(BUILD_SHARED_LIBRARY)

具体每行的含义可以参看我之前的一篇博客,或者自行搜索Android.mk的编写方法。

Applicatio.mk 内容如下:

APP_ABI := armeabi armeabi-v7a

三、编写本地方法接口文件speex

public native int open(int compression);public native int getFrameSize();public native int decode(byte encoded[], short lin[], int size);public native int encode(short lin[], int offset, byte encoded[], int size);public native void close();

四、使用java当中的javah工具编译这个jni接口文件。

使用cmd进入到项目bin/classes目录下,输入以下命令:javah -jni xxx.xxx.xxx.speex。前面的xxx为speex文件的包名。编译完成后会在classes文件下看到

一个com_poctalk_codec_Speex.h文件,将这个文件复制进jni目录下。

五、编写speex.cpp文件

#include #include "com_poctalk_codec_Speex.h"#include #include #include #include #include #pragma comment(lib,"libspeexdsp.lib")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;SpeexPreprocessState *preprocess_state;//SpeexEchoState *echo_state;static JavaVM *gJavaVM;extern "C"{ JNIEXPORT jint JNICALL Java_com_poctalk_codec_Speex_open(JNIEnv *env, jobject obj, jint compression) {int tmp;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);//设置编码为宽带编码//enc_state = speex_encoder_init(&speex_wb_mode);//dec_state = speex_decoder_init(&speex_wb_mode);tmp = compression;speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);//设置编码的比特率,即语音质量。由参数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);preprocess_state =speex_preprocess_state_init(160, 8000);//创建预处理对象//echo_state = speex_echo_state_init(160, 5000);//创建回声消除对象//int sampleRate = 8000;//speex_echo_ctl(echo_state, SPEEX_ECHO_SET_SAMPLING_RATE, &sampleRate);int denoise = 1;int noiseSuppress = -25;speex_preprocess_ctl(preprocess_state, SPEEX_PREPROCESS_SET_DENOISE, &denoise); //降噪speex_preprocess_ctl(preprocess_state, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &noiseSuppress); //设置噪声的dBint agc = 1;float q=24000;//actually default is 8000(0,32768),here make it louder for voice is not loudy enough by default. 8000speex_preprocess_ctl(preprocess_state, SPEEX_PREPROCESS_SET_AGC, &agc);//增益speex_preprocess_ctl(preprocess_state, SPEEX_PREPROCESS_SET_AGC_LEVEL,&q);int vad = 1;int vadProbStart = 80;int vadProbContinue = 65;speex_preprocess_ctl(preprocess_state, SPEEX_PREPROCESS_SET_VAD, &vad); //静音检测speex_preprocess_ctl(preprocess_state, SPEEX_PREPROCESS_SET_PROB_START , &vadProbStart); //Set probability required for the VAD to go from silence to voicespeex_preprocess_ctl(preprocess_state, SPEEX_PREPROCESS_SET_PROB_CONTINUE, &vadProbContinue); //Set probability required for the VAD to stay in the voice state (integer percent)return (jint)0; } JNIEXPORT jint JNICALL Java_com_poctalk_codec_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);//在每帧输入之前将所有的编码状态重置speex_echo_state_reset(echo_state);//for (i = 0; i < nsamples; i++) {env->GetShortArrayRegion(lin, offset + i*enc_frame_size, enc_frame_size, buffer);//input_frame麦克风采集到的数据,Echo_Data是从speaker处获取到的数据,out_frame为回声消除后的数据//speex_echo_cancellation(echo_state,input_frame,Echo_Data,out_frame);//回声消除speex_preprocess_run(preprocess_state, 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; } JNIEXPORT jint JNICALL Java_com_poctalk_codec_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; } JNIEXPORT jint JNICALL Java_com_poctalk_codec_Speex_getFrameSize(JNIEnv *env, jobject obj) {if (!codec_open)return 0;return (jint)enc_frame_size; } JNIEXPORT void JNICALL Java_com_poctalk_codec_Speex_close(JNIEnv *env, jobject obj) {if (--codec_open != 0){return;}//speex_echo_state_destroy(echo_state);//speex_preprocess_state_destroy(preprocess_state);speex_bits_destroy(&ebits);speex_bits_destroy(&dbits);speex_decoder_destroy(dec_state);speex_encoder_destroy(enc_state); }}

六、使用cygwin对整个项目进行编译。

编译完成后,refresh项目会在libs目录下生成两个文件夹armeabi,armeabi-v7a 其中分别有一个libspeex.so文件。

至此.so文件的编译已经完成了,我们就可以在项目中对本地方法进行调用,去进行语音的编解码。由于使用方面我已经在项目中进行应用了,所以就不

挂出来了,不过我的语言模块也是参考网上的一个项目进行编写的,名字叫做android-recorder-6.0,你可以下载他的源码进行模仿。不过还有一点需要

说明的是在编写.cpp文件时,我没有将回声消除的功能给加进去,在回声消除的这个问题上浪费了我很多时间,刚开始没有看他的api,不知道回声消除

是哪个模块实现的,不知道该怎样使用回声消除的api,后来看了api,又不知道怎样在调用回声消除的函数时,该怎样传递参数进去,后来问同事知道,

回声消除的功能是针对全双工的通信方式,也就是喇叭和录音模块都打开,如果是半双工的通信方式,比如:手持机,回声消除的功能其实可有可无。

但是既然提到了又浪费了很多时间,那就不妨讲一讲回声消除功能的调用。

首先我们在预处理时,就应该回声消除的预处理:

//echo_state = speex_echo_state_init(160, 5000);//创建回声消除对象//int sampleRate = 8000;//speex_echo_ctl(echo_state, SPEEX_ECHO_SET_SAMPLING_RATE, &sampleRate);

sampleRate就是我们设置好的录音采用频率。

然后在语音编码的时候,进行回声消除功能的调用:

//input_frame麦克风采集到的数据,Echo_Data是从speaker处获取到的数据,out_frame为回声消除后的数据//speex_echo_cancellation(echo_state,input_frame,Echo_Data,out_frame);//回声消除

上面speex_echo_cancellation函数的三个参数一次为,回声消除对象,inpt_frame为喇叭播放数据,Echo_Data为从麦克风获取的数据,out_frame为最后

回声消除后的数据。可能有人会对这几个参数比较迷惑,那是因为不了解回声的产生原因。由于是全双工通信,当我们在录音的时候,也可能在进行声音

播放,这样就会导致有时候录音也会将喇叭正在播放的声音给录进去,这样就产生了回声的效果,所以第二个参数才要将播放的数据作为参数传递进去。

在编译.so文件的过程中,我还遇到了这样一个问题:multiple definition 。后来才发现在我的android.mk文件中将speex_jni.cpp引用了两次。

直到找到这个博客才发现我的这个问题:http://www.cnblogs.com/hewei2012/p/3370299.html 在这个问题上花费我的时间最久,粗心害死人。

参考博客:

http://www.cppblog.com/tx7do/archive/2012/11/23/195607.html

http://www.cnblogs.com/stay/archive/2011/09/06/2168751.html

http://www.cnblogs.com/chef/archive/2012/07/19/2599067.html

http://www.cnblogs.com/rosesmall/archive/2012/04/18/2455395.html

http://www.cnblogs.com/myitm/archive/2011/07/21/2113299.html

http://www.cnblogs.com/kinyer/p/3326570.html

http://www.cnblogs.com/ldjrl2013/p/3687938.html

http://blog.csdn.net/zblue78/article/details/5841357

更多相关文章

  1. 无需Root也能使用Xposed!
  2. Android通信模块代码流程
  3. Android模拟器学framework和driver之battery & backlight-----in
  4. 为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接
  5. 融云 Android(安卓)sdk 2.1+ 稳定版 UI 和 模块功能自定义
  6. Android(安卓)小应用 - "Smart Help" 之模块介绍
  7. xposed hook框架的使用(一)
  8. 深入理解Android写作背后的故事
  9. Android(安卓)小型社交软件客户端开发技术栈+案例

随机推荐

  1. 可视化工具不知道怎么选?深度评测5大Pytho
  2. 原理+代码|Python实战多元线性回归模型
  3. 原理+代码|Python基于主成分分析的客户信
  4. Python办公自动化 | 从PPT到Word
  5. 十分钟快速了解Pandas的常用操作!
  6. 原理 + 代码|手把手教你用Python实现智能
  7. 分享我学习Pandas使用的资料,可能是新手入
  8. Python+Excel+Word一秒制作百份合同
  9. 微信群总是有人发广告?看我用Python写一个
  10. Python办公自动化|批量提取Excel数据