Android JNI:lame库的编译及使用

1.下载lame库

lame库下载地址(以下使用的是lame v3.100)

2.源码导入

解压下载的lame库,把libmp3lame文件夹下后缀为.c .h的文件(不包括子文件夹i386和vector下的)复制到cpp/lame文件夹内,同时把include目录下的lame.h也复制到cpp/lame文件夹内,此时 lame文件夹内包含42个文件。

3.修改库文件

打开刚刚拷贝的lame库文件,修改:

  1. util.h 文件,把 570 行的两处 ieee754_float32_t 改为 float 因为Android下并不支持该类型
  2. set_get.h 文件,把头部的 #include <lame.h> 改为 #include "lame.h"
  3. fft.c 文件,删除第47行 #include "vector/lame_intrin.h"
  4. id3tag.c和machine.h两个文件里,將HAVE_STRCHR和HAVE_MEMCPY的ifdef结构体删除或者注释
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#else
/*# ifndef HAVE_STRCHR
#  define strchr index
#  define strrchr rindex
# endif*/
char   *strchr(), *strrchr();
/*# ifndef HAVE_MEMCPY
#  define memcpy(d, s, n) bcopy ((s), (d), (n))
#  define memmove(d, s, n) bcopy ((s), (d), (n))
# endif*/
#endif

可参考以下完整修改文件

diff --git a/VbrTag.c b/VbrTag.c
index 5800a44..36ee7b6 100644
--- a/VbrTag.c
+++ b/VbrTag.c
@@ -26,6 +26,8 @@
 # include <config.h>
 #endif
 
+#include <stdlib.h>
+#include <string.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"
diff --git a/bitstream.c b/bitstream.c
index aa35915..a2fe294 100644
--- a/bitstream.c
+++ b/bitstream.c
@@ -29,6 +29,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "lame.h"
 #include "machine.h"
diff --git a/encoder.c b/encoder.c
index 48f46c7..437067f 100644
--- a/encoder.c
+++ b/encoder.c
@@ -30,6 +30,7 @@
 #endif
 
 
+#include <string.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"
diff --git a/fft.c b/fft.c
index 4eea1ad..27febdb 100644
--- a/fft.c
+++ b/fft.c
@@ -44,7 +44,7 @@
 #include "util.h"
--- a/fft.c
+++ b/fft.c
@@ -44,7 +44,7 @@
 #include "util.h"
 #include "fft.h"
 
-#include "vector/lame_intrin.h"
+//#include "vector/lame_intrin.h"
 
 
 
diff --git a/id3tag.c b/id3tag.c
index ac48510..8f148b8 100644
--- a/id3tag.c
+++ b/id3tag.c
@@ -41,17 +41,20 @@
 # include <string.h>
 # include <ctype.h>
 #else
-# ifndef HAVE_STRCHR
-#  define strchr index
-#  define strrchr rindex
-# endif
+//# ifndef HAVE_STRCHR
+//#  define strchr index
+//#  define strrchr rindex
+//# endif
 char   *strchr(), *strrchr();
-# ifndef HAVE_MEMCPY
-#  define memcpy(d, s, n) bcopy ((s), (d), (n))
-# endif
+//# ifndef HAVE_MEMCPY
+//#  define memcpy(d, s, n) bcopy ((s), (d), (n))
+//# endif
 #endif
 
 
+#include <malloc.h>
+#include <string.h>
+#include <stdlib.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"
diff --git a/lame.c b/lame.c
index cb82225..299fd56 100644
--- a/lame.c
+++ b/lame.c
@@ -31,6 +31,8 @@
 #endif
 
 
+#include <malloc.h>
+#include <string.h>
 #include "lame.h"
 #include "machine.h"
 
diff --git a/machine.h b/machine.h
index bf6fff2..c675c20 100644
--- a/machine.h
+++ b/machine.h
@@ -31,15 +31,15 @@
 # include <stdlib.h>
 # include <string.h>
 #else
-# ifndef HAVE_STRCHR
-#  define strchr index
-#  define strrchr rindex
-# endif
+//# ifndef HAVE_STRCHR
+//#  define strchr index
+//#  define strrchr rindex
+//# endif
 char   *strchr(), *strrchr();
-# ifndef HAVE_MEMCPY
-#  define memcpy(d, s, n) bcopy ((s), (d), (n))
-#  define memmove(d, s, n) bcopy ((s), (d), (n))
-# endif
+//# ifndef HAVE_MEMCPY
+//#  define memcpy(d, s, n) bcopy ((s), (d), (n))
+//#  define memmove(d, s, n) bcopy ((s), (d), (n))
+//# endif
 #endif
 
 #if  defined(__riscos__)  &&  defined(FPA10)
diff --git a/newmdct.c b/newmdct.c
index 596cac9..ac98abd 100644
--- a/newmdct.c
+++ b/newmdct.c
@@ -30,6 +30,7 @@
 # include <config.h>
 #endif
 
+#include <string.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"
diff --git a/psymodel.c b/psymodel.c
index 60076ee..1393c2a 100644
--- a/psymodel.c
+++ b/psymodel.c
@@ -145,7 +145,8 @@ blocktype_d[2]        block type to use for previous granule
 #endif
 
 #include <float.h>
-
+#include <stdlib.h>
+#include <string.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"
diff --git a/quantize.c b/quantize.c
index 9ba9c16..2906c00 100644
--- a/quantize.c
+++ b/quantize.c
@@ -28,6 +28,8 @@
 # include <config.h>
 #endif
 
+#include <stdlib.h>
+#include <string.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"
diff --git a/quantize_pvt.c b/quantize_pvt.c
:
 #endif
 
 #include <float.h>
-
+#include <stdlib.h>
+#include <string.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"
diff --git a/quantize.c b/quantize.c
index 9ba9c16..2906c00 100644
--- a/quantize.c
+++ b/quantize.c
@@ -28,6 +28,8 @@
 # include <config.h>
 #endif
 
+#include <stdlib.h>
+#include <string.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"
diff --git a/quantize_pvt.c b/quantize_pvt.c
index d8d6447..3cd9966 100644
--- a/quantize_pvt.c
+++ b/quantize_pvt.c
@@ -36,6 +36,7 @@
 #include "reservoir.h"
 #include "lame-analysis.h"
 #include <float.h>
+#include <string.h>
 
 
 #define NSATHSCALE 100  /* Assuming dynamic range=96dB, this value should be 92 */
diff --git a/set_get.h b/set_get.h
index 37e4bcd..99ab73c 100644
--- a/set_get.h
+++ b/set_get.h
@@ -21,7 +21,7 @@
 #ifndef __SET_GET_H__
 #define __SET_GET_H__
 
-#include <lame.h>
+#include "lame.h"
 
 #if defined(__cplusplus)
 extern  "C" {
diff --git a/takehiro.c b/takehiro.c
index 67aba1b..ca02f98 100644
--- a/takehiro.c
+++ b/takehiro.c
@@ -27,6 +27,7 @@
 #endif
 
 
+#include <string.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"
diff --git a/util.c b/util.c
index 43b457c..e9255fe 100644
--- a/util.c
+++ b/util.c
@@ -27,6 +27,7 @@
 #endif
 
 #include <float.h>
+#include <malloc.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"
diff --git a/util.h b/util.h
index 13f0cd4..b6bf306 100644
--- a/util.h
+++ b/util.h
@@ -567,7 +567,7 @@ extern  "C" {
 
 /* log/log10 approximations */
     extern void init_log_table(void);
-    extern ieee754_float32_t fast_log2(ieee754_float32_t x);
+    extern float fast_log2(float x);
 
     int     isResamplingNecessary(SessionConfig_t const* cfg);
 
diff --git a/vbrquantize.c b/vbrquantize.c
index 0f703b7..60834d3 100644
--- a/vbrquantize.c
+++ b/vbrquantize.c
@@ -27,6 +27,8 @@
 #endif
 
 
+#include <stdlib.h>
+#include <string.h>
 #include "lame.h"
 #include "machine.h"
 #include "encoder.h"

4.编写CmakeList.txt

cmake_minimum_required(VERSION 3.6.0)
set(CURRENT_DIR ${CMAKE_SOURCE_DIR})
message("CURRENT_DIR:" ${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_SOURCE_DIR}src/main/cpp/lame)

set(LAME_DIR src/main/cpp/lame)
message("LAME_DIR:" ${LAME_DIR})

aux_source_directory(src/main/cpp/lame SRC_LIST)

add_library(mp3lame
        SHARED
        src/main/cpp/MP3Recorder.c
        ${SRC_LIST})

#add_library(mp3lame
#        SHARED
#        src/main/cpp/MP3Recorder.c
#        src/main/cpp/lame/bitstream.c
#        src/main/cpp/lame/fft.c
#        src/main/cpp/lame/id3tag.c
#        src/main/cpp/lame/mpglib_interface.c
#        src/main/cpp/lame/presets.c
#        src/main/cpp/lame/quantize.c
#        src/main/cpp/lame/reservoir.c
#        src/main/cpp/lame/tables.c
#        src/main/cpp/lame/util.c
#        src/main/cpp/lame/VbrTag.c
#        src/main/cpp/lame/encoder.c
#        src/main/cpp/lame/gain_analysis.c
#        src/main/cpp/lame/lame.c
#        src/main/cpp/lame/newmdct.c
#        src/main/cpp/lame/psymodel.c
#        src/main/cpp/lame/quantize_pvt.c
#        src/main/cpp/lame/set_get.c
#        src/main/cpp/lame/takehiro.c
#        src/main/cpp/lame/vbrquantize.c
#        src/main/cpp/lame/version.c)


find_library( # Sets the name of the path variable.
        log-lib
        log)

target_link_libraries(mp3lame
        ${log-lib})

5.编写 java 类和 c 文件

public class MP3Recorder {
    static {
     System.loadLibrary("mp3lame");   
  }
    /**
     * 初始化 lame编码器
     *
     * @param inSampleRate
     *              输入采样率
     * @param outChannel
     *              声道数
     * @param outSampleRate
     *              输出采样率
     * @param outBitrate
     *              比特率(kbps)
     * @param quality
     *              0~9,0最好
     */
    public static native void init(int inSampleRate, int outChannel, int outSampleRate, int outBitrate, int quality);
    
    /**
     *  编码,把 AudioRecord 录制的 PCM 数据转换成 mp3 格式
     *
     * @param buffer_l
     *          左声道输入数据
     * @param buffer_r
     *          右声道输入数据
     * @param samples
     *          输入数据的size
     * @param mp3buf
     *          输出数据
     * @return
     *          输出到mp3buf的byte数量
     */
    public static native int encode(short[] buffer_l, short[] buffer_r, int samples, byte[] mp3buf);
    
    /**
     *  刷写
     *
     * @param mp3buf
     *          mp3数据缓存区
     * @return
     *          返回刷写的数量
     */
    public static native int flush(byte[] mp3buf);
    
    /**
     * 关闭 lame 编码器,释放资源
     */
    public static native void close();
}

6.编写MP3Recorder.c

#include "lame/lame.h"
#include "MP3Recorder.h"

static lame_global_flags *glf = NULL;
/*
 * Class:     com_android_liblame_MP3Recorder
 * Method:    init
 * Signature: (IIIII)V
 */

JNIEXPORT void JNICALL Java_com_android_liblame_MP3Recorder_init
        (JNIEnv *env, jclass instance, jint inSamplerate, jint outChannel, jint outSamplerate,
         jint outBitrate, jint quality) {
    if (glf != NULL) {
        lame_close(glf);
        glf = NULL;
    }
    glf = lame_init();
    lame_set_in_samplerate(glf, inSamplerate);
    lame_set_num_channels(glf, outChannel);
    lame_set_out_samplerate(glf, outSamplerate);
    lame_set_brate(glf, outBitrate);
    lame_set_quality(glf, quality);
    lame_init_params(glf);

}

/*
 * Class:     com_android_liblame_MP3Recorder
 * Method:    encode
 * Signature: ([S[SI[B)I
 */
JNIEXPORT jint JNICALL Java_com_android_liblame_MP3Recorder_encode
        (JNIEnv *env, jclass instance, jshortArray buffer_l, jshortArray buffer_r, jint samples,
         jbyteArray mp3buf) {
    jshort *j_buffer_l = (*env)->GetShortArrayElements(env, buffer_l, NULL);

    jshort *j_buffer_r = (*env)->GetShortArrayElements(env, buffer_r, NULL);

    const jsize mp3buf_size = (*env)->GetArrayLength(env, mp3buf);
    jbyte *j_mp3buf = (*env)->GetByteArrayElements(env, mp3buf, NULL);

    int result = lame_encode_buffer(glf, j_buffer_l, j_buffer_r,
                                    samples, j_mp3buf, mp3buf_size);

    (*env)->ReleaseShortArrayElements(env, buffer_l, j_buffer_l, 0);
    (*env)->ReleaseShortArrayElements(env, buffer_r, j_buffer_r, 0);
    (*env)->ReleaseByteArrayElements(env, mp3buf, j_mp3buf, 0);

    return result;
}

/*
 * Class:     com_android_liblame_MP3Recorder
 * Method:    flush
 * Signature: ([B)I
 */
JNIEXPORT jint JNICALL Java_com_android_liblame_MP3Recorder_flush
        (JNIEnv *env, jclass instance, jbyteArray mp3buf) {
    const jsize mp3buf_size = (*env)->GetArrayLength(env, mp3buf);
    jbyte *j_mp3buf = (*env)->GetByteArrayElements(env, mp3buf, NULL);

    int result = lame_encode_flush(glf, j_mp3buf, mp3buf_size);

    (*env)->ReleaseByteArrayElements(env, mp3buf, j_mp3buf, 0);

    return result;
}

/*
 * Class:     com_android_liblame_MP3Recorder
 * Method:    close
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_android_liblame_MP3Recorder_close
        (JNIEnv *env, jclass instance) {
    lame_close(glf);
    glf = NULL;

}

7.配置build.gradle

android {
    ....
    ...
    ..
    .
   //*
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

}

 

原文地址:

https://www.jianshu.com/p/d979ce51d804

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值