Reading a .wav file using libsndfile in C

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 07:25:09

问题


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.


回答1:


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 (for me it is located at C:\Program Files (x86)\Mega-Nerd\libsndfile\include)

libsndfile-1.lib (for me C:\Program Files (x86)\Mega-Nerd\libsndfile\lib)

libsndfile-1.dll (C:\Program Files (x86)\Mega-Nerd\libsndfile\bin)

Then you right click on your project and go to Build options... > Search directories > Compiler and add the address of sndfile.h directory.

Then, you go to Build options... >Linker settings > Link libraries: and add the address of libsndfile-1.lib.

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

Output is:

Sample Rate = 44100 Hz



来源:https://stackoverflow.com/questions/38283203/reading-a-wav-file-using-libsndfile-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!