PulseAudio  15.0

A simple recording tool using the simple API

This file is part of PulseAudio.
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
#include <config.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define BUFSIZE 1024
/* A simple routine calling UNIX write() in a loop */
static ssize_t loop_write(int fd, const void*data, size_t size) {
ssize_t ret = 0;
while (size > 0) {
ssize_t r;
if ((r = write(fd, data, size)) < 0)
return r;
if (r == 0)
ret += r;
data = (const uint8_t*) data + r;
size -= (size_t) r;
return ret;
int main(int argc, char*argv[]) {
/* The sample type to use */
static const pa_sample_spec ss = {
.rate = 44100,
.channels = 2
pa_simple *s = NULL;
int ret = 1;
int error;
/* Create the recording stream */
if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
goto finish;
for (;;) {
uint8_t buf[BUFSIZE];
/* Record some data ... */
if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
goto finish;
/* And write it to STDOUT */
if (loop_write(STDOUT_FILENO, buf, sizeof(buf)) != sizeof(buf)) {
fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
goto finish;
ret = 0;
if (s)
return ret;
Record stream.
Definition: def.h:161
Error management.
const char * pa_strerror(int error)
Return a human readable error message for the specified numeric error code.
Signed 16 Bit PCM, little endian (PC)
Definition: sample.h:144
A simple but limited synchronous playback and recording API.
int pa_simple_read(pa_simple *s, void *data, size_t bytes, int *error)
Read some data from the server.
void pa_simple_free(pa_simple *s)
Close and free the connection to the server.
pa_simple * pa_simple_new(const char *server, const char *name, pa_stream_direction_t dir, const char *dev, const char *stream_name, const pa_sample_spec *ss, const pa_channel_map *map, const pa_buffer_attr *attr, int *error)
Create a new connection to the server.
A sample format and attribute specification.
Definition: sample.h:252
pa_sample_format_t format
The sample format.
Definition: sample.h:253
An opaque simple connection object.