一款基于 Beep() 函数的神秘音乐播放器
bilibili_xigua · · 休闲·娱乐
前言
本蒟蒻发现洛谷上似乎没有任何人做音乐播放器之类的东西,于是闲的没事做了一个和目前主流写法不太一样的基于 Beep() 函数的音乐播放器。
目前网上容易找到的 Beep() 音乐播放器大多是基于打表存音高对应的赫兹值,过于麻烦且灵活性不够高。
原理
这个音乐播放器运用了一个比较冷门(存疑)的知识:相邻的高频音的频率与低频音的频率的比值为
操作
这个播放器的操作格式均为为一个操作符
播放器在当前版本具备以下功能:
改变 BPM
对应的操作符为
操作内容为一个浮点数
如果你不知道 BPM 的含义,点击这里。
举例:b 180 表示将 BPM 设置为
发音
对应的操作符为 p。
操作内容从前到后为:浮点数
其意义分别为:这个音是 #,则表示升半音,其他则表示不变音;这个音在简谱里表示为
举例:p 4 # 1 1 表示发出一个音高为升 Do 的高于标准音节一个八度的四分音,即 C#5 。
休止
对应的操作符为 s。
操作内容为一个浮点数
举例:s 4 表示一个四分休止符。
结束
对应的操作符为 e。
没有操作内容,表示演奏结束。
目前的程序内容只有这么多,后续有时间的话会加上循环小结、附点音符(目前处理附点音符时需手动计算附点音符对应的值,如浮点四分音符可记作一个四分音符加一个八分音符,即
代码
这是播放器的代码 (请忽略我的水印) 。
#include<bits/stdc++.h>
#include<windows.h>
#define Watermark cout<<"Made ";
#define The cout
#define author <<
#define of "by:"
#define the "bilibili"
#define musicplayer "_"
#define is "xigua"
#define bilibili_ "\n"
#define xigua ;
using namespace std;
double bpm;
queue<char>d;
queue<double>t;
queue<double>h;
void play_(double a,double b,int c,char d){
double time=4/a*(60/bpm)*1000;
double high=b*2-1;
if(d=='#')high+=1;
if(high>6)high--;
high=pow(2,c)*440*pow(2,(high-10)/12);
h.push(high),t.push(time);
}
void stop_(double a){
double time=a/4*(60/bpm)*1000;
t.push(time);
}
int main(){
Watermark The author of the musicplayer is bilibili_ xigua
cout<<"Please enter the sheet music:\n";
while(true){
char s;
cin>>s;
d.push(s);
if(s=='p'){
double a,b;int c;char d;
cin>>a>>d>>b>>c;
play_(a,b,c,d);
}
else if(s=='b'){
cin>>bpm;
t.push(bpm);
}
else if(s=='s'){
double a;
cin>>a;
stop_(a);
}
else if(s=='e')break;
}
while(!d.empty()){
char s=d.front();
d.pop();
if(s=='p'){
double time=t.front();
double high=h.front();
t.pop(),h.pop();
Beep(high,time);
cout<<"play:"<<high<<"Hz "<<time<<" ms.\n";
}
else if(s=='b'){
double time=t.front();
t.pop();
cout<<"change BPM to:"<<time<<endl;
}
else if(s=='s'){
double time=t.front();
t.pop();
Sleep(time);
cout<<"stop:"<<time<<" ms.\n";
}else if(s=='e'){
cout<<"end.";
}else{
cout<<"illegal operation.\n";
}
}
}
如果你用这个代码跑如下的样例,它会播放《铃儿响叮当》。
b 140
p 8 - 5 -1
p 8 - 3 0
p 8 - 2 0
p 8 - 1 0
p 4 - 5 -1
s 8
p 16 - 5 -1
p 16 - 5 -1
p 8 - 5 -1
p 8 - 3 0
p 8 - 2 0
p 8 - 1 0
p 4 - 6 -1
s 4
p 8 - 6 -1
p 8 - 4 0
p 8 - 3 0
p 8 - 2 0
p 4 - 7 -1
s 4
p 8 - 5 0
p 8 - 5 0
p 8 - 4 0
p 8 - 2 0
p 4 - 3 0
p 8 - 1 0
s 8
p 8 - 5 -1
p 8 - 3 0
p 8 - 2 0
p 8 - 1 0
p 4 - 5 -1
s 8
p 16 - 5 -1
p 16 - 5 -1
p 8 - 5 -1
p 8 - 3 0
p 8 - 2 0
p 8 - 1 0
p 4 - 6 -1
s 4
p 8 - 6 -1
p 8 - 4 0
p 8 - 3 0
p 8 - 2 0
p 8 - 5 0
p 8 - 5 0
p 8 - 6 0
p 8 - 5 0
p 8 - 2 0
p 4 - 1 0
s 8
p 8 - 3 0
p 8 - 3 0
p 4 - 3 0
p 8 - 3 0
p 8 - 3 0
p 4 - 3 0
p 8 - 3 0
p 8 - 5 0
p 5.33333 - 1 0
p 16 - 1 0
p 2.66666 - 3 0
s 8
p 8 - 4 0
p 8 - 4 0
p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