3 * writebuffer adapted for sound-playing
5 * readbuffer and writebuffer are:
6 * Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
8 * readbuffer is part of chiark backup, a system for backing up GNU/Linux and
9 * other UN*X-compatible machines, as used on chiark.greenend.org.uk.
11 * Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
12 * Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
14 * This is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2,
17 * or (at your option) any later version.
19 * This is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public
25 * License along with this file; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 const char *progname= "trivsoundd";
34 static int maxstartdelay=60, maxbadaccept=10;
37 struct inqnode *next, *back;
42 static struct { struct inqnode *head, *tail; } inq;
46 static void opensounddevice(void) {
50 sdev= open("/dev/dsp", O_WRONLY);
51 if (sdev<0) { perror("open sound device"); exit(8); }
53 snprintf(cbuf, sizeof(cbuf), "sox -t raw -s -w -r 44100 -c 2"
54 " - </dev/null -t ossdsp - >&%d", sdev);
55 r= system(cbuf); if (r) { fprintf(stderr,"sox gave %d\n",r); exit(5); }
58 static void selectcopy(void) {
59 int slave= inq.head ? inq.head->fd : -1;
60 wrbufcore_prepselect(slave, sdev);
63 wrbufcore_afterselect(slave, sdev);
66 static void expireoldconns(void) {
67 struct inqnode *searchold, *nextsearchold;
69 for (searchold= inq.head ? inq.head->next : 0;
71 searchold= nextsearchold) {
72 nextsearchold= searchold;
73 if (searchold->accepted < now-maxstartdelay) {
74 printf("expired %p\n",searchold);
75 LIST_UNLINK(inq,searchold);
81 static void acceptnewconns(void) {
87 if (!FD_ISSET(0,&readfds)) return;
91 if (!(errno == EINTR ||
93 errno == EWOULDBLOCK)) {
96 if (bad > maxbadaccept) {
97 fprintf(stderr,"accept failures repeating\n");
101 /* any transient error will just send us round again via select */
106 new= xmalloc(sizeof(struct inqnode));
109 LIST_LINK_TAIL(inq,new);
111 printf("accepted %p\n",new);
114 static void switchinput(void) {
116 if (!seeneof) return;
119 printf("finished %p\n",old);
121 LIST_UNLINK(inq,old);
126 int main(int argc, const char *const *argv) {
128 if (argv[1]) { fputs("no arguments allowed\n",stderr); exit(12); }
130 buffersize= 44100*4* 5/*seconds*/;
142 if (time(&now)==(time_t)-1) { perror("time(2)"); exit(4); }