1 /*
2  * koules.sndsrv.c - VoxWare(tm) Compatible Sound - Apr. 1995
3  *                 PC Speaker  Compatible Sound
4  *                 This server is Linux Specific.
5  *
6  * Copyright 1994-1995 Sujal M. Patel (smpatel@wam.umd.edu)
7  * Conditions in "copyright.h"
8  *
9  * Modified for Koules and bugfixed by Jan Hubicka
10  */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <sys/ioctl.h>
17 #include <sys/time.h>
18 #include <signal.h>
19 #include <string.h>
20 
21 #ifdef SOLARIS
22 #define bzero(s,l) memset(s, 0, l)
23 #define bcopy(s,d,l) memcpy(d,s,l)
24 #endif
25 
26 
27 
28 
29 char           *FILENAME[] =
30 {
31   "/start.raw",
32   "/end.raw",
33   "/colize.raw",
34   "/destroy1.raw",
35   "/destroy2.raw",
36   "/creator1.raw",
37   "/creator2.raw"
38 };
39 
40 #define NUM_SOUNDS	(sizeof(FILENAME)/sizeof(char*))
41 
42 signed char    *sound_buffer[NUM_SOUNDS];
43 int             sound_size[NUM_SOUNDS];
44 int             fragsize;
45 
46 
47 /* Terminate: Signal Handler */
48 void
quit()49 quit ()
50 {
51   exit (0);
52 }
53 
54 
55 
56 void
init(int argc,char ** argv)57 init (int argc, char **argv)
58 {
59   int             i;
60   char            s[1024];
61 
62   if (argc != 3)
63     {
64       printf ("This program is only executed by koules\n");
65       exit (1);
66     }
67 
68   for (i = 0; i < NUM_SOUNDS; i++)
69     {
70       s[0] = 0;
71       strcat (s, argv[1]);
72       if (s[(int) strlen (s) - 1] == '/')
73 	FILENAME[i]++;
74       strcat (s, FILENAME[i]);
75       FILENAME[i] = malloc ((int) strlen (s) + 1);
76       strcpy (FILENAME[i], s);
77       sound_buffer[i] = NULL;
78       sound_size[i] = 0;
79     }
80 
81   signal (SIGTERM, quit);	/* Setup Terminate Signal Handler */
82 }
83 
84 
85 /*
86    Setup DSP: Opens /dev/audio
87    Sets fragment size to 512
88    Error checking
89  */
90 int
setup_dsp(char * dspdev)91 setup_dsp (char *dspdev)
92 {
93   int             dsp, frag, value;
94   int             mixer;
95 
96   dsp = open (dspdev, O_RDWR);
97   if (dsp < 1)
98     {
99       fprintf (stderr, "koules.sndsrv: Couldn't open DSP %s\n", dspdev);
100       return -1;
101     }
102 
103   fragsize = 512;
104 
105   return dsp;
106 }
107 
108 /*
109    This just keeps the pipe from breaking...
110    Eventually I'll look at the koules signal handlers and
111    just trap this.
112  */
113 int
do_nothing(void)114 do_nothing (void)
115 {
116   fprintf (stderr, "koules.sndsrv: doing nothing, something is broken\n");
117   while (1)
118     sleep (5);
119 }
120 
121 int
read_sound(int k)122 read_sound (int k)
123 {
124   int             i, fd, size;
125 
126   /*fprintf(stderr,"loading sound %d, %s\n",k,FILENAME[k]); */
127 
128   fd = open (FILENAME[k], O_RDONLY);
129   if (fd <= 0)
130     {
131       fprintf (stderr, "koules.sndsrv: The sound %s could not be opened\n", FILENAME[k]);
132       sound_size[k] = -1;
133       return (0);
134     };
135   size = lseek (fd, 0, SEEK_END);
136   sound_size[k] = (size / fragsize) + 1;	/*size in fragments */
137   sound_buffer[k] = malloc (sound_size[k] * fragsize);
138   if (sound_buffer[k] == NULL)
139     {
140       fprintf (stderr, "koules.sndsrv: couldn't malloc memory for sound\n");
141       sound_size[k] = -1;
142       close (fd);
143       return (0);
144     };
145   lseek (fd, 0, SEEK_SET);
146   read (fd, sound_buffer[k], size);
147   close (fd);
148   for (i = 0; i < size; i++)
149     sound_buffer[k][i] ^= 0x80;
150   bzero (sound_buffer[k] + size, sound_size[k] * fragsize - size);
151 
152   /*fprintf(stderr,"sound has been loaded, %d bytes\n",size); *//*DEBUG */
153   return (1);
154 }
155 
156 
157 void
do_everything(int dsp)158 do_everything (int dsp)
159 {
160   char            k;
161   int             i, j;
162   int             terminate = -1;	/* Which Sound to Terminate                              */
163   int             playing[16];	/* Sound numbers that we are playing                     */
164   int             position[16];	/* Current position in each sound file */
165   int             playnum = 0;	/* Number of sounds currently being played               */
166   unsigned char   final[512];	/* Final Mixing Buffer                                   */
167   int             premix[512];
168   char           *sample;
169 
170   for (;;)
171     {
172       terminate = -1;
173       /* Try to open a new sound if we get an integer on the 'in' pipe */
174       i = read (STDIN_FILENO, &k, sizeof (k));
175       if (i == 0)
176 	{			/* EOF on pipe means parent has closed its end */
177 	  /*fprintf(stderr,"koules.sndsrv: shutting down\n"); */
178 	  kill (getpid (), SIGTERM);
179 	};
180       if (i != -1)
181 	{			/* there was something in the pipe */
182 	  /*fprintf(stderr,"Just read a %d from pipe\n",(int)k); *//*DEBUG */
183 	  /* Negative means terminate the FIRST sound in the buffer */
184 	  if (k < 0)
185 	    {
186 	      /*fprintf(stderr,"terminating sound\n"); *//*DEBUG */
187 	      terminate = 0;
188 	    }
189 	  else
190 	    {
191 	      if (sound_size[(int) k] == 0)
192 		read_sound (k);
193 	      if (sound_size[(int) k] > 0 && playnum < 16)
194 		{
195 		  position[playnum] = 0;
196 		  playing[playnum++] = k;
197 		  /*fprintf(stderr,"sound %d added to play queue\n",playnum-1); *//*DEBUG */
198 		};
199 	    };
200 	};
201 
202       /* terminate a sound if necessary */
203       for (i = 0; i < playnum; i++)
204 	{
205 	  if ((position[i] == sound_size[playing[i]]) || (terminate == i))
206 	    {
207 	      /*fprintf(stderr,"finished playing sound %d\n",i); *//*DEBUG */
208 	      /*fprintf(stderr,"is was at position %d\n",position[i]); *//*DEBUG */
209 	      bcopy (playing + i + 1, playing + i, (playnum - i) * sizeof (int));
210 	      bcopy (position + i + 1, position + i, (playnum - i) * sizeof (int));
211 	      playnum--;
212 	      i--;
213 	    };
214 	};
215 
216       if (playnum)
217 	{
218 	  /* Mix each sound into the final buffer */
219 	  bzero (premix, sizeof (premix));
220 	  for (i = 0; i < playnum; i++)
221 	    {
222 	      sample = sound_buffer[playing[i]] + position[i] * fragsize;
223 	      for (j = 0; j < fragsize; j++)
224 		{
225 		  premix[j] += *(sample + j);
226 		};
227 	      position[i]++;
228 	    };
229 	  for (i = 0; i < fragsize; i++)
230 	    final[i] = (premix[i] > 255) ? 255 : (premix[i] < -256 ? 0 : (premix[i] >> 1) + 128);
231 	}
232       else
233 	{
234 	  /*
235 	     We have no sounds to play
236 	     Just fill the buffer with silence and maybe play it
237 	   */
238 	  memset (final, 128, sizeof (final));
239 	};
240       write (dsp, final, fragsize);
241       /*
242          The sound server is in a tight loop, EXCEPT for this
243          write which blocks.  Any optimizations in the above
244          code would really be helpful.  Right now the server
245          takes up to 7% cpu on a 486DX/50.
246        */
247     }
248 }
249 
250 
251 
252 void
main(argc,argv)253 main (argc, argv)
254      int             argc;
255      char          **argv;
256 {
257   int             dsp;
258 
259   fcntl (STDIN_FILENO, F_SETFL, O_NONBLOCK);
260   init (argc, argv);
261   dsp = setup_dsp (argv[2]);
262 
263   if (!dsp)
264     do_nothing ();
265 
266   do_everything (dsp);
267 }
268