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