Skip to content

Instantly share code, notes, and snippets.

@regularfry
Created February 7, 2012 20:09
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 regularfry/1761661 to your computer and use it in GitHub Desktop.
Save regularfry/1761661 to your computer and use it in GitHub Desktop.
Basic pulseaudio initialisation
/* For instance... */
#define BUFSIZE 1024
pa_simple *init_pulse(int *perror)
{
/* The sample type to use.
This gives us 2 channels of 8 bits per channel. */
static const pa_sample_spec ss = {
.format = PA_SAMPLE_U8,
.rate = 44100,
.channels = 2
};
/*
// Debug info on the sample type we've got if you're interested
printf("Bytes per second:\t%d\n", (int)pa_bytes_per_second(&ss));
printf("Frame size:\t%d\n", (int)pa_frame_size(&ss));
printf("Sample size\t%d\n", (int)pa_sample_size(&ss));
*/
/* The only important bit here is BUFSIZE, where we tell Pulse
how many samples we want at a time */
static const pa_buffer_attr ba = {
.maxlength = -1,
.tlength = -1,
.prebuf = -1,
.minreq = -1,
.fragsize = BUFSIZE
};
pa_channel_map cm;
pa_channel_map_init_stereo(&cm);
return pa_simple_new(NULL,
__FILE__,
PA_STREAM_RECORD,
NULL,
"record",
&ss,
&cm,
&ba,
perror);
}
/* Now, during the main() somewhere: */
pa_simple *stream = init_pulse(&pulse_error);
/* Some time later... */
uint8_t buf[BUFSIZE*2];
for ( ; ; ) {
/* Record some data ... */
if (pa_simple_read(stream, buf, sizeof(buf), &pulse_error) < 0) {
fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n",
pa_strerror(pulse_error));
goto finish;
}
/* Now buf will contain 1024 left/right sample byte pairs. */
do_something_with( buf );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment