Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
283 lines (240 loc) · 9.27 KB
/
Copy pathmain.cpp
File metadata and controls
283 lines (240 loc) · 9.27 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <windows.h>
#include <cstdio>
#include <math.h>
#include <algorithm>
//#define byte int8_t;
using namespace std;
vector<double> freq(128);
vector<string> noteNames = {"Äî", "Äî#", "Ðå", "Ðå#", "Ìè", "Ôà", "Ôà#", "Ñîëü", "Ñîëü#", "Ëÿ", "Ëÿ#", "Ñè"};
string readStr4(ifstream &in) {
string r = "";
for (int i = 0; i < 4; ++i)
r += char(in.get());
return r;
}
uint32_t readInt(ifstream &in) {
char buffer[4];
for (int i = 0; i < 4; ++i)
in.get(buffer[i]);
return uint32_t((unsigned char) (buffer[0]) << 24 |
(unsigned char) (buffer[1]) << 16 |
(unsigned char) (buffer[2]) << 8 |
(unsigned char) (buffer[3]));
}
uint16_t readShort(ifstream &in) {
char buffer[2];
for (int i = 0; i < 2; ++i)
in.get(buffer[i]);
return uint16_t((unsigned char) (buffer[0]) << 8 | (unsigned char) (buffer[1]));
}
struct NoteEv {
int time = 0;
int freq = 0;
bool pressed = false;
int num = 0;
int vol = 0;
int channel = 0;
};
struct Note {
double freq = 0;
int dur = 1;
Note(double freq, int dur) {
this->freq = freq;
this->dur = dur;
}
};
vector<vector<NoteEv>> blocks;
bool timecmp(NoteEv a, NoteEv b) {
if (a.time != b.time)
return a.time < b.time;
else
return a.pressed < b.pressed;
}
NoteEv tNoteEv;
void readDataBlock(ifstream &in) {
vector<NoteEv> cblock;
string nameSection = readStr4(in);
uint32_t lengthSection = readInt(in); // 4 ???? ?????? ??? ?????.
uint32_t LoopIndex = lengthSection; // ?????? ???????? ??????? ???. ??? ?????? ????, ???? ???? ?? ??? = 0.
uint32_t realTime = 0; // ?????? ??? ????? ?????.
while (LoopIndex != 0) // ???? ?? ???? ?? ????.
{
// ??? ????????? ??????? ??? ????. ?????? ???? ?? ????? 8-?? ???? ??? (???? ?????).
int loopCount = 0; // ???????? ?????? ????.
uint8_t buffer; // ?? ?????? ?????? ??????.
uint32_t bufferTime = 0; // ?????? ??? ?????? ??.
do {
buffer = in.get(); // ???? ??????.
loopCount++; // ????????, ?? ???? ????.
bufferTime <<= 7; // ???????? ?? 7 ???? ????? ???????? ?????? ????? (?.?. 1 ???? ???? ?? ????????).
bufferTime |= uint8_t(
uint8_t(buffer) & uint8_t(0x7F)); // ?? ?????? ???? ????????? ???????? ???.
} while ((buffer & (1 << 7)) != 0); // ?????, ??? ?? ????? ??????? ???? ????? (???? ??? = 0).
realTime += bufferTime; // ?????? ???? ???.
buffer = in.get();
loopCount++; // ?????? ?????-????, ????????, ?? ???? ????.
// ?? ? ??? ???-????, ?...
if (buffer == 0xFF) {
buffer = in.get(); // ?????? ????? ???-????.
buffer = in.get(); // ?????? ?????.
loopCount += 2;
for (int loop = 0; loop < buffer; loop++)
in.get();
LoopIndex = LoopIndex - loopCount - buffer; // ?????? ?? ????? ?????? ???????.
}
// ?? ?? ???-???, ? ???, ????? ?? ??? ???? ????? ????.
else
switch ((int8_t) buffer & 0xF0) // ????? ?? ????? 4-? ????.
{
// ?????? ???? ????? ????.
case 0x80: // ????? ???????
tNoteEv.channel = (buffer & 0x0F); // ?????? ????? ??????.
tNoteEv.pressed = false; // ?? ?????? ???????.
tNoteEv.num = in.get();
tNoteEv.vol = in.get();
tNoteEv.time = realTime; // ???????? ???? ??? ????.
tNoteEv.freq = freq[tNoteEv.num];
cblock.push_back(tNoteEv);
LoopIndex = LoopIndex - loopCount - 2; // ?????? ???????.
break;
case 0x90: // ?????? ???????.
tNoteEv.channel = (buffer & 0x0F); // ?????? ????? ??????.
tNoteEv.pressed = true; // ?? ????????.
tNoteEv.num = in.get(); // ?????? ????? ????.
tNoteEv.vol = in.get(); // ?????? ???????? ????.
tNoteEv.time = realTime; // ???????? ???? ??? ????.
tNoteEv.freq = freq[tNoteEv.num];
cblock.push_back(tNoteEv); // ????? ????? ????????.
LoopIndex = LoopIndex - loopCount - 2; // ?????? ???????.
break;
case 0xA0: // ??????? ?? ?????? ???????.
tNoteEv.channel = (buffer & 0x0F); // ?????? ????? ??????.
tNoteEv.pressed = true; // ?? ????????.
tNoteEv.num = in.get(); // ?????? ????? ????.
tNoteEv.vol = in.get(); // ?????? ????? ???????? ????.
tNoteEv.time = realTime; // ???????? ???? ??? ????.
tNoteEv.freq = freq[tNoteEv.num];
cblock.push_back(tNoteEv); // ????? ????? ????????.
LoopIndex = LoopIndex - loopCount - 2; // ?????? ???????.
break;
// ?? 2-? ?????? ????????.
case 0xB0: {
uint8_t buffer2level = in.get(); // ???? ?? ???????.
in.get(); // ?????? ?????? ?????-? ????????? ???.
LoopIndex = LoopIndex - loopCount - 2; // ?????? ???????.
/*switch (buffer2level) // ????? ??????? ???? ????.
{
//default: ??? ????????? ????? ??????? (?? ??????).
in.get(); // ?????? ?????? ?????-? ????????? ???.
LoopIndex = LoopIndex - loopCount - 2; // ?????? ???????.
break;
}*/
};
break;
// ? ??? ????????? ?? ???? ??? ?????.
case 0xC0: // ???? ?????? ???? ?????.
in.get(); // ?????? ????? ?????.
LoopIndex = LoopIndex - loopCount - 1; // ?????? ???????.
break;
case 0xD0: // ???? ??????.
in.get(); // ?????? ????? ?????.
LoopIndex = LoopIndex - loopCount - 1; // ?????? ???????.
break;
case 0xE0: // ????? ????????? ?????.
in.get();
in.get();
LoopIndex = LoopIndex - loopCount - 2; // ?????? ???????.
break;
}
}
blocks.push_back(cblock);
}
vector<vector<Note>> melody;
void calcNoteDurations() {
melody.resize(blocks.size());
/*vector<int> downTime(1 << 16);
for (int i = 0; i < blocks.size(); ++i)
for (auto &j: blocks[i])
if (j.pressed)
downTime[j.num] = j.time;
else
melody[i].push_back(Note(freq[j.num],j.time-downTime[j.num]));*/
for (int i = 0; i < blocks.size(); ++i) {
for (int j = 0; j < int(blocks[i].size()) - 2; ++j)
if (blocks[i][j].pressed)
melody[i].push_back(Note(blocks[i][j].freq, blocks[i][j + 2].time - blocks[i][j].time));
if (blocks[i].size() > 0)
melody[i].push_back(
Note(blocks[i].back().freq, blocks[i].back().time - blocks[i][blocks[i].size() - 2].time));
}
}
double speedUpMelody(double mult) {
for (auto &i: melody)
for (auto &j: i)
j.dur /= mult;
}
void saveMelody(string filename, int channel) {
auto notes = &melody[channel];
ofstream out(filename);
for (auto &i: *notes) {
out << "M300 S" << int(i.freq) << " P" << i.dur << '\n';
cout << "M300 S" << int(i.freq) << " P" << i.dur << '\n';
}
out.close();
}
void loadMidi(string filename) {
ifstream in(filename);
int infoSize, fileType, blockCnt, timeFormat;
readStr4(in);
infoSize = readInt(in);
fileType = readShort(in);
blockCnt = readShort(in);
timeFormat = readShort(in);
char c;
for (int bn = 0; bn < blockCnt; ++bn)
readDataBlock(in);
while ((c = in.get()) != EOF) {
cout << hex << c + 0 << ' ';
}
in.close();
for (auto &i: blocks)
sort(i.begin(), i.end(), timecmp);
calcNoteDurations();
cout << "Events :\n";
for (int i=0;i<blocks.size();++i) {
cout << "Channel " << i + 1 << ":\n";
for (auto j: blocks[i])
cout << "(" << j.freq << " - " << j.time << ") ";
cout << "\n";
}
cout << "\nNotes :\n";
for (int i = 0; i < melody.size(); ++i) {
cout << "Channel " << i + 1 << ":\n";
for (auto j: melody[i])
cout << "(" << j.freq << " - " << j.dur << ") ";
cout << '\n';
}
cout << '\n';
}
// A 81 prob 57
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
double step = pow(2, 1.0 / 12.0);
freq[57] = 440;
for (int i = 58; i < 57 + 12; ++i)
freq[i] = freq[i - 1] * step;
for (int i = 57 + 12; i < 128; ++i)
freq[i] = freq[i - 12] * 2;
for (int i = 56; i >= 0; --i)
freq[i] = freq[i + 12] / 2;
//loadMidi("test1.mid");
loadMidi("D:\\Programming\\VS_CPP\\RandomThings\\midiReader\\test4_btt.mid");
//speedUpMelody(0.5);
saveMelody("D:\\Programming\\VS_CPP\\RandomThings\\midiReader\\test4_out.txt", 1);
return 0;
}