1 /* NCD Audio format - original code by Dave Lemke <lemke@verbosa.ncd.com> */
2 /* Modified by paul kendall for X Galaga. */
3 
4 /*
5  *  Include file dependencies:
6  */
7 
8 /* $Id: nas_sound.c,v 1.1.1.1 1998/04/12 06:03:21 mrogre Exp $ */
9 #include <config.h>
10 
11 #ifdef NAS_SOUND
12 
13 #include <stdio.h>
14 #ifdef STDC_HEADERS
15 # include <stdlib.h>
16 #endif
17 #ifdef HAVE_UNISTD_H
18 # include <unistd.h>
19 #endif
20 #ifdef HAVE_SYS_TIME_H
21 # include <sys/time.h>
22 #endif
23 #ifdef HAVE_FCNTL_H
24 # include <fcntl.h>
25 #endif
26 #include <signal.h>
27 #include <sys/stat.h>
28 #include "data.h"
29 #include <audio/audiolib.h>
30 #include <audio/soundlib.h>
31 
32 /*
33  *  Internal variable declarations:
34  */
35 
36 #define	MAX_SOUNDS	64
37 
38 AuServer   *aud = NULL;
39 AuDeviceID  device;
40 static int	audio_on = False;
41 static int  num_sounds = 0;
42 static char	errorString[255];
43 
44 static struct
45 {
46     char       *name;
47     char       *filename;
48     void       *private;
49 } sound_table[MAX_SOUNDS];
50 
51 typedef struct
52 {
53     int        playing;
54     AuBucketID  bucket;
55 } audioRec, *audioPtr;
56 
init_sound()57 init_sound ()
58 {
59     extern Display	*W_Display;
60     int         i;
61     char       *displayname = XDisplayString(W_Display);
62 
63     /* Do not initialize sound if it is not going to be used -- JEH */
64     if (!playSounds)
65 	    return;
66 
67     if(unixSoundPath[0] == '?')  {
68 	return;
69     };
70     if (audio_on)
71 	return;
72 
73     /* try and connect to the NCD audio server */
74     if (!(aud = AuOpenServer(displayname, 0, NULL, 0, NULL, NULL)))
75     {
76 	return;
77     }
78 
79     /* Look for an audio device that we can use */
80     for (i = 0; i < AuServerNumDevices(aud); i++)
81     {
82 	if ((AuDeviceKind(AuServerDevice(aud, i)) ==
83 	     AuComponentKindPhysicalOutput) &&
84 	    AuDeviceNumTracks(AuServerDevice(aud, i)) == 1)
85 	{
86 	    device = AuDeviceIdentifier(AuServerDevice(aud, i));
87 	    break;
88 	}
89     }
90 
91     /* Well we didn't get a device - all busy? */
92     if (!device)
93     {
94 	AuCloseServer(aud);
95 	return;
96     }
97 
98 #if defined(SOUNDLIB_VERSION) && SOUNDLIB_VERSION >= 2
99     AuSoundRestartHardwarePauses = AuFalse;
100 #endif
101 
102     /* Success - we have an audio device */
103     audio_on = True;
104 
105     return;
106 }
107 
doneCB(aud,handler,event,info)108 static void doneCB(aud, handler, event, info)
109     AuServer   			*aud;
110 AuEventHandlerRec 	*handler;
111 AuEvent    			*event;
112 audioPtr    		info;
113 {
114     info->playing = False;
115 }
116 
audioDevicePlay(filename,volume,private)117 void audioDevicePlay(filename, volume, private)
118     char	*filename;
119 int     volume;
120 void    **private;
121 {
122     audioPtr   *info = (audioPtr *) private;
123 
124     if (!*info)
125     {
126 	if (!(*info = (audioPtr) malloc(sizeof(audioRec))))
127 	{
128 	    return;
129 	}
130 
131 	(*info)->playing = 0;
132 	(*info)->bucket = AuSoundCreateBucketFromFile(aud, filename,
133 						      AuAccessAllMasks, NULL, NULL);
134     }
135 
136     /*    if ((*info)->bucket && (!(*info)->playing)) */
137     if ((*info)->bucket)
138     {
139 	(*info)->playing = 1;
140 	AuSoundPlayFromBucket(aud, (*info)->bucket, device,
141 			      AuFixedPointFromFraction(volume, 100),
142 			      (void (*)) doneCB, (AuPointer) * info, 1, NULL, NULL, NULL, NULL);
143 
144 	/* Flush sound */
145 	AuFlush(aud);
146     }
147 }
148 
check_sound()149 void check_sound()
150 {
151     if (aud) AuHandleEvents(aud);
152 }
153 
playSoundFile(filename,volume)154 void playSoundFile(filename, volume)
155     char       *filename;
156 int         volume;
157 {
158     int         i;
159     char        fbuf[1024];
160     char        *str;
161 
162     /* Loop through the sound table looking for sound */
163     for (i = 0; i < num_sounds; i++)
164     {
165 	if (!strcmp(sound_table[i].name, filename))
166 	{
167 	    /* Yeah - already in sound table */
168 	    break;
169 	}
170     }
171 
172     /* Ok - not found so add it to the sound table */
173     if (i == num_sounds)
174     {
175 	/* new one - so add it to the table */
176 	sound_table[num_sounds].name = malloc(strlen(filename) + 1);
177 	strcpy(sound_table[num_sounds].name, filename);
178 
179 	/* Use the environment variable if it exists */
180         if ((str = getenv("XGAL_SOUND_DIR")) != NULL)
181             sprintf(fbuf, "%s/%s", str, filename);
182         else
183             sprintf(fbuf, "%s/%s", unixSoundPath, filename);
184 
185 	sound_table[num_sounds].filename = malloc(strlen(fbuf) + 1);
186 	strcpy(sound_table[num_sounds].filename, fbuf);
187 
188 	num_sounds++;
189     }
190 
191     audioDevicePlay(sound_table[i].filename, volume, &sound_table[i].private);
192 }
193 
194 char *FILENAME[] = {
195     "explode.au",
196     "firetorp.au",
197     "shield.au",
198     "torphit.au",
199     "explode_big.au",
200     "ddloo.au",
201     "warp.au",
202     "smart.au",
203 };
204 
play_sound(k)205 void play_sound (k)
206     int k;
207 {
208     char c;
209 
210     c = k;
211     if (playSounds && aud)
212     {
213 	playSoundFile(FILENAME[k], 50);
214     }
215 }
216 
217 
maybe_play_sound(k)218 void maybe_play_sound (k)
219     int k;
220 {
221 }
222 
sound_completed(k)223 void sound_completed (k)
224     int k;
225 {
226 }
227 
kill_sound()228 void kill_sound ()
229 {
230 }
231 
232 #endif /*NAS_SOUND*/
233