00001 /* $Id: parec-simple.c 1033 2006-06-19 21:53:48Z lennart $ */ 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 00031 #include <pulse/simple.h> 00032 #include <pulse/error.h> 00033 #include <pulsecore/gccmacro.h> 00034 00035 #define BUFSIZE 1024 00036 00037 /* A simple routine calling UNIX write() in a loop */ 00038 static ssize_t loop_write(int fd, const void*data, size_t size) { 00039 ssize_t ret = 0; 00040 00041 while (size > 0) { 00042 ssize_t r; 00043 00044 if ((r = write(fd, data, size)) < 0) 00045 return r; 00046 00047 if (r == 0) 00048 break; 00049 00050 ret += r; 00051 data = (const uint8_t*) data + r; 00052 size -= r; 00053 } 00054 00055 return ret; 00056 } 00057 00058 int main(PA_GCC_UNUSED int argc, char*argv[]) { 00059 /* The sample type to use */ 00060 static const pa_sample_spec ss = { 00061 .format = PA_SAMPLE_S16LE, 00062 .rate = 44100, 00063 .channels = 2 00064 }; 00065 pa_simple *s = NULL; 00066 int ret = 1; 00067 int error; 00068 00069 /* Create the recording stream */ 00070 if (!(s = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) { 00071 fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error)); 00072 goto finish; 00073 } 00074 00075 for (;;) { 00076 uint8_t buf[BUFSIZE]; 00077 ssize_t r; 00078 00079 /* Record some data ... */ 00080 if (pa_simple_read(s, buf, sizeof(buf), &error) < 0) { 00081 fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error)); 00082 goto finish; 00083 } 00084 00085 /* And write it to STDOUT */ 00086 if ((r = loop_write(STDOUT_FILENO, buf, sizeof(buf))) <= 0) { 00087 fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno)); 00088 goto finish; 00089 } 00090 } 00091 00092 ret = 0; 00093 00094 finish: 00095 00096 if (s) 00097 pa_simple_free(s); 00098 00099 return ret; 00100 }