Android语音文件speex编码解码(一)

最近项目需要实现功能:用户通过语音发大幕,语音文件压缩后发送到后台,后台解析生成文本,显示文字。Android中使用AudioRecord录音后的格式为pcm,要想播放需要转换格式,可以加入44字节的头转换为wav格式后播放,并且在网络上传输最好把音频压缩,压缩为speex文件方便传输,节省流量,下面讲解如何生成speex的so库,和怎样使用so库实现wav和spx文件之间转换。

 一、在speex官网点击打开链接下载speex源码




 二、解压缩speex-1.2rc2.tar.gz


三、创建Android项目SpeexAudio,切换到Project模式,创建jni文件夹,将include、libspeex拷贝到jni下;

在jni目录下新增Android.mk文件,复制如下内容,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 :=  
LOCAL_SRC_FILES := speex_jni.cpp \
		./libspeex/bits.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/filters.c \
		./libspeex/gain_table_lbr.c \
		./libspeex/gain_table.c \
		./libspeex/hexc_10_32_table.c \
		./libspeex/hexc_table.c \
		./libspeex/high_lsp_tables.c \
		./libspeex/kiss_fft.c \
		./libspeex/kiss_fftr.c \
		./libspeex/lpc.c \
		./libspeex/lsp_tables_nb.c \
		./libspeex/lsp.c \
		./libspeex/ltp.c \
		./libspeex/modes_wb.c \
		./libspeex/modes.c \
		./libspeex/nb_celp.c \
		./libspeex/quant_lsp.c \
		./libspeex/sb_celp.c \
		./libspeex/smallft.c \
		./libspeex/speex_callbacks.c \
		./libspeex/speex_header.c \
		./libspeex/speex.c \
		./libspeex/stereo.c \
		./libspeex/vbr.c \
		./libspeex/vorbis_psy.c \
		./libspeex/vq.c \
		./libspeex/window.c \

include $(BUILD_SHARED_LIBRARY)

在jni下创建Application.mk,并添加如下内容,编译所有平台下的so文件

APP_ABI := all
在jni/include/speex/目录下新增speex_config_types.h文件,复制内容如下

#ifndef __SPEEX_TYPES_H__
#define __SPEEX_TYPES_H__

typedef short spx_int16_t;
typedef unsigned short spx_uint16_t;
typedef int spx_int32_t;
typedef unsigned int spx_uint32_t;

#endif

上述完成后,如下图所示


四、新建SpeexUtil类,其中定义了native方法

public class SpeexUtil {
    private static final int DEFAULT_COMPRESSION = 4;
    private static SpeexUtil speexUtil;

    static {
        try {
            System.loadLibrary("speex");
        } catch (Throwable var1) {
            var1.printStackTrace();
        }

        speexUtil = null;
    }

    SpeexUtil() {
        this.open(4);
    }

    public static SpeexUtil getInstance() {
        if(speexUtil == null) {
            Class var0 = SpeexUtil.class;
            synchronized(SpeexUtil.class) {
                if(speexUtil == null) {
                    speexUtil = new SpeexUtil();
                }
            }
        }

        return speexUtil;
    }

    public native int open(int var1);

    public native int getFrameSize();

    public native int decode(byte[] var1, short[] var2, int var3);

    public native int encode(short[] var1, int var2, byte[] var3, int var4);

    public native void close();
}

五、jni目录下新建speex_jni.cpp文件,该文件是SpeexUtil中native方法的具体实现代码,如下所示

#include 

#include 
#include 

#include 

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"
JNIEXPORT jint JNICALL Java_com_speex_util_SpeexUtil_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); 
	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);

	return (jint)0;
}

extern "C"
JNIEXPORT jint JNICALL Java_com_speex_util_SpeexUtil_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);
	}

	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"
JNIEXPORT jint JNICALL Java_com_speex_util_SpeexUtil_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"
JNIEXPORT jint JNICALL Java_com_speex_util_SpeexUtil_getFrameSize
    (JNIEnv *env, jobject obj) {

	if (!codec_open)
		return 0;
	return (jint)enc_frame_size;

}

extern "C"
JNIEXPORT void JNICALL Java_com_speex_util_SpeexUtil_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); 
}


此处注意:speex_jni.cpp中的Java_com_speex_util_SpeexUtil_open,要改成自己的包名,也就是SpeexUtil所在的包,encode、decode、getFrameSize、close等方法一样要改成SpeexUtil所在的包的包名。

六、通过ndk-build生成so库

命令行定位到Android.mk所在目录(即jni目录),通过ndk-build生成so库,如下图


编译完成后,会生成libs和obj两个目录,其中libs就是各个平台的so文件,如下图所示,下节我们会用到。



 代码下载:点击下载

Android开发交流群:576500546

你可能感兴趣的:(Android,Android,录音,speex)