home *** CD-ROM | disk | FTP | other *** search
/ Shareware 1 2 the Maxx / sw_1.zip / sw_1 / PROGRAM / SBDOS10.ZIP / PLAYER.C < prev    next >
Text File  |  1992-06-25  |  6KB  |  223 lines

  1. /* little test program to see if sb_drive.c will work under MSDOS
  2.  * just playing the input RAW file. You can send it a VOC file and
  3.  * its should still work, although you must supply the speed      */
  4. #include <stdio.h>
  5. #include "sb_regs.h"
  6. #include "sblast.h"
  7.  
  8. #define GOOD 1
  9. #define FAIL 0
  10.  
  11. #define TRUE 1
  12. #define FALSE 0
  13.  
  14. struct sb_conf dev;
  15. struct sb_mixer_levels mix;
  16.  
  17. unsigned char sndbuf[10000];
  18. unsigned char vochdr[]={'C','r','e','a','t','i','v','e',' ','V','o','i','c',
  19.                'e',' ','F','i','l','e'};
  20.  
  21.  
  22. #define USAGE \
  23. "input_file -r speed -m master_vol -v voice_vol -s"\
  24. "\n"\
  25. "input_file: Sound file name. Reqd\n"\
  26. "speed:     Output rate (samples/sec) (valid range [4000-43478] (SBPro)\n"\
  27. "master_vol:    Master Volume (SBPro only) [0-15]\n"\
  28. "voice_vol:     DAC Volume (SBPro only) [0-15]\n"\
  29. "\n" \
  30. "-s: Stereo output (SBPro only)\n"\
  31.  
  32.  
  33. int main(int argc, char** argv)
  34. {
  35.    int   tmp;
  36.    unsigned speed, utmp, vspeed;
  37.    int   speed_defined;
  38.    FILE  * infile;
  39.    unsigned long ltmp;
  40.    int stereo;
  41.    int master_volume, voice_volume;
  42.    char * infile_name;
  43.  
  44.   int           opt, error_flag; /* GNU getopt(3) variables */
  45.   extern int     optind;
  46.   extern char    *optarg;
  47.  
  48.   /* Default options */
  49.   speed=8000;
  50.   speed_defined=FALSE;
  51.   stereo= FALSE;
  52.   master_volume=15;
  53.   voice_volume=15;
  54.  
  55.   error_flag = 0;
  56.   while ((opt = getopt (argc, argv,
  57.             "r:R:M:m:V:v:sS")) != EOF)
  58.     switch (opt)
  59.       {
  60.       case 'r':                 /* output speed */
  61.       case 'R':
  62.     utmp = atoi(optarg);
  63.     if ((utmp>0)&&(utmp<43479))
  64.      {
  65.       speed=utmp;
  66.       speed_defined=TRUE;
  67.      }
  68.     else
  69.      {
  70.        printf("Invalid speed specified, valid limits are 0-43478.\n");
  71.        exit(1);
  72.      }
  73.     break;
  74.       case 's':                 /* Stereo output */
  75.       case 'S':
  76.     stereo = TRUE;
  77.     break;
  78.       case 'm':                 /* Master volume */
  79.       case 'M':
  80.         tmp=atoi(optarg);
  81.         if ((tmp>-1)&&(tmp<16))
  82.           master_volume=tmp;
  83.         else
  84.           {
  85.         printf("Invalid master volume, limits 0-15\n");
  86.         exit(1);
  87.           }
  88.         break;
  89.       case 'v':                 /* DAC volume */
  90.       case 'V':
  91.         tmp=atoi(optarg);
  92.         if ((tmp>-1)&&(tmp<16))
  93.           voice_volume=tmp;
  94.         else
  95.           {
  96.         printf("Invalid DAC voice volume, limits 0-15\n");
  97.         exit(1);
  98.           }
  99.         break;
  100.       default:                  /* unrecognized option */
  101.     error_flag = 1;
  102.     break;
  103.       }
  104.  
  105.   if (error_flag || !argv[optind])
  106.     {
  107.       fprintf(stderr, "Usage: player " USAGE);
  108.       exit(1);
  109.     }
  110.  
  111. /* get input filename */
  112.   infile_name = argv[optind];
  113.  
  114.    /* ok, see if sb exists */
  115.    /* set addr and irq to those for your card,
  116.     * factor default is given below              */
  117.    dev.addr = 0x220;
  118.    dev.irq  = 7;
  119.    tmp = sb_probe(&dev);
  120.  
  121.    if (tmp == GOOD)
  122.        printf("Found SB at address=%d\n",dev.addr);
  123.    else
  124.      {
  125.        printf("No card found\n");
  126.        exit(1);
  127.      }
  128.  
  129.    /* now "attach" sbpro, set up interupts, etc.. */
  130.    tmp=sb_attach(&dev);
  131.    if (tmp != GOOD)
  132.     {
  133.       printf("Couldnt attach to card, aborting..\n");
  134.       exit(1);
  135.     }
  136.  
  137.    /* set stereo/mono */
  138.    if (stereo) dsp_set_stereo(TRUE);
  139.  
  140.    /* set up mixer volumes */
  141.    mixer_get_levels( &mix );
  142.    mix.master.l = mix.master.r = master_volume;
  143.    mix.voc.l = mix.voc.r = voice_volume;
  144.    mixer_set_levels( &mix);
  145.    mixer_get_levels( &mix );
  146.  
  147.    /* ok, now lets send something to output */
  148.    infile = fopen(infile_name,"rb");
  149.    if (infile == NULL )
  150.      {
  151.        printf("Input file not found, aborting...\n");
  152.        exit(1);
  153.      }
  154.  
  155. /* Lets see if it is a .VOC file */
  156.    fread(sndbuf, 1, sizeof(vochdr), infile);
  157.    if (strncmp(sndbuf, vochdr, sizeof(vochdr))==0)
  158.      {
  159.        printf("Input file identified as a .VOC file.\n");
  160.  
  161.        /* now read over some more header stuff which just identifies
  162.     * the version number of the file - we'll assume its ok       */
  163.     fseek(infile, 7L, SEEK_CUR);
  164.  
  165.     /* next byte should be a 1, indicating data follows */
  166.     /* no support for silence or repeating type of blocks yet */
  167.     if (getc(infile)!=1)
  168.      {
  169.        printf("This player doesnt understand .VOC file, aborting.\n");
  170.        exit(1);
  171.      }
  172.  
  173.      /* next three bytes contain the length of the .VOC file */
  174.      /* we'll ignore this, just play till we reach EOF       */
  175.      fseek(infile, 3L, SEEK_CUR);
  176.  
  177.      /* next two bytes either have the speed and compression type,
  178.       * if it is a low speed file, else it will contain speed only
  179.       * for high speed files */
  180.       utmp=getc(infile)|(getc(infile)<<8);
  181.  
  182.      /* compression types only go up to a numeric type of 5 or so,
  183.       * and the lowest the MSB of the speed for high speed mode
  184.       * can be is D2, so if high byte of tmp is greater than 5, say,
  185.       * it should be safe to assume it is a high speed file. */
  186.       if (utmp > 0x0500)
  187.        {
  188.          vspeed=(unsigned int)(256000000L/(65536L-utmp));
  189.          printf("High speed file indentified, speed=%u.",vspeed);
  190.          if (!speed_defined) speed=vspeed;
  191.        }
  192.       else
  193.        {
  194.          vspeed=(unsigned int)(1000000L/(long)(256-(utmp&0xff)));
  195.          printf("Low speed file indentified, speed=%u.",speed);
  196.          if (!speed_defined) speed=vspeed;
  197.  
  198.          /* see if file is compressed, we dont know how to handle that*/
  199.          if (tmp > 0xff)
  200.            {
  201.         printf("Cant handle compressed files, aborting.\n");
  202.         exit(1);
  203.            }
  204.        }
  205.      } /* indentified as a .VOC file */
  206.  
  207.    /* now play to end of file */
  208.    /* set speed */
  209.    dsp_set_speed(&speed);
  210.    printf("Setting speed to %u.\n",speed);
  211.  
  212.    do
  213.     {
  214.      tmp=fread(sndbuf, 1, 10000, infile);
  215.      dsp_write(sndbuf, tmp);
  216.     }
  217.    while(!feof(infile));
  218.  
  219.    dsp_flush_dac();
  220.    fclose(infile);
  221.  
  222.   }
  223.