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