annotate paradise.sndsrv.hp.c @ 3:5a977ccbc7a9 default tip

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