語言聊天中的壓縮利器Speex
最近項目組在着手開發一個IM項目,即時聊天App.所以在工程師們正在搭建後臺服務器的時候,我必須解決一些前端和後端交互數據的一些事宜,例如協議,心跳包等。關於語音聊天的難點在於語音的壓縮加密。
參考了以下文章之後,決定是用Speex這個開源利器來壓縮語音吧。
http://code.csdn.net/news/313194
Google了以下Speex,很多demo信息都是13年12年的,所以我決定去他們Speex官網下載一個新的下來重新經過NDK編譯成.so的庫。
配置Speex,參考:http://www.phonesdevelopers.com/1787731/
解壓縮下載下來的壓縮包,我下載的是:speex-1.2rc1.tar.gz,把文件中的include和libspeex文件夾複製到工程的jni中。
1.
在工程中新建Speex.java類,利用javah的命令生成頭文件。
package com.park.speex;
public class Speex {
/* quality
* 1 : 4kbps (very noticeable artifacts, usually intelligible)
* 2 : 6kbps (very noticeable artifacts, good intelligibility)
* 4 : 8kbps (noticeable artifacts sometimes)
* 6 : 11kpbs (artifacts usually only noticeable with headphones)
* 8 : 15kbps (artifacts not usually noticeable)
*/
private static final int DEFAULT_COMPRESSION = 8;
public Speex() {
}
public void init() {
load();
open(DEFAULT_COMPRESSION);
}
private void load() {
try {
System.loadLibrary("speex");
} catch (Throwable e) {
e.printStackTrace();
}
}
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();
}
經過javaH之後生成了com_park_speex_Speex.h的頭文件,接着編寫speex.cpp文件用來真正實現壓縮解壓。
#include <jni.h>
#include <string.h>
#include <unistd.h>
#include <speex/speex.h>
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_park_speex_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);
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 Java_com_park_speex_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"
JNIEXPORT jint JNICALL Java_com_park_speex_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"
JNIEXPORT jint JNICALL Java_com_park_speex_Speex_getFrameSize
(JNIEnv *env, jobject obj) {
if (!codec_open)
return 0;
return (jint)enc_frame_size;
}
extern "C"
JNIEXPORT void JNICALL Java_com_park_speex_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);
}
注意:cpp文件中的方法必須與頭文件中的方法名稱一致
在其他工程中複製Android.mk和Application.mk到jni文件夾下面。
Android.mk如下:
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := speex
LOCAL_CFLAGS = -DFIXED_POINT -DUSE_KISS_FFT -DEXPORT="" -UHAVE_CONFIG_H
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SRC_FILES := \
./speex_jni.cpp \
./libspeex/buffer.c \
./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/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 \
include $(BUILD_SHARED_LIBRARY)
注意:在LOCAL_SRC_FILES後面接的文件名稱“\”後面不能跟空格,如下:
會發生這樣的“jni/Android.mk:26: * missing separator. Stop.”的編譯錯誤,這個錯誤說明在25行的“\”後面有空格了。
差不多了,可以進行編譯了,利用cygdrive的ndk-build命令進行jni的編譯,卻發現報錯:
jni/include/speex/speex_types.h:122:40: fatal error: speex/speex_config_types.h: No such file or directory
沒有找到speex_config_types.h的文件。
因此就去include\speex看看,發現只有一個命名爲:speex_config_types.h.in的文件。此時我心裏暗喜了一下,這都給我找到,這次應該可以編譯成功了吧。我把這個.in的文件直接複製命名爲:speex_config_types.h,再NDK,再報錯,我XXXXXXXX.
報錯信息爲:jni/include/speex/speex.h:325:35: error: ‘spx_int16_t’ has not been declared
說明這個文件裏面的“spx_int16_t”還未進行聲明就進行運用了。打開這個speex_config_types.h.in文件一看,果然存在一個叫“typedef @SIZE16@ spx_int16_t;”的東西,那麼如何改呢?參考了上述的文章,我直接複製他們的speex_config_types.h文件內容過來,如下:
#ifndef __SPEEX_TYPES_H__
#define __SPEEX_TYPES_H__
/* these are filled in by configure */
typedef short spx_int16_t;
typedef unsigned short spx_uint16_t;
typedef int spx_int32_t;
typedef unsigned int spx_uint32_t;
#endif
再進行NDK的編譯,終於順利的生成了libspeex.so文件了。
如果想了解這個C寫的Speex可以這篇文章來重新寫壓縮的和解壓的算法,有註釋說明。