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 <linux/soundcard.h>
18 #include "linux_pcsp.h"		/* /usr/include/linux/pcsp.h      */
19 #include <sys/time.h>
20 #include <signal.h>
21 #include <string.h>
22 
23 
24 
25 char           *FILENAME[] =
26 {
27   "/start.raw",
28   "/end.raw",
29   "/colize.raw",
30   "/destroy1.raw",
31   "/destroy2.raw",
32   "/creator1.raw",
33   "/creator2.raw"
34 
35 };
36 
37 #define NUM_SOUNDS	(sizeof(FILENAME)/sizeof(char*))
38 
39 signed char    *sound_buffer[NUM_SOUNDS];
40 int             sound_size[NUM_SOUNDS];
41 int             fragsize;
42 
43 
44 /* Terminate: Signal Handler */
45 void
quit()46 quit ()
47 {
48   exit (0);
49 }
50 
51 
52 
53 void
init(int argc,char ** argv)54 init (int argc, char **argv)
55 {
56   int             i;
57   char            s[1024];
58 
59   if (argc != 3)
60     {
61       printf ("This program is only executed by koules\n");
62       exit (1);
63     }
64 
65   for (i = 0; i < NUM_SOUNDS; i++)
66     {
67       s[0] = 0;
68       strcat (s, argv[1]);
69       if (s[(int) strlen (s) - 1] == '/')
70 	FILENAME[i]++;
71       strcat (s, FILENAME[i]);
72       FILENAME[i] = malloc ((int) strlen (s) + 1);
73       strcpy (FILENAME[i], s);
74       sound_buffer[i] = NULL;
75       sound_size[i] = 0;
76     }
77 
78   signal (SIGTERM, quit);	/* Setup Terminate Signal Handler */
79 }
80 
81 
82 /*
83    Setup DSP: Opens /dev/dsp or /dev/pcdsp
84    Sets fragment size on VoxWare
85    Sets speed to 8000hz
86    Should set mono mode
87    Error checking
88  */
89 int
setup_dsp(char * dspdev,int * is_pcsp)90 setup_dsp (char *dspdev, int *is_pcsp)
91 {
92   int             dsp, frag, value;
93 
94   dsp = open (dspdev, O_WRONLY);
95   if (dsp < 0)
96     {
97       fprintf (stderr, "koules.sndsrv: Couldn't open DSP >%s<\n", dspdev);
98       perror("error");
99       return -1;
100     }
101   *is_pcsp = 0;
102   fragsize = 0;
103 
104   frag = 0x00020007;		/* try 512 bytes, for 1/16 second frag size */
105   ioctl(dsp, SNDCTL_DSP_SAMPLESIZE, &value);
106   if(ioctl (dsp, SNDCTL_DSP_SETFRAGMENT, &frag))
107   {
108       fprintf (stderr, "koules.sndsrv: Couldn't set DSP fragment. Sounds will be ugly and delayed. Use USS lite driver!\n");
109   }
110   value = 8010;
111   if (ioctl (dsp, SNDCTL_DSP_SPEED, &value))
112     {
113       fprintf (stderr, "koules.sndsrv: Couldn't set DSP rate!\n");
114     };
115   value = 0;
116   ioctl (dsp, SNDCTL_DSP_STEREO, &value);
117   value = 1;
118   ioctl (dsp, SNDCTL_DSP_SYNC, &value);
119   ioctl (dsp, SNDCTL_DSP_GETBLKSIZE, &fragsize);
120   value=8;
121   ioctl(dsp, SNDCTL_DSP_SAMPLESIZE, &value);
122 
123   if (!fragsize)
124     {
125       /* Don't Assume just because you can't set the fragment, use proper IOCTL */
126       fprintf (stderr, "koules.sndsrv: Couldn't set Fragment Size.\nAssuming PC Speaker!\n");
127       fragsize = 128;
128       *is_pcsp = 1;
129     }
130 
131   return dsp;
132 }
133 
134 /*
135    This just keeps the pipe from breaking...
136    Eventually I'll look at the koules signal handlers and
137    just trap this.
138  */
139 int
do_nothing(void)140 do_nothing (void)
141 {
142   fprintf (stderr, "koules.sndsrv: doing nothing, something is broken\n");
143   while (1)
144     sleep (5);
145 }
146 
147 int
read_sound(int k)148 read_sound (int k)
149 {
150   int             i, fd, size;
151 
152   fd = open (FILENAME[k], O_RDONLY);
153   if (fd <= 0)
154     {
155       fprintf (stderr, "koules.sndsrv: The sound %s number %i could not be opened\n", FILENAME[k], k);
156       sound_size[k] = -1;
157       return (0);
158     }
159   size = lseek (fd, 0, SEEK_END);
160   sound_size[k] = (size / fragsize) + 1;	/*size in fragments */
161   sound_buffer[k] = malloc ((sound_size[k] + 1) * fragsize);
162   if (sound_buffer[k] == NULL)
163     {
164       fprintf (stderr, "koules.sndsrv: couldn't malloc memory for sound\n");
165       sound_size[k] = -1;
166       close (fd);
167       return (0);
168     };
169   lseek (fd, 0, SEEK_SET);
170   read (fd, sound_buffer[k], size);
171   close (fd);
172   for (i = 0; i < size; i++)
173     sound_buffer[k][i] ^= 0x80;
174   memset (sound_buffer[k] + size, 0, sound_size[k] * fragsize - size);
175 
176   /*fprintf(stderr,"sound has been loaded, %d bytes\n",size); *//*DEBUG */
177   return (1);
178 }
179 
180 
181 void
do_everything(int dsp,int is_pcsp)182 do_everything (int dsp, int is_pcsp)
183 {
184   char            k;
185   int             i, j;
186   int             terminate = -1;	/* Which Sound to Terminate                              */
187   int             playing[16];	/* Sound numbers that we are playing                     */
188   int             position[16];	/* Current position in each sound file */
189   int             playnum = 0;	/* Number of sounds currently being played               */
190   unsigned char   final[512];	/* Final Mixing Buffer                                   */
191   int             premix[512];
192   char           *sample;
193   audio_buf_info  info;
194 
195   for (;;)
196     {
197       terminate = -1;
198       /* Try to open a new sound if we get an integer on the 'in' pipe */
199       i = read (STDIN_FILENO, &k, sizeof (k));
200       if (i == 0)
201 	{			/* EOF on pipe means parent has closed its end */
202 	  fprintf(stderr,"koules.sndsrv: shutting down\n");
203 	  kill (getpid (), SIGTERM);
204 	  exit(-1);
205 	};
206       if (i != -1)
207 	{			/* there was something in the pipe */
208 	  /*fprintf(stderr,"Just read a %d from pipe\n",(int)k); *//*DEBUG */
209 	  /* Negative means terminate the FIRST sound in the buffer */
210 	  if (k < 0)
211 	    {
212 	      /*fprintf(stderr,"terminating sound\n"); *//*DEBUG */
213 	      terminate = 0;
214 	    }
215 	  else
216 	    {
217 	      if (sound_size[(int) k] == 0)
218 		read_sound (k);
219 	      if (sound_size[(int) k] > 0 && playnum < 16)
220 		{
221 		  position[playnum] = 0;
222 		  playing[playnum++] = k;
223 		  /*fprintf(stderr,"sound %d added to play queue\n",playnum-1); *//*DEBUG */
224 		};
225 	    };
226 	};
227 
228       /* terminate a sound if necessary */
229       for (i = 0; i < playnum; i++)
230 	{
231 	  if ((position[i] == sound_size[playing[i]]) || (terminate == i))
232 	    {
233 	      /*fprintf(stderr,"finished playing sound %d\n",i); *//*DEBUG */
234 	      /*fprintf(stderr,"is was at position %d\n",position[i]); *//*DEBUG */
235 	      memmove (playing + i, playing + i + 1, (playnum - i) * sizeof (int));
236 	      memmove (position + i, position + i + 1, (playnum - i) * sizeof (int));
237 	      playnum--;
238 	      i--;
239 	    };
240 	};
241 
242       if (playnum)
243 	{
244 	  /* Mix each sound into the final buffer */
245 	  memset (premix, 0, sizeof (premix));
246 	  for (i = 0; i < playnum; i++)
247 	    {
248 	      sample = sound_buffer[playing[i]] + position[i] * fragsize;
249 	      for (j = 0; j < fragsize; j++)
250 		{
251 		  premix[j] += *(sample + j);
252 		};
253 	      position[i]++;
254 	    };
255 	  for (i = 0; i < fragsize; i++)
256 	    final[i] = (premix[i] > 255) ? 255 : (premix[i] < -256 ? 0 : (premix[i] >> 1) + 128);
257 	}
258       else
259 	{
260 	  /*
261 	     We have no sounds to play
262 	     Just fill the buffer with silence and maybe play it
263 	   */
264 	  memset (final, 128, sizeof (final));
265 	};
266       write (dsp, final, fragsize);
267       if(!ioctl(dsp,SNDCTL_DSP_GETOSPACE,&info)) { /*this is code
268 			drivers that does not allow to set number of
269 			fragments to 2. (ultrasound project?)*/
270 	while((int)info.fragments<((int)info.fragstotal-2))  {
271 	  usleep(1000000*info.fragsize/8010);
272           ioctl(dsp,SNDCTL_DSP_GETOSPACE,&info);
273 	}
274       }
275       /*
276          The sound server is in a tight loop, EXCEPT for this
277          write which blocks.  Any optimizations in the above
278          code would really be helpful.  Right now the server
279          takes up to 7% cpu on a 486DX/50.
280        */
281     }
282 }
283 
284 
285 
286 int
main(argc,argv)287 main (argc, argv)
288      int             argc;
289      char          **argv;
290 {
291   int             dsp, is_pcsp;
292 
293   fcntl (STDIN_FILENO, F_SETFL, O_NONBLOCK);
294   init (argc, argv);
295   dsp = setup_dsp (argv[2], &is_pcsp);
296   if (dsp<0) {
297 	  fprintf(stderr,"Sound server: exit\n");
298 	  exit(0);
299   }
300 printf("2\n");
301 
302   do_everything (dsp, is_pcsp);
303 printf("3\n");
304   return 1;
305 }
306