annotate paradise.sndsrv.freebsd.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 FreeBSD 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.freebsd.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 <machine/soundcard.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
40 #include <sys/time.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
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
46 char *FILENAME[] = {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
47 "/explode.raw",
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
48 "/cloak.raw",
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
49 "/firetorp.raw",
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
50 "/phaser.raw",
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
51 "/plasma.raw",
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
52 "/shield.raw",
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
53 "/torphit.raw",
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
54 "/explode_big.raw",
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
55 "/paradise.raw",
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
56 "/thermal.raw",
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
57 "/redalert.raw"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
58 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
59
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
60 #define NUM_SOUNDS (sizeof(FILENAME)/sizeof(char*))
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
61
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
62 signed char *sound_buffer[NUM_SOUNDS];
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
63 int sound_size[NUM_SOUNDS];
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
64 int fragsize;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
65
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
66
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
67 /* Terminate: Signal Handler */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
68 void quit ()
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
69 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
70 exit (0);
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
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
75 void init (int argc, char **argv)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
76 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
77 int i;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
78 char s[1024];
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
79
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
80 if (argc != 3)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
81 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
82 printf ("This program is only executed by netrek.paradise\n");
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
83 exit (1);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
84 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
85
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
86 for (i=0; i < NUM_SOUNDS; i++)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
87 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
88 s[0] = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
89 strcat (s, argv[1]);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
90 if (s[(int)strlen(s) - 1] == '/') 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));
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/dsp or /dev/pcdsp
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
104 Sets fragment size on VoxWare
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
105 Sets speed to 8000hz
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
106 Should set mono mode
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
107 Error checking
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
108 */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
109 int setup_dsp (char *dspdev,int *is_pcsp)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
110 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
111 int dsp, frag, value;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
112 int mixer;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
113
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
114 dsp = open(dspdev, O_RDWR);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
115 if (dsp < 1)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
116 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
117 fprintf (stderr, "paradise.sndsrv: Couldn't open device %s\n",dspdev);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
118 return -1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
119 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
120
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
121 *is_pcsp = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
122 fragsize = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
123
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
124 frag = 0x00020009; /* try 512 bytes, for 1/16 second frag size */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
125 ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &frag);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
126 value = 8010;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
127 if (ioctl(dsp, SNDCTL_DSP_SPEED, &value))
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
128 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
129 fprintf (stderr, "paradise.sndsrv: Couldn't set DSP rate!\n");
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
130 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
131 value = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
132 ioctl(dsp, SNDCTL_DSP_STEREO, &value);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
133 ioctl(dsp, SNDCTL_DSP_GETBLKSIZE, &fragsize);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
134 /*fprintf(stderr,"paradise.sndsrv: fragment set to %d\n",fragsize);*/
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
135
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
136 if (!fragsize)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
137 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
138 /* Don't Assume just because you can't set the fragment, use proper IOCTL */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
139 fprintf (stderr, "paradise.sndsrv: Couldn't set Fragment Size.\nAssuming PC Speaker!\n");
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
140 fragsize = 128;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
141 *is_pcsp = 1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
142 } else {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
143 mixer = open("/dev/mixer",O_RDWR | O_NONBLOCK);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
144 if(mixer==-1) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
145 fprintf(stderr,"paradise.sndsrv: Couldn't open mixer %s\n","/dev/mixer");
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
146 return(-1);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
147 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
148 value=0x6464;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
149 ioctl(mixer,SOUND_MIXER_WRITE_PCM,&value);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
150 ioctl(mixer,SOUND_MIXER_WRITE_VOLUME,&value); /*what does this do?*/
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
151 close(mixer);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
152 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
153
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
154 return dsp;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
155 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
156
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
157 /*
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
158 This just keeps the pipe from breaking...
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
159 Eventually I'll look at the paradise signal handlers and
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
160 just trap this.
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
161 */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
162 int do_nothing(void)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
163 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
164 while(1) sleep (5);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
165 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
166
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
167 int read_sound(int k)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
168 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
169 int i,fd,size;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
170
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
171 /*fprintf(stderr,"loading sound %d, %s\n",k,FILENAME[k]);*/
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
172
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
173 fd = open(FILENAME[k], O_RDONLY);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
174 if(fd<=0)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
175 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
176 fprintf (stderr, "paradise.sndsrv: The sound %s could not be opened\n", FILENAME[k]);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
177 sound_size[k]=-1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
178 return(0);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
179 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
180 size=lseek(fd,0,SEEK_END);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
181 sound_size[k]=(size/fragsize)+1; /*size in fragments*/
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
182 sound_buffer[k]=malloc(sound_size[k]*fragsize);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
183 if(sound_buffer[k]==NULL)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
184 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
185 fprintf(stderr,"paradise.sndsrv: couldn't malloc memory for sound\n");
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
186 sound_size[k]=-1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
187 close(fd);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
188 return(0);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
189 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
190 lseek(fd,0,SEEK_SET);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
191 read(fd,sound_buffer[k],size);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
192 close(fd);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
193 for(i=0;i<size;i++) sound_buffer[k][i]^=0x80;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
194 memset(sound_buffer[k]+size,0,sound_size[k]*fragsize-size);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
195
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
196 /*fprintf(stderr,"sound has been loaded, %d bytes\n",size);*/ /*DEBUG*/
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
197 return(1);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
198 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
199
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
200
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
201 void do_everything (int dsp, int is_pcsp)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
202 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
203 char k;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
204 int i, j ;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
205 int terminate = -1; /* Which Sound to Terminate */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
206 int playing[16]; /* Sound numbers that we are playing */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
207 int position[16]; /* Current position in each sound file */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
208 int playnum = 0; /* Number of sounds currently being played */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
209 unsigned char final[512]; /* Final Mixing Buffer */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
210 int premix[512];
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
211 char *sample;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
212
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
213 for(;;) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
214 terminate = -1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
215 /* Try to open a new sound if we get an integer on the 'in' pipe */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
216 i=read(STDIN_FILENO,&k,sizeof(k));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
217 if(i==0) { /* EOF on pipe means parent has closed its end */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
218 /*fprintf(stderr,"paradise.sndsrv: shutting down\n"); */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
219 kill(getpid(), SIGTERM);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
220 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
221 if(i!=-1) { /* there was something in the pipe */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
222 /*fprintf(stderr,"Just read a %d from pipe\n",(int)k);*/ /*DEBUG*/
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
223 /* Negative means terminate the FIRST sound in the buffer */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
224 if(k<0) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
225 /*fprintf(stderr,"terminating sound\n");*/ /*DEBUG*/
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
226 terminate = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
227 } else {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
228 if(sound_size[k]==0) read_sound(k);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
229 if(sound_size[k]>0 && playnum<16) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
230 position[playnum]=0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
231 playing[playnum++]=k;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
232 /*fprintf(stderr,"sound %d added to play queue\n",playnum-1);*/ /*DEBUG*/
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
233 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
234 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
235 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
236
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
237 /* terminate a sound if necessary */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
238 for(i=0;i<playnum;i++)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
239 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
240 if((position[i]==sound_size[playing[i]]) || (terminate==i))
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
241 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
242 /*fprintf(stderr,"finished playing sound %d\n",i);*/ /*DEBUG*/
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
243 /*fprintf(stderr,"is was at position %d\n",position[i]);*/ /*DEBUG*/
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
244 memmove(playing+i,playing+i+1,(playnum-i)*sizeof(int));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
245 memmove(position+i,position+i+1,(playnum-i)*sizeof(int));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
246 playnum--;i--;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
247 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
248 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
249
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
250 if(playnum) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
251 /* Mix each sound into the final buffer */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
252 memset(premix,0,sizeof(premix));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
253 for(i=0;i<playnum;i++) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
254 sample=sound_buffer[playing[i]]+position[i]*fragsize;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
255 for(j=0;j<fragsize;j++) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
256 premix[j]+=*(sample+j);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
257 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
258 position[i]++;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
259 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
260 for(i=0;i<fragsize;i++)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
261 final[i]=(premix[i]>255)?255:(premix[i]<-256?0:(premix[i]>>1)+128);
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 memset(final,128,sizeof(final));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
268 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
269 write (dsp, final, fragsize);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
270 /*
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
271 The sound server is in a tight loop, EXCEPT for this
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
272 write which blocks. Any optimizations in the above
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
273 code would really be helpful. Right now the server
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
274 takes up to 7% cpu on a 486DX/50.
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
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
281 void main (argc, argv)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
282 int argc;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
283 char **argv;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
284 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
285 int dsp, is_pcsp, ppid;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
286 char filename[512];
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
287
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
288 fcntl(STDIN_FILENO,F_SETFL,O_NONBLOCK);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
289 init (argc, argv);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
290 dsp = setup_dsp (argv[2],&is_pcsp);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
291
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
292 if (dsp < 1) do_nothing();
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
293
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
294 do_everything (dsp, is_pcsp);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
295 }