Speex for Android
http://blog.csdn.net/chenfeng0104/article/details/7088138
在Android开发中,需要录音并发送到对方设备上。这时问题来了,手机常会是GPRS、3G等方式上网,所以节省流量是非常关键的,使用Speex来压缩音频文件,可以将音频压文件小数倍。
1.去Speex官网下载最新Speex源码。
2.创建一个新的应用(我创建的应用名为Audio),并创建一个jni目录($project/jni)。
3.把speex源码目录下的libspeex和include目录及其子目录文件全部拷贝到$project/jni目录下($project/jni/libspeex and $project/jni/include)。
4.在jni目录下新增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 := \
- ./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.c
- include $(BUILD_SHARED_LIBRARY)
5.在jni目录下新增Application.mk文件,编辑内容如下
- APP_ABI := armeabi armeabi-v7a
6.在$project/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
7.创建JNI包装类speex_jni.cpp,用来调用Speex中的C代码函数,编辑内容如下
- #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_audio_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_audio_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_audio_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_audio_getFrameSize
- (JNIEnv *env, jobject obj) {
- if (!codec_open)
- return 0;
- return (jint)enc_frame_size;
- }
- extern "C"
- JNIEXPORT void JNICALL Java_com_audio_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);
- }
8.在Java层创建Speex工具类,内容如下
- package com.audio;
- 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;
- 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();
- }
9.打开cygwin工具,切换到项目目录(我项目是在F:\workspace\Audio),输入$NDK/ndk-build
cygwin工具的安装与配置,可以看这篇文章——使用NDK与环境搭建


会在项目中生成libs目录和libspeex.so文件,这就是Speex类中System.loadLibrary("speex");代码引用的,系统会根据操作系统由"speex"找到对应的动态库libspeex.so,Windows下是.dll文件,linux下是.so文件。
当前,我的项目结构如下图

Speex for Android的更多相关文章
- speex编解码在android上实现
以前在应用中使用到了Speex编解码,近来总结了一下Speex在android上的实现.Speex是一套主要针对语音的开源免费,无专利保护的音频压缩格式.Speex工程着力于通过提供一个可以替代高性能 ...
- Android中使用speex将PCM录音格式转Wav格式
Android中使用speex将PCM录音格式转Wav格式 2013-09-17 17:24:00| 分类: android | 标签:android speex wav |举报|字号 订阅 ...
- Android - 基于 Speex 的高度封装语音库,0 耦合使用
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...
- Android 基于 Speex 的高度封装语音库,0 耦合,没三方jar包
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...
- Android 开发:开源库Speex支持arm64的动态库文件
随着处理器制造工艺的不断进步,和Android系统的不断发展,最近出了arm64-v8a的架构,由于项目中用到了speex的第三方语音编解码的动态库,其他架构的处理器暂不用说,一切正常,唯独到arm6 ...
- Android 5.0 到 Android 6.0 + 的深坑之一 之 .so 动态库的适配
(原创:http://www.cnblogs.com/linguanh) 目录: 前序 一,问题描述 二,为何会如此"无情"? 三,目前存在该问题的知名SDK 四,解决方案,1 对 ...
- Android源码目录结构详解(转载)
转自:http://blog.csdn.net/xiangjai/article/details/9012387 在学习Android的过程中,学习写应用还好,一开始不用管太多代码,直接调用函数就可以 ...
- android源码的目录结构
android源码的目录结构 [以下网络摘抄] |-- Makefile ! l/ a5 n% S% @- `0 d# z# a$ P4 V3 o7 R|-- bionic ...
- Android 4.0 源代码结构
Android源码的第一级目录结构 Android/abi (abi相关代码.ABI:application binary interface,应用程序二进制接口) Android/bioni ...
随机推荐
- 1430. Crime and Punishment(枚举)
1430 即使是枚举 也是有一定技术含量的 对于ax+by = n: 枚举max(a,b)中的系数 这样可以确定另一个 但问题是如何确定上限 假设max(a,b) = a,很显然是不会超n/a的 但这 ...
- tc srm 632 500 (规律)
We have a sequence of N positive integers: a[0] through a[N-1]. You do not know these integers. All ...
- [ionic开源项目教程] - 第1讲 前言,技术储备,环境搭建,常用命令
前言 这是一个系列文章,将持续更新到项目完结,从环境搭建开始讲解,包括实战开发中遇到的各种问题的解决方案,都将毫无保留的分享给大家. 技术储备 开始本项目之前,请确保自己对以下技术点都有所了解. ht ...
- 读取Properties文件工具类
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...
- 谷歌的ajax.googleapis.com被墙导致访问很多国外网站很慢的解决方法
比如访问StackOverflow, 更比如flexerasoftware.com(导致Visual Studio的打包程序InstallShield Limited Edition不能注册和下载) ...
- java MVC设计模式
MVC(Model View Control)模型-视图-控制器 一.MVC与模板概念的理解 MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是 ...
- OCJP考试介绍
OCJP考试介绍 考试名称:SCJP / OCJP / 1Z0-851 考试时间:150分钟 考题题目:60道题 通过条件:大于等于61%的题目正确 考点查询:http://www.pearsonvu ...
- BZOJ 3668 起床困难综合症
按位贪心. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- 计算机视觉入门 Intorduction To Computer Vision
本文将主要介绍图像分类问题,即给定一张图片,我们来给这张图片打一个标签,标签来自于预先设定的集合,比如{people,cat,dog...}等,这是CV的核心问题,图像分类在实际应用中也有许多变形,而 ...
- RequireJS进阶(三) 转
进阶的前面两篇讲述了r.js如何通过命令行把所有的模块压缩为一个js文件或把所有的css压缩为一个css文件.其中包括一些压缩配置参数的使用. 但以上两种方式有几个问题 1.通过命令手动配置压缩选项显 ...