I am not well versed when it comes to the unix terminal, and the notion of building libraries from source gives me the heebie jeebies. If you're like me, the next few paragraphs might be of help.
To obtain the library, i used macports, but I have it on good authority that homebrew is a better choice for managing your code libraries. Assuming you've installed macports, you should enter the following commands into a terminal window.
> sudo port - d selfupdate
> sudo port install libsndfile
Aside - If your mac has a Core 2 Duo processor or higher, it's very likely that your machine's architecture is 64-bit or 'X86_64' in the xcode parlance. If this is the case, the above command will build a X86_64 version of the library. This is all well and good, but the OpenFrameworks examples are configured to build for the i386 32-bit architecture. As such, you'll have to execute the following command to make sure the libsndfile library is built to work with the 32-bit OF libraries, and can play nicely with the OpenFrameworks code. Again, this is probably painfully obvious to any C/C++ programmer, but it wasn't immediately obvious to me. If you're encountering a build error along the lines of "libsndfile.dylib is built blah blah blah and not the target architecture i386", try to reinstall libsndfile with the +universal argument as shown below:
> sudo port install libsndfile +universal
Once this is complete you should open your OpenFrameworks project, or copy/paste/rename one of the existing example projects in the OpenFrameworks download. For instance, I used the 'soundPlayerFFTExample' project. In xcode, do the following:
Project ->add to Project...
Add the following three files:
/your_mac_HD/opt/local/include/sndfile.h (the C header file)
/your_mac_HD/opt/local/include/sndfile.hh (the C++ wrappers for libsndfile)
/your_mac_HD/opt/local/lib/libsndfile.a (the library)
At the top of your main C/C++ file i.e. in 'testApp.cpp' you should add an include statement for the libsndfile library, and then some testing code in the body of the setup() method (Please make sure to actually specify a valid path to a wav file in the declaration of fn):
#include "testApp.h"
#include <sndfile.hh>
#include <stdio.h>
void testApp::setup(){
.
.
.
const char* fn = "/Users/.../test.wav";
SndfileHandle myf = SndfileHandle(fn);
printf ("Opened file '%s'\n", fn) ;
printf (" Sample rate : %d\n", myf.samplerate ()) ;
printf (" Channels : %d\n", myf.channels ()) ;
printf (" Error : %s\n", myf.strError());
printf (" Frames : %d\n", int(myf.frames())); // frames is essentially samples
puts("");
.
.
.
Build and run your project, open your xcode console (Run -> console), and you should hopefully see something like this:
run
run
[Switching to process 11816]
Running…
Opened file '/Users/Jack/Documents/coles.wav'
Sample rate : 44100
Channels : 2
Error : No Error.
Frames : 161943
If you get meaningful data for the sample rate, number of channels, and frame number, you're in business! If you get bogus values, ensure that the path you specified to the file is correct. Again, this is probably quite trivial for the C/C++ programmers coming to OpenFrameworks, but for the Processing, Flash, Java, and Matlab users, hopefully you'll find this helpful.