Mercurial > ~darius > hgwebdir.cgi > paradise_client
comparison paradise.sndsrv.freebsd.c @ 3:5a977ccbc7a9 default tip
Empty changelog
author | darius |
---|---|
date | Sat, 06 Dec 1997 05:41:29 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2:fba0b6e6cdc7 | 3:5a977ccbc7a9 |
---|---|
1 /* | |
2 * paradise.sndsrv.c - USS-Lite Compatible Sound - July 1996 | |
3 * This server is FreeBSD Specific. | |
4 * Sujal M. Patel (smpatel@umiacs.umd.edu) | |
5 * | |
6 * | |
7 * Copyright (c) 1994-1996, Sujal M. Patel | |
8 * All rights reserved. | |
9 * | |
10 * Redistribution and use in source and binary forms, with or without | |
11 * modification, are permitted provided that the following conditions | |
12 * are met: | |
13 * 1. Redistributions of source code must retain the above copyright | |
14 * notice, this list of conditions and the following disclaimer. | |
15 * 2. Redistributions in binary form must reproduce the above copyright | |
16 * notice, this list of conditions and the following disclaimer in the | |
17 * documentation and/or other materials provided with the distribution. | |
18 * | |
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 * SUCH DAMAGE. | |
30 * | |
31 * $Id: paradise.sndsrv.freebsd.c,v 1.1.1.1 1997/12/06 05:41:29 darius Exp $ | |
32 */ | |
33 | |
34 #include <stdio.h> | |
35 #include <stdlib.h> | |
36 #include <unistd.h> | |
37 #include <fcntl.h> | |
38 #include <sys/ioctl.h> | |
39 #include <machine/soundcard.h> | |
40 #include <sys/time.h> | |
41 #include <signal.h> | |
42 #include <string.h> | |
43 | |
44 | |
45 | |
46 char *FILENAME[] = { | |
47 "/explode.raw", | |
48 "/cloak.raw", | |
49 "/firetorp.raw", | |
50 "/phaser.raw", | |
51 "/plasma.raw", | |
52 "/shield.raw", | |
53 "/torphit.raw", | |
54 "/explode_big.raw", | |
55 "/paradise.raw", | |
56 "/thermal.raw", | |
57 "/redalert.raw" | |
58 }; | |
59 | |
60 #define NUM_SOUNDS (sizeof(FILENAME)/sizeof(char*)) | |
61 | |
62 signed char *sound_buffer[NUM_SOUNDS]; | |
63 int sound_size[NUM_SOUNDS]; | |
64 int fragsize; | |
65 | |
66 | |
67 /* Terminate: Signal Handler */ | |
68 void quit () | |
69 { | |
70 exit (0); | |
71 } | |
72 | |
73 | |
74 | |
75 void init (int argc, char **argv) | |
76 { | |
77 int i; | |
78 char s[1024]; | |
79 | |
80 if (argc != 3) | |
81 { | |
82 printf ("This program is only executed by netrek.paradise\n"); | |
83 exit (1); | |
84 } | |
85 | |
86 for (i=0; i < NUM_SOUNDS; i++) | |
87 { | |
88 s[0] = 0; | |
89 strcat (s, argv[1]); | |
90 if (s[(int)strlen(s) - 1] == '/') FILENAME[i]++; | |
91 strcat (s, FILENAME[i]); | |
92 FILENAME[i] = malloc ((int)strlen (s)); | |
93 strcpy (FILENAME[i],s); | |
94 sound_buffer[i]=NULL; | |
95 sound_size[i]=0; | |
96 } | |
97 | |
98 signal(SIGTERM, quit); /* Setup Terminate Signal Handler */ | |
99 } | |
100 | |
101 | |
102 /* | |
103 Setup DSP: Opens /dev/dsp or /dev/pcdsp | |
104 Sets fragment size on VoxWare | |
105 Sets speed to 8000hz | |
106 Should set mono mode | |
107 Error checking | |
108 */ | |
109 int setup_dsp (char *dspdev,int *is_pcsp) | |
110 { | |
111 int dsp, frag, value; | |
112 int mixer; | |
113 | |
114 dsp = open(dspdev, O_RDWR); | |
115 if (dsp < 1) | |
116 { | |
117 fprintf (stderr, "paradise.sndsrv: Couldn't open device %s\n",dspdev); | |
118 return -1; | |
119 } | |
120 | |
121 *is_pcsp = 0; | |
122 fragsize = 0; | |
123 | |
124 frag = 0x00020009; /* try 512 bytes, for 1/16 second frag size */ | |
125 ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &frag); | |
126 value = 8010; | |
127 if (ioctl(dsp, SNDCTL_DSP_SPEED, &value)) | |
128 { | |
129 fprintf (stderr, "paradise.sndsrv: Couldn't set DSP rate!\n"); | |
130 }; | |
131 value = 0; | |
132 ioctl(dsp, SNDCTL_DSP_STEREO, &value); | |
133 ioctl(dsp, SNDCTL_DSP_GETBLKSIZE, &fragsize); | |
134 /*fprintf(stderr,"paradise.sndsrv: fragment set to %d\n",fragsize);*/ | |
135 | |
136 if (!fragsize) | |
137 { | |
138 /* Don't Assume just because you can't set the fragment, use proper IOCTL */ | |
139 fprintf (stderr, "paradise.sndsrv: Couldn't set Fragment Size.\nAssuming PC Speaker!\n"); | |
140 fragsize = 128; | |
141 *is_pcsp = 1; | |
142 } else { | |
143 mixer = open("/dev/mixer",O_RDWR | O_NONBLOCK); | |
144 if(mixer==-1) { | |
145 fprintf(stderr,"paradise.sndsrv: Couldn't open mixer %s\n","/dev/mixer"); | |
146 return(-1); | |
147 }; | |
148 value=0x6464; | |
149 ioctl(mixer,SOUND_MIXER_WRITE_PCM,&value); | |
150 ioctl(mixer,SOUND_MIXER_WRITE_VOLUME,&value); /*what does this do?*/ | |
151 close(mixer); | |
152 } | |
153 | |
154 return dsp; | |
155 } | |
156 | |
157 /* | |
158 This just keeps the pipe from breaking... | |
159 Eventually I'll look at the paradise signal handlers and | |
160 just trap this. | |
161 */ | |
162 int do_nothing(void) | |
163 { | |
164 while(1) sleep (5); | |
165 } | |
166 | |
167 int read_sound(int k) | |
168 { | |
169 int i,fd,size; | |
170 | |
171 /*fprintf(stderr,"loading sound %d, %s\n",k,FILENAME[k]);*/ | |
172 | |
173 fd = open(FILENAME[k], O_RDONLY); | |
174 if(fd<=0) | |
175 { | |
176 fprintf (stderr, "paradise.sndsrv: The sound %s could not be opened\n", FILENAME[k]); | |
177 sound_size[k]=-1; | |
178 return(0); | |
179 }; | |
180 size=lseek(fd,0,SEEK_END); | |
181 sound_size[k]=(size/fragsize)+1; /*size in fragments*/ | |
182 sound_buffer[k]=malloc(sound_size[k]*fragsize); | |
183 if(sound_buffer[k]==NULL) | |
184 { | |
185 fprintf(stderr,"paradise.sndsrv: couldn't malloc memory for sound\n"); | |
186 sound_size[k]=-1; | |
187 close(fd); | |
188 return(0); | |
189 }; | |
190 lseek(fd,0,SEEK_SET); | |
191 read(fd,sound_buffer[k],size); | |
192 close(fd); | |
193 for(i=0;i<size;i++) sound_buffer[k][i]^=0x80; | |
194 memset(sound_buffer[k]+size,0,sound_size[k]*fragsize-size); | |
195 | |
196 /*fprintf(stderr,"sound has been loaded, %d bytes\n",size);*/ /*DEBUG*/ | |
197 return(1); | |
198 } | |
199 | |
200 | |
201 void do_everything (int dsp, int is_pcsp) | |
202 { | |
203 char k; | |
204 int i, j ; | |
205 int terminate = -1; /* Which Sound to Terminate */ | |
206 int playing[16]; /* Sound numbers that we are playing */ | |
207 int position[16]; /* Current position in each sound file */ | |
208 int playnum = 0; /* Number of sounds currently being played */ | |
209 unsigned char final[512]; /* Final Mixing Buffer */ | |
210 int premix[512]; | |
211 char *sample; | |
212 | |
213 for(;;) { | |
214 terminate = -1; | |
215 /* Try to open a new sound if we get an integer on the 'in' pipe */ | |
216 i=read(STDIN_FILENO,&k,sizeof(k)); | |
217 if(i==0) { /* EOF on pipe means parent has closed its end */ | |
218 /*fprintf(stderr,"paradise.sndsrv: shutting down\n"); */ | |
219 kill(getpid(), SIGTERM); | |
220 }; | |
221 if(i!=-1) { /* there was something in the pipe */ | |
222 /*fprintf(stderr,"Just read a %d from pipe\n",(int)k);*/ /*DEBUG*/ | |
223 /* Negative means terminate the FIRST sound in the buffer */ | |
224 if(k<0) { | |
225 /*fprintf(stderr,"terminating sound\n");*/ /*DEBUG*/ | |
226 terminate = 0; | |
227 } else { | |
228 if(sound_size[k]==0) read_sound(k); | |
229 if(sound_size[k]>0 && playnum<16) { | |
230 position[playnum]=0; | |
231 playing[playnum++]=k; | |
232 /*fprintf(stderr,"sound %d added to play queue\n",playnum-1);*/ /*DEBUG*/ | |
233 }; | |
234 }; | |
235 }; | |
236 | |
237 /* terminate a sound if necessary */ | |
238 for(i=0;i<playnum;i++) | |
239 { | |
240 if((position[i]==sound_size[playing[i]]) || (terminate==i)) | |
241 { | |
242 /*fprintf(stderr,"finished playing sound %d\n",i);*/ /*DEBUG*/ | |
243 /*fprintf(stderr,"is was at position %d\n",position[i]);*/ /*DEBUG*/ | |
244 memmove(playing+i,playing+i+1,(playnum-i)*sizeof(int)); | |
245 memmove(position+i,position+i+1,(playnum-i)*sizeof(int)); | |
246 playnum--;i--; | |
247 }; | |
248 }; | |
249 | |
250 if(playnum) { | |
251 /* Mix each sound into the final buffer */ | |
252 memset(premix,0,sizeof(premix)); | |
253 for(i=0;i<playnum;i++) { | |
254 sample=sound_buffer[playing[i]]+position[i]*fragsize; | |
255 for(j=0;j<fragsize;j++) { | |
256 premix[j]+=*(sample+j); | |
257 }; | |
258 position[i]++; | |
259 }; | |
260 for(i=0;i<fragsize;i++) | |
261 final[i]=(premix[i]>255)?255:(premix[i]<-256?0:(premix[i]>>1)+128); | |
262 } else { | |
263 /* | |
264 We have no sounds to play | |
265 Just fill the buffer with silence and maybe play it | |
266 */ | |
267 memset(final,128,sizeof(final)); | |
268 }; | |
269 write (dsp, final, fragsize); | |
270 /* | |
271 The sound server is in a tight loop, EXCEPT for this | |
272 write which blocks. Any optimizations in the above | |
273 code would really be helpful. Right now the server | |
274 takes up to 7% cpu on a 486DX/50. | |
275 */ | |
276 } | |
277 } | |
278 | |
279 | |
280 | |
281 void main (argc, argv) | |
282 int argc; | |
283 char **argv; | |
284 { | |
285 int dsp, is_pcsp, ppid; | |
286 char filename[512]; | |
287 | |
288 fcntl(STDIN_FILENO,F_SETFL,O_NONBLOCK); | |
289 init (argc, argv); | |
290 dsp = setup_dsp (argv[2],&is_pcsp); | |
291 | |
292 if (dsp < 1) do_nothing(); | |
293 | |
294 do_everything (dsp, is_pcsp); | |
295 } |