在C中使用libsndfile读取.wav文件 [英] Reading a .wav file using libsndfile in C

查看:541
本文介绍了在C中使用libsndfile读取.wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用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.libis 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:

sndfile.h(对我来说,它位于C:\Program Files (x86)\Mega-Nerd\libsndfile\include)

sndfile.h (for me it is located at C:\Program Files (x86)\Mega-Nerd\libsndfile\include)

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;
}

输出为:

Sample Rate = 44100 Hz

这篇关于在C中使用libsndfile读取.wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆