Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

mingw - Reading a .wav file using libsndfile in C

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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-Nerdlibsndfileinclude)

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

libsndfile-1.dll (C:Program Files (x86)Mega-Nerdlibsndfilein)

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

compiler directory

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

link lib

Finally, you copy the libsndfile-1.dll next to where the .exe file will be created (for me it's in MyProjectinDebug).

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
", fs);

  return 0;
}

Output is:

Sample Rate = 44100 Hz


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...