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