Opus是一个开放格式的有损声音编码的格式,并在其使用上没有任何专利或限制。还可以处理各种音频应用,包括IP语音、视频会议、游戏内聊天、流音乐、甚至远程现场音乐表演。它可以从低比特率窄带语音扩展到非常高清音频的立体声音乐。支持的功能包括:
6 kb/秒到510 kb/秒的比特率;单一频道最高256 kb/秒
采样率从8 kHz(窄带)到48 kHz(全频)
帧大小从2.5毫秒到60毫秒
支持恒定比特率(CBR)、受约束比特率(CVBR)和可变比特率(VBR)
支持语音(SILK层)和音乐(CELT层)的单独或混合模式
支持单声道和立体声;支持多达255个音轨(多数据流的帧)
可动态调节比特率,音频带宽和帧大小
良好的鲁棒性丢失率和数据包丢失隐藏(PLC)
浮点和定点实现
快捷获取so库传送门:https://download.csdn.net/download/fepengwang/18623320
https://download.csdn.net/download/fepengwang/18623164
解压下载好的opus-1.3.1.tar.gz
编写Android.mk文件到opus-1.3.1\
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#我使用的是NDK 18
#NDK 17及以上不再支持ABIs [mips64, armeabi, mips]
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
APP_CPPFLAGS += -std=c++11
APP_STL := gnustl_shared
APP_PLATFORM := android-16
include $(LOCAL_PATH)/celt_sources.mk
include $(LOCAL_PATH)/silk_sources.mk
include $(LOCAL_PATH)/opus_sources.mk
LOCAL_MODULE := opus
# Fixed point sources
SILK_SOURCES += $(SILK_SOURCES_FIXED)
# ARM build
CELT_SOURCES += $(CELT_SOURCES_ARM)
SILK_SOURCES += $(SILK_SOURCES_ARM)
LOCAL_SRC_FILES := \
$(CELT_SOURCES) $(SILK_SOURCES) $(OPUS_SOURCES) $(OPUS_SOURCES_FLOAT)
LOCAL_LDLIBS := -lm -llog
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/include \
$(LOCAL_PATH)/silk \
$(LOCAL_PATH)/silk/fixed \
$(LOCAL_PATH)/celt
LOCAL_CFLAGS := -DNULL=0 -DSOCKLEN_T=socklen_t -DLOCALE_NOT_USED -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64
LOCAL_CFLAGS += -Drestrict='' -D__EMX__ -DOPUS_BUILD -DFIXED_POINT -DUSE_ALLOCA -DHAVE_LRINT -DHAVE_LRINTF -O3 -fno-math-errno
LOCAL_CPPFLAGS := -DBSD=1
LOCAL_CPPFLAGS += -ffast-math -O3 -funroll-loops
include $(BUILD_SHARED_LIBRARY)
编译
$ cd opus-1.3.1
$ ndk-build APP_BUILD_SCRIPT=Android.mk NDK_PROJECT_PATH=.
注意:如果失败,看一下是否配置了ndk-build的环境变量
在libs中查看自己配置的平台是否完全产生,这里需要等2分钟左右哦!
转载请注明出处:https://blog.csdn.net/fepengwang/article/details/116714006#comments_16390157
拷贝文件至Android项目中
opus-1.3.1/include —> app\src\main\cpp
opus-1.3.1/libs/* —> app\src\main\jniLibs\
以下为JNI文件内容
#include
#include
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jlong JNICALL Java_com_wpf_opususedemo_utils_OpusUtils_createEncoder
(JNIEnv *env, jobject thiz, jint sampleRateInHz, jint channelConfig, jint complexity) {
int error;
OpusEncoder *pOpusEnc = opus_encoder_create(sampleRateInHz, channelConfig,
OPUS_APPLICATION_RESTRICTED_LOWDELAY,
&error);
if (pOpusEnc) {
opus_encoder_ctl(pOpusEnc, OPUS_SET_VBR(0));//0:CBR, 1:VBR
opus_encoder_ctl(pOpusEnc, OPUS_SET_VBR_CONSTRAINT(true));
opus_encoder_ctl(pOpusEnc, OPUS_SET_BITRATE(32000));
opus_encoder_ctl(pOpusEnc, OPUS_SET_COMPLEXITY(complexity));//8 0~10
opus_encoder_ctl(pOpusEnc, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
opus_encoder_ctl(pOpusEnc, OPUS_SET_LSB_DEPTH(16));
opus_encoder_ctl(pOpusEnc, OPUS_SET_DTX(0));
opus_encoder_ctl(pOpusEnc, OPUS_SET_INBAND_FEC(0));
opus_encoder_ctl(pOpusEnc, OPUS_SET_PACKET_LOSS_PERC(0));
}
return (jlong) pOpusEnc;
}
JNIEXPORT jlong JNICALL Java_com_wpf_opususedemo_utils_OpusUtils_createDecoder
(JNIEnv *env, jobject thiz, jint sampleRateInHz, jint channelConfig) {
int error;
OpusDecoder *pOpusDec = opus_decoder_create(sampleRateInHz, channelConfig, &error);
return (jlong) pOpusDec;
}
JNIEXPORT jint JNICALL Java_com_wpf_opususedemo_utils_OpusUtils_encode
(JNIEnv *env, jobject thiz, jlong pOpusEnc, jshortArray samples, jint offset,
jbyteArray bytes) {
OpusEncoder *pEnc = (OpusEncoder *) pOpusEnc;
if (!pEnc || !samples || !bytes)
return 0;
jshort *pSamples = env->GetShortArrayElements(samples, 0);
jsize nSampleSize = env->GetArrayLength(samples);
jbyte *pBytes = env->GetByteArrayElements(bytes, 0);
jsize nByteSize = env->GetArrayLength(bytes);
if (nSampleSize - offset < 320 || nByteSize <= 0)
return 0;
int nRet = opus_encode(pEnc, pSamples + offset, nSampleSize, (unsigned char *) pBytes,
nByteSize);
env->ReleaseShortArrayElements(samples, pSamples, 0);
env->ReleaseByteArrayElements(bytes, pBytes, 0);
return nRet;
}
JNIEXPORT jint JNICALL Java_com_wpf_opususedemo_utils_OpusUtils_decode
(JNIEnv *env, jobject thiz, jlong pOpusDec, jbyteArray bytes,
jshortArray samples) {
OpusDecoder *pDec = (OpusDecoder *) pOpusDec;
if (!pDec || !samples || !bytes)
return 0;
jshort *pSamples = env->GetShortArrayElements(samples, 0);
jbyte *pBytes = env->GetByteArrayElements(bytes, 0);
jsize nByteSize = env->GetArrayLength(bytes);
jsize nShortSize = env->GetArrayLength(samples);
if (nByteSize <= 0 || nShortSize <= 0) {
return -1;
}
int nRet = opus_decode(pDec, (unsigned char *) pBytes, nByteSize, pSamples, nShortSize, 0);
env->ReleaseShortArrayElements(samples, pSamples, 0);
env->ReleaseByteArrayElements(bytes, pBytes, 0);
return nRet;
}
JNIEXPORT void JNICALL Java_com_wpf_opususedemo_utils_OpusUtils_destroyEncoder
(JNIEnv *env, jobject thiz, jlong pOpusEnc) {
OpusEncoder *pEnc = (OpusEncoder *) pOpusEnc;
if (!pEnc)
return;
opus_encoder_destroy(pEnc);
}
JNIEXPORT void JNICALL Java_com_wpf_opususedemo_utils_OpusUtils_destroyDecoder
(JNIEnv *env, jobject thiz, jlong pOpusDec) {
OpusDecoder *pDec = (OpusDecoder *) pOpusDec;
if (!pDec)
return;
opus_decoder_destroy(pDec);
}
#ifdef __cplusplus
}
#endif
注意注意
CMakeLists.txt内容的编写!!!受自己本地NDK开发环境的影响,3.4.3版本中必须添加${CMAKE_CURRENT_SOURCE_DIR}否则会报错
这里记录一下我的踩坑:missing and no known rule to make it
解决方式:
CMakeLists.txt中配置的路径都是默认相对于CMakeLists.txt,但是在路径前还是要添加CMakeLists.txt的相对位置标识符${CMAKE_CURRENT_SOURCE_DIR}
CMakeList.tex内容:
cmake_minimum_required(VERSION 3.10.2)
#set(libs_DIR src/main/jniLibs/${ANDROID_ABI}/libopus.so)
set(libs_include_DIR src/main/cpp/opus/include)
#支持-std=gnu++11
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
find_library( # Sets the name of the path variable.
log-lib
log )
add_library( # Sets the name of the library.
opus-use
SHARED
${
CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/opus-use.cpp)
#关联头文件目录到库名
target_include_directories(
opus-use # 库名
PRIVATE # 库属性
${
CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/opus/include # 头文件目录
)
#[[
每个库要单独添加一次
add_library(第三方库名称 共享动态库 导入)
set_target_properties(
第三方库名称
配置导入库
具体第三方库路径
)
]]
add_library(opus_my SHARED IMPORTED)
set_target_properties(
opus_my
PROPERTIES IMPORTED_LOCATION
${
CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${
ANDROID_ABI}/libopus.so
)
target_link_libraries( # Specifies the target library.
opus-use
${
log-lib}
opus_my)
完整版代码链接:
https://download.csdn.net/download/fepengwang/18631244