// // read.cpp // MUSIC256a/CS476a - Lecture 4a // ----------------------------- // Shows how to read an audio file using libsndfile // Uses libsndfile, by Erik de Castro Lopo // // Created by Jorge Herrera on 2011-10-17. // // To compile this example you'll need libsndfile installed in your system. // Then type: // // g++ read.cpp -o read -lsndfile #include #include #include #define SAMPLE double #define MY_SRATE 44100 using namespace std; int main() { const char* infilename="foo.wav"; // struct that will hold the details of the file being read (sample rate, num channels. etc.) SF_INFO sfinfo ; // a pointer to the file to read SNDFILE * infile; // open the file for reading and get the if ( !(infile = sf_open(infilename, SFM_READ, &sfinfo)) ) { printf ("Not able to open input file %s.\n", infilename) ; // Print the error message from libsndfile. puts (sf_strerror (NULL)) ; // quit the program return 1 ; } // explore the file's info cout << "Channels: " << sfinfo.channels << endl; cout << "Frames: " << sfinfo.frames << endl; cout << "Sample Rate: " << sfinfo.samplerate << endl; cout << "Format: " << sfinfo.format << endl; // read the contents of the file in chunks of 512 samples const int buffSize = 512; double samples[buffSize]; bool empty = false; do { // read the samples as doubles sf_count_t count = sf_read_double( infile, &samples[0], buffSize); // break if we reached the end of the file if ( count == 0) { empty = true; continue; } // print the sample values to screen for(size_t i = 0; i < count; ++i) { cerr << samples[i] << " "; } } while( !empty ); // don't forget to close the file sf_close( infile ); return 0; }