home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / SOUNDUTILS / audioplay.lzh / AUDIOPLAY / SRC / play.c < prev    next >
C/C++ Source or Header  |  1995-04-19  |  3KB  |  182 lines

  1. #define MAIN
  2. #include "play.h"
  3.  
  4. extern int auplay(), iffplay(), wavplay();
  5.  
  6. int32 value;
  7.  
  8. static int locked = FALSE;
  9.  
  10. void gotsignl(signum)
  11. signal_code signum;
  12. {
  13.     sigarrived = signum;
  14.  
  15.     if (signum == 2 || signum == 3 ) {
  16.         cleanup(0, NULL);
  17.     }
  18.  
  19.     return;
  20. }
  21.  
  22.  
  23. main(argc,argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.     u_int32 magic;
  28.     path_id fd;
  29.     u_int32 isFile;
  30.     char **p;
  31.  
  32.     /* set defaults */
  33.     debug = 0;
  34.     quiet = 0;
  35.     force = 0;
  36.     file[0] = NULL;
  37.     sig = 0;
  38.     pid = 0;
  39.     eventID = -1;
  40.     bufferSize = DEFAULT_BUFFER;
  41.  
  42.     /* parse command line arguments */
  43.     p = argv + 1;
  44.     while (*p != NULL) {
  45.         if (**p == '-') {
  46.             optparse(*p);
  47.         } else {
  48.             isFile = 1;
  49.         }
  50.         p++;
  51.     }
  52.  
  53.     if (isFile == 0) {
  54.         /* no files were specified */
  55.         printuse();
  56.         exit(0);
  57.     }
  58.  
  59.     /* set up signal handler */
  60.     intercept(gotsignl);
  61.     /* create/link to the sound event */
  62.     eventID = _ev_creat(SND_FREE ,SND_WAIT_INC, SND_SIG_INC, SND_EVENT_NAME);
  63.     if (eventID == -1) {
  64.         /* event probably already exists, link to it */
  65.         if ((eventID = _ev_link(SND_EVENT_NAME)) == -1) {
  66.             eventID = -1;
  67.             cleanup(0, "cannot create/link to event");
  68.         }
  69.         if (force) {
  70.             while(_ev_delete(SND_EVENT_NAME) == -1)
  71.                 _ev_unlink(eventID);
  72.             eventID = _ev_creat(SND_FREE ,SND_WAIT_INC, SND_SIG_INC, SND_EVENT_NAME);
  73.             if (eventID == -1) {
  74.                 cleanup(0, "cannot create/link to event");
  75.             }
  76.         }
  77.     }
  78.  
  79. #if 0
  80.     /* wait for resource to free up */
  81.     value = _ev_wait(eventID, SND_FREE, SND_FREE);
  82.  
  83.     /* we have the event!  now let's play sound */
  84. #endif
  85.  
  86.     fileCount = 0;
  87.  
  88.     p = argv + 1;
  89.  
  90.     while (*p != NULL) {
  91.         if (**p == '-') {
  92.             p++;
  93.             continue;
  94.         }
  95.  
  96.         if ((fd = open(*p, S_IREAD)) == -1) {
  97.             cleanup(errno, "cannot open file");
  98.         }
  99.  
  100.         size = 4;
  101.         if (read(fd, &magic, size) == -1) {
  102.             cleanup(errno, "cannot read header");
  103.         }
  104.  
  105.         /* seek back to beginning of file */
  106.         lseek(fd,0,SEEK_SET);
  107.  
  108.         if (!quiet) {
  109.             fprintf(stderr, "\nFile: %s\n", *p);
  110.         }
  111.  
  112.         lock_snd();
  113.         switch (magic) {
  114.             case MagicSun :
  115.             case MagicSunI :
  116.             case MagicDec :
  117.             case MagicDecI :
  118.             case DecDiva :
  119.                 auplay(fd);
  120.                 break;
  121.             case MagicIFF :
  122.                 iffplay(fd,*p);
  123.                 break;
  124.             case MagicVOC :
  125.                 vocplay(fd);
  126.                 break;
  127.             case MagicRIFF :
  128.                 wavplay(fd);
  129.                 break;
  130.             default:
  131.                 cleanup(0, "unknown sound format");
  132.         }
  133.         unlock_snd();
  134.         p++;
  135.     }
  136.     cleanup(0, NULL);
  137. }
  138.  
  139.  
  140. cleanup(error, string)
  141. int error;
  142. char *string;
  143. {
  144.     if (!quiet && string != NULL) {
  145.         fprintf(stderr, "%s\n", string);
  146.     }
  147.  
  148.     if (eventID != -1) {
  149.         unlock_snd();
  150.         /* unlink event */
  151.         _ev_unlink(eventID);
  152.     }
  153.  
  154.     if (pid != 0 && sig != 0) {
  155.         /* notify pid with sig */
  156.         kill(pid, sig);
  157.     }
  158.  
  159.     exit(error);
  160. }
  161.  
  162. lock_snd()
  163. {
  164.     int value;
  165.     
  166.     if (!locked) {
  167.         /* wait for resource to free up */
  168.         value = _ev_wait(eventID, SND_FREE, SND_FREE);
  169.         locked = TRUE;
  170.     }
  171. }
  172.  
  173. unlock_snd()
  174. {
  175.     if (locked) {
  176.         _ev_signal(eventID, SND_FREE);
  177.         /* unlink event */
  178.         locked = FALSE;
  179.     }
  180. }
  181.         
  182.