先从 Microsoft C++ Build Tools - Visual Studio
下载 1.73GB 安装 "Microsoft C++ Build Tools“
访问 Swift.org - Download Swift
找到 Windows 10:x86_64
下载 swift-5.10-RELEASE-windows10.exe 大约490MB
建议安装在 D:\Swift\ ,安装后大约占2.56GB
简单来说,Clang是一个编译器,目前用来编译C、C++、Objective-C语言。
where clang
D:\Swift\Toolchains\5.10.0+Asserts\usr\bin\clang.exe
cd D:\Swift\testc
短笛伴奏《祝你生日快乐》,编辑 happy_birthday_2u.cpp 如下
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
using namespace std;enum Scale // 音阶参数
{Rest = 0, C8 = 108, B7 = 107, A7s = 106, A7 = 105, G7s = 104, G7 = 103, F7s = 102, F7 = 101, E7 = 100,D7s = 99, D7 = 98, C7s = 97, C7 = 96, B6 = 95, A6s = 94, A6 = 93, G6s = 92, G6 = 91, F6s = 90, F6 = 89,E6 = 88, D6s = 87, D6 = 86, C6s = 85, C6 = 84, B5 = 83, A5s = 82, A5 = 81, G5s = 80, G5 = 79, F5s = 78,F5 = 77, E5 = 76, D5s = 75, D5 = 74, C5s = 73, C5 = 72, B4 = 71, A4s = 70, A4 = 69, G4s = 68, G4 = 67,F4s = 66, F4 = 65, E4 = 64, D4s = 63, D4 = 62, C4s = 61, C4 = 60, B3 = 59, A3s = 58, A3 = 57, G3s = 56,G3 = 55, F3s = 54, F3 = 53, E3 = 52, D3s = 51, D3 = 50, C3s = 49, C3 = 48, B2 = 47, A2s = 46, A2 = 45,G2s = 44, G2 = 43, F2s = 42, F2 = 41, E2 = 40, D2s = 39, D2 = 38, C2s = 37, C2 = 36, B1 = 35, A1s = 34,A1 = 33, G1s = 32, G1 = 31, F1s = 30, F1 = 29, E1 = 28, D1s = 27, D1 = 26, C1s = 25, C1 = 24, B0 = 23,A0s = 22, A0 = 21
};
enum Voice // F大调
{H1 = F2, H2 = G2, H3 = A2, H4 = B2, H5 = C3, H6 = D3, H7 = E3,J1 = F3, J2 = G3, J3 = A3, J4 = B3, J5 = C4, J6 = D4, J7 = E4,K1 = F4, K2 = G4, K3 = A4, K4 = B4, K5 = C5, K6 = D5, K7 = E5,L1 = F5, L2 = G5, L3 = A5, L4 = B5, L5 = C6, L6 = D6, L7 = E6,LOW_SPEED = 500, MIDDLE_SPEED = 400, HIGH_SPEED = 300,_ = 0xFF
};
// score: Happy birthday to you
int hb2u[] = {J5,J5,J6,J5,K1,J7,J5,J5,J6,J5,K2,K1,J5,J5,K5,K3,K1,J7,J6,K4,K4,K3,K1,K2,K1,
};
// 时长数组 MIDDLE_SPEED
int shichang[] = {400,400,800,800,800,1600,400,400,800,800,800,1600,400,400,800,800,800,800,800,400,400,800,800,800,2000,
};
// 力度
int lidu[] = {1,0,0,2,0,0,1,0,0,2,0,0,1,0,0,2,1,0,0,2,1,1,2,0,0,
};
// 演奏歌曲《祝你生日快乐》
void happy_birthday()
{HMIDIOUT handle;midiOutOpen(&handle, 0, 0, 0, CALLBACK_NULL); // 打开midi设备midiOutShortMsg(handle, 72 << 8 | 0xC2); // 设置通道2的乐器为 短笛unsigned char sd = 0x90 | (2 & 0x0F); // statusByte: 0为声道unsigned short note = 0; //音符编号unsigned short v = 120; // velocity 力度值,范围通常在0-127int n = sizeof(lidu) / sizeof(lidu[0]); cout <<"n= " << n << endl;for (int i=0; i < n; i++) {note = hb2u[i];if (lidu[i] ==0) v = 40;else if(lidu[i] ==1) v = 80;else if (lidu[i] ==2) v = 120;unsigned long noteOnMsg = (((unsigned long)v <<16) | (((unsigned long)note) <<8) | (unsigned long)sd);midiOutShortMsg(handle, noteOnMsg); // 发送MIDI消息Sleep(shichang[i]); // 停顿,给音节演奏的时间midiOutShortMsg(handle, 0x7BB2); // 停止通道2上的演奏}midiOutShortMsg(handle, 0x7BB2); // 停止通道2上的演奏midiOutClose(handle); // 关闭midi设备
}int main()
{happy_birthday();return 0;
}
编译 clang happy_birthday_2u.cpp -o happy_birthday_2u.exe
运行 happy_birthday_2u.exe