-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynMusicEngine.h
More file actions
153 lines (124 loc) · 2.87 KB
/
Copy pathDynMusicEngine.h
File metadata and controls
153 lines (124 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once
#include "stdafx.h"
#include "rtmidi-2.1.0\RtMidi.h"
#include <Windows.h>
#ifdef _WIN32
#pragma comment(lib, "winmm.lib")
#endif
#ifdef _DEBUG
#pragma comment(lib, "rtmidid.lib")
#else
#pragma comment(lib, "rtmidi.lib")
#endif
struct Note {
sf::Int64 time;
unsigned char on;
unsigned char pitch;
unsigned char force;
};
class DynMusicEngine
{
public:
DynMusicEngine();
~DynMusicEngine();
int start() {
m_mout = new RtMidiOut();
if (m_mout->getPortCount() == 0) {
printf("[Error] in %s:%d: Failed to find usable midi device.\n", __FILE__, __LINE__);
return -1;
}
else
printf("[Info] %d port available\n", m_mout->getPortCount());
printf("[Info] Trying to open port name \"%s\"\n", m_mout->getPortName(0).c_str());
try {
m_mout->openPort(0);
}
catch (RtMidiError& err) {
printf("[Error] %s", err.what());
return -2;
}
printf("[Info] DynMusicEngine successfully started.\n");
m_timer.restart(); //Start timer
auto hand = std::async(std::launch::async, &DynMusicEngine::Thread, this);
//Thread();
return 0;
}
int Thread() {
//This thread is the music (not necessarily the sound thread)
while (1) {
if (this->m_tmp.size() == 0) {
//Nothing left, quit this thread
return 1;
}
std::vector<unsigned char> message;
sf::Int64 usecs = m_timer.getElapsedTime().asMicroseconds();
if (this->m_tmp.front()->time <= usecs) {
//Needs to be played
message.push_back(m_tmp.front()->on);
message.push_back(m_tmp.front()->pitch);
message.push_back(m_tmp.front()->force);
try {
m_mout->sendMessage(&message);
}
catch (RtMidiError& err) {
err.printMessage();
}
delete this->m_tmp.front();
this->m_tmp.pop_front();
}
}
return 0;
}
int create(const char* seed) {
for (size_t i = 0; i < 6; i++) {
Note* n = new Note;
if (n == NULL) {
printf("[Error] Failed to allocate memory in %s:%d\n", __FILE__, __LINE__);
}
if (i < 3) {
n->on = (unsigned char)144;
n->time = 0;
}
else {
n->on = 128;
n->time = 3000000;
}
switch (i % 3) {
case 0:
n->pitch = 48;
break;
case 1:
n->pitch = 52;
break;
case 2:
n->pitch = 55;
break;
}
n->force = 103;
this->m_tmp.push_back(n);
}
return 0;
}
int play(const char* seed) {
std::vector<unsigned char> message;
message.push_back(144);
message.push_back(64);
message.push_back(90);
m_mout->sendMessage(&message);
Sleep(1000);
message[0] = 128;
message[1] = 64;
message[2] = 40;
m_mout->sendMessage(&message);
return 0;
}
int end() {
m_mout->closePort();
delete m_mout;
return 0;
}
private:
RtMidiOut* m_mout;
sf::Clock m_timer;
std::deque<Note*> m_tmp;
};