Beep的使用
简介
这是我用C++Beep函数整活用的,使用Beep()函数需要调用#include <windows.h>
这个头文件。Beep() 是Windows自带的发声函数(蜂鸣器)
原型如下
BOOL Beep(DWORD dwFreq, DWORD dwDuration);
参数中dwFreq
是发声频率(单位 Hz),dwDuration
是发声时长(单位 ms)
音符对应的发声频率
目前网络上主要流行以下这段宏定义代码,可以满足一般的需要:
//q前缀为低音,p后缀为高音,s前缀为半音阶
#define qdo 262
#define qre 294
#define qmi 330
#define qfa 349
#define qso 392
#define qla 440
#define qsi 494
#define do 523//一般情况下 do 建议改为 do_, DO, Do 等非关键字的内容
#define re 587
#define mi 659
#define fa 698
#define so 784
#define la 880
#define si 988
#define dop 1046
#define rep 1175
#define mip 1318
#define fap 1397
#define sop 1568
#define lap 1760
#define sip 1976
#define sqdo 277
#define sqre 311
#define sqfa 370
#define sqso 415
#define sqla 466
#define sdo 554
#define sre 622
#define sfa 740
#define sso 831
#define sla 932
#define sdop 1046
#define srep 1245
#define sfap 1480
#define ssop 1661
#define slap 1865
// 转载 https://blog.csdn.net/Leo_Chenjy/article/details/127812962?spm=1001.2014.3001.5506
整活代码
#pragma comment(lib,"winmm.lib")
#include <Windows.h>
#include <iostream>
#include <tchar.h>
#include <conio.h>
#include <cstdio>
#include <cmath>
using namespace std;
//q前缀为低音,p后缀为高音,s前缀为半音阶
#define qdo 262
#define qre 294
#define qmi 330
#define qfa 349
#define qso 392
#define qla 440
#define qsi 494
#define do 523//一般情况下 do 建议改为 do_, DO, Do 等非关键字的内容
#define re 587
#define mi 659
#define fa 698
#define so 784
#define la 880
#define si 988
#define dop 1046
#define rep 1175
#define mip 1318
#define fap 1397
#define sop 1568
#define lap 1760
#define sip 1976
#define sqdo 277
#define sqre 311
#define sqfa 370
#define sqso 415
#define sqla 466
#define sdo 554
#define sre 622
#define sfa 740
#define sso 831
#define sla 932
#define sdop 1046
#define srep 1245
#define sfap 1480
#define ssop 1661
#define slap 1865
float f(float x, float y, float z) {
float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
}
float h(float x, float z) {
for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
if (f(x, y, z) <= 0.0f)
return y;
return 0.0f;
}
void hideCursor() {
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = 0;
cursor.dwSize = 1;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(hOut, &cursor);
}
void my_beep() {
// 一拍0.5秒
const int p = 400;
const int t = p / 1.5;
cout << "我们之间的回忆\n";
Beep(dop, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
Beep(mip, p);
Sleep(t);
Beep(sop, p);
Sleep(t);
Beep(dop, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
cout << "全部都小心地收集\n";
Beep(la, p);
Sleep(t);
Beep(si, p);
Sleep(t);
Beep(so, p);
Sleep(t);
Beep(so, 2 * p);
Sleep(2 * t);
Beep(rep, p);
Sleep(t);
Beep(dop, p);
Sleep(t);
Beep(dop, p);
Sleep(t);
cout << "我总是偷偷地哭泣\n";
Beep(dop, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
Beep(mip, p);
Sleep(t);
Beep(sop, 2 * p);
Sleep(2 * t);
Beep(dop, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
cout << "像倦鸟失了归期\n";
Beep(rep, p);
Sleep(t);
Beep(mip, p);
Sleep(t);
Beep(lap, p);
Sleep(t);
Beep(sop, p);
Sleep(t);
Beep(mip, p);
Sleep(t);
Beep(sop, p);
Sleep(t);
Beep(sop, p);
Sleep(t);
cout << "但愿我相信的爱情\n";
Beep(dop, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
Beep(mip, p);
Sleep(t);
Beep(sop, 2 * p);
Sleep(2 * t);
Beep(dop, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
cout << "结局紧握在我手心\n";
Beep(la, p);
Sleep(t);
Beep(si, p);
Sleep(t);
Beep(so, p);
Sleep(t);
Beep(so, 2 * p);
Sleep(2 * t);
Beep(rep, p);
Sleep(t);
Beep(dop, p);
Sleep(t);
Beep(dop, p);
cout << "时光匆匆却没有遗失过去";
Beep(dop, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
Beep(mip, p);
Sleep(t);
Beep(fap, p);
Sleep(t);
Beep(mip, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
Beep(mip, p);
Sleep(t);
Beep(rep, p);
Sleep(t);
Beep(lap, p);
Sleep(t);
Beep(mip, p);
Sleep(t);
Beep(dop, p);
Sleep(t);
_getch();
system("cls");
}
void heart() {
mciSendString("open music.mp3 alias bkmusic", NULL, 0, NULL);
mciSendString(TEXT("play bkmusic repeat"), NULL, 0, NULL);
system("color 0C");
HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
_TCHAR buffer[25][80] = { _T(' ') };
_TCHAR ramp[] = _T(".:-=+*#%@");
for (float t = 0.0f;; t += 0.1f) {
short sy = 0;
float s = sinf(t);
float a = s * s * s * s * 0.2f;
for (float z = 1.3f; z > -1.2f; z -= 0.1f) {
_TCHAR* p = &buffer[sy++][0];
float tz = z * (1.2f - a);
for (float x = -1.5f; x < 1.5f; x += 0.05f) {
float tx = x * (1.2f + a);
float v = f(tx, 0.0f, tz);
if (v <= 0.0f) {
float y0 = h(tx, tz);
float ny = 0.01f;
float nx = h(tx + ny, tz) - y0;
float nz = h(tx, tz + ny) - y0;
float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
*p++ = ramp[(int)(d * 5.0f)];
} else
*p++ = ' ';
}
}
for (sy = 0; sy < 25; sy++) {
COORD coord = { 0, sy };
SetConsoleCursorPosition(o, coord);
WriteConsole(o, buffer[sy], 79, NULL, 0);
}
Sleep(33);
}
}
int main() {
hideCursor();
my_beep();
heart();
return 0;
}
在结尾有一个爱心跳动的画面然后播放一个叫music.mp3
的音乐文件,运行效果如以下视频。(注:代码不能直接运行,源代码已经压缩好了,用Dev-C++打开文件里的.dev文件即可)
C++Beep函数整活之《把回忆拼好给你》
跳动的爱心
https://blog.csdn.net/kekedeLa/article/details/128908349?spm=1001.2014.3001.5506
音符对应的频率
https://blog.csdn.net/Leo_Chenjy/article/details/127812962?spm=1001.2014.3001.5506
结束
你们还想看看什么整活?https://space.bilibili.com/1282934090?spm_id_from=333.1007.0.0
私信。