-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidiMap.cpp
482 lines (444 loc) · 13.3 KB
/
midiMap.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include "midiMap.h"
#include "mempool.h"
namespace mgnr{
typedef mempool<note> npool;
midiMap::midiMap(){
pool=new npool;
XShift = 0;
baseTone = 0;
noteTimeMax = 0;
noteUpdated = false;
chord_map["maj3"] = {0, 4, 7, 0}; // 大三和弦 根音-大三度-纯五度
chord_map["min3"] = {0, 3, 7, 0}; //小三和弦 根音-小三度-纯五度
chord_map["aug3"] = {0, 4, 8, 0}; //增三和弦 根音-大三度-增五度
chord_map["dim3"] = {0, 3, 6, 0}; //减三和弦 根音-小三度-减五度
chord_map["M7"] = {0, 4, 7, 11}; //大七和弦 根音-大三度-纯五度-大七度
chord_map["Mm7"] = {0, 4, 7, 10}; //属七和弦 根音-大三度-纯五度-小七度
chord_map["m7"] = {0, 3, 7, 10}; //小七和弦 根音-小三度-纯五度-小七度
chord_map["mM7"] = {0, 3, 7, 11}; //小大七和弦 根音-小三度-纯五度-大七度
chord_map["aug7"] = {0, 4, 8, 10}; //增七和弦 根音-大三度-增五度-小七度
chord_map["augM7"]= {0, 4, 8, 11}; //增大七和弦 根音-大三度-增五度-小七度
chord_map["m7b5"] = {0, 3, 6, 10}; //半减七和弦 根音-小三度-减五度-减七度
chord_map["dim7"] = {0, 3, 6, 9}; //减减七和弦 根音-小三度-减五度-减七度
note_number_map["1"] = 0;
note_number_map["1#"] = 1;
note_number_map["2"] = 2;
note_number_map["2#"] = 3;
note_number_map["3"] = 4;
note_number_map["4"] = 5;
note_number_map["4#"] = 6;
note_number_map["5"] = 7;
note_number_map["5#"] = 8;
note_number_map["6"] = 9;
note_number_map["6#"] = 10;
note_number_map["7"] = 11;
int n = 60;
chord_map_note["C"] = n;
n+=2;
chord_map_note["D"] = n;
n+=2;
chord_map_note["E"] = n;
n+=1;
chord_map_note["F"] = n;
n+=2;
chord_map_note["G"] = n;
n+=2;
chord_map_note["A"] = n;
n+=2;
chord_map_note["B"] = n;
}
midiMap::~midiMap(){
clear();
if(pool)
delete (npool*)pool;
}
static bool isNotHalfNote(int note,int base){
const static bool l[] = {true,false,true,false,true,true,false,true,false,true,false,true};
return l[(note-base+12)%12];
}
static bool checkMajor(int note){
const static bool l[] = {true,false,false,false,true,false,false,true,false,false,false,false};
return l[note%12];
}
bool midiMap::updateTimeMax(){
if(noteUpdated){
noteTimeMax = 0;
noteToneMin = 255;
noteToneMax = 0;
for(auto & it:notes){
double tm = it->begin + it->duration;
if(tm>noteTimeMax){
noteTimeMax = tm;
}
double tone = it->tone;
if(tone>0 && tone<128){
if(tone>noteToneMax){
noteToneMax = tone;
}
if(tone<noteToneMin){
noteToneMin = tone;
}
}
}
noteUpdated = false;
return true;
}
return false;
}
int midiMap::getBaseTone(){
int single_note_count[12];//记录每个音符出现次数
for(int i=0;i<12;++i){
single_note_count[i] = 0;
}
int note_count = 0;
for (auto note:notes) {
note_count += note->duration;
single_note_count[(((int)note->tone)+12)%12] += note->duration;
}
std::tuple<int, float> major_prob[12];
for (int base = 0; base < 12; ++base) {
int nh_count = 0;
for(int i=0;i<12;++i){
if (isNotHalfNote(i, base)) {
nh_count += single_note_count[i];
}
}
if (note_count == 0) {
major_prob[base] = std::make_tuple(base, 0);
} else {
major_prob[base] = std::make_tuple(base, ((float) nh_count) / ((float) note_count));
}
}
std::sort(major_prob, major_prob + 12,
[](const std::tuple<int, float> &x, const std::tuple<int, float> &y) {
return std::get<1>(x) > std::get<1>(y);
});
if (checkMajor(std::get<1>(major_prob[0]))) {
baseTone = std::get<0>(major_prob[0]);
} else {
if (std::get<1>(major_prob[1]) == std::get<1>(major_prob[0])) {
baseTone = std::get<0>(major_prob[1]);
} else {
baseTone = std::get<0>(major_prob[0]);
}
}
//识别大小调
int num_do = single_note_count[baseTone];
int num_la = single_note_count[(baseTone+9)%12];
isMajor = (num_do>num_la);
return baseTone;
}
note * midiMap::addNote(double position,double tone,double duration,int v,const std::string & info,int ins_id){
HBB::vec from;
HBB::vec to;
from.set(position , tone);
to = from;
to.X += duration;
to.Y += 0.9;
auto p = ((npool*)pool)->get();
p->construct();
p->id=++id;
p->startId=++id;//0是默认值,用0可能会出问题
p->endId=++id;//分配两个id,防止时间为0的情况
p->begin = position;
p->tone = tone;
p->duration = duration;
p->volume = v;
p->info = info;
p->ins_id = ins_id;
p->getBeginIndex();
p->getEndIndex();
noteIDs[p->id] = p;
timeIndex[p->beginIndex] = p;
timeIndex[p->endIndex] = p;
auto bx = indexer.add(from,to,p);
p->indexer = bx;
notes.insert(p);
if(!info.empty()){
if(info[0]=='@'){
addControl(position,info);
}
}
noteUpdated = true;
return p;
}
void midiMap::resizeNote(note * p){
if(p){
if(p->indexer){
auto i = (HBB::AABB*)(p->indexer);
i->autodrop();
}
HBB::vec from;
HBB::vec to;
from.set(p->begin , p->tone);
to = from;
to.X += p->duration;
to.Y += 0.9;
timeIndex.erase(p->endIndex);
p->getEndIndex();//更新尾部索引
timeIndex[p->endIndex] = p;
auto bx = indexer.add(from,to,p);
p->indexer = bx;
noteUpdated = true;
}
}
void midiMap::removeNote(note * p){
if(p){
if(p->indexer){
auto i = (HBB::AABB*)(p->indexer);
i->autodrop();
}
if(!p->info.empty()){
if(p->info[0]=='@'){
removeControl(p->begin,p->info);
}
}
notes.erase(p);
noteIDs.erase(p->id);
timeIndex.erase(p->beginIndex);
timeIndex.erase(p->endIndex);
((npool*)pool)->del(p);
noteUpdated = true;
}
}
void midiMap::clear(){
for(auto p : notes){
if(p->indexer){
auto i = (HBB::AABB*)(p->indexer);
i->autodrop();
}
((npool*)pool)->del(p);
//不能直接removeNote,否则会出现野指针
}
notes.clear();
timeIndex.clear();
noteUpdated = true;
}
int midiMap::find(const HBB::vec & from,const HBB::vec & to,void(*callback)(note*,void*),void * arg){
HBB::AABB tmpbox;
tmpbox.from=from;
tmpbox.to=to;
struct self{
void(*callback)(note*,void*);
void * arg;
midiMap * s;
int num;
}s;
s.arg=arg;
s.callback=callback;
s.s=this;
s.num=0;
indexer.collisionTest(&tmpbox,[](HBB::AABB * p,void * arg){
auto s = (self*)arg;
auto np = (note*)(p->data);
if(np && (s->s->infoFilter.empty() || np->info==s->s->infoFilter)){
s->callback(np,s->arg);
s->num++;
}
},&s);
return s.num;
}
int midiMap::find(const HBB::vec & pt,void(*callback)(note*,void*),void * arg){
struct self{
void(*callback)(note*,void*);
void * arg;
midiMap * s;
int num;
}s;
s.arg=arg;
s.callback=callback;
s.s=this;
s.num=0;
indexer.fetchByPoint(pt,[](HBB::AABB * p,void * arg){
auto s = (self*)arg;
auto np = (note*)(p->data);
if(np && (s->s->infoFilter.empty() || np->info==s->s->infoFilter)){
s->callback(np,s->arg);
s->num++;
}
},&s);
return s.num;
}
int midiMap::find(double step,void(*callback)(note*,void*),void * arg){
struct self{
void(*callback)(note*,void*);
void * arg;
midiMap * s;
int num;
}s;
s.arg=arg;
s.callback=callback;
s.s=this;
s.num=0;
indexer.fetchByStep(step,[](HBB::AABB * p,void * arg){
auto s = (self*)arg;
auto np = (note*)(p->data);
if(np && (s->s->infoFilter.empty() || np->info==s->s->infoFilter)){
s->callback(np,s->arg);
s->num++;
}
},&s);
return s.num;
}
double midiMap::getTempo(int tick){
if(timeMap.empty())
return 120;
auto it = timeMap.upper_bound(tick);//获取大于tick的第一个元素
if(it==timeMap.end()){//指向结尾
it--;
return it->second;//返回最后一个元素
}
if(it==timeMap.begin()){//指向开头
return it->second;
}
it--;//向前移动一步
return it->second;
}
bool midiMap::addTempo(int tick,double tp){
if(timeMap.find(tick)==timeMap.end()){
printf("add tempo:%d %f\n",tick,tp);
timeMap[tick]=tp;
return true;
}else{
return false;
}
}
void midiMap::getTempo(int begin,const std::function<bool(int,double)> & callback){
if(timeMap.empty()){
callback(0,120);
return;
}
auto it = timeMap.upper_bound(begin);//获取大于tick的第一个元素
if(it==timeMap.end()){//指向结尾
it--;
callback(it->first , it->second);
return;
}
if(it==timeMap.begin()){//指向开头
}else{
it--;//向前移动一步
}
while(it!=timeMap.end()){
if(!callback(it->first , it->second))break;
++it;
}
}
std::tuple<bool,int,double> midiMap::removeTempoBeforePos(int tick){
if(timeMap.empty())
return std::make_tuple(false,0,0.0);
auto it = timeMap.upper_bound(tick);//获取大于tick的第一个元素
if(it==timeMap.end()){//指向结尾
it--;
auto res = std::make_tuple(true,it->first,it->second);
timeMap.erase(it);
return res;
}
if(it==timeMap.begin()){//指向开头
auto res = std::make_tuple(true,it->first,it->second);
timeMap.erase(it);
return res;
}
it--;//向前移动一步
auto res = std::make_tuple(true,it->first,it->second);
timeMap.erase(it);
return res;
}
void midiMap::removeControl(double begin,const std::string & info){
if(!info.empty() && info[0]=='@' && info.size()>2){
auto str=info.c_str();
if(str[1]=='T'){
auto r=str+2;
if(strlen(r)>0){
int tick=atoi(r);
if(tick>0){
printf("remove tempo:%f %d\n",begin,tick);
timeMap.erase(begin);
}
}
}
}
}
void midiMap::addControl(double begin,const std::string & info){
if(!info.empty() && info[0]=='@' && info.size()>2){
auto str=info.c_str();
if(str[1]=='T'){
auto r=str+2;
if(strlen(r)>0){
double tick=atof(r);
if(tick>0){
printf("add tempo:%f %f\n",begin,tick);
timeMap[begin]=tick;
}
}
}else
if(str[1]=='S'){
auto r=str+2;
if(strlen(r)>0){
int sec=atoi(r);
if(sec>0 && sec<8){
printf("set section:%d\n",sec);
setSection(sec);
}
}
}
}
}
int midiMap::getAreaNote(double begin,double len,const std::string & info,double forceLen,double minLen){
struct self_t{
std::vector<std::tuple<double,double,int> > tones;
double sum;
double begin;
std::string info;
}self;
self.sum = 0;
self.info = info;
self.begin = begin;
find(HBB::vec(begin,0),HBB::vec(begin+len,128),[](note * n,void * arg){//获取
auto self = (self_t*)arg;
if(!self->info.empty()){
if(n->info!=self->info){
return;
}
}
if(n->begin<self->begin){
double delta = n->begin - self->begin;
double dur = n->duration+delta;
if(dur>=1){
self->tones.push_back(std::make_tuple(n->begin-delta , dur , n->tone));
self->sum+=dur;
}
}else{
self->tones.push_back(std::make_tuple(n->begin,n->duration,n->tone));
self->sum+=n->duration;
}
},&self);
if(self.tones.empty() || self.sum<=0)
return -1;
std::map<int,double> tone_time;
for(auto & it:self.tones){
tone_time[std::get<2>(it)] += std::get<1>(it)/self.sum;
}
for(auto & it:tone_time){
if(it.second > forceLen){
return it.first;
}
}
for(auto & it:self.tones){
if(tone_time[std::get<2>(it)]>=minLen){
return std::get<2>(it);
}
}
return -1;
}
int midiMap::getSectionNote(double sec,const std::string & info,double forceLen,double minLen){
double len = TPQ;
double pos = len*(section*sec+XShift);
for(int i=0;i<section;++i){
int res = getAreaNote(pos,len,info,forceLen,minLen);
if(res!=-1){
return res;
}
}
return -1;
}
}//namespace mgnr