Documentation work
[platform/upstream/pulseaudio.git] / polyp / parec-simple.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10  
11   polypaudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15  
16   You should have received a copy of the GNU General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <polyp/polyplib-simple.h>
32 #include <polyp/polyplib-error.h>
33
34 #define BUFSIZE 1024
35
36 /* A simple routine calling UNIX write() in a loop */
37 static ssize_t loop_write(int fd, const void*data, size_t size) {
38     ssize_t ret = 0;
39
40     while (size > 0) {
41         ssize_t r;
42
43         if ((r = write(fd, data, size)) < 0)
44             return r;
45
46         if (r == 0)
47             break;
48         
49         ret += r;
50         data += r;
51         size -= r;
52     }
53
54     return ret;
55 }
56
57 int main(int argc, char*argv[]) {
58     /* The sample type to use */
59     static const struct pa_sample_spec ss = {
60         .format = PA_SAMPLE_S16LE,
61         .rate = 44100,
62         .channels = 2
63     };
64     struct pa_simple *s = NULL;
65     int ret = 1;
66     int error;
67
68     /* Create the recording stream */
69     if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, &error))) {
70         fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
71         goto finish;
72     }
73
74     for (;;) {
75         uint8_t buf[BUFSIZE];
76         ssize_t r;
77
78         /* Record some data ... */
79         if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) {
80             fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
81             goto finish;
82         }
83
84         /* And write it to STDOUT */
85         if ((r = loop_write(STDOUT_FILENO, buf, sizeof(buf))) <= 0) {
86             fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno));
87             goto finish;
88         }
89     }
90
91     ret = 0;
92
93 finish:
94
95     if (s)
96         pa_simple_free(s);
97     
98     return ret;
99 }