Skip to content

Instantly share code, notes, and snippets.

@reening
Created April 9, 2012 09:48
Show Gist options
  • Select an option

  • Save reening/2342592 to your computer and use it in GitHub Desktop.

Select an option

Save reening/2342592 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Apr 9, 2012.
    66 changes: 66 additions & 0 deletions pulse.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    #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;
    }