【Android】Speex简易使用

本文介绍了在Android上使用Speex进行音频压缩的步骤,包括库文件下载、构建NDK环境、打包.so库、引用库、音频录制、编码、解码及播放解码数据。Speex虽已由Opus替代,但作为历史记录,文章详细记录了Speex在移动端的应用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

speex是主要用于音频压缩,降噪;减少原始数据体积,提升各端数据传输效率,最后一次更新是在2016年,已被opus代替,该篇旨在记录对speex在android上的使用过程

库文件下载

  1. 去Speex官网下载最新Speex源码
  2. 将库文件中的includelibspeex放入工程的jni目录中
  3. 新建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_LDLIBS:=-llog

LOCAL_SRC_FILES :=\
libspeex/bits.c\
libspeex/cb_search.c\
libspeex/exc_5_64_table.c\
libspeex/exc_5_256_table.c\
libspeex/exc_8_128_table.c\
libspeex/exc_10_32_table.c\
libspeex/exc_10_16_table.c\
libspeex/exc_20_32_table.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/kiss_fft.c\
libspeex/kiss_fftr.c\
libspeex/lpc.c\
libspeex/lsp.c\
libspeex/lsp_tables_nb.c\
libspeex/ltp.c\
libspeex/modes.c\
libspeex/modes_wb.c\
libspeex/nb_celp.c\
libspeex/quant_lsp.c\
libspeex/sb_celp.c\
libspeex/smallft.c\
libspeex/speex.c\
libspeex/speex_callbacks.c\
libspeex/speex_header.c\
libspeex/stereo.c\
libspeex/testenc.c\
libspeex/vbr.c\
libspeex/vorbis_psy.c\
libspeex/vq.c\
libspeex/window.c\
speex_jni.cpp\



include $(BUILD_SHARED_LIBRARY)
  1. 新建Application.mk文件,写入需要打包的配置
APP_ABI := all
APP_PLATFORM := android-19

5.在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

  1. jni目录下新建speex_jni.cpp文件,用于调用speex的C函数
#include <jni.h>



#include "speex/speex.h"

#include <android/log.h>

#define  LOG_TAG    "Speex JNI :"
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)



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_trunkbow_speextest_Speex_open
//JNIEXPORT jint JNICALL Java_com_trunkbow_speextest_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_trunkbow_speextest_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];

    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值