Skip to content

Instantly share code, notes, and snippets.

@reening
Created April 9, 2012 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reening/2342592 to your computer and use it in GitHub Desktop.
Save reening/2342592 to your computer and use it in GitHub Desktop.
libsndfile with Pulse Audio
#include <stdio.h>
#include <sndfile.h>
#include <pulse/pulseaudio.h>
#include <pulse/simple.h>
int main(int argc, char* argv[])
{
if (argc != 2)
{
printf("Usage: %s [filename]\n", argv[0]);
return 0;
}
SNDFILE *f;
SF_INFO info;
f = sf_open(argv[1], SFM_READ, &info);
int res = sf_error(f);
if (res != 0)
{
printf("File error: %s\n", sf_strerror(f));
return 0;
}
pa_simple *s;
pa_sample_spec ss;
ss.format = PA_SAMPLE_FLOAT32NE;
ss.channels = info.channels;
ss.rate = info.samplerate;
s = pa_simple_new(NULL, "Nexus", PA_STREAM_PLAYBACK, NULL, "Music", &ss, NULL, NULL, NULL);
double duration;
duration = (1.0 * info.frames) / info.samplerate;
printf("Sample rate: %iHz\n", info.samplerate);
printf("Channels: %i\n", info.channels);
printf("Duration: %fs\n", duration);
int done = 0;
int *err;
float *buf;
buf = (float*) malloc(256 * info.channels * sizeof(float));
while (!done && (err >= 0))
{
int count = sf_readf_float(f, buf, 256);
pa_simple_write(s, buf, (256 * info.channels * sizeof(float)), err);
if (count < 256)
done = 1;
}
free(buf);
sf_close(f);
pa_simple_free(s);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment