-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
142 lines (136 loc) · 4.68 KB
/
main.cpp
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
#include "Options.h"
#include "midiLoader.h"
#include "midiMap.h"
#include "sampler.h"
int main(int argc, char** argv) {
bool processError = false;
if (argc < 5) {
return 0;
}
std::string input = argv[1];
std::string output_ori = std::string(argv[2]);
int noteChannel = atoi(argv[3]);
int chordChannel = atoi(argv[4]);
std::cout << "输入:" << input << std::endl;
std::cout << "旋律音轨:" << noteChannel << std::endl;
std::cout << "和弦音轨:" << chordChannel << std::endl;
char buf_noteChannel[64];
char buf_chordChannel[64];
snprintf(buf_noteChannel, sizeof(buf_noteChannel), ".%d", noteChannel);
snprintf(buf_chordChannel, sizeof(buf_chordChannel), ".%d", chordChannel);
mgnr::midiMap midiMap;
mgnr::loadMidi(midiMap, input);
midiMap.updateTimeMax();
//识别节拍
int beat_3 = 0;
int beat_2 = 0;
for (auto it : midiMap.notes) {
if (strstr(it->info.c_str(), buf_chordChannel) != NULL) {
int beatNum = round(it->duration / midiMap.TPQ);
if (beatNum % 3 == 0) {
++beat_3;
} else if (beatNum % 2 == 0) {
++beat_2;
}
}
}
if (beat_3 > beat_2) {
printf("识别为三拍子,暂不支持处理\n");
return 1;
}
static const char* noteMap_1[] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
static const char* noteMap_2[] = {"C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"};
static const char* noteMap_3[] = {"Am", "A#m", "Bm", "Cm", "C#m", "Dm", "D#m", "Em", "Fm", "F#m", "Gm", "G#m"};
static const char* noteMap_4[] = {"Am", "Bbm", "Bm", "Cm", "Dbm", "Dm", "Ebm", "Em", "Fm", "Gbm", "Gm", "Abm"};
int basetone = 0;
if (argc >= 6) {
for (int i = 0; i < 12; ++i) {
if (
strcmp(argv[5], noteMap_1[i]) == 0 ||
strcmp(argv[5], noteMap_2[i]) == 0 ||
strcmp(argv[5], noteMap_3[i]) == 0 ||
strcmp(argv[5], noteMap_4[i]) == 0) {
basetone = i;
break;
}
}
std::cout << "设置调性:";
} else {
std::cout << "识别调性:";
basetone = midiMap.baseTone;
}
std::cout << basetone << "(" << noteMap_1[basetone] << ")" << std::endl;
int lenInBeat = (midiMap.noteTimeMax / midiMap.TPQ);
std::vector<int> notes;
std::vector<std::set<int> > chords;
char channel_note[64];
char channel_chord[64];
int numNote = 0;
int numChord = 0;
for (int i = 0; i < lenInBeat; i = i + 1) {
chords.push_back(mgnr::sampleChord(midiMap, i, 1, basetone, [&](mgnr::note* n) {
auto res = (strstr(n->info.c_str(), buf_chordChannel) != NULL);
if (res) {
if (n->duration < midiMap.TPQ) {
std::cout << "长度过短:len=" << n->duration << " TPQ=" << midiMap.TPQ << std::endl;
processError = true;
}
}
return res;
}));
++numChord;
for (int j = 0; j < 4; ++j) {
notes.push_back(mgnr::sample(midiMap, i + j * 0.25, 0.25, basetone, [&](mgnr::note* n) {
return (strstr(n->info.c_str(), buf_noteChannel) != NULL);
}));
++numNote;
}
}
//std::cout << "旋律:" << numNote << "和弦:" << numChord << std::endl;
if(processError){
std::cout << "出现错误,停止写入" << std::endl;
return 1;
}
std::cout << "旋律:" << notes.size() << "和弦:" << chords.size() << std::endl;
auto fp = fopen(output_ori.c_str(), "a");
if (fp) {
if (argc >= 6) {
fprintf(fp, "%s %s|[", input.c_str(),argv[5]);
}else{
fprintf(fp, "%s|[", input.c_str());
}
bool first = true;
for (auto it : notes) {
if (first) {
fprintf(fp, "%d", it);
} else {
fprintf(fp, ",%d", it);
}
first = false;
}
fprintf(fp, "]|[");
first = true;
for (auto it : chords) {
if (!first) {
fprintf(fp, ",");
}
{
fprintf(fp, "[");
bool first = true;
for (auto n : it) {
if (first) {
fprintf(fp, "%d", n);
} else {
fprintf(fp, ",%d", n);
}
first = false;
}
fprintf(fp, "]");
}
first = false;
}
fprintf(fp, "]\n");
fclose(fp);
}
return 0;
}