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.
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 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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…