home *** CD-ROM | disk | FTP | other *** search
/ Mega CD-ROM 1 / megacd_rom_1.zip / megacd_rom_1 / MUSIC / INT_MUS.ZIP / INT_MUS.C next >
Text File  |  1988-09-25  |  3KB  |  144 lines

  1. /**********************************************/
  2. /*  int_music.c                               */
  3. /*  written by Raymond F. Genovese            */
  4. /**********************************************/
  5.  
  6.  
  7. #include <dos.h>
  8.  
  9. /* the note defines */
  10. #define C    0
  11. #define Cs   1
  12. #define Db   1
  13. #define D    2
  14. #define Ds   3
  15. #define Eb   3
  16. #define E    4
  17. #define F    5
  18. #define Fs   6
  19. #define Gb   6
  20. #define G    7
  21. #define Gs   8
  22. #define Ab   8
  23. #define A    9
  24. #define As  10
  25. #define Bb  10
  26. #define B   11
  27.  
  28. /* psuedo operations defines */
  29. #define REPEAT 12   /* repeat from begining    */
  30. #define SO     13   /* switch octaves          */
  31. #define R      14   /* rest (duration follows) */
  32. #define END    15   /* end of the song         */
  33.  
  34. /* duration defines */
  35. #define S    1
  36. #define EI   2
  37. #define Q    4
  38. #define H    8
  39. #define W   16
  40. #define DW  32
  41.  
  42. /* the notes:frequency array */
  43. unsigned int notes[6][12]={
  44.   /* actually starts at octave #3 */
  45.   {17145,17173,16209,15299,14441,13630,12865,12143,11462,10818,10211,9638},
  46.   {9097,8586,8105,7650,7224,6816,6432,6071,5729,5409,5105,4819},
  47.   {4548,4293,4052,3825,3610,3410,3216,3036,2867,2705,2554,2409},
  48.   {2274,2147,2026,1912,1805,1702,1608,1518,1433,1352,1276,1205},
  49.   {1137,1073,1013,956,903,852,804,759,716,676,638,602},
  50.   {569,537,507,478,451,426,402,380,358,338,319,301}
  51.   };
  52.  
  53. unsigned char taps[]={SO,1,G,W,R,EI,G,Q,R,EI,SO,2,C,DW,R,W,
  54.    SO,1,G,W,R,EI,SO,2,C,Q,R,EI,E,DW,R,DW,
  55.    SO,1,G,W,R,EI,SO,2,C,Q,R,EI,E,W,R,S,
  56.    SO,1,G,W,R,EI,SO,2,C,Q,R,EI,E,W,R,S,
  57.    SO,1,G,W,R,EI,SO,2,C,Q,R,EI,E,W,R,S,
  58.    R,W,C,W,R,EI,E,Q,R,EI,G,W,
  59.    R,W,E,W,R,EI,C,W,R,EI,SO,1,G,W,R,W,
  60.    G,W,R,EI,G,Q,R,EI,SO,2,C,DW,R,DW,R,DW,
  61.    REPEAT};
  62.  
  63. /* globals for the song routines */
  64.   char c_note,c_duration,c_octave,*ptr,*s_song;
  65.   char sp_on,sp_off,lsb,msb,end,save_add1c[4];
  66.  
  67. void install(),restore();
  68. void interrupt p_song();
  69. void interrupt (*oldvector)();
  70.  
  71. void install()
  72. {
  73. /* installs the p_song interrupt                               */
  74. /* set ptr to the song data array before calling this routine! */
  75.  
  76. /* set up */
  77.   sp_off=inportb(97);
  78.   sp_on=sp_off|3;
  79.   /* set up song */
  80.   c_duration=0;
  81.   oldvector=getvect(0x1c);
  82.   disable();
  83.   setvect(0x1c,&p_song);
  84.   enable();
  85.   /* it's playing */
  86. }
  87.  
  88. void restore()
  89. {
  90. /* restores the old timer vector and speaker state               */
  91. /* *oldvector MUST have been initialized by install()!           */
  92.   disable();
  93.   setvect(0x1c,oldvector);
  94.   enable();
  95.   outportb(97,sp_off);
  96. }
  97.  
  98. void interrupt p_song()
  99. {
  100.   if(c_duration)
  101.     c_duration--;
  102.   else
  103.     {
  104.     c_note=*ptr;
  105.     switch(c_note)
  106.       {
  107.       case 12: ptr=s_song;
  108.            break;
  109.       case 13: ptr++;
  110.            c_octave=*ptr;
  111.            ptr++;
  112.            break;
  113.       case 14: ptr++;
  114.            c_duration=*ptr;
  115.                outportb(97,sp_off);
  116.            ptr++;
  117.            break;
  118.       case 15: end=1;
  119.            break;
  120.       default: ptr++;
  121.            c_duration=*ptr;
  122.            lsb=notes[c_octave][c_note]%256;
  123.            msb=notes[c_octave][c_note]/256;
  124.            outportb(67,182);
  125.            outportb(66,lsb);
  126.            outportb(66,msb);
  127.            outportb(97,sp_on);
  128.            ptr++;
  129.                break;
  130.        }
  131.      }
  132.  }
  133.  
  134. main()
  135. {
  136.   ptr=taps;
  137.   s_song=ptr;
  138.   install();
  139.   while(!kbhit())
  140.     printf("This is printing while playing\n");
  141.   restore();
  142.   
  143. }
  144.