Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created August 28, 2011 18:17
Show Gist options
  • Save dtinth/1177001 to your computer and use it in GitHub Desktop.
Save dtinth/1177001 to your computer and use it in GitHub Desktop.
Convert to PCM using libsndfile.
#include <stdio.h>
#include <sndfile.h>
#include <string.h>
// gcc topcm.c -lsndfile -o topcm
int main(int argc, char **argv) {
if (argc < 3) {
printf("Usage: %s in-filename out-filename\n", argv[0]); return 0;
}
SF_INFO readInfo, writeInfo;
readInfo.format = 0;
char *inFilename = argv[1];
char *outFilename = argv[2];
SNDFILE *in = sf_open(inFilename, SFM_READ, &readInfo);
if (in == NULL) {
printf("Can't open file for reading!\n"); return 1;
}
writeInfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
writeInfo.samplerate = readInfo.samplerate;
writeInfo.channels = readInfo.channels;
SNDFILE *out = sf_open(outFilename, SFM_WRITE, &writeInfo);
if (out == NULL) {
printf("Can't open file for writing!\n"); return 2;
}
short samples[2048];
for (;;) {
int items = sf_read_short(in, samples, 2048);
if (items > 0) {
sf_write_short(out, samples, items);
} else {
break;
}
}
sf_close(in);
sf_close(out);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment