00001 /* $Id: pacat-simple.c 1418 2007-01-04 13:43:45Z ossman $ */ 00002 00003 /*** 00004 This file is part of PulseAudio. 00005 00006 PulseAudio is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published 00008 by the Free Software Foundation; either version 2 of the License, 00009 or (at your option) any later version. 00010 00011 PulseAudio is distributed in the hope that it will be useful, but 00012 WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with PulseAudio; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00019 USA. 00020 ***/ 00021 00022 #ifdef HAVE_CONFIG_H 00023 #include <config.h> 00024 #endif 00025 00026 #include <stdio.h> 00027 #include <unistd.h> 00028 #include <string.h> 00029 #include <errno.h> 00030 #include <fcntl.h> 00031 00032 #include <pulse/simple.h> 00033 #include <pulse/error.h> 00034 #include <pulsecore/gccmacro.h> 00035 00036 #define BUFSIZE 1024 00037 00038 int main(PA_GCC_UNUSED int argc, char*argv[]) { 00039 00040 /* The Sample format to use */ 00041 static const pa_sample_spec ss = { 00042 .format = PA_SAMPLE_S16LE, 00043 .rate = 44100, 00044 .channels = 2 00045 }; 00046 00047 pa_simple *s = NULL; 00048 int ret = 1; 00049 int error; 00050 00051 /* replace STDIN with the specified file if needed */ 00052 if (argc > 1) { 00053 int fd; 00054 00055 if ((fd = open(argv[1], O_RDONLY)) < 0) { 00056 fprintf(stderr, __FILE__": open() failed: %s\n", strerror(errno)); 00057 goto finish; 00058 } 00059 00060 if (dup2(fd, STDIN_FILENO) < 0) { 00061 fprintf(stderr, __FILE__": dup2() failed: %s\n", strerror(errno)); 00062 goto finish; 00063 } 00064 00065 close(fd); 00066 } 00067 00068 /* Create a new playback stream */ 00069 if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error))) { 00070 fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); 00071 goto finish; 00072 } 00073 00074 for (;;) { 00075 uint8_t buf[BUFSIZE]; 00076 ssize_t r; 00077 00078 #if 0 00079 pa_usec_t latency; 00080 00081 if ((latency = pa_simple_get_latency(s, &error)) == (pa_usec_t) -1) { 00082 fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error)); 00083 goto finish; 00084 } 00085 00086 fprintf(stderr, "%0.0f usec \r", (float)latency); 00087 #endif 00088 00089 /* Read some data ... */ 00090 if ((r = read(STDIN_FILENO, buf, sizeof(buf))) <= 0) { 00091 if (r == 0) /* EOF */ 00092 break; 00093 00094 fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno)); 00095 goto finish; 00096 } 00097 00098 /* ... and play it */ 00099 if (pa_simple_write(s, buf, r, &error) < 0) { 00100 fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error)); 00101 goto finish; 00102 } 00103 } 00104 00105 /* Make sure that every single sample was played */ 00106 if (pa_simple_drain(s, &error) < 0) { 00107 fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error)); 00108 goto finish; 00109 } 00110 00111 ret = 0; 00112 00113 finish: 00114 00115 if (s) 00116 pa_simple_free(s); 00117 00118 return ret; 00119 }