Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
melody.h
Go to the documentation of this file.
1
11
12/*
13Copyright (c) 2026 MrXie1109
14
15Permission is hereby granted, free of charge, to any person obtaining a copy
16of this software and associated documentation files (the "Software"), to deal
17in the Software without restriction, including without limitation the rights
18to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19copies of the Software, and to permit persons to whom the Software is
20furnished to do so, subject to the following conditions:
21
22The above copyright notice and this permission notice shall be included in all
23copies or substantial portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31SOFTWARE.
32*/
33
34#pragma once
35#include <windows.h>
36#include <mmsystem.h>
37#include <thread>
38
39#ifdef _WIN32
40#ifndef CONSOLE_IGNORE_ADVICE
41#pragma message("Remember to link against winmm library (e.g., -lwinmm or winmm.lib)")
42#pragma message("Define 'CONSOLE_IGNORE_ADVICE' for ignore it")
43#endif
44#else
45#error "Unsupported platform"
46#endif
47
48namespace console
49{
55 enum class Instrument : unsigned char
56 {
65 Celesta = 8,
67 MusicBox = 10,
69 Marimba = 12,
70 Xylophone = 13,
72 Dulcimer = 15,
75 RockOrgan = 18,
77 ReedOrgan = 20,
78 Accordion = 21,
79 Harmonica = 22,
93 SlapBass1 = 36,
94 SlapBass2 = 37,
97 Violin = 40,
98 Viola = 41,
99 Cello = 42,
104 Timpani = 47,
113 Trumpet = 56,
114 Trombone = 57,
115 Tuba = 58,
122 AltoSax = 65,
123 TenorSax = 66,
125 Oboe = 68,
127 Bassoon = 70,
128 Clarinet = 71,
129 Piccolo = 72,
130 Flute = 73,
131 Recorder = 74,
132 PanFlute = 75,
135 Whistle = 78,
136 Ocarina = 79,
146 Pad2Warm = 89,
151 Pad7Halo = 94,
153 Fx1Rain = 96,
159 Fx7Echoes = 102,
160 Fx8SciFi = 103,
161 Sitar = 104,
162 Banjo = 105,
163 Shamisen = 106,
164 Koto = 107,
165 Kalimba = 108,
166 Bagpipe = 109,
167 Fiddle = 110,
168 Shanai = 111,
170 Agogo = 113,
172 Woodblock = 115,
173 TaikoDrum = 116,
175 SynthDrum = 118,
179 Seashore = 122,
180 BirdTweet = 123,
183 Applause = 126,
184 Gunshot = 127
185 };
186
191 namespace pitches
192 {
195 const int C0 = 12, D0 = 14, E0 = 16, F0 = 17, G0 = 19, A0 = 21, B0 = 23;
196 const int C1 = 24, D1 = 26, E1 = 28, F1 = 29, G1 = 31, A1 = 33, B1 = 35;
197 const int C2 = 36, D2 = 38, E2 = 40, F2 = 41, G2 = 43, A2 = 45, B2 = 47;
198 const int C3 = 48, D3 = 50, E3 = 52, F3 = 53, G3 = 55, A3 = 57, B3 = 59;
199 const int C4 = 60, D4 = 62, E4 = 64, F4 = 65, G4 = 67, A4 = 69, B4 = 71;
200 const int C5 = 72, D5 = 74, E5 = 76, F5 = 77, G5 = 79, A5 = 81, B5 = 83;
201 const int C6 = 84, D6 = 86, E6 = 88, F6 = 89, G6 = 91, A6 = 93, B6 = 95;
202 const int C7 = 96, D7 = 98, E7 = 100, F7 = 101, G7 = 103, A7 = 105, B7 = 107;
203 const int C8 = 108, D8 = 110, E8 = 112, F8 = 113, G8 = 115, A8 = 117, B8 = 119;
204 const int C9 = 120, D9 = 122, E9 = 124, F9 = 125, G9 = 127;
206
209 const int Cs0 = 13, Ds0 = 15, Fs0 = 18, Gs0 = 20, As0 = 22;
210 const int Cs1 = 25, Ds1 = 27, Fs1 = 30, Gs1 = 32, As1 = 34;
211 const int Cs2 = 37, Ds2 = 39, Fs2 = 42, Gs2 = 44, As2 = 46;
212 const int Cs3 = 49, Ds3 = 51, Fs3 = 54, Gs3 = 56, As3 = 58;
213 const int Cs4 = 61, Ds4 = 63, Fs4 = 66, Gs4 = 68, As4 = 70;
214 const int Cs5 = 73, Ds5 = 75, Fs5 = 78, Gs5 = 80, As5 = 82;
215 const int Cs6 = 85, Ds6 = 87, Fs6 = 90, Gs6 = 92, As6 = 94;
216 const int Cs7 = 97, Ds7 = 99, Fs7 = 102, Gs7 = 104, As7 = 106;
217 const int Cs8 = 109, Ds8 = 111, Fs8 = 114, Gs8 = 116, As8 = 118;
218 const int Cs9 = 121, Ds9 = 123, Fs9 = 126;
220
222 const int REST = -1;
223 }
224
229 struct Note
230 {
231 signed char pitch;
232 double beats;
233 signed char volume;
234
241 Note(signed char p = 60, double b = 1, signed char v = -1)
242 : pitch(p), beats(b), volume(v) {}
243 };
244
253 class MIDI
254 {
255 HMIDIOUT handle;
256 unsigned char bpm;
257 unsigned char volume;
258 Instrument instrument;
259
260 public:
270 unsigned char bpm = 120,
271 unsigned char volume = 100,
272 unsigned deviceID = 0)
273 : bpm(bpm), volume(volume), instrument(instrument)
274 {
275 HMIDIOUT h = nullptr;
276 if (midiOutOpen(&h, deviceID, 0, 0, CALLBACK_NULL) == MMSYSERR_NOERROR)
277 {
278 handle = h;
279 DWORD program_change = 0xC0 | ((unsigned char)(instrument) << 8);
280 midiOutShortMsg(handle, program_change);
281 DWORD volume_change = 0xB0 | (0x07 << 8) | (volume << 16);
282 midiOutShortMsg(handle, volume_change);
283 }
284 else
285 handle = nullptr;
286 }
287
289 MIDI(const MIDI &) = delete;
291 MIDI(MIDI &&midi) = delete;
293 const MIDI &operator=(const MIDI &) = delete;
295 const MIDI &operator=(MIDI &&) = delete;
296
301 ~MIDI() noexcept
302 {
303 if (handle)
304 midiOutClose(handle);
305 }
306
310 unsigned char get_bpm() const { return bpm; }
312 void set_bpm(unsigned char new_bpm) { bpm = new_bpm; }
313
315 Instrument get_instrument() { return instrument; }
321 void set_instrument(Instrument new_instrument)
322 {
323 instrument = new_instrument;
324 if (!handle)
325 return;
326 DWORD program_change = 0xC0 | ((unsigned char)(new_instrument) << 8);
327 midiOutShortMsg(handle, program_change);
328 }
329
331 unsigned char get_volume() const { return volume; }
336 void set_volume(unsigned char vol)
337 {
338 volume = vol;
339 if (!handle)
340 return;
341 DWORD volume_change = 0xB0 | (0x07 << 8) | (vol << 16);
342 midiOutShortMsg(handle, volume_change);
343 }
344
345
353 void play(Note note)
354 {
355 if (!handle)
356 return;
357 if (note.pitch != -1)
358 {
359 DWORD note_on = 0x90 | (note.pitch << 8) |
360 ((note.volume == -1
361 ? volume
362 : note.volume)
363 << 16);
364 midiOutShortMsg(handle, note_on);
365 Sleep(60000 * note.beats / bpm);
366 DWORD note_off = 0x80 | (note.pitch << 8) | (64 << 16);
367 midiOutShortMsg(handle, note_off);
368 }
369 else
370 Sleep(60000 * note.beats / bpm);
371 }
372
379 void nplay(Note note)
380 {
381 std::thread([this, note]()
382 { play(note); })
383 .detach();
384 }
385
392 template <class Iter>
393 void play(Iter begin, Iter end)
394 {
395 for (; begin != end; ++begin)
396 play(*begin);
397 }
398
405 template <class Iter>
406 void nplay(Iter begin, Iter end)
407 {
408 std::thread([=]()
409 { play(begin, end); })
410 .detach();
411 }
412 };
413}
unsigned char get_bpm() const
Definition melody.h:310
void nplay(Note note)
异步播放一个音符(非阻塞)。
Definition melody.h:379
MIDI(MIDI &&midi)=delete
禁止移动构造。
void set_bpm(unsigned char new_bpm)
设置 BPM。
Definition melody.h:312
void play(Note note)
同步播放一个音符(阻塞)。
Definition melody.h:353
void play(Iter begin, Iter end)
同步播放一个音符范围(阻塞)。
Definition melody.h:393
MIDI(Instrument instrument=Instrument::AcousticGrandPiano, unsigned char bpm=120, unsigned char volume=100, unsigned deviceID=0)
构造 MIDI 对象,打开默认 MIDI 设备并设置乐器、音量和 BPM。
Definition melody.h:269
void nplay(Iter begin, Iter end)
异步步播放一个音符范围(非阻塞)。
Definition melody.h:406
Instrument get_instrument()
获取当前乐器。
Definition melody.h:315
unsigned char get_volume() const
获取默认音量。
Definition melody.h:331
const MIDI & operator=(const MIDI &)=delete
禁止拷贝赋值。
~MIDI() noexcept
析构函数,关闭 MIDI 设备。
Definition melody.h:301
MIDI(const MIDI &)=delete
禁止拷贝构造。
void set_instrument(Instrument new_instrument)
切换乐器。
Definition melody.h:321
void set_volume(unsigned char vol)
设置默认音量。
Definition melody.h:336
const MIDI & operator=(MIDI &&)=delete
禁止移动赋值。
const int D2
Definition melody.h:197
const int Cs7
Definition melody.h:216
const int As8
Definition melody.h:217
const int C9
Definition melody.h:204
const int D4
Definition melody.h:199
const int B3
Definition melody.h:198
const int B7
Definition melody.h:202
const int C7
Definition melody.h:202
const int A4
Definition melody.h:199
const int Fs7
Definition melody.h:216
const int C3
Definition melody.h:198
const int E5
Definition melody.h:200
const int B5
Definition melody.h:200
const int Ds2
Definition melody.h:211
const int G8
Definition melody.h:203
const int Gs0
Definition melody.h:209
const int F3
Definition melody.h:198
const int Ds3
Definition melody.h:212
const int D6
Definition melody.h:201
const int G5
Definition melody.h:200
const int As6
Definition melody.h:215
const int D7
Definition melody.h:202
const int Ds4
Definition melody.h:213
const int D1
Definition melody.h:196
const int Gs8
Definition melody.h:217
const int F6
Definition melody.h:201
const int Gs2
Definition melody.h:211
const int F2
Definition melody.h:197
const int As1
Definition melody.h:210
const int Cs2
Definition melody.h:211
const int Cs4
Definition melody.h:213
const int E2
Definition melody.h:197
const int D9
Definition melody.h:204
const int A8
Definition melody.h:203
const int G7
Definition melody.h:202
const int Ds1
Definition melody.h:210
const int E7
Definition melody.h:202
const int As0
Definition melody.h:209
const int F1
Definition melody.h:196
const int C2
Definition melody.h:197
const int E1
Definition melody.h:196
const int Gs1
Definition melody.h:210
const int C1
Definition melody.h:196
const int F8
Definition melody.h:203
const int Gs7
Definition melody.h:216
const int Cs8
Definition melody.h:217
const int B8
Definition melody.h:203
const int Cs9
Definition melody.h:218
const int G3
Definition melody.h:198
const int E6
Definition melody.h:201
const int Fs9
Definition melody.h:218
const int G4
Definition melody.h:199
const int A6
Definition melody.h:201
const int Fs4
Definition melody.h:213
const int E4
Definition melody.h:199
const int Fs8
Definition melody.h:217
const int Fs6
Definition melody.h:215
const int A1
Definition melody.h:196
const int A0
Definition melody.h:195
const int As7
Definition melody.h:216
const int Ds8
Definition melody.h:217
const int F7
Definition melody.h:202
const int Gs4
Definition melody.h:213
const int A2
Definition melody.h:197
const int Gs6
Definition melody.h:215
const int Gs5
Definition melody.h:214
const int Ds6
Definition melody.h:215
const int C8
Definition melody.h:203
const int Cs3
Definition melody.h:212
const int Ds0
Definition melody.h:209
const int B1
Definition melody.h:196
const int As2
Definition melody.h:211
const int Cs6
Definition melody.h:215
const int F0
Definition melody.h:195
const int F5
Definition melody.h:200
const int G9
Definition melody.h:204
const int C6
Definition melody.h:201
const int Cs0
Definition melody.h:209
const int D3
Definition melody.h:198
const int B4
Definition melody.h:199
const int Cs5
Definition melody.h:214
const int Ds5
Definition melody.h:214
const int G1
Definition melody.h:196
const int B2
Definition melody.h:197
const int G0
Definition melody.h:195
const int D0
Definition melody.h:195
const int Fs5
Definition melody.h:214
const int D8
Definition melody.h:203
const int G6
Definition melody.h:201
const int As4
Definition melody.h:213
const int E0
Definition melody.h:195
const int B0
Definition melody.h:195
const int F4
Definition melody.h:199
const int As3
Definition melody.h:212
const int A7
Definition melody.h:202
const int As5
Definition melody.h:214
const int Cs1
Definition melody.h:210
const int Gs3
Definition melody.h:212
const int Ds9
Definition melody.h:218
const int C0
Definition melody.h:195
const int E9
Definition melody.h:204
const int A3
Definition melody.h:198
const int A5
Definition melody.h:200
const int D5
Definition melody.h:200
const int E3
Definition melody.h:198
const int E8
Definition melody.h:203
const int Fs3
Definition melody.h:212
const int Fs2
Definition melody.h:211
const int B6
Definition melody.h:201
const int F9
Definition melody.h:204
const int Fs0
Definition melody.h:209
const int REST
休止符(不发音,仅占用时值)
Definition melody.h:222
const int C5
Definition melody.h:200
const int C4
Definition melody.h:199
const int Ds7
Definition melody.h:216
const int Fs1
Definition melody.h:210
const int G2
Definition melody.h:197
本库所有组件所在的顶层命名空间。
Instrument
128 种 GM 标准乐器编号(0-127)。
Definition melody.h:56
@ OrchestralHarp
竖琴
Definition melody.h:103
@ Violin
小提琴
Definition melody.h:97
@ GuitarFretNoise
吉他擦弦声
Definition melody.h:177
@ Lead2Sawtooth
锯齿波领奏
Definition melody.h:138
@ Trombone
长号
Definition melody.h:114
@ GuitarHarmonics
吉他泛音
Definition melody.h:88
@ Celesta
钢片琴
Definition melody.h:65
@ Pad5Bowed
弓弦背景
Definition melody.h:149
@ AcousticGrandPiano
大钢琴 (默认)
Definition melody.h:57
@ StringEnsemble1
弦乐合奏 1
Definition melody.h:105
@ Trumpet
小号
Definition melody.h:113
@ Flute
长笛
Definition melody.h:130
@ TelephoneRing
电话铃
Definition melody.h:181
@ Cello
大提琴
Definition melody.h:99
@ Recorder
竖笛
Definition melody.h:131
@ PanFlute
排箫
Definition melody.h:132
@ Agogo
阿果果铃
Definition melody.h:170
@ HonkyTonkPiano
酒吧钢琴
Definition melody.h:60
@ Gunshot
枪声
Definition melody.h:184
@ ElectricBassPick
拨片电贝司
Definition melody.h:91
@ AcousticGuitarNylon
尼龙弦吉他
Definition melody.h:81
@ ChurchOrgan
教堂风琴
Definition melody.h:76
@ BlownBottle
吹瓶口
Definition melody.h:133
@ TenorSax
次中音萨克斯
Definition melody.h:123
@ SynthDrum
合成鼓
Definition melody.h:175
@ StringEnsemble2
弦乐合奏 2
Definition melody.h:106
@ Helicopter
直升机
Definition melody.h:182
@ SynthBrass2
合成铜管 2
Definition melody.h:120
@ Fx7Echoes
回声
Definition melody.h:159
@ Seashore
海浪
Definition melody.h:179
@ BrassSection
铜管组
Definition melody.h:118
@ Bassoon
巴松管
Definition melody.h:127
@ Lead3Calliope
汽笛风琴
Definition melody.h:139
@ RockOrgan
摇滚风琴
Definition melody.h:75
@ SlapBass1
击弦贝司 1
Definition melody.h:93
@ DrawbarOrgan
拉杆风琴
Definition melody.h:73
@ EnglishHorn
英国管
Definition melody.h:126
@ PercussiveOrgan
打击型风琴
Definition melody.h:74
@ Harpsichord
大键琴
Definition melody.h:63
@ Oboe
双簧管
Definition melody.h:125
@ ChoirAahs
合唱 "啊"
Definition melody.h:109
@ AcousticBass
原声贝司
Definition melody.h:89
@ Clavinet
击弦古钢琴
Definition melody.h:64
@ DistortionGuitar
失真吉他
Definition melody.h:87
@ Piccolo
短笛
Definition melody.h:129
@ BirdTweet
鸟鸣
Definition melody.h:180
@ OverdrivenGuitar
过载吉他
Definition melody.h:86
@ SynthStrings2
合成弦乐 2
Definition melody.h:108
@ TaikoDrum
太鼓
Definition melody.h:173
@ ElectricBassFinger
指拨电贝司
Definition melody.h:90
@ Woodblock
木鱼
Definition melody.h:172
@ Lead7Fifths
五度领奏
Definition melody.h:143
@ TubularBells
管钟
Definition melody.h:71
@ Fx8SciFi
科幻
Definition melody.h:160
@ MelodicTom
旋律通鼓
Definition melody.h:174
@ SopranoSax
高音萨克斯
Definition melody.h:121
@ Accordion
手风琴
Definition melody.h:78
@ Lead1Square
方波领奏
Definition melody.h:137
@ Glockenspiel
钟琴
Definition melody.h:66
@ TinkleBell
铃铛
Definition melody.h:169
@ Banjo
班卓琴
Definition melody.h:162
@ BrightAcousticPiano
明亮的钢琴
Definition melody.h:58
@ SynthBrass1
合成铜管 1
Definition melody.h:119
@ PizzicatoStrings
弦乐拨奏
Definition melody.h:102
@ Pad6Metallic
金属背景
Definition melody.h:150
@ Bagpipe
风笛
Definition melody.h:166
@ Clarinet
单簧管
Definition melody.h:128
@ Tuba
大号
Definition melody.h:115
@ ElectricGuitarJazz
爵士吉他
Definition melody.h:83
@ Kalimba
卡林巴
Definition melody.h:165
@ Pad7Halo
光环背景
Definition melody.h:151
@ Timpani
定音鼓
Definition melody.h:104
@ Ocarina
奥卡雷纳
Definition melody.h:136
@ ElectricPiano1
电钢琴 1
Definition melody.h:61
@ Lead8BassLead
贝司领奏
Definition melody.h:144
@ Fx3Crystal
水晶
Definition melody.h:155
@ Lead5Charang
炭精吉他
Definition melody.h:141
@ ElectricGuitarMuted
闷音吉他
Definition melody.h:85
@ SynthBass1
合成贝司 1
Definition melody.h:95
@ Pad8Sweep
扫弦背景
Definition melody.h:152
@ BreathNoise
呼吸声
Definition melody.h:178
@ Dulcimer
扬琴
Definition melody.h:72
@ Shakuhachi
尺八
Definition melody.h:134
@ ReedOrgan
簧风琴
Definition melody.h:77
@ AcousticGuitarSteel
钢弦吉他
Definition melody.h:82
@ Fiddle
民间提琴
Definition melody.h:167
@ Contrabass
低音提琴
Definition melody.h:100
@ MutedTrumpet
弱音小号
Definition melody.h:116
@ Fx4Atmosphere
大气
Definition melody.h:156
@ Fx1Rain
雨声
Definition melody.h:153
@ Pad2Warm
温暖背景
Definition melody.h:146
@ Vibraphone
颤音琴
Definition melody.h:68
@ Pad3Polysynth
复音合成背景
Definition melody.h:147
@ Sitar
西塔琴
Definition melody.h:161
@ Koto
古筝
Definition melody.h:164
@ AltoSax
中音萨克斯
Definition melody.h:122
@ SynthStrings1
合成弦乐 1
Definition melody.h:107
@ Shamisen
三味线
Definition melody.h:163
@ VoiceOohs
人声 "哦"
Definition melody.h:110
@ ElectricGrandPiano
电子大钢琴
Definition melody.h:59
@ Lead6Voice
人声领奏
Definition melody.h:142
@ Marimba
马林巴
Definition melody.h:69
@ ElectricGuitarClean
清音电吉他
Definition melody.h:84
@ Whistle
口哨
Definition melody.h:135
@ ReverseCymbal
反奏钹
Definition melody.h:176
@ Viola
中提琴
Definition melody.h:98
@ TremoloStrings
弦乐颤音
Definition melody.h:101
@ TangoAccordion
探戈手风琴
Definition melody.h:80
@ SteelDrums
钢鼓
Definition melody.h:171
@ MusicBox
八音盒
Definition melody.h:67
@ FretlessBass
无品贝司
Definition melody.h:92
@ SynthVoice
合成人声
Definition melody.h:111
@ Harmonica
口琴
Definition melody.h:79
@ OrchestraHit
乐队打击乐
Definition melody.h:112
@ Lead4Chiff
气声领奏
Definition melody.h:140
@ Fx5Brightness
明亮
Definition melody.h:157
@ Fx6Goblins
小妖
Definition melody.h:158
@ Xylophone
木琴
Definition melody.h:70
@ Pad4Choir
合唱背景
Definition melody.h:148
@ Applause
掌声
Definition melody.h:183
@ SynthBass2
合成贝司 2
Definition melody.h:96
@ Shanai
唢呐
Definition melody.h:168
@ Pad1NewAge
新世纪背景
Definition melody.h:145
@ SlapBass2
击弦贝司 2
Definition melody.h:94
@ ElectricPiano2
电钢琴 2
Definition melody.h:62
@ BaritoneSax
上低音萨克斯
Definition melody.h:124
@ Fx2Soundtrack
电影配乐
Definition melody.h:154
@ FrenchHorn
圆号
Definition melody.h:117
标准 MIDI 音高常量,包含中央 C4=60 的所有半音音高,以及休止符 REST。
表示一个音符,包含音高、时值和力度。
Definition melody.h:230
signed char volume
力度(0-127),-1 表示使用 MIDI 对象的默认音量
Definition melody.h:233
Note(signed char p=60, double b=1, signed char v=-1)
构造一个音符。
Definition melody.h:241
signed char pitch
MIDI 音高编号(0-127),或 pitches::REST 表示休止符
Definition melody.h:231
double beats
音符时值(以四分音符为单位,1 表示一拍)
Definition melody.h:232