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 / xgal.sndsrv.freebsd.c < prev    next >
C/C++ Source or Header  |  1998-04-12  |  7KB  |  271 lines

  1. /*
  2.  * xgal.sndsrv.c - VoxWare(tm) Compatible Sound - Apr. 1995
  3.  *                 PC Speaker  Compatible Sound 
  4.  *                 This server is FreeBSD Specific.
  5.  *
  6.  * Copyright 1994-1995 Sujal M. Patel (smpatel@wam.umd.edu)
  7.  * Conditions in "copyright.h"
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <sys/ioctl.h>
  15. #include <machine/soundcard.h>
  16. #include <sys/time.h>
  17. #include <signal.h>
  18. #include <string.h>
  19.  
  20.  
  21.  
  22. char *FILENAME[] = {
  23.                      "/explode.raw",
  24.                      "/firetorp.raw",
  25.                      "/shield.raw",
  26.                      "/torphit.raw",
  27.                      "/explode_big.raw",
  28.              "/ddloo.raw",
  29.                    };
  30.  
  31. #define NUM_SOUNDS    (sizeof(FILENAME)/sizeof(char*))
  32.  
  33. signed char *sound_buffer[NUM_SOUNDS];
  34. int sound_size[NUM_SOUNDS];
  35. int fragsize;
  36.  
  37.  
  38. /* Terminate: Signal Handler */
  39. void quit ()
  40. {
  41.   exit (0);
  42. }
  43.  
  44.  
  45.  
  46. void init (int argc, char **argv)
  47. {
  48.   int i;
  49.   char s[1024];
  50.  
  51.   if (argc != 3)
  52.   {
  53.     printf ("This program is only executed by xgal\n");
  54.     exit (1);
  55.   }
  56.  
  57.   for (i=0; i < NUM_SOUNDS; i++)
  58.   {
  59.     s[0] = 0;
  60.     strcat (s, argv[1]);
  61.     if (s[(int)strlen(s) - 1] == '/') FILENAME[i]++;
  62.     strcat (s, FILENAME[i]);
  63.     FILENAME[i] = malloc ((int)strlen (s));
  64.     strcpy (FILENAME[i],s);
  65.     sound_buffer[i]=NULL;
  66.     sound_size[i]=0;
  67.   }
  68.  
  69.   signal(SIGTERM, quit);   /* Setup Terminate Signal Handler */
  70. }
  71.  
  72.  
  73. /*
  74.    Setup DSP: Opens /dev/dsp or /dev/pcdsp
  75.               Sets fragment size on VoxWare
  76.               Sets speed to 8000hz
  77.               Should set mono mode
  78.               Error checking                
  79. */
  80. int setup_dsp (char *dspdev,int *is_pcsp)
  81. {
  82.   int dsp, frag, value;
  83.   int mixer;
  84.  
  85.   dsp = open(dspdev, O_RDWR);
  86.   if (dsp < 1)
  87.   {
  88.     fprintf (stderr, "xgal.sndsrv: Couldn't open DSP %s\n",dspdev);
  89.     return -1;
  90.   }
  91.  
  92.   *is_pcsp = 0;
  93.   fragsize = 0;
  94.  
  95.   frag = 0x00020009;   /* try 512 bytes, for 1/16 second frag size */
  96.   ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &frag);
  97.   value = 8010;
  98.   if (ioctl(dsp, SNDCTL_DSP_SPEED, &value))
  99.   {
  100.       fprintf (stderr, "xgal.sndsrv: Couldn't set DSP rate!\n");
  101.   };
  102.   value = 0;
  103.   ioctl(dsp, SNDCTL_DSP_STEREO, &value);
  104.   ioctl(dsp, SNDCTL_DSP_GETBLKSIZE, &fragsize);
  105.   fprintf(stderr,"xgal.sndsrv: fragment set to %d\n",fragsize);
  106.  
  107.   if (!fragsize)
  108.   { 
  109.     /* Don't Assume just because you can't set the fragment, use proper IOCTL */
  110.     fprintf (stderr, "xgal.sndsrv: Couldn't set Fragment Size.\nAssuming PC Speaker!\n");
  111.     fragsize = 128;
  112.     *is_pcsp = 1;
  113.   } else {
  114.       mixer = open("/dev/mixer",O_RDWR | O_NONBLOCK);
  115.       if(mixer==-1)  {
  116.           fprintf(stderr,"xgal.sndsrv: Couldn't open mixer %s\n","/dev/mixer");
  117.           return(-1);
  118.       };
  119. #if 0
  120.       value=0x6464;
  121.       ioctl(mixer,SOUND_MIXER_WRITE_PCM,&value);
  122.       ioctl(mixer,SOUND_MIXER_WRITE_VOLUME,&value);  /*what does this do?*/
  123. #endif
  124.       close(mixer);
  125.  
  126.   }
  127.   
  128.   return dsp;
  129. }
  130.  
  131. /*
  132.    This just keeps the pipe from breaking...
  133.    Eventually I'll look at the xgal signal handlers and
  134.    just trap this.
  135. */
  136. int do_nothing(void)
  137. {
  138.     fprintf(stderr,"xgal.sndsrv: doing nothing, something is broken\n");
  139.     while(1)  sleep (5);
  140. }
  141.  
  142. int read_sound(int k)
  143. {
  144.   int i,fd,size;
  145.  
  146.   /*fprintf(stderr,"loading sound %d, %s\n",k,FILENAME[k]);*/
  147.  
  148.   fd = open(FILENAME[k], O_RDONLY);
  149.   if(fd<=0) 
  150.   {
  151.     fprintf (stderr, "xgal.sndsrv: The sound %s could not be opened\n", FILENAME[k]);
  152.     sound_size[k]=-1;
  153.     return(0);
  154.   };
  155.   size=lseek(fd,0,SEEK_END);
  156.   sound_size[k]=(size/fragsize)+1;   /*size in fragments*/
  157.   sound_buffer[k]=malloc(sound_size[k]*fragsize);
  158.   if(sound_buffer[k]==NULL)
  159.   {
  160.     fprintf(stderr,"xgal.sndsrv: couldn't malloc memory for sound\n");
  161.     sound_size[k]=-1;
  162.     close(fd);
  163.     return(0);
  164.   };
  165.   lseek(fd,0,SEEK_SET);
  166.   read(fd,sound_buffer[k],size);
  167.   close(fd);
  168.   for(i=0;i<size;i++)  sound_buffer[k][i]^=0x80;
  169.   memset(sound_buffer[k]+size,0,sound_size[k]*fragsize-size);
  170.   
  171.   /*fprintf(stderr,"sound has been loaded, %d bytes\n",size);*/ /*DEBUG*/
  172.   return(1);
  173. }
  174.  
  175.  
  176. void do_everything (int dsp, int is_pcsp)
  177. {
  178.   char k;
  179.   int i, j ;
  180.   int terminate = -1;             /* Which Sound to Terminate                              */
  181.   int playing[16];                /* Sound numbers that we are playing                     */
  182.   int position[16];          /* Current position in each sound file */
  183.   int playnum = 0;                /* Number of sounds currently being played               */
  184.   unsigned char final[512];       /* Final Mixing Buffer                                   */
  185.   int premix[512];
  186.   char *sample;
  187.  
  188.   for(;;)  {
  189.     terminate = -1;
  190.     /* Try to open a new sound if we get an integer on the 'in' pipe */
  191.     i=read(STDIN_FILENO,&k,sizeof(k));
  192.     if(i==0)  {   /* EOF on pipe means parent has closed its end */
  193.         /*fprintf(stderr,"xgal.sndsrv: shutting down\n"); */
  194.         kill(getpid(), SIGTERM);
  195.     };
  196.     if(i!=-1)  {  /* there was something in the pipe */
  197.         /*fprintf(stderr,"Just read a %d from pipe\n",(int)k);*/ /*DEBUG*/
  198.         /* Negative means terminate the FIRST sound in the buffer */
  199.         if(k<0)  {
  200.             /*fprintf(stderr,"terminating sound\n");*/ /*DEBUG*/
  201.             terminate = 0;
  202.         } else {
  203.             if(sound_size[k]==0) read_sound(k);
  204.             if(sound_size[k]>0 && playnum<16)  {
  205.             position[playnum]=0;
  206.                 playing[playnum++]=k;
  207.                 /*fprintf(stderr,"sound %d added to play queue\n",playnum-1);*/ /*DEBUG*/
  208.             };
  209.         };
  210.     };
  211.  
  212.     /* terminate a sound if necessary */
  213.     for(i=0;i<playnum;i++)
  214.     {
  215.       if((position[i]==sound_size[playing[i]]) || (terminate==i))
  216.       {
  217.         /*fprintf(stderr,"finished playing sound %d\n",i);*/ /*DEBUG*/
  218.     /*fprintf(stderr,"is was at position %d\n",position[i]);*/ /*DEBUG*/
  219.         memmove(playing+i,playing+i+1,(playnum-i)*sizeof(int));
  220.         memmove(position+i,position+i+1,(playnum-i)*sizeof(int));
  221.         playnum--;i--;
  222.       };
  223.     };
  224.  
  225.     if(playnum)  {
  226.         /* Mix each sound into the final buffer */
  227.         memset(premix,0,sizeof(premix));
  228.         for(i=0;i<playnum;i++)  {
  229.             sample=sound_buffer[playing[i]]+position[i]*fragsize;
  230.             for(j=0;j<fragsize;j++)  {
  231.                 premix[j]+=*(sample+j);
  232.             };
  233.             position[i]++;
  234.         };
  235.         for(i=0;i<fragsize;i++)
  236.             final[i]=(premix[i]>255)?255:(premix[i]<-256?0:(premix[i]>>1)+128);
  237.     } else {
  238.         /* 
  239.            We have no sounds to play
  240.            Just fill the buffer with silence and maybe play it 
  241.         */
  242.         memset(final,128,sizeof(final));
  243.     };
  244.     write (dsp, final, fragsize);
  245.     /*
  246.        The sound server is in a tight loop, EXCEPT for this
  247.        write which blocks.  Any optimizations in the above
  248.        code would really be helpful.  Right now the server
  249.        takes up to 7% cpu on a 486DX/50.
  250.     */
  251.   }
  252. }
  253.  
  254.  
  255.  
  256. void main (argc, argv)
  257. int argc;
  258. char **argv;
  259. {
  260.   int dsp, is_pcsp, ppid;
  261.   char filename[512];
  262.  
  263.   fcntl(STDIN_FILENO,F_SETFL,O_NONBLOCK);
  264.   init (argc, argv);
  265.   dsp = setup_dsp (argv[2],&is_pcsp);
  266.  
  267.   if (!dsp) do_nothing();
  268.  
  269.   do_everything (dsp, is_pcsp);
  270. }
  271.