Reading a .wav file using libsndfile in C

2019-01-20 15:56发布

问题:

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