问题描述
我想用C读取.wav文件,类似于Matlab的wavread命令.我遇到了这个库 http://www.mega-nerd.com/libsndfile/似乎是解决方案.但是有人可以解释如何安装该库,以便我可以使用它的功能吗? (我之前从未做过,所以请帮忙).我尝试包含sndfile.h,但弹出诸如cannot find -lsndfile-1.lib
之类的错误.我相信这是因为我没有正确集成库.
I want to read a .wav file in C similar to what Matlab's wavread command does. I came across this library http://www.mega-nerd.com/libsndfile/ that seems to be the solution. But can someone explain how to install this library so that I may use its functions? (I've never done that before so please help). I tried including the sndfile.h but errors like cannot find -lsndfile-1.lib
is popping up. I believe it is because I'm not integrating the library properly.
推荐答案
第一件事是安装库(我选择libsndfile-1.0.28-w32-setup.exe
是因为我使用预先安装的MinGW codeblocks-17.12mingw-setup.exe
运行code :: blocks,我认为它默认情况下具有32位编译器),然后找到以下三个文件:
The first thing is to install the library (I chose libsndfile-1.0.28-w32-setup.exe
because I run code::blocks with the pre-installed MinGW codeblocks-17.12mingw-setup.exe
and I think it has 32bit compiler by default) and locate these three files:
libsndfile-1.lib
(对我来说是C:\Program Files (x86)\Mega-Nerd\libsndfile\lib
)
libsndfile-1.dll
(C:\Program Files (x86)\Mega-Nerd\libsndfile\bin
)
然后右键单击您的项目,然后转到构建选项...>搜索目录>编译器,然后添加sndfile.h
目录的地址.
Then you right click on your project and go to Build options... > Search directories > Compiler and add the address of sndfile.h
directory.
然后,转到构建选项...>链接器设置>链接库:并添加libsndfile-1.lib
的地址.
Then, you go to Build options... >Linker settings > Link libraries: and add the address of libsndfile-1.lib
.
最后,将libsndfile-1.dll
复制到将创建.exe
文件的位置(对我来说,它位于MyProject\bin\Debug
中).
Finally, you copy the libsndfile-1.dll
next to where the .exe
file will be created (for me it's in MyProject\bin\Debug
).
这是一个简单的示例代码:
Here is a simple example code:
#include <stdio.h>
#include <stdlib.h>
#include "sndfile.h"
int main(void)
{
char *inFileName;
SNDFILE *inFile;
SF_INFO inFileInfo;
int fs;
inFileName = "noise.wav";
inFile = sf_open(inFileName, SFM_READ, &inFileInfo);
sf_close(inFile);
fs = inFileInfo.samplerate;
printf("Sample Rate = %d Hz\n", fs);
return 0;
}
输出为:
这篇关于在C中使用libsndfile读取.wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!