码迷,mamicode.com
首页 > 编程语言 > 详细

c语言乐曲演奏——《千本樱》

时间:2018-05-13 18:27:26      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:else   img   jpg   分享   info   delay   out   pre   mys   

这个程序着实花费了我好长的时间,我本身对音乐一窍不通,先是跟着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 }

 

c语言乐曲演奏——《千本樱》

标签:else   img   jpg   分享   info   delay   out   pre   mys   

原文地址:https://www.cnblogs.com/wkfvawl/p/9032832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!