java libmp3lame_录制MP3格式的音频( lame 库的编译及使用)

众多周知,mp3 是跨平台性最好的音频格式,由于采用了压缩率更高的有损压缩算法,文件大小是大约每分钟1M,使其在网络中传输更快,占用存储空间也更少;与此同时,它的声音质量也不错,尤其是人声(相声、评书、脱口秀),当然追求无损音乐的除外。

Android 中没有提供录制 mp3 的 API,需要使用开源库 lame,lame 是专门用于编码 mp3 的轻量高效的 c 代码库。由于采用 c 语言编写,故需要用到 jni。

下载lame库

源码导入

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

d979ce51d804

images.png

修改库文件

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

util.h 文件,把 570 行的两处 ieee754_float32_t 改为 float 因为Android下并不支持该类型

set_get.h 文件,把头部的 #include 改为 #include "lame.h"

fft.c 文件,删除第47行 #include "vector/lame_intrin.h"

id3tag.c和machine.h两个文件里,將HAVE_STRCHR和HAVE_MEMCPY的ifdef结构体删除或者注释

#ifdef STDC_HEADERS

# include

# include

#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

#endif

+#include

+#include

#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

#include

+#include

#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

#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

# include

#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

+

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你要使用libmp3lame.lib静态,你需要在你的工程中添加libmp3lame.lib文件的链接。具体的步骤如下: 1. 在你的工程中添加libmp3lame.lib文件。 2. 在你的代码中包含Lame的头文件,例如lame.h。 3. 在你的代码中调用Lame提供的函数进行编码或解码。 4. 在你的编译器中设置Lame的头文件和文件的路径,以便编译器能够正确地找到它们。 下面是一个C++的例子程序,演示了如何使用Lame进行音频编码: ```c++ #include <stdio.h> #include <lame.h> int main(int argc, char **argv) { FILE *pcm = fopen("input.pcm", "rb"); FILE *mp3 = fopen("output.mp3", "wb"); const int PCM_SIZE = 8192; const int MP3_SIZE = 8192; short pcm_buffer[PCM_SIZE * 2]; unsigned char mp3_buffer[MP3_SIZE]; lame_t lame = lame_init(); lame_set_num_channels(lame, 2); lame_set_in_samplerate(lame, 44100); lame_set_out_samplerate(lame, 44100); lame_set_brate(lame, 128); lame_init_params(lame); int read, write; do { read = fread(pcm_buffer, 2 * sizeof(short), PCM_SIZE, pcm); if (read == 0) write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE); else write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE); fwrite(mp3_buffer, write, 1, mp3); } while (read != 0); lame_close(lame); fclose(mp3); fclose(pcm); return 0; } ``` 需要注意的是,上面的代码只是一个示例,你需要根据你的实际需求进行修改。同时,你需要根据你的编译环境和应用程序的需求进行设置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值