在Android上使用Speex(上)

最近项目上用到语音编解码、降噪相关处理,通过查找资料发现需要用到Speex这个免费的开源库。发现要把这个库放到android里面用,还是要花一点时间与心思,在此做个总结,有需要的小伙伴可以做个参考。

一、speex简介

Speex是一种开放源代码/免费软件,专为语音设计的无专利音频压缩格式。 Speex项目旨在通过免费提供昂贵专有语音编解码器的替代方案来降低语音应用程序的进入壁垒。 而且,Speex非常适合Internet应用程序,并提供了大多数其他编解码器中没有的有用功能。 最后,Speex是GNU项目的一部分,可以在经过修订的BSD许可下使用。

Speex当前源代码分为两部分:

speex:这部分主要是针对音频的编解码,这部分现在看官网上说是被opus取代了;speexdsp:这部分主要针对是音频信号处理,比如:降噪,回声消除等。

当前最新版本是Speex 1.2.0和SpeexDSP 1.2rc3

官网地址:https://www.speex.org/

二、集成Speex 1.2.0

1、把从官网上面下载下来的Speex 1.2.0压缩文件解压后,里面include、libspeex、doc目录比较重要:其中include目录里面包含了相关头文件,libspeex目录是codec的实现,这两个是我们必需的,doc目录里面只是包含相关说明文档,有需要可以参考。我们把include、libspeex这两个目录拷贝到我们工程中cpp目录下面,如下所示:


2、打开刚拷贝到工程中这两个目录,把里面的Makefile文件全删掉。再把speex_config_types.h.in重命令为speex_config_types.h,同时修改它的内容为如下所示:

#ifndef__SPEEX_TYPES_H__

#define__SPEEX_TYPES_H__

#include <stdint.h>

typedef int16_t  spx_int16_t;

typedef uint16_t spx_uint16_t;

typedef int32_t spx_int32_t;

typedef uint32_t spx_uint32_t;

#endif

三、编译配置

打开工程目录中CMakeLists.txt文件,再输入下面几行内容:

# For more information about using CMake with Android Studio, read the

# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)

# Declares and names the project.

project("myapplication")

#设置语言和标准

enable_language(C)

set(CMAKE_C_STANDARD_REQUIRED ON)

set(CMAKE_C_STANDARD 99)

#定义宏

add_definitions(-DEXPORT= -DFLOATING_POINT)

#包含头文件

include_directories(include)

#设置源码目录变量

aux_source_directory(libspeex/ LIB_SPEEX_DIR)

# Creates and names a library, sets it as either STATIC

# or SHARED, and provides the relative paths to its source code.

# You can define multiple libraries, and CMake builds them for you.

# Gradle automatically packages shared libraries with your APK.

add_library(# Specifies the name of the library.

        Speex

        # Sets the library as a shared library.

        SHARED

        # Provides a relative path to your source file(s).

        ${LIB_SPEEX_DIR})

四、生成SO库

1、先点击Android Studio菜单栏中Build中的Refresh Linked C++ Projects

2、再点击Rebuild Project即可生成相应so库。

在编译过程中可能会出现如下错误:


出现这个错误是重复定义了main方法,根据错误提示,打开这几个文件,发现只是一些测试例子,所以可以直接删掉。不过由于缓存的原因,在重新编译之前,请先把.cxx/cmake/debug/下面几个目录给删除掉。

最后再点击“Rebuild Project”,编译成功,查看是否有相应的libSpeex.so库文件,如下:

最后码字不容易,如果帮到您,您的赞赏就是我继续码字的动力!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容