您的位置:首页 > 移动开发 > IOS开发

开源语音格式speex教程(for iOS)

2012-11-06 12:03 405 查看

为实现类似微信上的对讲机功能,做了两个Demo。一种使用lib-amr库,用amr格式实现。另一个用的是speex库,这个提的人很多,但是出教程的不多,大家有很多不明所以的地方,新手对此较

为实现类似微信上的对讲机功能,做了两个Demo。一种使用lib-amr库,用amr格式实现。另一个用的是speex库,这个提的人很多,但是出教程的不多,大家有很多不明所以的地方,新手对此较为困惑。

网上晃了3天终于搞出个像样的Demo了,Demo中我将一个录好的.caf格式的PCM音频 编码成speex格式然后将speex格式的再转回PCM。当然Demo只是实现了音频的编码解码,并没有增加过多的功能。

本人测试了下,60秒的录音(8khz,单声道),转成最小格式的amr(大小为39k),和微信一分钟录音文件的大小差不多。speex格式最小(大小18k)但是比较模糊,可以接受的大小是30k,所以还是会比amr格式小点。另外speex库可以对声音进行降噪、增益以及静音判定等处理。但是考虑到 Android支持amr格式的音频,wp也支持,所以最后还是选了转成amr格式的方案,可惜apple现在已经不支持amr了,所以才需要转码。下面 我就和大家一起一步一步做Demo。

编译静态库:

这个着实让我费解了一天,其实linux下的C/C++程序员搞这个应该很轻松,但是我的那些都还给老师了,按照网上的教程愣是折腾了很久。

首先下载你所需要的源文件,因为speex是依赖ogg库的,所以先下载ogg库,这里我一并打包上传了,大家也可以去官网上下载。

源文件:



libogg-1.3.0.zip
(496 K)


speex-1.2rc1.tar
(3890 K)
编译是在命令行模式下进行的。打开终端,首先进入ogg所在的文件夹,我们先编译ogg。我用的是xcode4.4.1,大家根据自己xcode命令也需要略微调整,4.2的编译教程网上有,这里我就不重复了。

在终端键入以下内容:
1.ogg-i386的库

./configure-prefix=/Users/你的电脑用户名/Desktop/speexLibrary/libogg-1.3.0/i386 -host=i386-apple-darwin -build=x86_64-apple-darwin11.3.0 CC="/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -std=c99 -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/"

make

make install

make clean (一定要clean,不然后面编译的都是第一次的编译的内容)

2、ogg-armv6的库

./configure -prefix=/Users/你的电脑用户名/Desktop/speexLibrary/libogg-1.3.0/armv6 -host=armv6-apple-darwin -build=x86_64-apple-darwin11.3.0 CC="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -std=c99
-arch armv6 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/"

make

make install

make clean 

3、ogg-armv7的库

./configure -prefix=/Users/你的电脑用户名/Desktop/speexLibrary/libogg-1.3.0/armv7 -host=armv7-apple-darwin -build=x86_64-apple-darwin11.3.0 CC="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -std=c99
-arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/"

make

make install

make clean

接着编译speex库:

1.speex-i386的库

./configure -prefix=/Users/你的电脑用户名/Desktop/speexLibrary/speex-1.2rc1/i386 -host=i386-apple-darwin -disable-shared -enable-static -disable-oggtest -disable-fixed-point -enable-float-api -build=x86_64-apple-darwin11.3.0 -with-ogg=/Users/你的电脑用户名/Desktop/speexLibrary/libogg-1.3.0/i386
CC="/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -std=c99 -arch i386 -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/"

make

make install

make clean 

2.speex-armv6的库

./configure -prefix=/Users/你的电脑用户名/Desktop/speexLibrary/speex-1.2rc1/armv6 -host=armv6-apple-darwin -disable-shared -enable-static -disable-oggtest -enable-fixed-point -disable-float-api -build=x86_64-apple-darwin11.3.0 -with-ogg=/Users/你的电脑用户名/Desktop/speexLibrary/libogg-1.3.0/armv6
CC="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -std=c99 -arch armv6 -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/"

make

make install

make clean

3.speex-armv7的库

./configure -prefix=/Users/你的电脑用户名/Desktop/speexLibrary/speex-1.2rc1/armv7 -host=armv7-apple-darwin -disable-shared -enable-static -disable-oggtest -enable-fixed-point -disable-float-api -build=x86_64-apple-darwin11.3.0 -with-ogg=/Users/你的电脑用户名/Desktop/speexLibrary/libogg-1.3.0/armv7
CC="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -std=c99 -arch armv7 -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/"

make

make install

make clean 

最后合并静态库,进入speexLibrary文件夹,终端键入:

lipo -create i386/lib/libogg.a armv6/lib/libogg.a armv7/lib/libogg.a -output libogg.a

lipo -create i386/lib/libspeex.a armv6/lib/libspeex.a armv7/lib/libspeex.a -output libspeex.a

lipo -create i386/lib/libspeexdsp.a armv6/lib/libspeexdsp.a armv7/lib/libspeexdsp.a -output libspeexdsp.a 

现在应该已经编译完成了。如果合并静态库不成功,那么请检查上述步骤(我当时就是忘了make clean,导致编译的都是i386的.a文件)。你可以lipo -info xxx.a文件,后面会显示库内文件使用的平台信息,正常显示为i386,armv6,armv7那么就成功了。打包好的库一并上传了,方便大家下载。

打包好的speex库:



_speex.zip (821
K)

下面开始我们的工程:

新建一个工程,导入我们的静态库。我是将包直接拖到工程里面的,记得设置静态库的查找路径,我当时就是忘了设置路径,结果就是各种找不到头文件。导入完以后就进入主题了。

为了方便起见,我已经录好一个60秒的.caf文件。

详细的Demo解释我就不说明了,我这里讲一下步骤

1.我们需要从.caf文件中将文件头去掉得到纯的PCM数据

2.将纯的PCM数据编码成纯speex格式

3.给纯speex格式数据添加文件头

4.解码speex格式数据(带文件头的)成纯PCM格式

5.添加wav格式文件头

6.将wav数据写入文件导出caf文件(导出的文件可以在模拟下该工程的tmp文件夹内找到,一个caf文件,点击可以播放出声音).

下面是我给出的Demo,请结合speex官方的用户手册对照,用户手册上有编码解码的C语言示例代码,一定要仔细研究。

我的Demo:


TEST_Speex_001.zip
(1585 K)
 
 
 
 
 
社区原帖:http://www.cocoachina.com/bbs/read.php?tid=114755
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: