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