1 /*
2  * xgal.sndsrv.c - VoxWare(tm) Compatible Sound - Apr. 1995
3  *                 PC Speaker  Compatible Sound
4  *                 This server is OSS Specific.
5  *
6  * Copyright 1994-1995 Sujal M. Patel (smpatel@wam.umd.edu)
7  * Conditions in "copyright.h"
8  */
9 
10 /* $Id: xgal.sndsrv.linux.c,v 1.3 1998/05/11 06:37:20 mrogre Exp $ */
11 #include <config.h>
12 
13 #include <time.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <sys/ioctl.h>
19 #ifdef __NetBSD__
20 #include <soundcard.h>
21 #else
22  #include <sys/soundcard.h>
23 /*#include "linux_pcsp.h"       /usr/include/linux/pcsp.h      */
24 #endif
25 #include <sys/time.h>
26 #include <signal.h>
27 #include <string.h>
28 
29 #ifndef PCSP_ONLY
30 #define PCSP_ONLY 0
31 #endif
32 
33 char *FILENAME[] = {
34                      "/explode.raw",
35                      "/firetorp.raw",
36                      "/shield.raw",
37                      "/torphit.raw",
38                      "/explode_big.raw",
39 		     "/ddloo.raw",
40 		     "/warp.raw",
41 		     "/smart.raw",
42                    };
43 
44 #define NUM_SOUNDS	(sizeof(FILENAME)/sizeof(char*))
45 
46 char *sound_buffer[NUM_SOUNDS];
47 int sound_size[NUM_SOUNDS];
48 int fragsize;
49 
50 
51 /* Terminate: Signal Handler */
quit()52 void quit ()
53 {
54   exit (0);
55 }
56 
57 
58 
init(int argc,char ** argv)59 void init (int argc, char **argv)
60 {
61   int i;
62   char s[1024];
63 
64   if (argc != 3)
65   {
66     printf ("This program is only executed by xgalaga\n");
67     exit (1);
68   }
69 
70   for (i=0; i < NUM_SOUNDS; i++)
71   {
72     s[0] = 0;
73     strcat (s, argv[1]);
74     if (s[(int)strlen(s) - 1] == '/') FILENAME[i]++;
75     strcat (s, FILENAME[i]);
76     FILENAME[i] = malloc ((int)strlen (s)+1);
77     strcpy (FILENAME[i],s);
78     sound_buffer[i]=NULL;
79     sound_size[i]=0;
80   }
81 
82   signal(SIGTERM, quit);  /* Setup Terminate Signal Handler */
83 }
84 
85 
86 /*
87    Setup DSP: Opens /dev/dsp or /dev/pcdsp
88               Sets fragment size on VoxWare
89               Sets speed to 8000hz
90               Should set mono mode
91               Error checking
92 */
setup_dsp(char * dspdev,int * is_pcsp)93 int setup_dsp (char *dspdev,int *is_pcsp)
94 {
95   int dsp, frag, value;
96   int mixer;
97 
98 /*  dsp = open(dspdev, O_RDWR);  could not hear my oggs the same time */
99 /*  dsp = open(dspdev,  O_WRONLY | O_NONBLOCK); sound broken only fragmented*/
100   dsp = open(dspdev, O_WRONLY); /* sound is ok*/
101    if (dsp < 0)
102   {
103     fprintf (stderr, "xgal.sndsrv: Couldn't open DSP %s\n",dspdev);
104     return -1;
105   }
106 
107   *is_pcsp = 0;
108   fragsize = 0;
109 
110   frag = 0x00020009;   /* try 512 bytes, for 1/16 second frag size */
111   ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &frag);
112   value = 8010;
113   if (ioctl(dsp, SNDCTL_DSP_SPEED, &value))
114   {
115       fprintf (stderr, "xgal.sndsrv: Couldn't set DSP rate!\n");
116   };
117   value = 0;
118   ioctl(dsp, SNDCTL_DSP_STEREO, &value);
119   ioctl(dsp, SNDCTL_DSP_GETBLKSIZE, &fragsize);
120 /*  fprintf(stderr,"xgal.sndsrv: fragment set to %d\n",fragsize);*/
121 
122   if (!fragsize || PCSP_ONLY)
123   {
124     /* Don't Assume just because you can't set the fragment, use proper IOCTL */
125     fprintf (stderr, "xgal.sndsrv: Couldn't set Fragment Size.\nAssuming PC Speaker!\n");
126     fragsize = 128;
127     *is_pcsp = 1;
128   } else {
129       mixer = open("/dev/mixer",O_RDWR | O_NONBLOCK);
130       if(mixer==-1)  {
131           fprintf(stderr,"xgal.sndsrv: Couldn't open mixer %s\n","/dev/mixer");
132           return(-1);
133       };
134 #if 0
135       value=0x6464;
136       ioctl(mixer,SOUND_MIXER_WRITE_PCM,&value);
137       ioctl(mixer,SOUND_MIXER_WRITE_VOLUME,&value);  /*what does this do?*/
138 #endif
139       close(mixer);
140 
141   }
142 
143   return dsp;
144 }
145 
read_sound(int k)146 int read_sound(int k)
147 {
148   int i,fd,size;
149 
150   /*fprintf(stderr,"loading sound %d, %s\n",k,FILENAME[k]);*/
151 
152   fd = open(FILENAME[k], O_RDONLY);
153   if(fd < 0)
154   {
155     fprintf (stderr, "xgal.sndsrv: The sound %s could not be opened\n", FILENAME[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]*fragsize);
162   if(sound_buffer[k]==NULL)
163   {
164     fprintf(stderr,"xgal.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   size = read(fd,sound_buffer[k],size);
171   close(fd);
172   if (size) {
173     for(i=0;i<size;i++)  sound_buffer[k][i]^=0x80;
174     memset(sound_buffer[k]+size,0,sound_size[k]*fragsize-size);
175   }
176 
177   /*fprintf(stderr,"sound has been loaded, %d bytes\n",size);*/ /*DEBUG*/
178   return(1);
179 }
180 
181 
do_everything(int dsp,int is_pcsp)182 void do_everything (int dsp, int is_pcsp)
183 {
184   signed 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[fragsize];       /* Final Mixing Buffer                                   */
191   int premix[fragsize];           /* Use fragsize in case sound card sets it big. -- JEH */
192   char *sample;
193 
194   for(;;)  {
195     terminate = -1;
196     /* Try to open a new sound if we get an integer on the 'in' pipe */
197     i=read(STDIN_FILENO,&k,sizeof(k));
198     if(i==0)  {   /* EOF on pipe means parent has closed its end */
199         /*fprintf(stderr,"xgal.sndsrv: shutting down\n"); */
200         kill(getpid(), SIGTERM);
201     };
202     if(i!=-1)  {  /* there was something in the pipe */
203         /*fprintf(stderr,"Just read a %d from pipe\n",(int)k);*/ /*DEBUG*/
204         /* Negative means terminate the FIRST sound in the buffer */
205         if(k<0)  {
206             /*fprintf(stderr,"terminating sound\n");*/ /*DEBUG*/
207             terminate = 0;
208         } else {
209             if(sound_size[(int)k]==0) read_sound(k);
210             if(sound_size[(int)k]>0 && playnum<16)  {
211 	        position[playnum]=0;
212                 playing[playnum++]=k;
213                 /*fprintf(stderr,"sound %d added to play queue\n",playnum-1);*/ /*DEBUG*/
214             };
215         };
216     };
217 
218     /* terminate a sound if necessary */
219     for(i=0;i<playnum;i++)
220     {
221       if((position[i]==sound_size[playing[i]]) || (terminate==i))
222       {
223         /*fprintf(stderr,"finished playing sound %d\n",i);*/ /*DEBUG*/
224 	/*fprintf(stderr,"is was at position %d\n",position[i]);*/ /*DEBUG*/
225         memmove(playing+i,playing+i+1,(playnum-i)*sizeof(int));
226         memmove(position+i,position+i+1,(playnum-i)*sizeof(int));
227         playnum--;i--;
228       };
229     };
230 
231     if(playnum)  {
232         /* Mix each sound into the final buffer */
233         memset(premix,0,sizeof(premix));
234         for(i=0;i<playnum;i++)  {
235             sample=sound_buffer[playing[i]]+position[i]*fragsize;
236             for(j=0;j<fragsize;j++)  {
237                 premix[j]+=*(sample+j);
238             };
239             position[i]++;
240         };
241         for(i=0;i<fragsize;i++)
242             final[i]=(premix[i]>255)?255:(premix[i]<-256?0:(premix[i]>>1)+128);
243     } else {
244         /*
245            We have no sounds to play
246            Just fill the buffer with silence and maybe play it
247         */
248         memset(final,128,sizeof(final));
249     };
250     i = write (dsp, final, fragsize);
251     /*
252        The sound server is in a tight loop, EXCEPT for this
253        write which blocks.  Any optimizations in the above
254        code would really be helpful.  Right now the server
255        takes up to 7% cpu on a 486DX/50.
256     */
257   }
258 }
259 
260 
261 
main(argc,argv)262 int main (argc, argv)
263 int argc;
264 char **argv;
265 {
266   int dsp, is_pcsp;
267 
268   fcntl(STDIN_FILENO,F_SETFL,O_NONBLOCK);
269   init (argc, argv);
270   dsp = setup_dsp (argv[2],&is_pcsp);
271 
272   if (dsp<0) exit(1);
273 
274   do_everything (dsp, is_pcsp);
275   return 0;
276 }
277