本程序可以通过输入简谱用windows自带蜂鸣器函数播放出音乐,会编程的你可以借此在朋友面前show一下啦,哈哈
首先,下面这个是样例程序,里面有《国歌》《樱花草》《菊花台》《朋友》《生日快乐》这几首歌曲,以后有时间再做一些
播放直接引用相应函数即可
#include<windows.h>
#include<stdio.h>
void check();
void Birthday();
void JuHuaTai();
void YingHuaCao();
void GuoGe();
void PengYou();
int main()
{
PengYou();
return 0 ;
}
void check()
{
int ff=21;
unsigned a[]=
{
262,294,330,349,392,440,494,523,587,659,698,784,880,988,1046,1175,1318,1397,1568,1760,1976,
};
unsigned b[]=
{
1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,
};
for(int i=0; i<ff; i++)
Beep(a[i],b[i]);
}
void Birthday()
{
unsigned frequency[]=
{
392,392,440,392,523,494,
392,392,440,392,587,523,
392,392,784,659,523,494,440,
698,698,659,523,587,523
}
;
unsigned delay[]=
{
375,125,500,500,500,1000,
375,125,500,500,500,1000,
375,125,500,500,500,500,1000,
375,125,500,500,500,1000,
}
;
for(int i=0; i<25; i++)
{
Beep(frequency[i],delay[i]);
}
}
void JuHuaTai()
{
int LL=182;
unsigned frequency[]=
{
0,0,0,523,587,
659,659,784,880,880,1318,
1175,1046,1046,880,784,
880,784,659,587,523,440,523,
587,587,659,587,523,587,
659,659,784,880,880,1318,
1175,1046,1046,1175,1046,
784,784,659,988,1046,523,587,
659,587,523,
659,659,587,659,0,
659,784,659,587,659,
523,523,587,659,784,659,
587,587,523,587,0,
659,784,659,880,784,
880,784,784,659,784,784,
659,587,659,784,659,587,
587,587,523,587,
659,659,587,659,0,
659,784,659,587,659,
523,523,587,659,784,659,
587,587,523,587,0,
659,784,659,880,784,
880,784,784,659,784,
659,587,659,784,659,587,
587,523,523,
0,0,0,523,587,
659,659,784,880,880,1318,
1175,1046,1046,880,784,
880,784,659,587,523,440,523,
587,587,523,587,523,587,
659,659,784,880,880,1318,
1175,1046,1046,1175,1046,
784,784,659,988,1046,523,587,
659,587,
523,
};
unsigned delay[]=
{
500,500,500,250,250,
500,250,250,500,250,250,
250,250,250,250,1000,
250,250,250,250,500,250,250,
500,250,250,500,250,250,
500,250,250,500,250,250,
250,250,250,250,1000,
500,250,250,250,250,250,250,
1000,750,4250,
500,250,250,500,500,
250,250,250,250,1000,
500,250,250,250,250,500,
500,250,250,500,500,
750,125,125,250,750,
250,250,250,250,750,250,
500,250,250,500,250,250,
500,250,250,1000,
500,250,250,500,500,
250,250,250,250,1000,
500,250,250,250,250,500,
500,250,250,500,500,
750,125,125,250,750,
250,250,250,250,1000,
500,250,250,500,250,250,
250,250,1500,
500,500,500,250,250,
500,250,250,500,250,250,
250,250,250,250,1000,
250,250,250,250,500,250,250,
500,250,250,500,250,250,
500,250,250,500,250,250,
250,250,250,250,1000,
500,250,250,250,250,250,250,
1000,1000,
2000,
};
for(int i=0; i<LL; i++)
{
Beep(frequency[i],delay[i]);
}
}
void YingHuaCao()
{
int ff=228;
unsigned a[]=
{
523,659,587,659,
523,659,587,
523,659,587,659,
523,
523,659,587,659,
784,659,587,
523,
523,659,587,659,
523,659,587,
523,659,587,659,
523,
523,659,587,659,
784,659,587,
523,
659,587,
523,392,523,659,
587,659,587,
523,330,440,523,
494,523,494,440,523,440,392,0,523,523,
698,659,587,523,659,
587,659,587,
523,392,523,659,
587,659,587,
523,330,440,523,
494,523,494,440,523,440,392,0,523,523,
698,659,587,523,659,587,0,659,
523,587,523,0,494,440,
392,523,659,698,
587,0,659,
523,523,587,
523,0,494,440,
392,523,659,698,587,880,784,
659,784,784,659,587,587,880,784,
659,784,784,659,587,523,523,587,659,
784,659,587,784,659,
523,587,659,784,587,880,784,
659,784,784,659,587,
587,880,784,
659,784,784,659,587,
523,523,587,659,784,659,587,784,659,
523,587,659,784,587,0,659,
523,
523,659,587,659,
523,659,587,
523,659,587,659,
523,
523,659,587,659,
784,659,587,
523,
523,659,587,659,
523,659,587,
523,659,587,659,
523,
523,659,587,659,
784,659,587,
523,
};
unsigned b[]=
{
350,350,350,350,
350,350,700,
350,350,350,350,
1400,
350,350,350,350,
1050,175,175,
2800,
350,350,350,350,
350,350,700,
350,350,350,350,
1400,
350,350,350,350,
1050,175,175,
2100,
525,175,
350,350,350,350,
700,525,175,
350,350,350,350,
700,350,175,875,350,175,875,350,175,175,
350,350,350,175,175,
700,525,175,
350,350,350,350,700,525,175,
350,350,350,350,700,350,175,875,350,175,875,350,175,175,
350,350,350,175,175,
700,350,350,
1050,350,700,350,175,175,
350,350,350,350,700,350,350,
875,175,350,
700,350,175,175,
350,350,350,350,
700,525,175,
350,350,350,175,875,
525,175,
350,350,350,175,175,
700,350,175,875,350,175,875,525,175,
350,350,350,175,175,
700,525,175,
350,350,350,175,175,
700,525,175,
350,350,350,175,175,
700,350,175,875,350,175,875,525,175,
350,350,350,175,875,
350,350,
1400,
350,350,350,350,
350,350,700,
350,350,350,350,
1400,
350,350,350,350,
1050,175,175,
2800,
350,350,350,350,
350,350,700,
350,350,350,350,
1400,
350,350,350,350,
1050,175,175,
2100,
};
for(int i=0; i<ff; i++)
{
printf("第%d个音符\n",i);
Beep(a[i],b[i]);
}
}
void GuoGe()
{
int ff=113;
unsigned a[]=
{
523,659,784,784,
880,784,
659,523,784,784,784,
659,523,
392,392,392,392,392,392,
523,0,392,
523,523,523,523,392,440,494,
523,523,0,659,523,587,659,
784,784,659,659,523,659,
784,659,587,587,
880,784,587,659,
784,659,0,784,
659,587,659,523,659,0,
392,440,523,523,
659,659,784,784,
587,587,587,440,
587,392,
523,523,659,659,784,
523,659,784,784,880,784,
659,523,784,784,784,
659,0,523,0,
392,523,659,523,784,784,784,
659,0,523,0,
392,523,392,523,392,523,523,0,
};
unsigned b[]=
{
350,350,350,350,700,700,
525,175,350,350,350,
700,700,350,350,350,350,350,350,700,
350,350,1050,350,525,175,350,175,175,
700,700,350,350,350,175,175,
700,700,525,175,525,175,
525,175,700,1400,
700,700,700,700,
350,350,350,350,350,175,175,700,
700,700,525,175,350,350,
525,175,350,350,350,175,175,700,
1050,350,
1050,350,
1050,350,
1400,
525,175,350,350,
700,700,525,175,350,350,350,
350,350,350,350,
700,700,525,175,350,350,350,
350,350,350,350,
700,700,700,700,700,700,700,700,
};
for(int i=0; i<ff; i++)
{
printf("第%d个音符\n",i);
Beep(a[i],b[i]);
}
}
void PengYou()
{
int ff=131;
unsigned a[]=
{
0,587,659,523,587,659,587,659,784,
880,784,659,659,587,659,523,587,659,392,587,659,
587,440,523,
587,659,523,587,659,523,587,659,
784,784,880,523,440,523,
587,587,523,440,440,523,
587,659,587,523,587,587,659,587,523,
523,587,587,659,523,587,659,
784,784,784,880,523,440,523,
587,587,523,440,440,392,440,
523,0,
659,659,784,
784,784,784,880,784,880,988,
1046,880,880,784,659,659,587,523,
523,880,784,659,587,523,
523,440,587,659,659,784,
784,784,784,880,784,880,988,
1046,880,880,784,659,659,587,523,
523,880,784,659,587,523,
440,440,523,523,
};
unsigned b[]=
{
900,450,450,900,450,450,900,450,450,
900,450,450,900,450,450,900,450,450,900,450,450,
900,450,4950,
450,450,900,450,450,900,450,450,900,450,450,900,450,450,
900,450,450,900,450,450,
450,450,450,225,225,900,450,225,225,
900,450,225,225,900,450,450,
900,450,225,225,900,450,450,
900,450,225,225,900,450,450,
1800,900,
450,225,225,
450,450,450,225,1125,450,450,
450,450,450,225,1125,450,225,1125,
450,225,1125,450,225,1125,
225,225,1350,450,225,225,
450,450,450,225,1125,450,450,
450,450,450,225,1125,450,225,1125,
450,225,1125,450,225,1125,
450,225,675,1350,
};
for(int i=0; i<ff; i++)
{
printf("第%d个音符\n",i);
Beep(a[i],b[i]);
}
}
有没有运行看看?神奇吧?下面我来教你自己做音乐~
Beep(音调,音长);
这个函数是主角~~
音长以毫秒为单位~
下面这个程序是用来写程序能识别的谱子的,我已经注释的很详细了,接下来会有例子
#define inandout
#include <stdio.h>
#define one 900
int main()
{
#ifdef inandout
freopen("1.in","r",stdin);
freopen("1.out","w+",stdout);
#endif
//*******************************************
char a[4];
int b;
int a1,b1;
int pai;
scanf("%d%d",&a1,&b1);//先输入X/X
pai=4*one/b1;//定义每拍几毫秒
int T=0;
printf("本曲谱以%d分音符为一拍每小节%d拍\n",b1,a1);
while(scanf("%s",a),a[0]!='@')//输入音符x#高音,xb低音,\回车,@结束输入
{
if(a[0]=='\\')
printf("\n");
else
{ T++;
switch(a[0])
{
case '0':
printf("0,");
break;
case '1':
{
if(a[1]=='#')
printf("%d,",1046);
else if(a[1]=='b')
printf("%d,",262);
else
printf("%d,",523);
break;
}
case '2':
{
if(a[1]=='#')
printf("%d,",1175);
else if(a[1]=='b')
printf("%d,",294);
else
printf("%d,",587);
break;
}
case '3':
{
if(a[1]=='#')
printf("%d,",1318);
else if(a[1]=='b')
printf("%d,",330);
else
printf("%d,",659);
break;
}
case '4':
{
if(a[1]=='#')
printf("%d,",1397);
else if(a[1]=='b')
printf("%d,",349);
else
printf("%d,",698);
break;
}
case '5':
{
if(a[1]=='#')
printf("%d,",1568);
else if(a[1]=='b')
printf("%d,",392);
else
printf("%d,",784);
break;
}
case '6':
{
if(a[1]=='#')
printf("%d,",1760);
else if(a[1]=='b')
printf("%d,",440);
else
printf("%d,",880);
break;
}
case '7':
{
if(a[1]=='#')
printf("%d,",1976);
else if(a[1]=='b')
printf("%d,",494);
else
printf("%d,",988);
break;
}
}
}
}
printf("\n共音符%d个\n\n",T);
T=0;
while(scanf("%d",&b)!=EOF)//按顺序输入每个音符拍数 下面每加一杠,数值乘2 如4分音符为1,8分为2,0为回车,若有连音只需按顺序输入音长 5 - -为111
{
if(b==0)
printf("\n");
else
{
T++;
if(b>10)
{
int sum=0;
while(b!=0)
{
if(b%10!=0)
sum+=(pai/(b%10));
b/=10;
}
printf("%d,",sum);
}
else
printf("%d,",pai/b);
}
}
printf("\n共音长%d个\n\n",T);
return 0;
}
想要写谱子,至少需要知道一些基本的乐理,就是每个音符的长度表示(否则你无法输入长度的数据),几分音符,延音占几拍啦之类的,不懂得自行脑补
下面以欢乐颂为例子来写个谱子
1,建立一个1.txt文件在写谱程序的根目录下,就是跟他放一块
2,输入曲谱,格式如下
先输入拍号,欢乐颂为4分音符为一拍,每小节4拍,输入:4 4回车
下面把谱子里的数字打进去(注意!高音为x#,低音为xb),为了方便看,最好每小节回车一次,像这样
记住,\为分小结回车标志,程序会在输入/的地方输出一个回车,主要是方便查错
4 4
3 3 4 5 \
5 4 3 2 \
1 1 2 3 \
3 2 2 \
3 3 4 5 \
5 4 3 2 \
1 1 2 3 \
2 1 1 \
2 3 1\
2 3 4 3 1 \
2 3 4 3 2 \
1 2 5b 3 \
3 3 4 5 \
5 4 3 2 \
1 1 2 3 \
2 1 1 \
@
@为结束标志,在这里我只输入了一次循环,完整的可以自行补上
下面输入音长
格式如下
按顺序输入每个音符拍数 下面每加一杠,数值乘2 如4分音符为1,8分为2,0为回车,若有连音只需按顺序输入音长 5 - -为111 ,附点音符点为音符x音长的一半
不同音符音长一定要用空格隔开,相同的一定要连起来(有时候由乐曲决定),每小节的0是换行,最好输入好查错
4 4
3 3 4 5 \
5 4 3 2 \
1 1 2 3 \
3 2 2 \
3 3 4 5 \
5 4 3 2 \
1 1 2 3 \
2 1 1 \
2 3 1\
2 3 4 3 1 \
2 3 4 3 2 \
1 2 5b 3 \
3 3 4 5 \
5 4 3 2 \
1 1 2 3 \
2 1 1 \
@
1 1 1 1 0
1 1 1 1 0
1 1 1 1 0
12 2 11 0
1 1 1 1 0
1 1 1 1 0
1 1 1 1 0
12 2 11 0
12 1 1 0
1 2 2 1 1 0
1 2 2 1 1 0
1 1 1 1 0
1 1 1 1 0
1 1 1 1 0
1 1 1 1 0
12 1 11 0
像这样输入完了保存在1.txt里
运行作曲程序
得到1.out,打开后是这样的
本曲谱以4分音符为一拍每小节4拍
659,659,698,784,
784,698,659,587,
523,523,587,659,
659,587,587,
659,659,698,784,
784,698,659,587,
523,523,587,659,
587,523,523,
587,659,523,587,659,698,659,523,
587,659,698,659,587,
523,587,392,659,
659,659,698,784,
784,698,659,587,
523,523,587,659,
587,523,523,
共音符62个
900,900,900,900,
900,900,900,900,
900,900,900,900,
1350,450,1800,
900,900,900,900,
900,900,900,900,
900,900,900,900,
1350,450,1800,
1350,900,900,
900,450,450,900,900,
900,450,450,900,900,
900,900,900,900,
900,900,900,900,
900,900,900,900,
900,900,900,900,
1350,900,1800,
共音长62个
看到音长和音符数值一样,可以笑一下了,但是还没成功呢,得去程序里验证一下,把上面串东西复制到下面的程序标注处并运行
#include<windows.h>
#include<stdio.h>
void check();
int main()
{
check();
return 0 ;
}
void check()
{
int ff=62;//音符个数别忘了写
unsigned a[]=
{
//音符
};
unsigned b[]=
{
//音长
};
for(int i=0; i<ff; i++)
{
printf("第%d个音符\n",i+1);//显示出来好找错
Beep(a[i],b[i]);
}
}
贴好是这样的
#include<windows.h>
#include<stdio.h>
void check();
int main()
{
check();
return 0 ;
}
void check()
{
int ff=62;//音符个数别忘了写
unsigned a[]=
{
659,659,698,784,
784,698,659,587,
523,523,587,659,
659,587,587,
659,659,698,784,
784,698,659,587,
523,523,587,659,
587,523,523,
587,659,523,587,659,698,659,523,
587,659,698,659,587,
523,587,392,659,
659,659,698,784,
784,698,659,587,
523,523,587,659,
587,523,523,
//音符
};
unsigned b[]=
{
900,900,900,900,
900,900,900,900,
900,900,900,900,
1350,450,1800,
900,900,900,900,
900,900,900,900,
900,900,900,900,
1350,450,1800,
1350,900,900,
900,450,450,900,900,
900,450,450,900,900,
900,900,900,900,
900,900,900,900,
900,900,900,900,
900,900,900,900,
1350,900,1800,
//音长
};
for(int i=0; i<ff; i++)
{
printf("第%d个音符\n",i+1);//显示出来好找错
Beep(a[i],b[i]);
}
}
但是运行后我们发现,节奏太慢,完全不像欢乐颂啊,不要紧,打开作谱程序
看到开头的#define one 900
这里定义了每个4分音符的时间,单位毫秒,我们改为400,再运行,得到1.out,复制音长粘贴
#include<windows.h>
#include<stdio.h>
void check();
int main()
{
check();
return 0 ;
}
void check()
{
int ff=62;//音符个数别忘了写
unsigned a[]=
{
659,659,698,784,
784,698,659,587,
523,523,587,659,
659,587,587,
659,659,698,784,
784,698,659,587,
523,523,587,659,
587,523,523,
587,659,523,587,659,698,659,523,
587,659,698,659,587,
523,587,392,659,
659,659,698,784,
784,698,659,587,
523,523,587,659,
587,523,523,
//音符
};
unsigned b[]=
{
400,400,400,400,
400,400,400,400,
400,400,400,400,
600,200,800,
400,400,400,400,
400,400,400,400,
400,400,400,400,
600,200,800,
600,400,400,
400,200,200,400,400,
400,200,200,400,400,
400,400,400,400,
400,400,400,400,
400,400,400,400,
400,400,400,400,
600,400,800,
//音长
};
for(int i=0; i<ff; i++)
{
printf("第%d个音符\n",i+1);//显示出来好找错
Beep(a[i],b[i]);
}
}
这下好多了,大功告成!
希望读者能做出自己的音乐也跟我分享一下,我会把你的贴在这里,另外有什么不懂得可以跟帖,我尽量回复。
程序均为原创,转载请注明出处,谢谢~