]> code.delx.au - pulseaudio/blob - src/tests/pacat-simple.c
big s/polyp/pulse/g
[pulseaudio] / src / tests / pacat-simple.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser 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 PulseAudio 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 Lesser General Public License
17 along with PulseAudio; 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 #include <fcntl.h>
31
32 #include <pulse/simple.h>
33 #include <pulse/error.h>
34 #include <pulsecore/gccmacro.h>
35
36 #define BUFSIZE 1024
37
38 int main(PA_GCC_UNUSED int argc, char*argv[]) {
39
40 /* The Sample format to use */
41 static const pa_sample_spec ss = {
42 .format = PA_SAMPLE_S16LE,
43 .rate = 44100,
44 .channels = 2
45 };
46
47 pa_simple *s = NULL;
48 int ret = 1;
49 int error;
50
51 /* replace STDIN with the specified file if needed */
52 if (argc > 1) {
53 int fd;
54
55 if ((fd = open(argv[1], O_RDONLY)) < 0) {
56 fprintf(stderr, __FILE__": open() failed: %s\n", strerror(errno));
57 goto finish;
58 }
59
60 if (dup2(fd, STDIN_FILENO) < 0) {
61 fprintf(stderr, __FILE__": dup2() failed: %s\n", strerror(errno));
62 goto finish;
63 }
64
65 close(fd);
66 }
67
68 /* Create a new playback stream */
69 if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, 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 #if 0
79 pa_usec_t latency;
80
81 if ((latency = pa_simple_get_latency(s, &error)) == (pa_usec_t) -1) {
82 fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
83 goto finish;
84 }
85
86 fprintf(stderr, "%0.0f usec \r", (float)latency);
87 #endif
88
89 /* Read some data ... */
90 if ((r = read(STDIN_FILENO, buf, sizeof(buf))) <= 0) {
91 if (r == 0) /* EOF */
92 break;
93
94 fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
95 goto finish;
96 }
97
98 /* ... and play it */
99 if (pa_simple_write(s, buf, r, &error) < 0) {
100 fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
101 goto finish;
102 }
103 }
104
105 /* Make sure that every single sample was played */
106 if (pa_simple_drain(s, &error) < 0) {
107 fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
108 goto finish;
109 }
110
111 ret = 0;
112
113 finish:
114
115 if (s)
116 pa_simple_free(s);
117
118 return ret;
119 }