home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 4 Drivers / 04-Drivers.zip / SBOS2DEV.ZIP / vocstuff / player.c < prev    next >
C/C++ Source or Header  |  1992-10-23  |  5KB  |  204 lines

  1. /* program to play VOC or raw sound files */
  2.  
  3. #include <stdio.h>
  4. #include <os2.h>
  5. #include <getopt.h>
  6. #include "sblast_user.h"
  7.  
  8. unsigned char sndbuf[10000];
  9. unsigned char vochdr[]={'C','r','e','a','t','i','v','e',' ','V','o','i','c',
  10.                'e',' ','F','i','l','e'};
  11.  
  12.  
  13. #define USAGE \
  14. "-r speed -s input_file"\
  15. "\n"\
  16. "input_file: Sound file name.\n"\
  17. "speed:     Output rate (samples/sec) (valid range [4000-43478] on SBPro)\n"\
  18. "                                                  [4000-22000] on SBREG)\n"\
  19. "\n" \
  20. "-s: Stereo output (SBPro only)\n"\
  21. "-b: Read in RAW file (no .VOC HDR)\n"
  22.  
  23.  
  24. int main(int argc, char** argv)
  25. {
  26.    FILE  * infile;
  27.    char * infile_name;
  28.  
  29.    int speed_defined;
  30.    int raw_mode;
  31.    FLAG stereo;
  32.  
  33.  
  34.    HFILE  sndhandle;
  35.    USHORT speed, utmp, vspeed;
  36.    ULONG ltmp, action, status, datlen, parlen, numwritten;
  37.    int   tmp;
  38.  
  39.    
  40.  
  41.  
  42.   int           opt, error_flag; /* GNU getopt(3) variables */
  43.   extern int     optind;
  44.   extern char    *optarg;
  45.  
  46.   /* Default options */
  47.   speed=8000;
  48.   speed_defined=FALSE;
  49.   stereo= FALSE;
  50.   raw_mode = 0;
  51.  
  52.   error_flag = 0;
  53.   while ((opt = getopt (argc, argv,
  54.             "r:R:sSbB")) != EOF)
  55.     switch (opt)
  56.       {
  57.       case 'r':                 /* output speed */
  58.       case 'R':
  59.     utmp = atoi(optarg);
  60.     if ((utmp>0)&&(utmp<43479))
  61.      {
  62.       speed=utmp;
  63.       speed_defined=TRUE;
  64.      }
  65.     else
  66.      {
  67.        printf("Invalid speed specified, valid limits are 0-43478.\n");
  68.        exit(1);
  69.      }
  70.     break;
  71.       case 's':                 /* Stereo output */
  72.       case 'S':
  73.     stereo = TRUE;
  74.     break;
  75.       case 'b':                 /* Raw mode output */
  76.       case 'B':
  77.     raw_mode = TRUE;
  78.     break;
  79.       default:                  /* unrecognized option */
  80.     error_flag = 1;
  81.     break;
  82.       }
  83.  
  84.   if (error_flag || !argv[optind])
  85.     {
  86.       fprintf(stderr, "Usage: player " USAGE);
  87.       exit(1);
  88.     }
  89.  
  90. /* get input filename */
  91.   infile_name = argv[optind];
  92.  
  93.    /* ok, see if sb exists */
  94.    status = DosOpen( "SBDSP$", &sndhandle, &action, 0,
  95.                           FILE_NORMAL, FILE_OPEN,
  96.    OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | OPEN_FLAGS_WRITE_THROUGH,
  97.               NULL );
  98.  
  99.    if (status)
  100.      {
  101.        printf("Error opening sound card.\n");
  102.        exit(1);
  103.      }
  104.  
  105.  
  106.    /* set stereo/mono */
  107.    if (stereo) 
  108.      {
  109.        printf("Stereo output requested\n");
  110.        datlen=1;
  111.        parlen=0;
  112.        status=DosDevIOCtl(sndhandle, DSP_CAT, DSP_IOCTL_STEREO,
  113.               NULL, 0, &parlen,
  114.               &stereo, datlen, &datlen);
  115.        if (status)
  116.      {
  117.        printf("Error setting to stereo mode - is this a SB Pro?\n");
  118.        exit(1);
  119.      }
  120.      }
  121.  
  122.    /* ok, now lets send something to output */
  123.    infile = fopen(infile_name,"rb");
  124.    if (infile == NULL )
  125.      {
  126.        printf("Input file not found, aborting...\n");
  127.        exit(1);
  128.      }
  129.  
  130. /* Lets see if it is a .VOC file */
  131.   if (!raw_mode)
  132.   {
  133.    fread(sndbuf, 1, sizeof(vochdr), infile);
  134.    if (strncmp(sndbuf, vochdr, sizeof(vochdr))==0)
  135.      {
  136.        printf("Input file identified as a .VOC file.\n");
  137.  
  138.        /* now read over some more header stuff which just identifies
  139.     * the version number of the file - we'll assume its ok       */
  140.     fseek(infile, 7L, SEEK_CUR);
  141.  
  142.     /* next byte should be a 1, indicating data follows */
  143.     /* no support for silence or repeating type of blocks yet */
  144.     if (getc(infile)!=1)
  145.      {
  146.        printf("This player doesnt understand .VOC file, aborting.\n");
  147.        exit(1);
  148.      }
  149.  
  150.      /* next three bytes contain the length of the .VOC file */
  151.      /* we'll ignore this, just play till we reach EOF       */
  152.      fseek(infile, 3L, SEEK_CUR);
  153.  
  154.      /* next two bytes either have the speed and compression type,
  155.       * if it is a low speed file, else it will contain speed only
  156.       * for high speed files */
  157.       utmp=getc(infile)|(getc(infile)<<8);
  158.  
  159.      /* compression types only go up to a numeric type of 5 or so,
  160.       * and the lowest the MSB of the speed for high speed mode
  161.       * can be is D2, so if high byte of tmp is greater than 5, say,
  162.       * it should be safe to assume it is a high speed file. */
  163.       if (utmp > 0x0500)
  164.        {
  165.          vspeed=(unsigned int)(256000000L/(65536L-utmp));
  166.          if (!speed_defined) speed=vspeed;
  167.          printf("High speed file indentified, speed=%u.",vspeed);
  168.        }
  169.       else
  170.        {
  171.          vspeed=(unsigned int)(1000000L/(long)(256-(utmp&0xff)));
  172.          if (!speed_defined) speed=vspeed;
  173.          printf("Low speed file indentified, speed=%u.",speed);
  174.  
  175.          /* see if file is compressed, we dont know how to handle that*/
  176.          if (utmp > 0xff)
  177.            {
  178.         printf("Cant handle compressed files, aborting.\n");
  179.         exit(1);
  180.            }
  181.        }
  182.      } /* indentified as a .VOC file */
  183.    } /* if (!raw_mode) */
  184.  
  185.    /* now play to end of file */
  186.    /* set speed */
  187.    datlen=2;
  188.    parlen=0;
  189.    status=DosDevIOCtl(sndhandle, DSP_CAT, DSP_IOCTL_SPEED,
  190.               NULL, 0, &parlen,
  191.               &speed, datlen, &datlen);
  192.    printf("Setting speed to %u.\n",speed);
  193.  
  194.    do
  195.     {
  196.      tmp=fread(sndbuf, 1, 10000, infile);
  197.      DosWrite(sndhandle, &sndbuf, tmp, &numwritten);
  198.     }
  199.    while(!feof(infile));
  200.  
  201.    fclose(infile);
  202.    DosClose(sndhandle);
  203.   }
  204.