home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / SBF3.ZIP / DACDMA.C < prev    next >
C/C++ Source or Header  |  1993-09-20  |  3KB  |  113 lines

  1. /* Output to SB DAC using DMA mode (up to 64K only) */
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <dos.h>
  6. #include <io.h>
  7. #include <alloc.h>
  8. #include <stdlib.h>
  9.  
  10. #include "sb.h"
  11.  
  12. extern int optind;    /* index of which argument is next      */
  13. extern char *optarg;  /* pointer to argument of current option */
  14. extern int opterr;    /* allow error message  */
  15. int getopt(int argc, char *argv[], char *optionS);
  16.  
  17. void main(int argc, char *argv[])
  18. {
  19.     FILE *f;
  20.     signed char far *raw, far *aligned;
  21.     long sample_len;
  22.     unsigned sl, nr, sr=11000;
  23.     unsigned long physical, aligned_physical;
  24.     int stereo = 0, error = 0;
  25.     char ch;
  26.  
  27.     while((ch = getopt(argc,argv,"sr:")) != EOF)
  28.     {
  29.     switch(ch)
  30.     {
  31.         case 's':
  32.         stereo = 1;
  33.         break;
  34.         case 'r':
  35.         sr = atoi(optarg);
  36.         break;
  37.         case '?':
  38.         error++;
  39.         break;
  40.     }
  41.     }
  42.     if(error || optind == argc)
  43.     {
  44.     puts("Usage: dacdma [-r rate] [-s] sample");
  45.     exit(1);
  46.     }
  47.  
  48.     if(Sb_Get_Params())
  49.     {
  50.         puts("BLASTER environment variable not set.");
  51.         exit(1);
  52.     }
  53.  
  54.     if(Sb_Init())
  55.     {
  56.     printf("Could not find Soundblaster!\n");
  57.     exit(1);
  58.     }
  59.     printf("Found Soundblaster at address %xh, IRQ %d, DMA %d.\n",
  60.     SbIOaddr,SbIRQ,SbDMAchan);
  61.  
  62.     f = fopen(argv[optind],"rb");
  63.     if(f == NULL)
  64.     {
  65.     printf("Could not open sample file %s\n",argv[optind]);
  66.     exit(1);
  67.     }
  68.     sample_len = filelength(fileno(f));
  69.     if(sample_len > 64000U)
  70.         sl = 64000U;
  71.     else
  72.         sl = (int)sample_len;
  73.  
  74.     raw = (signed char far *)farmalloc((unsigned long)sl + 65535L);
  75.     physical = ((unsigned long)FP_OFF(raw)) + (((unsigned long)FP_SEG(raw)) << 4);
  76.     aligned_physical = physical+0x0FFFFL;
  77.     aligned_physical &= 0xF0000L;
  78.     aligned=MK_FP((unsigned )((aligned_physical >> 4) & 0xFFFF),0);
  79.  
  80.     nr = fread(aligned,1,sl,f);
  81.     fclose(f);
  82.  
  83.     Sb_Init_Voice_DMA(NULL);   /* Use default interrupt handler */
  84.  
  85.     sr = Sb_Sample_Rate(sr,PLAY);
  86.     printf("Sample rate = %u Hz\n",sr);
  87.     printf("Output is %s.\n",(stereo) ? "stereo" : "mono");
  88.  
  89.     Sb_Voice(1);
  90.     printf("Playing sample\n");
  91.  
  92.     Sb_Voice_DMA(aligned,nr,stereo,PLAY);
  93.  
  94.     while(!kbhit() && !Sb_DMA_Complete())
  95.     ;
  96.     if(!Sb_DMA_Complete())
  97.     {
  98.     Sb_Halt_DMA();
  99.         Sb_Init(); /* Necessary after early termination of a high-speed xfer */
  100.     getch();
  101.     }
  102.  
  103.     Sb_Voice(0);
  104.  
  105.     printf("Done.\n");
  106.  
  107.     Sb_DeInit_Voice_DMA();
  108.  
  109.     farfree(raw);
  110.  
  111.     exit(0);
  112. }
  113.