home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / xgalaga-2_0_tar.gz / xgalaga-2_0_tar / xgalaga-2.0 / nas_sound.c < prev    next >
C/C++ Source or Header  |  1998-04-12  |  4KB  |  229 lines

  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.  
  57. init_sound ()
  58. {
  59.     extern Display    *W_Display;
  60.     int         i;
  61.     char       *displayname = XDisplayString(W_Display);
  62.  
  63.     if(unixSoundPath[0] == '?')  {
  64.     return;
  65.     };
  66.     if (audio_on)
  67.     return;
  68.  
  69.     /* try and connect to the NCD audio server */
  70.     if (!(aud = AuOpenServer(displayname, 0, NULL, 0, NULL, NULL)))
  71.     {
  72.     return;
  73.     }
  74.  
  75.     /* Look for an audio device that we can use */
  76.     for (i = 0; i < AuServerNumDevices(aud); i++)
  77.     {
  78.     if ((AuDeviceKind(AuServerDevice(aud, i)) == 
  79.          AuComponentKindPhysicalOutput) && 
  80.         AuDeviceNumTracks(AuServerDevice(aud, i)) == 1) 
  81.     {
  82.         device = AuDeviceIdentifier(AuServerDevice(aud, i));
  83.         break;
  84.     }
  85.     }
  86.  
  87.     /* Well we didn't get a device - all busy? */
  88.     if (!device) 
  89.     {
  90.     AuCloseServer(aud);
  91.     return;
  92.     }
  93.  
  94. #if defined(SOUNDLIB_VERSION) && SOUNDLIB_VERSION >= 2
  95.     AuSoundRestartHardwarePauses = AuFalse;
  96. #endif        
  97.  
  98.     /* Success - we have an audio device */
  99.     audio_on = True;
  100.  
  101.     return;
  102. }
  103.  
  104. static void doneCB(aud, handler, event, info)
  105.     AuServer               *aud;
  106. AuEventHandlerRec     *handler;
  107. AuEvent                *event;
  108. audioPtr            info;
  109. {
  110.     info->playing = False;
  111. }
  112.  
  113. void audioDevicePlay(filename, volume, private)
  114.     char    *filename;
  115. int     volume;
  116. void    **private;
  117. {
  118.     audioPtr   *info = (audioPtr *) private;
  119.  
  120.     if (!*info) 
  121.     {
  122.     if (!(*info = (audioPtr) malloc(sizeof(audioRec))))
  123.     {
  124.         return;
  125.     }
  126.  
  127.     (*info)->playing = 0;
  128.     (*info)->bucket = AuSoundCreateBucketFromFile(aud, filename, 
  129.                               AuAccessAllMasks, NULL, NULL);
  130.     }
  131.  
  132.     /*    if ((*info)->bucket && (!(*info)->playing)) */
  133.     if ((*info)->bucket) 
  134.     {
  135.     (*info)->playing = 1;
  136.     AuSoundPlayFromBucket(aud, (*info)->bucket, device,
  137.                   AuFixedPointFromFraction(volume, 100),
  138.                   (void (*)) doneCB, (AuPointer) * info, 1, NULL, NULL, NULL, NULL);
  139.     
  140.     /* Flush sound */
  141.     AuFlush(aud);
  142.     }
  143. }
  144.  
  145. void check_sound()
  146. {
  147.     if (aud) AuHandleEvents(aud);
  148. }
  149.  
  150. void playSoundFile(filename, volume)
  151.     char       *filename;
  152. int         volume;
  153. {
  154.     int         i;
  155.     char        fbuf[1024];
  156.     char        *str;
  157.  
  158.     /* Loop through the sound table looking for sound */
  159.     for (i = 0; i < num_sounds; i++) 
  160.     {
  161.     if (!strcmp(sound_table[i].name, filename)) 
  162.     {
  163.         /* Yeah - already in sound table */
  164.         break;
  165.     }
  166.     }
  167.  
  168.     /* Ok - not found so add it to the sound table */
  169.     if (i == num_sounds) 
  170.     {    
  171.     /* new one - so add it to the table */
  172.     sound_table[num_sounds].name = malloc(strlen(filename) + 1);
  173.     strcpy(sound_table[num_sounds].name, filename);
  174.  
  175.     /* Use the environment variable if it exists */
  176.         if ((str = getenv("XGAL_SOUND_DIR")) != NULL)
  177.             sprintf(fbuf, "%s/%s", str, filename);
  178.         else            
  179.             sprintf(fbuf, "%s/%s", unixSoundPath, filename);
  180.  
  181.     sound_table[num_sounds].filename = malloc(strlen(fbuf) + 1);
  182.     strcpy(sound_table[num_sounds].filename, fbuf);
  183.  
  184.     num_sounds++;
  185.     }
  186.  
  187.     audioDevicePlay(sound_table[i].filename, volume, &sound_table[i].private);
  188. }
  189.  
  190. char *FILENAME[] = {
  191.     "explode.au",
  192.     "firetorp.au",
  193.     "shield.au",
  194.     "torphit.au",
  195.     "explode_big.au",
  196.     "ddloo.au",
  197.     "warp.au",
  198.     "smart.au",
  199. };
  200.  
  201. void play_sound (k)
  202.     int k;
  203. {
  204.     char c;
  205.  
  206.     c = k;
  207.     if (playSounds && aud)
  208.     {
  209.     playSoundFile(FILENAME[k], 50);
  210.     }
  211. }
  212.  
  213.  
  214. void maybe_play_sound (k)
  215.     int k;
  216. {
  217. }
  218.  
  219. void sound_completed (k)
  220.     int k;
  221. {
  222. }
  223.  
  224. void kill_sound ()
  225. }
  226.  
  227. #endif /*NAS_SOUND*/
  228.