home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ABUSESRC.ZIP / AbuseSrc / imlib / port / aix / sound.c < prev   
Encoding:
C/C++ Source or Header  |  1996-07-25  |  5.9 KB  |  274 lines

  1. /*************************************
  2.   Threaded sound driver system
  3. **************************************/
  4.  
  5. // comminicates with sound sub-system via shared memory
  6.  
  7. #include "macs.hpp"
  8. #include "sound.hpp"
  9. #include "readwav.hpp"
  10. #include "dprint.hpp"
  11. #include "jmalloc.hpp"
  12. #include <sys/types.h>
  13. #include <sys/ipc.h>
  14. #include <sys/shm.h>
  15. #include "timing.hpp"
  16. #include <sys/ipc.h>
  17. #include <sys/shm.h>
  18. #include <signal.h>
  19. #include <unistd.h>
  20.  
  21. enum { SFXCMD_QUIT,
  22.        SFXCMD_REGISTER,
  23.        SFXCMD_UNREGISTER,
  24.        SFXCMD_PLAY
  25.      };
  26.  
  27. #define DIN_NAME  "/tmp/sfxdrv.signal"
  28. #define DOUT_NAME "/tmp/sfxdrv.command"
  29.  
  30. class sfx_handle
  31. {
  32.   public :
  33.   int handle;
  34.   sound_effect *me;
  35.   sfx_handle   *next;  
  36.   sfx_handle(sound_effect *Me, sfx_handle *Next, int Handle)
  37.   { me=Me;
  38.     next=Next;
  39.     handle=Handle;
  40.   }
  41. } ;
  42.  
  43. static sfx_handle *sfx_list=NULL;
  44. static ushort last_allocated_sfx_id=0;
  45. static int sfx_driver_out_fd=-1,sfx_driver_in_fd=-1;
  46. static int sdriver_pid=0;
  47.  
  48. static int sfx_shm_id;
  49. static void *sfx_shm_addr;
  50. static int sfx_end;
  51.  
  52. static void kill_sound_driver()
  53. {
  54.   if (sfx_driver_out_fd>=0)
  55.   {
  56.     uchar cmd=SFXCMD_QUIT;        // failed, tell the driver good-bye
  57.     write(sfx_driver_out_fd,&cmd,1);
  58.     close(sfx_driver_out_fd);
  59.     close(sfx_driver_in_fd);
  60.     sfx_driver_out_fd=-1;
  61.  
  62.     milli_wait(10);
  63.     kill(sdriver_pid,SIGUSR1);
  64.     unlink(DIN_NAME);
  65.     unlink(DOUT_NAME);
  66.         shmdt(sfx_shm_addr);
  67.   }
  68. }
  69.  
  70.  
  71.  
  72. #ifdef __sgi
  73. static void sfx_clean_up(...)
  74. #else
  75. static void sfx_clean_up(int why)      // on exit unattach all shared memory links
  76. #endif
  77. {  
  78.   while (sfx_list)
  79.   {
  80.     sfx_handle *last=sfx_list;
  81.     sfx_list=sfx_list->next;
  82.     jfree(last);
  83.   }
  84. }
  85.  
  86.  
  87.  
  88. #define TOTAL_SIGS 29
  89.  
  90. int sigs[TOTAL_SIGS]={SIGHUP,SIGINT,SIGQUIT,SIGILL,SIGTRAP,
  91.               SIGABRT,SIGIOT,SIGBUS,SIGFPE,SIGKILL,
  92.               SIGUSR1,SIGSEGV,SIGUSR2,SIGPIPE,SIGALRM,
  93.               SIGTERM,SIGCHLD,SIGCONT,SIGSTOP,
  94.               SIGTSTP,SIGTTIN,SIGTTOU,SIGIO,
  95.               SIGURG,SIGXCPU,SIGXFSZ,SIGVTALRM,SIGPROF,
  96.               SIGWINCH};
  97.  
  98. #include <errno.h>
  99.  
  100. sound_effect::sound_effect(char *filename)
  101. {
  102.     int handle;
  103.  
  104.   if (sfx_driver_out_fd < 0) return;
  105.  
  106.     long sample_speed;
  107.     void *dat=read_wav(filename,sample_speed,size); 
  108.  
  109.     handle = sfx_end;
  110.     memcpy(sfx_shm_addr + handle, dat, size);
  111.     jfree(dat);
  112.     sfx_end += size;
  113.  
  114.     int fail=0;
  115.     uchar cmd=SFXCMD_REGISTER;
  116.  
  117.     if (write(sfx_driver_out_fd,&cmd,1)==0)
  118.         fail=1;
  119.     else if (write(sfx_driver_out_fd,&handle,sizeof(handle))!=sizeof(handle))
  120.         fail=1;
  121.     else if (write(sfx_driver_out_fd,&size,sizeof(size))!=sizeof(size))
  122.         fail=1;
  123.     else if (read(sfx_driver_in_fd,&cmd,1)!=1)
  124.         fail=1;
  125.     else if (cmd!=1)    // did we get an 'OK' back so when can delete the shm ID?
  126.         fail=1;
  127.  
  128.     sfx_list=new sfx_handle(this,sfx_list,handle);    
  129.     data=(void *)sfx_list;
  130.  
  131. }
  132.  
  133.  
  134. sound_effect::~sound_effect()
  135. {
  136. }
  137.  
  138.  
  139. void sound_effect::play(int volume, int pitch, int panpot)
  140. {
  141.   if (sfx_driver_out_fd>=0 && data)
  142.   {   
  143.     sfx_handle *h=(sfx_handle *)data;
  144.     uchar cmd=SFXCMD_PLAY;
  145.     int fail=0;
  146.     if (write(sfx_driver_out_fd,&cmd,1)!=1)
  147.       fail=1;
  148.     else if (write(sfx_driver_out_fd,&h->handle,sizeof(h->handle))!=sizeof(h->handle))
  149.       fail=1;
  150.     else if (write(sfx_driver_out_fd,&volume,sizeof(volume))!=sizeof(volume))
  151.        fail=1;
  152.  
  153.     if (fail) 
  154.        kill_sound_driver();
  155.   }
  156. }
  157.  
  158.  
  159. int sound_init(int argc, char **argv) 
  160. {
  161.   int i;
  162.  
  163.   for (i=1;i<argc;i++)
  164.   {
  165.     if (!strcmp(argv[i],"-nosound"))
  166.     {
  167.       dprintf("sound : disabled with (-nosound)\n");
  168.       sfx_driver_out_fd=-1;
  169.       return 0;
  170.     }
  171.   }
  172.  
  173. #if defined(__linux__)
  174.   FILE *sfx_driver_fp=popen("lnx_sdrv","r");
  175. #elif defined(_AIX)
  176.   FILE *sfx_driver_fp=popen("/usr/bin/run_ums aix_sdrv","r");
  177. #else
  178.   FILE *sfx_driver_fp=popen("sgi_sdrv","r");
  179. #endif
  180.  
  181.   if (!sfx_driver_fp)
  182.   {
  183.     dprintf("Error starting sound effect, could not run sfx driver\n"
  184.         "make sure it is in your path and you execute permission\n");
  185.     sfx_driver_out_fd=-1;
  186.     return 0;
  187.   }
  188.  
  189.   char str[100];
  190.   if (!fgets(str,100,sfx_driver_fp) || !sscanf(str,"%d",&sdriver_pid) || sdriver_pid<0)
  191.   {
  192. //    pclose(sfx_driver_fp);
  193.     dprintf("sound effects driver returned failure, sound effects disabled\n");
  194.  //   pclose(sfx_driver_fp);
  195.     sfx_driver_out_fd=-1;
  196.     return 0;
  197.   } else
  198.   {
  199. //    pclose(sfx_driver_fp);
  200.  
  201.     do
  202.     { milli_wait(50);
  203.     } while (access(DIN_NAME,R_OK)); 
  204.     sfx_driver_in_fd=open(DIN_NAME,O_RDWR);
  205.  
  206.     do
  207.     { milli_wait(50);
  208.     } while (access(DOUT_NAME,W_OK)); 
  209.     fprintf(stderr,"opening %s for writing\n",DOUT_NAME);
  210.     sfx_driver_out_fd=open(DOUT_NAME,O_RDWR);
  211.  
  212. // create large (3Mb) shared mem segment for storing all sfx
  213.         sfx_shm_id=shmget(IPC_PRIVATE,3*1024*1024,IPC_CREAT | 0777);
  214.         sfx_shm_addr=shmat(sfx_shm_id,NULL,0);
  215.  
  216.         if (write(sfx_driver_out_fd, &sfx_shm_id, sizeof(sfx_shm_id))!=sizeof(sfx_shm_id))
  217.         {
  218.             dprintf("can't talk with sdrv\n");
  219.             return 0;
  220.         }
  221.         char cmd;
  222.         read(sfx_driver_in_fd, &cmd, 1);
  223.         shmctl(sfx_shm_id,IPC_RMID,NULL);
  224.  
  225.     for (i=0;i<TOTAL_SIGS;i++)
  226.        signal(sigs[i],sfx_clean_up);
  227.  
  228.     atexit(sound_uninit);
  229.   }
  230.   
  231.   return SFX_INITIALIZED;
  232. }
  233.  
  234.  
  235. void sound_uninit()
  236. {
  237.   if (sfx_driver_out_fd>=0)
  238.   {
  239.     if (sfx_list)
  240.     {
  241. #ifdef __sgi
  242.       sfx_clean_up();
  243. #else
  244.       sfx_clean_up(1);
  245. #endif
  246.     }
  247.    
  248.     uchar cmd;
  249.     cmd=SFXCMD_QUIT;
  250.     write(sfx_driver_out_fd,&cmd,1);   // send quit commmand to the driver
  251.     close(sfx_driver_out_fd);          // close connection
  252.     close(sfx_driver_in_fd);          // close connection
  253.     sfx_driver_out_fd=-1;
  254.     sfx_driver_in_fd=-1;
  255.   }
  256. }
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263. song::song(char *filename) { ; }
  264.  
  265. void song::play(unsigned char volume) { ; }
  266. int song::playing() { return 0; }
  267. void song::set_volume(int vol)  { ; }
  268. void song::stop(long fadeout_time) { ;  }
  269. int playing() { return 0; }
  270. song::~song() { ; }
  271.  
  272.  
  273.  
  274.