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

  1. /* little test program to see if sb_drive.c will work under MSDOS */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <conio.h> /* for keyboard polling */
  5.  
  6. #include "sb_regs.h"
  7. #include "sblast.h"
  8.  
  9. #define GOOD 1
  10. #define FAIL 0
  11.  
  12. #define TRUE 1
  13. #define FALSE 0
  14.  
  15. struct sb_conf dev;
  16. struct sb_mixer_levels mix;
  17. struct sb_mixer_params mixpar;
  18.  
  19. unsigned char string1[]={'C','r','e','a','t','i','v','e',' ','V','o','i','c',
  20.                'e',' ','F','i','l','e',0x1a,0x1a,0,0x0a,1,0x29,0x11};
  21.  
  22. unsigned char sndbuf[10000];
  23.  
  24. #define USAGE \
  25. "output_file -r speed -m mic_vol -b"\
  26. "\n"\
  27. "output_file: Sound file name. Reqd\n"\
  28. "speed:     Output rate (samples/sec) (valid range [4000-43478] (SBPro)\n"\
  29. "mic_vol:    Microphone Volume (SBPro only) [0-7]\n"\
  30. "\n"\
  31. "\w:    Raw output (No header). Default is .VOC output\n"
  32.  
  33.  
  34.  
  35. int main(int argc, char** argv)
  36. {
  37.    int   tmp;
  38.    unsigned int speed, utmp;
  39.    unsigned time_constant;
  40.    int   mic_volume;
  41.    int   voc_file;
  42.    long  length;
  43.    long  count;
  44.    FILE  * outfile;
  45.    char  * outfile_name;
  46.    long  header_offset;
  47.  
  48.   int           opt, error_flag; /* GNU getopt(3) variables */
  49.   extern int     optind;
  50.   extern char    *optarg;
  51.  
  52.   /* Default options */
  53.   speed=8000;
  54.   mic_volume=7;
  55.   voc_file=TRUE;
  56.  
  57.   error_flag = 0;
  58.   while ((opt = getopt (argc, argv,
  59.             "r:R:M:m:bB")) != EOF)
  60.     switch (opt)
  61.       {
  62.       case 'b':                 /* raw output */
  63.       case 'B':
  64.      voc_file = FALSE;
  65.      break;
  66.       case 'r':                 /* output speed */
  67.       case 'R':
  68.     utmp = atoi(optarg);
  69.     if ((utmp>0)&&(utmp<43479))
  70.      {
  71.       speed=utmp;
  72.      }
  73.     else
  74.      {
  75.        printf("Invalid speed specified, valid limits are 0-43478.\n");
  76.        exit(1);
  77.      }
  78.     break;
  79.       case 'm':                 /* Microphone volume */
  80.       case 'M':
  81.         tmp=atoi(optarg);
  82.         if ((tmp>-1)&&(tmp<8))
  83.           mic_volume=tmp;
  84.         else
  85.           {
  86.         printf("Invalid microphone volume, limits 0-7\n");
  87.         exit(1);
  88.           }
  89.         break;
  90.       default:                  /* unrecognized option */
  91.     error_flag = 1;
  92.     break;
  93.       }
  94.  
  95.   if (error_flag || !argv[optind])
  96.     {
  97.       fprintf(stderr, "Usage: recorder " USAGE);
  98.       exit(1);
  99.     }
  100.  
  101. /* get input filename */
  102.   outfile_name = argv[optind];
  103.  
  104.    /* ok, see if sb exists */
  105.    dev.addr = 0x220;
  106.    dev.irq  = 7;
  107.    tmp = sb_probe(&dev);
  108.  
  109.    if (tmp == GOOD)
  110.        printf("Found SB at address=%d\n",dev.addr);
  111.    else
  112.      {
  113.        printf("No card found\n");
  114.        exit(1);
  115.      }
  116.  
  117.    /* now "attach" sbpro, set up interupts, etc.. */
  118.    tmp=sb_attach(&dev);
  119.    printf("result of attach was %d\n",tmp);
  120.  
  121.    /* checkout mixer settings */
  122.    mixer_get_levels( &mix );
  123.    printf("Mixer setup: \n");
  124.    printf("Master -> L:%d  R:%d\n",mix.master.l, mix.master.r);
  125.    printf("Voice  -> L:%d  R:%d\n",mix.voc.l, mix.voc.r);
  126.    printf("FM     -> L:%d  R:%d\n",mix.fm.l, mix.fm.r);
  127.    printf("Line   -> L:%d  R:%d\n",mix.line.l, mix.line.r);
  128.    printf("CD     -> %d\n",mix.cd);
  129.    printf("Mic    -> %d\n",mix.mic);
  130.  
  131.    mix.mic = mic_volume;
  132.    mixer_set_levels( &mix);
  133.  
  134.    /* record from mic, turn on filter so it doesnt sound like crap */
  135.    mixpar.hifreq_filter = FALSE;
  136.    mixpar.filter_input = TRUE;
  137.    mixpar.record_source=SRC_MIC;
  138.    mixer_set_params( &mixpar);
  139.  
  140.    if (speed) {
  141.      printf("setting speed to %u\n",speed);
  142.      dsp_set_speed(&speed);
  143.      printf("Using speed=%u\n",speed);
  144.    }
  145.    else
  146.      {
  147.        speed=-1;
  148.        dsp_set_speed(&speed);
  149.        printf("Using speed=%u\n",speed);
  150.      }
  151.  
  152.  
  153.  
  154.    /* ok, now lets send something to output */
  155.    outfile = fopen(outfile_name,"wb");
  156.    if (outfile==NULL) {
  157.      printf("Cannot open output file. Aborting\n");
  158.      exit(1);
  159.    }
  160.  
  161.  
  162. /* put a voc header on here if requested */
  163.    if (voc_file)
  164.    {
  165.     /* write out header */
  166.     fwrite(string1,1,sizeof(string1),outfile);
  167.  
  168.     /* now insert speed, length info */
  169.     /* guess length for now */
  170.     length = 0;
  171.  
  172.     /* mark start of sound data */
  173.     fputc(1, outfile);
  174.  
  175.     /* length is 3 bytes, LSB first, store length + 2 (why ask why?) */
  176.     /* store pos so we can go back later when we know length */
  177.     header_offset= ftell(outfile);
  178.     length += 2;
  179.     fputc(length&0xff,outfile);
  180.     fputc((length&0xff00)>>8,outfile);
  181.     fputc((length&0xff0000)>>16,outfile);
  182.     length -= 2;
  183.  
  184.     /* speed byte (SB) is given by
  185.      *      speed < 22222 ->   SB = 256-(1000000/speed)
  186.      *      speed > 22222 ->   SB = 65536-(256000000/speed)     */
  187.      if (speed > 22222)
  188.        time_constant = 65536L - (256000000L / (long)speed);
  189.      else
  190.        time_constant = 256 - (1000000/speed);
  191.  
  192.      fputc(time_constant & 0xff, outfile);
  193.      fputc((time_constant & 0xff00)>>8, outfile);
  194.    }
  195.      /* now record  */
  196.  
  197.    printf("Hit return to start recording.\n");
  198.    tmp=getchar();
  199.    printf("Hit a key to end recording\n");
  200.    length = 0;
  201.    do{
  202.      dsp_read(sndbuf, 5000);
  203.      tmp=fwrite(sndbuf, 1, 5000, outfile);
  204.      length += tmp;
  205.     }
  206.    while(!kbhit());
  207.    tmp=getch();
  208.  
  209.    /* end file, then stick length in header */
  210.    if (voc_file)
  211.     {
  212.       /* terminate file */
  213.       fputc(0,outfile);
  214.  
  215.       /* seek back to where length is stored and write it */
  216.       fseek(outfile, header_offset, SEEK_SET);
  217.  
  218.       /* length is 3 bytes, LSB first, store length + 2 (why ask why?) */
  219.       length += 2;
  220.       fputc(length&0xff,outfile);
  221.       fputc((length&0xff00)>>8,outfile);
  222.       fputc((length&0xff0000)>>16,outfile);
  223.     }
  224.  
  225.    /* we're outa here */
  226.    fclose(outfile);
  227.  
  228.   }
  229.