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