這個程序着實花費了我好長的時間,我本身對音樂一竅不通,先是跟着girl friend學習了簡譜,根據c調44拍的《千本櫻》寫下了下面的程序。
1 #include<stdio.h> 2 #include<dos.h> 3 void mysound(unsigned int freq,int times) 4 { 5 int i; 6 union 7 { 8 int divisor; 9 char c[2]; 10 } count; 11 char bits; 12 count.divisor=1193180/freq; 13 outportb(0x43,0xb6); 14 outportb(0x42,count.c[0]); 15 outportb(0x42,count.c[1]); 16 bits=inportb(0x61); 17 outportb(0x61,bits|3); 18 for(i=0; i<times; i++) 19 delay(120); 20 outport(0x61,bits&0xfc); 21 outportb(0x61,bits); 22 } 23 int main() 24 { 25 int freq[8]= {0,256,294,330,349,392,440,494}; 26 int song[]= {5,6,2,1,2,1,5,6,2,1,2,1, 27 5,6,2,1,2,1,4,3,4,3,2,1, 28 5,6,2,1,2,1,5,6,2,1,2,1, 29 5,6,8,11,10,11,10,9,8,6, 30 31 5,6,2,1,2,1,5,6,2,1,2,1, 32 5,6,2,1,2,1,4,3,4,3,2,1, 33 2,1,2,4,2,4,6,5,6,8,6,8, 34 11,10,11,10,9,8,9,2,4, 35 36 5,6,2,1,2,1,5,6,2,1,2,1, 37 5,6,2,1,2,1,4,3,4,3,2,1, 38 5,6,2,1,2,1,5,6,2,1,2,1, 39 5,6,8,11,10,11,10,9,8,6, 40 41 5,6,2,1,2,1,5,6,2,1,2,1, 42 5,6,2,1,2,1,4,3,4,3,2,1, 43 5,4,6,8,9,8,6,5,2,4,5,6, 44 2,2,2,0,1,2,0, 45 46 2,2,2,1,2,4,4,5, 47 2,2,2,1,2,1,-6,1, 48 2,2,2,1,2,4,4,5, 49 6,5,6,5,4,2, 50 51 2,2,2,1,2,4,4,5, 52 2,2,2,1,2,1,-6,1, 53 2,2,2,1,2,4,4,5, 54 6,5,6,5,4,2, 55 56 4,3,2,1, 57 1,1,2,-6,-5,-6,-6, 58 -6,1,-2,5,3, 59 4,4,3,1,2,2, 60 61 4,3,2,1, 62 1,1,2,-6,-5,-6,-6,1, 63 2,2,2,4,5, 64 3,3,3,2,4, 65 66 5,5,5,5,6,6,6, 67 8,9,5,4,6,2,4, 68 5,5,5,5,6,6,6, 69 6,6,5,4,4,2,4, 70 71 5,5,5,5,6,6,6, 72 8,9,5,4,6,2,4, 73 6,6,5,4, 74 5,6,4,1,2,2,4, 75 76 77 5,4,6,7,8,0, 78 0,0,0,0, 79 0,0,0,0, 80 81 82 }; 83 float div[]= {8,8,16,16,16,16,8,8,16,16,16,16, 84 8,8,16,16,16,16,8,8,32,32,16,8, 85 8,8,16,16,16,16,8,8,16,16,16,16, 86 8,8,8,8,16,16,16,16,8,8, 87 88 8,8,16,16,16,16,8,8,16,16,16,16, 89 8,8,16,16,16,16,8,8,32,32,16,8, 90 8,16,16,8,16,16,8,16,16,8,16,16, 91 8,16,32,32,8,8,4,8,8, 92 93 8,8,16,16,16,16,8,8,16,16,16,16, 94 8,8,16,16,16,16,8,8,32,32,16,8, 95 8,8,16,16,16,16,8,8,16,16,16,16, 96 8,8,8,8,16,16,16,16,8,8, 97 98 8,8,16,16,16,16,8,8,16,16,16,16, 99 8,8,16,16,16,16,8,8,32,32,16,8, 100 16,16,16,16,16,16,16,16,8,8,8,8, 101 8,16,16,8,8,4,4, 102 103 8,8,8,8,8,8,8,8, 104 8,8,8,8,8,8,8,8, 105 8,8,8,8,8,8,8,8, 106 4,8,16,16,4,4, 107 108 8,8,8,8,8,8,8,8, 109 8,8,8,8,8,8,8,8, 110 8,8,8,8,8,8,8,8, 111 4,8,16,16,4,4, 112 113 4,4,4,4, 114 8,16,16,8,8,4,4, 115 8,4,8,4,4, 116 8,8,8,8,4,4, 117 118 4,4,4,4, 119 8,16,16,8,8,4,8,8, 120 8,4,8,4,4, 121 4,4,4,8,8, 122 123 8,16,16,8,8,4,4, 124 8,8,8,8,4,8,8, 125 8,16,16,8,8,4,4, 126 8,8,8,8,4,8,8, 127 128 8,16,16,8,8,4,4, 129 8,8,8,8,4,8,8, 130 4,4,4,4, 131 8,8,8,8,4,8,8, 132 133 8,8,8,8,4,4, 134 4,4,4,4, 135 4,4,4,4, 136 137 }; 138 unsigned int f; 139 int times,i; 140 for(i=0; song[0]; i++) 141 { 142 if(song[i]<0) 143 f=freq[-song[i]]/2; 144 else if(song[i]>7) 145 f=freq[song[i]%7]*2; 146 else if(song[i]==0) 147 f=100000000000000; 148 else 149 f=freq[song[i]]; 150 times=16/div[i]; 151 mysound(f,times); 152 } 153 return 0; 154 }
再附上兩首簡單音樂的代碼:
1 int song[]={ 8,8,5,5,3,6,5,3, 2 2,1,2,3,1,0, 3 3,1,3,3,1, 4 3,3,5,6,5,0, 5 6,6,6,5,4,4,4, 6 2,3,2,1,2,0, 7 3,1,0,3,1,0, 8 3,3,5,6,6,0, 9 8,5,5,6,3, 10 2,1,2,3,5,5, 11 8,5,5,6,3, 12 2,1,2,3,1,1 13 14 15 }; 16 float div[]={ 8,8,8,8,8,8,8,8, 17 8,8,8,8,4,4, 18 4,4,8,8,4, 19 8,8,8,8,4,4, 20 8,8,8,8,8,8,4, 21 8,8,8,8,4,4, 22 4,8,8,4,8,8, 23 8,8,8,8,4,4, 24 4,8,8,4,4, 25 8,8,8,8,4,4, 26 4,8,8,4,4, 27 8,8,8,8,4,4 28 29 };
int song[]= { 3,3,5,6,8,8,6, 5,5,6,5,5, 3,3,5,6,8,8,6, 5,5,6,5,5, 5,5,5,3,5, 6,6,5,5,5, 3,2,3,5,3,2, 1,1,2,1,1, 3,2,1,3,2,3, 5,6,8,5,5, 2,3,5,2,3,1,-6, 5,5,-6,1, 2,3,1,2,1,6, 5,5,5,5, 0,0,0,0 }; float div[]= { 4,8,8,8,8,8,8, 4,8,8,4,4, 4,8,8,8,8,8,8, 4,8,8,4,4, 4,4,4,8,8, 4,8,8,4,4, 4,8,8,4,8,8, 4,8,8,4,4, 8,8,8,8,4,4, 4,8,8,4,4, 4,8,8,8,8,8,8, 4,4,4,4, 4,8,8,8,8,8, 4,4,4,4, 4,4,4,4 };