0
点赞
收藏
分享

微信扫一扫

Windows下编译libsndfile音频库

沐之轻语 2022-08-17 阅读 181


libsndfile是一个C语言开发的音频文件读写库,开发源代码。过去libsndfile因为专利原因不支持mp3,但是mp3专利已经过期,最新源代码已经支持mp3了。
官网:​​​http://www.mega-nerd.com/libsndfile​​​,里面列举了目前支持的音频格式,mp3尚未添加进去。
github:​​​https://github.com/libsndfile/libsndfile ​​

一.使用vcpkg安装依赖库

首先是编译器,我的编译器是2017,如果使用VS2017的话需要先把英文语言包装上(Visual Studio Installer-->修改-->语言包),这个很重要,否则在使用vcpkg时会报错。 vcpkg是windows下的c++包管理器,使用vcpkg可以极大地简化三方库的获取、编译和安装流程,而且并不依赖于Windows注册表或Visual Studio。比如说安装一个mysql客户端,只需要执行vcpkg install libmysql,而不用单独去下载mysql源码编译和安装,节省了大量时间。 vcpkg的github链接:​​https://github.com/microsoft/vcpkg​​ 我下载的是vcpkg最新release:vcpkg-2021.05.12.zip,解压到E盘根目录。
打开vs2017命令提示符,然后:

cd E:\vcpkg-2021.05.12
.\bootstrap-vcpkg.bat

 此时会在该目录生成一个vcpkg.exe
参考​​​https://github.com/libsndfile/libsndfile​​中的README.md,执行:

vcpkg install libogg:x86-windows-static
vcpkg install libvorbis:x86-windows-static
vcpkg install libflac:x86-windows-static
vcpkg install opus:x86-windows-static
vcpkg install mp3lame:x86-windows-static
vcpkg install mpg123:x86-windows-static

其中,mp3lame用于mp3编码,mpg123用于mp3解码。如果要安装64位的话,就把x86改成x64。这一步一般不会报错,如果提示下载失败,尝试科学上网,再试试。
安装成功的静态库在这里:E:\vcpkg-2021.05.12\installed\x86-windows-static\debug\lib

二.编译

环境:CMake 3.1.3或以上,VS2015或以上
clone libsndfile到E盘根目录,然后:

cd E:\libsndfile
mkdir CMakeBuild
cd CMakeBuild

让配置和编译时的临时文件都存到CMakeBuild文件夹,接着:

cmake .. -DCMAKE_TOOLCHAIN_FILE=E:/vcpkg-2021.05.12/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x86-windows-static

成功了如下图所示,红框中提示已支持mp3;蓝框中提示不支持的项,因为未安装相关第三方库,如果需要用到这些功能,可以自己安装。如果失败了,删掉CMakeBuild文件夹中的文件,重新再来一遍。

Windows下编译libsndfile音频库_libsndfile


注意:对于CMAKE项目来说,如果要通过find_package()使用安装后的三方库,必须指定CMAKE_TOOLCHAIN_FILE参数

然后,开始编译,执行:

cmake --build .

编译结束,如下图所示,有两个关于sqlite的错误,本人不关心,所以,编译成功

Windows下编译libsndfile音频库_音频库_02

当然也可以打开E:\libsndfile\CMakeBuild\libsndfile.sln,用IDE编译
编译成功的静态库位于:E:\libsndfile\CMakeBuild\Debug
可以看到使用默认步骤安装的第三方库和编译的libsndfile.lib都是debug版本。下面是编译release版本要注意的地方:

1.在E:\vcpkg-2021.05.12\triplets\community中新加一个triplets:x86-windows-static-release.cmake(复制x86-windows-static.cmake并改成自己满意的名字),打开并在最后添加set(VCPKG_BUILD_TYPE release)

Windows下编译libsndfile音频库_qt_03

Windows下编译libsndfile音频库_音频库_04

2.修改安装指令

vcpkg install libogg:x86-windows-static-release
vcpkg install libvorbis:x86-windows-static-release
vcpkg install libflac:x86-windows-static-release
vcpkg install opus:x86-windows-static-release
vcpkg install mp3lame:x86-windows-static-release
vcpkg install mpg123:x86-windows-static-release

 3.修改cmake参数

cmake .. -DCMAKE_TOOLCHAIN_FILE=E:/vcpkg-2021.05.12/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x86-windows-static-release -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=RELEASE

 4.最后执行

nmake

生成的sndfile.lib在E:\libsndfile\CMakeBuild目录中。

三.测试

IDE用的是Qt Creator,在pro文件中添加:

INCLUDEPATH += E:\libsndfile\include \
E:\libsndfile\CMakeBuild\include

CONFIG(debug, debug|release){
LIBS += -LE:\libsndfile\CMakeBuild\Debug -lsndfile \
-LE:\vcpkg-2021.05.12\installed\x86-windows-static\debug\lib -lvorbisenc -lvorbis -lFLAC -logg -lopus -llibmpg123 -llibmp3lame-static -llibmpghip-static
} else {
LIBS += -LE:\libsndfile\CMakeBuild -lsndfile \
-LE:\vcpkg-2021.05.12\installed\x86-windows-static-release\lib -lvorbisenc -lvorbis -lFLAC -logg -lopus -llibmpg123 -llibmp3lame-static -llibmpghip-static
}

两个include目录中分别包括sndfile.hh和sndfile.h。如果使用C++的话,可以用作者封装的SndfileHandle类(sndfile.hh头文件)来完成大部分操作。

#include "sndfile.hh"
#include <iostream>
int main()
{
SndfileHandle snd(R"(C:\Users\Administrator\Desktop\NorwayForest-500.mp3)");

// 获取文件信息.
std::cout << "frame length:" << snd.frames() << std::endl;
std::cout << "channels:" << snd.channels() << std::endl;
std::cout << "samplerate:" << snd.samplerate() << std::endl;
// 音频格式采用MajorType+SubType的形式.
if(snd.format()==SF_FORMAT_MPEG|SF_FORMAT_MPEG_LAYER_III)
{
std::cout << "format:mp3" << std::endl;
}

// 读取PCM全部数据.
// pcm-16每个通道每帧为一个short的长度.
long long buffer_len = snd.frames()*snd.channels();
short *buffer = new short[buffer_len];
int read_count = 0;
if(0)
{
// 可以一次全部读取.
read_count = snd.read(buffer, buffer_len);
}
else
{
// 也可以分批读取.
while (read_count < buffer_len)
{
const int read_limit = (buffer_len - read_count) > 1024
? 1024: (buffer_len - read_count);
const int read_len = snd.read(buffer+read_count, read_limit);
read_count += read_len;
}
}
std::cout << "read count(short):" << read_count << std::endl;

system("pause");
}

运行结果:

Windows下编译libsndfile音频库_音频库_05

举报

相关推荐

0 条评论