home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Aktief 1995 #3 / CDA3.iso / sound / tnypl211.zip / EX2.C < prev    next >
C/C++ Source or Header  |  1994-06-21  |  4KB  |  146 lines

  1. /* ex2.c - example #2 */
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include "modplay.h"
  6.  
  7. #ifdef __BORLANDC__
  8.  
  9. void waitvr(void)
  10. {
  11.     asm  mov  dx,0x3da
  12. l0: asm  in   al,dx
  13.     asm  test al,8
  14.     asm  jne  l0
  15. l1: asm  in   al,dx
  16.     asm  test al,8
  17.     asm  je   l1
  18. l2: asm  in   al,dx
  19.     asm  test al,1
  20.     asm  jne  l2
  21. }
  22.  
  23. void setborder(char n)
  24. {
  25.     asm  mov  dx,0x3c0
  26.     asm  mov  al,0x31
  27.     asm  out  dx,al
  28.     asm  mov  al,[n]
  29.     asm  out  dx,al
  30. }
  31.  
  32. void starttimer(void)
  33. {
  34.     asm  in   al,0x61
  35.     asm  or   al,0x01
  36.     asm  out  0x61,al
  37.     asm  mov  al,0xb4
  38.     asm  out  0x43,al
  39.     asm  mov  al,0xff
  40.     asm  out  0x42,al
  41.     asm  out  0x42,al
  42. }
  43.  
  44. unsigned short readtimer(void)
  45. {
  46.     asm  mov  al,0xb0
  47.     asm  out  0x43,al
  48.     asm  in   al,0x42
  49.     asm  mov  ah,al
  50.     asm  in   al,0x42
  51.     asm  xchg al,ah
  52.     asm  neg  ax
  53.     return _AX;
  54. }
  55.  
  56. #else
  57.  
  58. #pragma aux waitvr =    " mov  dx,3dah "\
  59.                         "L0:           "\
  60.                         " in   al,dx   "\
  61.                         " test al,8    "\
  62.                         " jne  L0      "\
  63.                         "L1:           "\
  64.                         " in   al,dx   "\
  65.                         " test al,8    "\
  66.                         " je   L1      "\
  67.                         "L2:           "\
  68.                         " in   al,dx   "\
  69.                         " test al,1    "\
  70.                         " jne  L2      " parm caller [] modify [ax dx];
  71.  
  72. #pragma aux setborder = " mov  dx,3c0h "\
  73.                         " mov  al,31h  "\
  74.                         " out  dx,al   "\
  75.                         " mov  al,ah   "\
  76.                         " out  dx,al   " parm caller [ah] modify [ax dx];
  77.  
  78. #pragma aux starttimer= " in   al,61h  "\
  79.                         " or   al,01h  "\
  80.                         " out  61h,al  "\
  81.                         " mov  al,0b4h "\
  82.                         " out  43h,al  "\
  83.                         " mov  al,0ffh "\
  84.                         " out  42h,al  "\
  85.                         " out  42h,al  " parm caller [] modify [ax];
  86.  
  87. #pragma aux readtimer = " mov  al,0b0h "\
  88.                         " out  43h,al  "\
  89.                         " in   al,42h  "\
  90.                         " mov  ah,al   "\
  91.                         " in   al,42h  "\
  92.                         " xchg al,ah   "\
  93.                         " neg  ax      " parm caller [] modify [ax];
  94.  
  95. void waitvr(void);
  96. void setborder(char n);
  97. void starttimer(void);
  98. unsigned short readtimer(void);
  99.  
  100. #endif
  101.  
  102. void main(void)
  103. {
  104.     Module *Song;
  105.     word Port;
  106.     byte IRQ,DRQ;
  107.     unsigned long counter,frames;
  108.     unsigned short ticks;
  109.  
  110.     if (MODDetectCard(&Port,&IRQ,&DRQ)) {
  111.         printf("Sound Blaster not found.\n");
  112.         return;
  113.     }
  114.     printf("Sound Blaster found at Addr:%03x IRQ:%d DMA:%d\n",Port,IRQ,DRQ);
  115.     if ((Song = MODLoadModule("TUNE.MOD")) != NULL) {
  116.         if (MODPlayModule(Song,8,22222,Port,IRQ,DRQ,PM_MANUAL))
  117.             printf("Error initializing the sound system.\n");
  118.         else {
  119.             printf("Press any key to stop ...\n");
  120.             for (frames = counter = 0; !kbhit(); frames++) {
  121.                 waitvr();
  122.                 setborder(1);
  123.                 starttimer();
  124.                 MODPoll();
  125.                 ticks = readtimer();
  126.                 setborder(0);
  127.                 counter += (unsigned long)ticks;
  128.                 printf("%5u ticks\r", ticks);
  129.             }
  130.             MODStopModule();
  131.             if (frames) {
  132.                 printf("%10lu frames\n%10lu ticks per frame (average)\n",
  133.                         frames, counter/frames);
  134.                 printf("%4.5f microseconds per frame (average)\n",
  135.                     (((float)counter/(float)frames) / 1193182.0) * 1000000.0);
  136.                 printf("\nNOTE: In my 386DX-40 i got 1150us per frame for the 8 channels module.\n");
  137.             }
  138.         }
  139.         MODFreeModule(Song);
  140.     }
  141.     else {
  142.         printf("Error loading modulefile.\n");
  143.     }
  144.     return;
  145. }
  146.