home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk1.iso / altsrc / articles / 11211 < prev    next >
Text File  |  1994-09-01  |  7KB  |  269 lines

  1. Path: wupost!gumby!newsxfer.itd.umich.edu!europa.eng.gtefsd.com!howland.reston.ans.net!newsserver.jvnc.net!gateway.rosedale.org!rosedale.org!mvachharajani
  2. From: mvachharajani@rosedale.org (Madhavi Vachharajani)
  3. Newsgroups: alt.sources
  4. Subject: Sound Blaster Code
  5. Date: 1 Sep 1994 16:39:14 GMT
  6. Organization: Educational Testing Service
  7. Lines: 258
  8. Message-ID: <mvachharajani.19.0@rosedale.org>
  9. NNTP-Posting-Host: mxv2521.pclan.rosedale.org
  10.  
  11. This code plays .VOC files through the Sound Blaster.  It requires that your
  12. 8 bit DMA is 1 and that your BLASTER environment variable is set.
  13.  
  14.  
  15. #include<string.h>
  16. #include<io.h>
  17. #include<fcntl.h>
  18. #include<sys\stat.h>
  19. #include<stdlib.h>
  20. #include<dos.h>
  21. #include<alloc.h>
  22. #include<stdio.h>
  23. #include<conio.h>
  24.  
  25. #define writedac(x) {   while(inportb(PORT + 0xC) & 0x80);  outportb(PORT + 0xC, (x)); }
  26.  
  27. void resetdsp();
  28. void setsamplerate(int);
  29. void initdma(char far *);
  30. void loadsong(char *);
  31. void playback();
  32. void deinit();
  33. void initsb();
  34. void irq(int, char);
  35. void getblaster();
  36.  
  37. int PORT=0x210, FREQ=22000, IRQ, DMA;
  38. unsigned int FSIZE, FSIZE2=0;
  39. char CLOSEIRQ = 0;
  40. int finished = 0;
  41. int DMACONST[4] = {7, 3, 1, 2};
  42. char far *vocbuf;
  43.  
  44. static void far interrupt (*OLDSB)(...);
  45. static void far interrupt SBHANDLER(...)
  46. {
  47.         enable();
  48.         inportb(PORT + 0xE);
  49.         if(FSIZE2 != 0)
  50.         {
  51.                 CLOSEIRQ=1;
  52.                 FSIZE = FSIZE2;
  53.                 FSIZE2 = 0;
  54.                 initdma(vocbuf+64000);
  55.                 playback();
  56.         }
  57.         else
  58.         {
  59.                 finished = 1;
  60.                 outportb(0x20,0x20);
  61.         }
  62. }
  63.  
  64. void main()
  65. {
  66.         char file[100];
  67.         clrscr();
  68.         getblaster();
  69.         printf("DMA Voc Play Version 1.0\n");
  70.         printf("Copyright 1994\n");
  71.         printf("By:\n   Neil Vachharajani\n");
  72.         printf("\nEnter Voc Filename: ");
  73.         gets(file);
  74.  
  75.         loadsong(file);
  76.         initsb();
  77.         initdma(vocbuf);
  78.         setsamplerate(FREQ);
  79.         playback();
  80.         while(!finished);
  81.         deinit();
  82.         farfree(vocbuf);
  83. }
  84.  
  85. void resetdsp()
  86. {
  87.         int cntr = 0;
  88.         int cntr2 = 0;
  89.         int found = 0;
  90.  
  91.         while(PORT <= 0x260 && !found)
  92.         {
  93.                 outportb(PORT + 0x6, 1);
  94.                 delay(3);
  95.                 outportb(PORT + 0x6, 0);
  96.                 cntr = 0;
  97.                 while((inportb(PORT + 0xE) < 128) && cntr < 100)
  98.                         cntr++;
  99.  
  100.                 if(cntr < 10 || (inportb(PORT+0xA) != 0xAA))
  101.                 {
  102.                         cntr2++;
  103.                         if(cntr == 10 || (inportb(PORT + 0xA) != 0xAA))
  104.                         {
  105.                                 PORT += 0x10;
  106.                                 cntr2 = 0;
  107.                         }
  108.                 }
  109.                 else
  110.                         found = 1;
  111.         }
  112.         if(!found)
  113.         {
  114.                 printf("Sound Blaster not found!!");
  115.                 getch();
  116.                 exit(0);
  117.         }
  118.         delay(3);
  119. }
  120.  
  121. void setsamplerate(int freq)
  122. {
  123.         writedac(0xD1);
  124.         int byte = 256 - (1000000 / freq);
  125.         writedac(0x40);
  126.         writedac(byte);
  127. }
  128.  
  129. void initdma(char far *s)
  130. {
  131.         unsigned int segment = FP_SEG(s);
  132.         unsigned int offset1 = FP_OFF(s);
  133.         long int to;
  134.         int t1;
  135.         char t2;
  136.  
  137.         to = ((unsigned long)segment << 4) + (unsigned long)offset1;
  138.         t1 = int(to & 0xFFFF);
  139.         t2 = to >> 16;
  140.  
  141.         disable();
  142.         outportb(0xA, 0x5);
  143.         outportb(0xC, 0x0);
  144.         outportb(0xB, 0x49);
  145.         outportb(DMA * 2, t1 & 0x00FF);
  146.         delay(1);
  147.         outportb(DMA * 2, t1 >> 8);
  148.         delay(1);
  149.         outportb(0x80 + DMACONST[DMA], t2);
  150.         delay(1);
  151.         outportb((DMA * 2) + 1, (FSIZE-1) & 0x00FF);
  152.         delay(1);
  153.         outportb((DMA * 2) + 1, ((FSIZE-1) & 0xFF00) >> 8);
  154.         delay(1);
  155.         outportb(0xA, 0x1);
  156.         delay(1);
  157.         enable();
  158.         delay(3);
  159. }
  160.  
  161.  
  162. void playback()
  163. {
  164.         if(CLOSEIRQ)
  165.                 outportb(0x20, 0x20);
  166.         writedac(0x14);
  167.         writedac((FSIZE-1) & 0xFF)
  168.         writedac((FSIZE-1) >> 8);
  169. }
  170.  
  171.  
  172. void loadsong(char *fname)
  173. {
  174.         int handle;
  175.  
  176.         if((handle = open(fname, O_BINARY | O_RDWR, S_IREAD | S_IWRITE)) == -1)
  177.         {
  178.                 printf("ERROR: Cannot load song\r\n");
  179.                 printf("Abnormal Exit from DMA Play");
  180.                 free(vocbuf);
  181.                 exit(0);
  182.         }
  183.         unsigned long size = filelength(handle);
  184.         if((vocbuf = (char far *)farmalloc(size+16))==NULL)
  185.         {
  186.                 close(handle);
  187.                 exit(0);
  188.         }
  189.         if(size <= 64000)
  190.         {
  191.                 read(handle, vocbuf, 64000);
  192.                 FSIZE = size;
  193.         }
  194.         else
  195.         {
  196.                 read(handle, vocbuf, 64000);
  197.                 read(handle, vocbuf+64000, size - 64000);
  198.                 FSIZE = 64000;
  199.                 FSIZE2 = size - 64000;
  200.         }
  201.         close(handle);
  202. }
  203.  
  204. void deinit()
  205. {
  206.         disable();
  207.         setvect(0x8 + IRQ, OLDSB);
  208. //      irq(5, 0);
  209.         enable();
  210. }
  211.  
  212. As I said earlier I would post code for playing VOCS.  
  213. void initsb()
  214. {
  215.         resetdsp();
  216.         disable();
  217.         OLDSB = getvect(0x8 + IRQ);
  218.         setvect(0x8 + IRQ, SBHANDLER);
  219.         irq(5, 1);
  220.         enable();
  221. }
  222.  
  223. void irq(int irq, char oo)
  224. {
  225.         char orig, edited, temp;
  226.         orig = inportb(0x21);
  227.         if(oo)
  228.         {
  229.                 temp = ~(1 << irq);
  230.                 edited = orig & temp;
  231.         }
  232.         else
  233.         {
  234.                 temp = 1 << temp;
  235.                 edited = orig | temp;
  236.         }
  237.  
  238.         outportb(0x21, edited);
  239. }
  240.  
  241. void getblaster()
  242. {
  243.         char temp[2];
  244.         char *blaster = getenv("BLASTER");
  245.         if(blaster[0] == NULL)
  246.         {
  247.                 printf("Blaster String not found.");
  248.                 exit(0);
  249.         }
  250.         char *ptr = strchr(blaster, 'I');
  251.         temp[0] = *(ptr+1);
  252.         temp[1] = 0;
  253.         IRQ = atoi(temp);
  254.         ptr = strchr(blaster, 'D');
  255.         temp[0] = *(ptr+1);
  256.         temp[1] = 0;
  257.         DMA = atoi(temp);
  258.         if(DMA > 3)
  259.         {
  260.                 printf("8 Bit DMA's only.  \nEdit the Dx Parameter in your Blaster String.");
  261.                 exit(0);
  262.         }
  263. }
  264.  
  265. /----------------------------------------------------------------------\
  266. | Disclaimer:  Opinions expressed are those of the author and do not   |
  267. |              reflect those of the Educational Testing Service.       |
  268. \----------------------------------------------------------------------/
  269.