Skip to content

Instantly share code, notes, and snippets.

@udalov
Created February 22, 2013 22:41
Show Gist options
  • Save udalov/5017161 to your computer and use it in GitHub Desktop.
Save udalov/5017161 to your computer and use it in GitHub Desktop.
libsndfile usage
#include <cstdio>
#include <vector>
#include <sndfile.h>
using namespace std;
int main() {
SF_INFO sfinfo;
const int BUF_LEN = 2048;
double buffer[BUF_LEN];
SNDFILE* f = sf_open("/Users/udalov/c/ch24/PreEC/input/P/1.wav", SFM_READ, &sfinfo);
int read;
vector<double> data;
while ((read = sf_read_double(f, buffer, BUF_LEN))) {
for (int i = 0; i < read; i++) {
data.push_back(buffer[i]);
}
}
printf("Sample rate: %d\n", sfinfo.samplerate);
printf("Samples: %lu\n", data.size());
for (size_t i = 0; i < data.size(); i++) {
printf("%.3lf\n", data[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment