annotate sound.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 * sound.c - Platform Independant Sound Support - July 1996
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
3 * Sujal M. Patel (smpatel@umiacs.umd.edu)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
4 *
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: sound.c,v 1.1.1.1 1997/12/06 05:41:30 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 #include <stdio.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
34 #ifdef __STDC__
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
35 #include <stdlib.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
36 #endif
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
37 #include <unistd.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
38 #include <sys/time.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
39 #include <fcntl.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
40 #include <signal.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
41 #include <sys/stat.h>
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
42 #include "data.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 static int soundfd;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
47 static char audioOK = 1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
48 static char sound_flags[20]; /* Sound Flag for sound 1-19 */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
49
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
50
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
51
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
52 void init_sound ()
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
53 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
54 int i, pid, child,fd[2];
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
55 char *argv[4];
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
56 char filename[512];
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
57 #ifdef linux
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
58 char *arch = "linux";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
59 #else
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
60 #ifdef __FreeBSD__
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
61 char *arch = "freebsd";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
62 #else
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
63 #ifdef __NetBSD__
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
64 char *arch = "netbsd";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
65 #else
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
66 #ifdef sun
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
67 char *arch = "sun";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
68 #else
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
69 #ifdef foo
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
70 char *arch = "foobar";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
71 #else
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
72 char *arch = "generic";
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
73 #endif
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
74 #endif
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
75 #endif
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
76 #endif
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
77 #endif
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
78
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
79 signal(SIGCHLD, SIG_IGN);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
80
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
81 if(unixSoundPath[0] == '?') {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
82 audioOK = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
83 return;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
84 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
85
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
86 /* Create a pipe, set the write end to close when we exec the sound server,
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
87 and set both (is the write end necessary?) ends to non-blocking */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
88 pipe(fd);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
89 soundfd=fd[1];
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
90
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
91 if( !(child=fork()) ) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
92 close(fd[1]);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
93 dup2(fd[0],STDIN_FILENO);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
94 close(fd[0]);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
95 sprintf (filename, "paradise.sndsrv.%s", arch);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
96 argv[0] = filename;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
97 argv[1] = unixSoundPath;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
98 argv[2] = unixSoundDev;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
99 argv[3] = NULL;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
100 execvp(filename, argv);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
101 fprintf (stderr, "Couldn't Execute Sound Server (paradise.sndsrv.%s)!\n", arch);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
102 exit (0);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
103 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
104 close(fd[0]);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
105
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
106 sleep(1);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
107
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
108 if (kill(child, 0)) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
109 audioOK = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
110 close(soundfd);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
111 };
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
112
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
113 for (i = 0; i < 19; i++) sound_flags[i] = 0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
114 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
115
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
116 void play_sound (k)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
117 int k;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
118 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
119 char c;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
120
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
121 c = k;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
122 if ((playSounds) && (audioOK)) write (soundfd, &c, sizeof (c));
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
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
127 void maybe_play_sound (k)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
128 int k;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
129 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
130 char c;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
131
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
132 if (sound_flags[k] & 1) return;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
133
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
134 sound_flags[k] |= 1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
135
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
136 c = (unsigned char)(k);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
137 if ((playSounds) && (audioOK)) write (soundfd, &c, sizeof (c));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
138 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
139
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
140
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
141
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
142 void sound_completed (k)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
143 int k;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
144 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
145 sound_flags[k] &= ~1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
146 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
147
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
148
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
149
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
150 void kill_sound ()
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
151 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
152 char c;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
153
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
154 c = -1;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
155 if ((playSounds) && (audioOK)) write (soundfd, &c, sizeof (c));
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
156 }