-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathseg.cpp
2339 lines (2087 loc) · 96.5 KB
/
seg.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This is the code for the phrase-level analysis of the paper: Automatic Analysis and Influence of Hierarchical Structure on Melody, Rhythm and Harmony in Popular Music
https://www.cs.cmu.edu/~rbd/papers/dai-mume2020.pdf
Input: Preprocessing results of a midi song from preprocessing.sh, including melody.txt, finalized_chord.txt, analyzed_key.txt, time signature
Output: Phrase-level structure analysis result
Notice that it is an approximate version, if you want to run the full version, turn off the timing code at line 1822
Author: Shuqi Dai
Oct, 2020
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <cmath>
#include <random>
#include <time.h>
#include <set>
using namespace std;
/* ===================== CLASS Definitions ====================== */
class Chord {
public:
char name[10];
bool tones[12]; // from pitch C (0) to B (11)
int bass;
int duration; // in unit of beat
Chord(char* name_, int bass_, int duration_, vector<int> tone_) {
strcpy(name, name_);
bass = bass_;
duration = duration_;
memset(tones, 0, sizeof(tones));
for (int i = 0; i < tone_.size(); ++i)
tones[tone_[i]] = 1;
}
Chord(const Chord& c) {
strcpy(name, c.name);
for (int i = 0; i < 12; ++i)
tones[i] = c.tones[i];
bass = c.bass;
duration = c.duration;
}
Chord &operator=(const Chord& c) {
if (this == &c) {
return *this;
}
strcpy(this->name, c.name);
this->bass = c.bass;
this->duration = c.duration;
for (int i = 0; i < 12; ++i)
this->tones[i] = c.tones[i];
return *this;
}
void printChord() const {
printf("%s duration: %d bass: %d (", name, duration, bass);
for (int k = 0; k < 12; ++k)
printf("%d, ", tones[k]);
printf(")\n");
}
};
class Note {
public:
int pitch, duration;
// pitch is MIDI number, -2 means REST note
// duration in sixteenth notes
Note(int pitch_, int duration_) {
pitch = pitch_;
duration = duration_;
}
Note(const Note& x) {
pitch = x.pitch;
duration = x.duration;
}
void printNote() const {
printf(" (%d, %d) ", pitch, duration);
}
};
// Section class means a fragment of the song
class Section {
public:
vector<Note> melody;
vector<Chord> chords;
int start, end; // bar number in original piece
int time_signature; // how many 16th beats are there in each bar, 4/4 - 16, 3/4 -12
vector<string> key_name;
Section() {
melody.clear();
chords.clear();
start = end = 0;
key_name.clear();
time_signature = 0;
}
Section(int length, int time_signature_) {
melody.clear();
chords.clear();
start = 0;
end = length;
key_name.clear();
time_signature = time_signature_;
melody.push_back(Note(-2, (end - start) * time_signature));
}
Section(const Section& x) {
melody = x.melody;
chords = x.chords;
start = x.start;
end = x.end;
time_signature = x.time_signature;
key_name = x.key_name;
}
bool is_same(const Section& x) const {
if (start == x.start && end == x.end) return 1;
return 0;
}
bool non_overlap(const Section& x) const {
if (start >= x.end || end <= x.start) return 1;
return 0;
}
// transpose the whole section to another key
// notice that, here we do not rewrite the key name or chord name
// only melody pitch and chord tones are changed
void transpose_section(int delta) {
for (int i = 0; i < melody.size(); ++i)
if (melody[i].pitch > -2) melody[i].pitch += delta;
for (int i = 0; i < chords.size(); ++i) {
bool tmp_tones[12];
for (int j = 0; j < 12; ++j)
tmp_tones[(j + delta) % 12] = chords[i].tones[j];
for (int j = 0; j < 12; ++j)
chords[i].tones[j] = tmp_tones[j];
}
}
// return a subsection of current section
Section subsection(int l, int r) const {
Section sub;
sub.start = l;
sub.end = r;
sub.time_signature = time_signature;
sub.key_name = key_name;
int mt = start * time_signature, i = 0;
while (i < melody.size() && mt + melody[i].duration < l * time_signature) {
mt = mt + melody[i].duration;
++i;
}
if (i < melody.size()) {
Note tmp_melody(melody[i]);
tmp_melody.duration = mt + melody[i].duration - l * time_signature;
mt = mt + melody[i].duration;
++i;
if (tmp_melody.duration > 0)
sub.melody.push_back(tmp_melody);
while (i < melody.size() && mt < r * time_signature) {
if (mt + melody[i].duration > r * time_signature) {
Note tmp_note_again(melody[i]);
tmp_note_again.duration = r * time_signature - mt;
sub.melody.push_back(tmp_note_again);
break;
}
sub.melody.push_back(Note(melody[i]));
mt += melody[i].duration;
++i;
}
}
int t = start * time_signature / 4;
i = 0;
while (i < chords.size() && t + chords[i].duration < l * time_signature / 4) {
t = t + chords[i].duration;
++i;
}
if (i < chords.size()) {
Chord tmp_chord(chords[i]);
tmp_chord.duration = t + chords[i].duration - l * time_signature / 4;
t = t + chords[i].duration;
++i;
if (tmp_chord.duration > 0)
sub.chords.push_back(tmp_chord);
while (i < chords.size() && t < r * time_signature / 4) {
if (t + chords[i].duration > r * time_signature / 4) {
Chord tmp_chord_again(chords[i]);
tmp_chord_again.duration = r * time_signature / 4 - t;
sub.chords.push_back(tmp_chord_again);
break;
}
sub.chords.push_back(Chord(chords[i]));
t = t + chords[i].duration;
++i;
}
}
return sub;
}
// if this section is a bridge (means empty)
bool is_bridge(int tip = 4) const {
int t = 0;
int mel_len = melody.size();
int time_len = ((end - start) * time_signature);
for (int i = 0; i < mel_len && t < time_len - tip; ++i) {
if (melody[i].pitch > -2 && t + melody[i].duration > tip && t < time_len - tip)
return false;
t += melody[i].duration;
}
return true;
}
bool is_subsec_bridge(int l, int r, int tip = 4) const {
Section tmp_sec = this->subsection(l, r);
if (tmp_sec.is_bridge()) return true;
if (l > start) {
Section tmp_sec = this->subsection(l - 1, r);
int t = 0, i = 0;
while(i < tmp_sec.melody.size()) {
if (tmp_sec.melody[i].pitch > -2 && t < time_signature - 2 && t + tmp_sec.melody[i].duration > time_signature) {
int note_length = tmp_sec.melody[i++].duration;
while (i < tmp_sec.melody.size() && tmp_sec.melody[i].pitch == -2) note_length += tmp_sec.melody[i++].duration;
if (t + note_length >= time_signature * (r - l + 1) - tip) return true;
else return false;
}
t += tmp_sec.melody[i++].duration;
}
}
return false;
}
bool contains_bridge(int default_rest = 40) {
int mel_len = melody.size(), t = 0, d, time_len = ((end - start) * time_signature);
for (int i = 0; i < mel_len && t < time_len; ++i) {
if (t + melody[i].duration > time_len) d = time_len - t;
else d = melody[i].duration;
if (melody[i].pitch == -2 && d >= default_rest) return true;
t += d;
}
return false;
}
void printSection() const {
printf("section (%d, %d): \n", start, end);
printf("melody ");
for (int i = 0; i < melody.size(); ++i)
melody[i].printNote();
printf("\n");
for (int i = 0; i < chords.size(); ++i)
chords[i].printChord();
}
};
// Match class is a set of sections that considered repetition to each other
// contains an average matching score
class Match {
public:
vector<Section> sections;
double score;
Match() {}
Match(const Match& x) {
sections = x.sections;
score = x.score;
}
void insert_section (Section sec) {
for (vector<Section>::iterator idx = sections.begin(); idx < sections.end(); ++idx)
if (idx->start > sec.start) {
sections.insert(idx, sec);
return;
}
sections.push_back(sec);
}
bool is_same(const Match& x) {
if (sections.size() != x.sections.size()) return false;
for (int i = 0; i < sections.size(); ++i)
if (!sections[i].is_same(x.sections[i])) return false;
return true;
}
void printMatch() const {
for (int i = 0; i < sections.size(); ++i)
printf( " (%2d, %2d) ", sections[i].start, sections[i].end);
printf(" : %.6lf \n", score);
}
};
int bitcount_table[256] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
};
int clear_outtro(const Section &song);
int clear_intro(const Section &song);
// Covermap class is used in searching for the best match cover.
// using bitmap to represent the cover status
class Covermap {
public:
unsigned long long map[3]; // cover status, bitmap map[0] 0 - 63, map[1] 64 - 127, map[2] 128 - 191
int length;
double cost; // current SDL
double match_score; //current composed rhythm similarity, contour similarity, chord similarity
Covermap *from; // previous step cover status
Match cover; // latest match added
Section *original_song;
string string_result; // cover results in string representation
string string_result_merge_extra; // cover result after merging extra 'X' measures
Covermap(int length_, double g, double h, Section* a) {
length = length_;
map[0] = map[1] = map[2] = 0;
cost = h + double(length) * g;
match_score = 0.0;
from = NULL;
original_song = a;
string_result = "X" + to_string(length);
string_result_merge_extra = "";
}
Covermap(const Covermap& x) {
length = x.length;
cost = x.cost;
from = x.from;
cover= x.cover;
match_score = x.match_score;
map[0] = x.map[0];
map[1] = x.map[1];
map[2] = x.map[2];
string_result = x.string_result;
string_result_merge_extra = x.string_result_merge_extra;
original_song = x.original_song;
}
bool is_same(Covermap x) {
if (length != x.length) return 0;
if (map[0] == x.map[0] && map[1] == x.map[1] && map[2] == x.map[2]) return 1;
return 0;
}
bool is_ok2add_cover(Match x) {
unsigned long long a = 1, mask, t = - 1;
int section_len = x.sections.size();
for (int idx = 0; idx < section_len; ++idx) {
int s = x.sections[idx].start, e = x.sections[idx].end;
if (s < 64) {
if (e < 64)
mask = ((a << (e)) - 1) - ((a << (s)) - 1);
else
mask = t - ((a << (s)) - 1);
}
else mask = 0;
if ((map[0] & mask) > 0) return 0;
if (s < 64) s = 64;
if (s < 128 && e > 64) {
if (e < 128)
mask = ((a << (e - 64)) - 1) - ((a << (s - 64)) - 1);
else
mask = t - ((a << (s - 64)) - 1);
}
else mask = 0;
if ((map[1] & mask) > 0) return 0;
if (s < 128) s = 128;
if (e > 128) {
mask = ((a << (e - 128)) - 1) - ((a << (s - 128)) - 1);
}
else mask = 0;
if ((map[2] & mask) > 0) return 0;
}
return 1;
}
int bitonecount(const unsigned long long n) {
return bitcount_table[n & 0xff] +
bitcount_table[(n >>8) & 0xff] +
bitcount_table[(n >>16) & 0xff] +
bitcount_table[(n >>24) & 0xff] +
bitcount_table[(n >>32) & 0xff] +
bitcount_table[(n >>40) & 0xff] +
bitcount_table[(n >>48) & 0xff] +
bitcount_table[(n >>56) & 0xff];
}
int getmap_at(int position) {
if (position < 64) return int((map[0] >> position) & 1);
if (position < 128) return int((map[1] >> (position - 64)) & 1);
return int((map[2] >> (position - 128)) & 1);
}
double best_possible_cost(double g, double h, int begin, int real_end) {
unsigned long long a = 1;
int l = 0;
if (begin < 64) l = bitonecount(map[0]) + bitonecount(map[1]) + bitonecount(map[2]) - bitonecount(map[0] & ((a << begin) - 1));
else if (begin < 128) l = bitonecount(map[1]) + bitonecount(map[2]) - bitonecount(map[1] & ((a << (begin - 64)) - 1));
else l = bitonecount(map[2]) - bitonecount(map[2] & ((a << (begin - 128)) - 1));
l = real_end - begin - l;
if (l < 8) return cost;
return cost - g * double(l - 4) - h * (1.0 - double(l /4));
}
void printMap () const {
printf("Covermap: Length %d, c %.2lf, m %.2lf [ ", length, cost, match_score);
for (int i = 0; i < length; ++i) {
if (i < 64) printf("%d ", int((map[0] >> i) & 1));
else if (i < 128) printf("%d ", int((map[1] >> (i - 64)) & 1));
else printf("%d ", int((map[2] >> (i - 128)) & 1));
}
printf("], last cover ");
for (int i = 0; i < cover.sections.size(); ++i)
printf(" (%d, %d) ", cover.sections[i].start, cover.sections[i].end);
if (from != NULL)
printf(" from %.2lf\n", from->cost);
printf("\n");
}
void toString() {
int real_end = clear_outtro(*original_song);
int real_start = clear_intro(*original_song);
if (real_end > length) real_end = length;
if (map[0] == 0 && map[1] == 0 && map[2] == 0) {
string_result = "";
if (real_start > 0) string_result += "i" + to_string(real_start);
string_result += "X" + to_string(real_end - real_start);
if (real_end < length) string_result += "o" + to_string(length - real_end);
return;
}
Covermap* i = this;
vector<Match> used;
vector<double> costs;
while (1) {
if (i->cover.sections.size() > 0) {
used.push_back(i->cover);
costs.push_back(i->cost);
}
if (i->from == NULL) break;
i = i->from;
}
Match ordered;
int result_map[length];
memset(result_map, 'X', sizeof(result_map));
for (int j = 0; j < used.size(); ++j) {
for (int k = 0; k < used[j].sections.size(); ++k) {
ordered.insert_section(used[j].sections[k]);
for (int r = used[j].sections[k].start; r < used[j].sections[k].end; ++r)
result_map[r] = j;
}
}
int t = 0;
string result = "";
char f[used.size()];
memset(f, 0, sizeof(f));
char flag = 'A';
char bridge_flag = 'b';
if (real_start > 0) {
result += "i" + to_string(real_start);
t = real_start;
}
for (int j = 0; j < ordered.sections.size(); ++j) {
if (ordered.sections[j].start > t) {
Section tmp_sub = original_song->subsection(t, ordered.sections[j].start);
if (tmp_sub.is_bridge() || original_song->is_subsec_bridge(t, ordered.sections[j].start)) {
result += 'x';
result += to_string(ordered.sections[j].start - t);
} else if (tmp_sub.contains_bridge()) {
int tmp_measure_count = 0, kr;
for (kr = 0; kr < tmp_sub.melody.size(); ++kr) {
if (tmp_sub.melody[kr].duration >= 40 && tmp_sub.melody[kr].pitch == -2)
break;
tmp_measure_count += tmp_sub.melody[kr].duration;
}
int bridge_start = tmp_measure_count / (original_song->time_signature) + int((tmp_measure_count % (original_song->time_signature)) > 4);
int bridge_end = (tmp_measure_count + tmp_sub.melody[kr].duration) / (original_song->time_signature) + int(((tmp_measure_count + tmp_sub.melody[kr].duration) % (original_song->time_signature)) > ((original_song->time_signature / 2)));
if (bridge_start > 0) {
result += 'X';
result += to_string(bridge_start);
}
if (t + bridge_end > ordered.sections[j].start)
bridge_end = ordered.sections[j].start - t;
result += 'x';
int left_over = (ordered.sections[j].start - t) * (original_song->time_signature) -
(tmp_measure_count + tmp_sub.melody[kr].duration);
if (t + bridge_end + 1 == ordered.sections[j].start && left_over <= (original_song->time_signature / 2))
result += to_string(bridge_end - bridge_start + 1);
else {
result += to_string(bridge_end - bridge_start);
if (t + bridge_end < ordered.sections[j].start) {
result += 'X';
result += to_string(ordered.sections[j].start - t - bridge_end);
}
}
} else {
if (tmp_sub.melody[0].duration >= (original_song->time_signature) && tmp_sub.melody[0].pitch == -2) {
result += 'x';
result += to_string(tmp_sub.melody[0].duration / (original_song->time_signature) +
int((tmp_sub.melody[0].duration % (original_song->time_signature)) >= (original_song->time_signature / 2)));
t += tmp_sub.melody[0].duration / (original_song->time_signature) +
int((tmp_sub.melody[0].duration % (original_song->time_signature)) >= (original_song->time_signature / 2));
}
if (t < ordered.sections[j].start) {
result += 'X';
result += to_string(ordered.sections[j].start - t);
}
}
}
if (f[result_map[ordered.sections[j].start]] == 0) {
if (ordered.sections[j].is_bridge())
f[result_map[ordered.sections[j].start]] = bridge_flag++;
else
f[result_map[ordered.sections[j].start]] = flag++;
}
result += f[result_map[ordered.sections[j].start]];
result += to_string(ordered.sections[j].end - ordered.sections[j].start);
t = ordered.sections[j].end;
}
if (t < real_end) {
result += 'X';
result += to_string(real_end - t);
t = real_end;
}
if (t < length) {
result += 'o';
result += to_string(length - t);
}
string_result = result;
}
int longest_note(vector<Note> melody, int tip = 16) {
int longest = 1;
int i = melody.size() - 1;
int pos = 0;
while (i >= 0 && pos <= tip) {
if (melody[i].pitch == -2 && i > 0) {
pos += melody[i].duration;
i--;
int tmp = melody[i].duration + melody[i + 1].duration;
if (tmp > longest) longest = tmp;
}
else if (melody[i].duration > longest) longest = melody[i].duration;
pos += melody[i--].duration;
}
return longest;
}
bool contain_match(const pair<Section, Section> candidate, const vector<pair<Section, Section> > &matches) {
for (int i = int(matches.size()) - 1; i >= 0; --i) {
if (candidate.first.start == matches[i].first.start &&
candidate.first.end == matches[i].first.end &&
candidate.second.start == matches[i].second.start &&
candidate.second.end == matches[i].second.end)
return true;
}
return false;
}
void small_extra_merge() {
if (string_result_merge_extra == "") string_result_merge_extra = string_result;
if (string_result_merge_extra == "") return;
int i = 0;
vector<pair<char, int> > phrases;
phrases.clear();
while (i < string_result_merge_extra.length()) {
char label = string_result_merge_extra[i++];
int num = 0;
while (i < string_result_merge_extra.length() &&
string_result_merge_extra[i] >= '0' && string_result_merge_extra[i] <= '9')
num = num * 10 + string_result_merge_extra[i++] - '0';
phrases.push_back(make_pair(label, num));
}
for (int times = 0; times <= 1; ++times) {
for (i = phrases.size() - 2; i >= 0; --i) {
if (phrases[i].first >= 'A' && phrases[i].first <= 'Z' && phrases[i].first != 'X' &&
(phrases[i + 1].first == 'x' || phrases[i + 1].first == 'X') && phrases[i + 1].second == 1) {
bool flag1 = (i + 2 == phrases.size() || (phrases[i + 2].first >= 'A' && phrases[i + 2].first <= 'Z'));
bool flag2 = ((phrases[i].second + phrases[i + 1].second) % 4 == 0 || phrases[i + 1].first == 'x');
if (flag1 && flag2){
phrases[i].second += phrases[i + 1].second;
phrases.erase(phrases.begin() + i + 1);
}
else {
bool delete_flag = false;
for (int j = 0; j < phrases.size() && !delete_flag; ++j)
if (phrases[i].first == phrases[j].first && phrases[i].second + 1 == phrases[j].second) {
phrases[i].second += phrases[i + 1].second;
phrases.erase(phrases.begin() + i + 1);
delete_flag = true;
}
}
}
}
}
string new_merge_result = "";
for (i = 0; i < phrases.size(); ++i)
new_merge_result += phrases[i].first + to_string(phrases[i].second);
string_result_merge_extra = new_merge_result;
}
void merge_extra(const vector<pair<Section, Section> > &matches, bool small_extra_only = false) {
if (string_result == "") return;
int i = 0;
int current_pos = 0;
char prev_label = 'i';
vector<pair<char, int> > new_result;
vector<int> new_pos_end;
vector<pair<int, Section> > long_extra;
char max_label = 'A';
while (i < string_result.length()) {
char label = string_result[i++];
if (label >= 'A' && label <= 'Z' && label != 'X' && label > max_label) max_label = label;
int num = 0;
while (i < string_result.length() && string_result[i] >= '0' && string_result[i] <= '9')
num = num * 10 + string_result[i++] - '0';
current_pos += num;
if ((prev_label >= 'A' && prev_label <= 'Z' && (!small_extra_only || (small_extra_only && num <= 2))) &&
(label == 'X' || (label == 'x' && !original_song->subsection(current_pos - num, current_pos).is_bridge()))) {
int lowerbound_longest_note = -1;
int prev_num = new_result.back().second;
for (int j = 0; j < new_result.size() - 1; ++j)
if (new_result[j].first == prev_label) {
Section tmp = (original_song->subsection(new_pos_end[j] - 1, new_pos_end[j]));
int tmp_ln = longest_note(tmp.melody, original_song->time_signature);
if (lowerbound_longest_note == -1 || tmp_ln < lowerbound_longest_note) lowerbound_longest_note = tmp_ln;
}
if (lowerbound_longest_note >= 8 || lowerbound_longest_note == -1) lowerbound_longest_note = 8;
int extra_lowerbound_longest_note = lowerbound_longest_note;
if (extra_lowerbound_longest_note < 6) extra_lowerbound_longest_note = 6;
bool flag0 = false, flag1 = false, flag2 = false;
Section m0 = (original_song->subsection(current_pos - num - 1, current_pos - num));
Section m1 = (original_song->subsection(current_pos - num, current_pos - num + 1));
//Section m2 = (original_song->subsection(current_pos - num + 1, current_pos - num + 2));//
int ln0 = longest_note(m0.melody, original_song->time_signature);
int ln1 = longest_note(m1.melody, original_song->time_signature), ln2 = 0;
if (extra_lowerbound_longest_note < 8 && extra_lowerbound_longest_note < ln0 + 2)
extra_lowerbound_longest_note = ln0 + 2;
if (num > 1) {
Section m2 = (original_song->subsection(current_pos - num + 1, current_pos - num + 2));
ln2 = longest_note(m2.melody, original_song->time_signature);
if (ln2 >= extra_lowerbound_longest_note) flag2 = true;
if (m2.is_bridge(0)) flag2 = false;
}
if (ln0 >= lowerbound_longest_note) flag0 = true;
if (ln1 >= extra_lowerbound_longest_note) flag1 = true;
if (m1.is_bridge(0)) flag1 = flag2 = false;
if (m0.is_bridge(0)) flag1 = flag2 = false;
if (ln0 >= 8 && prev_num % 4 == 0 &&
(num > 2 || (i < string_result.length() && string_result[i] >= 'A' && string_result[i] <= 'Z')))
{flag0 = true; flag1 = flag2 = false;}
if (label == 'x' && ln0 >= 4 && prev_num % 4 == 0) {flag0 = true; flag1 = flag2 = false;}
int decision = 0;
if (flag2) {
if (flag0 && flag1) {
if (ln2 >= ln1 && ln2 >= ln0) decision = 2;
else if (ln1 >= ln0 && ln1 >= ln2) decision = 1;
}
else if (flag0 || flag1) {
if (flag1) {
if (ln2 >= ln1) decision = 2;
else decision = 1;
}
else {
if (ln2 >= ln0) decision = 2;
}
}
else decision = 2;
}
else if (flag1) {
if (flag0) {
if (ln1 >= ln0) decision = 1;
}
else decision = 1;
}
else if (!flag0 && !flag1 && !flag2) {
if (ln2 >= ln1 && ln2 >= ln0) decision = 2;
else if (ln1 >= ln2 && ln1 >= ln0) decision = 1;
}
num -= decision;
new_result[new_result.size() - 1].second += decision;
new_pos_end[new_result.size() - 1] += decision;
}
if (num > 0) {
new_result.push_back(make_pair(label, num));
new_pos_end.push_back(current_pos);
if (num >= 4 && label == 'X')
long_extra.push_back(
make_pair(new_result.size() - 1, original_song->subsection(current_pos - num, current_pos)));
}
prev_label = label;
}
for (int i = 0; i < long_extra.size(); ++i) {
int j;
for (j = i + 1; j < long_extra.size(); ++j) {
if (new_result[long_extra[i].first].second == new_result[long_extra[j].first].second &&
contain_match(make_pair(long_extra[i].second, long_extra[j].second), matches)) {
new_result[long_extra[i].first].first = max_label + 1;
new_result[long_extra[j].first].first = max_label + 1;
max_label++;
break;
}
}
if (j < long_extra.size()) break;
}
char current_label = 'A' - 1;
for (int i = 0; i < new_result.size(); ++i) {
if (new_result[i].first >= 'A' && new_result[i].first <= 'Z' && new_result[i].first != 'X'
&& current_label < new_result[i].first) {
if (current_label + 1 == new_result[i].first) current_label++;
else {
//switch label
char label1 = current_label + 1;
char label2 = new_result[i].first;
vector<int> candidate1;
vector<int> candidate2;
for (int j = i; j < new_result.size(); ++j) {
if (new_result[j].first == label1) candidate1.push_back(j);
else if (new_result[j].first == label2) candidate2.push_back(j);
}
for (int j = 0; j < candidate1.size(); ++j)
new_result[candidate1[j]].first = label2;
for (int j = 0; j < candidate2.size(); ++j)
new_result[candidate2[j]].first = label1;
current_label++;
}
}
}
vector<int> start_pos;
start_pos.push_back(0);
for (i = 1; i < new_result.size(); ++i)
start_pos.push_back(start_pos[i - 1] + new_result[i - 1].second);
i = 0;
while (i < new_result.size()) {
if (i + 1 < new_result.size() &&
new_result[i].first >= 'A' && new_result[i].first <= 'Z' && new_result[i].first != 'X' &&
new_result[i].second >= 6 && new_result[i].second < 8 && new_result[i + 1].first == 'X' &&
new_result[i + 1].second + new_result[i].second == 8) {
vector<int> candidates;
candidates.clear();
candidates.push_back(i);
bool valid_flag = true;
for (int j = i + 2; j + 1 < new_result.size(); ++j) {
if (new_result[j].first == new_result[i].first &&
new_result[j + 1].first == 'X' && new_result[j + 1].second + new_result[j].second == 8 &&
contain_match(make_pair(original_song->subsection(start_pos[i], start_pos[i] + 8),
original_song->subsection(start_pos[j], start_pos[j] + 8)), matches)) {
candidates.push_back(j);
}
else if (new_result[j].first == new_result[i].first && new_result[j].second <= 5)
valid_flag = false;
}
for (int j = 0; j < i && valid_flag; ++j) {
if (new_result[j].first == new_result[i].first && new_result[j].second <= 5 &&
new_result[j + 1].first != 'X')
valid_flag = false;
}
if (candidates.size() > 1 && valid_flag) {
for (int j = candidates.size() - 1; j >= 0; --j) {
int t = candidates[j];
new_result[t].second += new_result[t + 1].second;
new_result.erase(new_result.begin() + t + 1);
start_pos.erase(start_pos.begin() + t + 1);
}
}
}
++i;
}
string_result_merge_extra = "";
start_pos.clear();
start_pos.push_back(0);
for (i = 1; i < new_result.size(); ++i)
start_pos.push_back(start_pos[i - 1] + new_result[i - 1].second);
for (i = 0; i < new_result.size(); ++i) {
if (new_result[i].first == 'X' &&
(original_song->subsection(start_pos[i], start_pos[i] + new_result[i].second)).is_bridge()) {
new_result[i].first = 'x';
if (i + 1 < new_result.size() && new_result[i + 1].first == 'x') {
new_result[i].second += new_result[i + 1].second;
new_result[i + 1].second = 0;
}
else if (i + 1 < new_result.size() && new_result[i + 1].first == 'o') {
new_result[i + 1].second += new_result[i].second;
new_result[i].second = 0;
}
}
if (new_result[i].second > 0)
string_result_merge_extra += new_result[i].first + to_string(new_result[i].second);
}
}
double calculate_cost(double g, double h, const string &result) {
if (result == "") return (original_song->end - original_song->start) * g + h;
int i = 0;
vector<pair<char, int> > phrases;
phrases.clear();
while (i < result.length()) {
char label = result[i++];
int num = 0;
while (i < result.length() && result[i] >= '0' && result[i] <= '9')
num = num * 10 + result[i++] - '0';
phrases.push_back(make_pair(label, num));
}
for (int times = 0; times <= 1; ++times) {
for (i = phrases.size() - 2; i >= 0; --i) {
if (phrases[i].first >= 'A' && phrases[i].first <= 'Z' && phrases[i].first != 'X' &&
phrases[i + 1].first == 'x' && phrases[i + 1].second <= 1) {
if (i + 2 == phrases.size() || (phrases[i + 2].first >= 'A' && phrases[i + 2].first <= 'Z')) {
phrases[i].second += phrases[i + 1].second;
phrases.erase(phrases.begin() + i + 1);
}
else {
bool delete_flag = false;
for (int j = 0; j < phrases.size() && !delete_flag; ++j)
if (phrases[i].first == phrases[j].first && phrases[i].second + 1 == phrases[j].second) {
phrases[i].second += phrases[i + 1].second;
phrases.erase(phrases.begin() + i + 1);
delete_flag = true;
}
}
}
}
}
for (i = phrases.size() - 2; i >= 0; --i) {
if (phrases[i].first >= 'A' && phrases[i].first <= 'Z' && phrases[i].first != 'X' &&
phrases[i + 1].first == 'x' && phrases[i + 1].second <= 1 &&
(i + 2 == phrases.size() || (phrases[i + 2].first >= 'A' && phrases[i + 2].first <= 'Z'))){
phrases[i].second += phrases[i + 1].second;
phrases.erase(phrases.begin() + i + 1);
}
}
double new_cost = -2.0;
int counts[54], sum[54];
memset(counts, 0, sizeof(counts));
memset(sum, 0, sizeof(sum));
for (i = 0; i < phrases.size(); i++) {
if (phrases[i].first >= 'A' && phrases[i].first <= 'Z' && phrases[i].first != 'X') {
counts[phrases[i].first - 'A']++;
sum[phrases[i].first - 'A'] += phrases[i].second;
}
else if (phrases[i].first >= 'a' && phrases[i].first <= 'z' && phrases[i].first != 'x'){
counts[phrases[i].first - 'a' + 26]++;
sum[phrases[i].first - 'a' + 26] += phrases[i].second;
}
else if (phrases[i].first == 'x' || phrases[i].first == 'X'){
new_cost += g * phrases[i].second;
if (i == 0 || (phrases[i - 1].first != 'x' && phrases[i - 1].first != 'X')) new_cost += h;
else if (phrases[i].first != phrases[i - 1].first &&
phrases[i].second >= 2 && phrases[i - 1].second >= 2 &&
phrases[i].second + phrases[i - 1].second > 4) new_cost += h;
}
}
for (i = 0; i < 54; ++i)
if (counts[i] > 0)
new_cost += h * double(counts[i]) + g * double(sum[i]) / double(counts[i]);
return new_cost;
}
int calculate_cover_area(string result) {
if (result == "") return 0;
int area = 0;
int i = 0;
int current_pos = 0;
while (i < result.size()) {
char label = result[i++];
int num = 0;
while (i < result.length()
&& result[i] >= '0' && result[i] <= '9')
num = num * 10 + result[i++] - '0';
if (label >= 'A' && label <='Z' && label != 'X') area += num;
else if (label >= 'a' && label <= 'z') area += num;
current_pos += num;
}
return area;
}
bool boundary_checking(string result) {
if (result == "") return false;
int i = 0;
char prev_label = 'i';
int prev_num = 16;
int current_pos = 0;
while (i < result.size()) {
char label = result[i++];
int num = 0;
while (i < result.length()
&& result[i] >= '0' && result[i] <= '9')
num = num * 10 + result[i++] - '0';
if (label >= 'A' && label <='Z' && label != 'X'
&& prev_label >= 'A' && prev_label <='Z' && prev_label != 'X'
&& prev_num % 4 != 0 && prev_label != label) {
Section m0 = original_song->subsection(current_pos - 2, current_pos - 1);
Section m1 = original_song->subsection(current_pos - 1, current_pos);
Section m2 = original_song->subsection(current_pos, current_pos + 1);
int ln0 = longest_note(m0.melody, original_song->time_signature);
int ln1 = longest_note(m1.melody, original_song->time_signature);
int ln2 = longest_note(m2.melody, original_song->time_signature);
if ((ln1 <= (original_song->time_signature) / 2 - 2 && ln2 >= (original_song->time_signature - 4)) ||
(ln1 <= 4 && ln2 - ln1 >= 4)) return false;
if ((ln1 <= (original_song->time_signature) / 2 - 2 && ln0 >= (original_song->time_signature - 4)) ||
(ln1 <= 4 && ln0 - ln1 >= 4) || (ln1 <= 8 && ln0 >= 14)) return false;
}
prev_label = label;
prev_num = num;
current_pos += num;
}
return true;
}
// extra control in addition to SDL
double length_variance(string result) {
if (result == "") return 100000.0;
int i = 0;
vector<pair<char, int> > phrases;
char max_label = ' ';
int total_length = 0;
while (i < result.size()) {
char label = result[i++];
int num = 0;
while (i < result.length() && result[i] >= '0' && result[i] <= '9')
num = num * 10 + result[i++] - '0';
phrases.push_back(make_pair(label, num));
if (label != 'o' && label != 'i') total_length += num;
if (label >= 'A' && label <= 'Z' && label != 'X' && label > max_label) max_label = label;
}
if (max_label < 'A') return 0.0;
for (i = phrases.size() - 2; i >= 0; --i) {
if (phrases[i].first >= 'A' && phrases[i].first <= 'Z' && phrases[i].first != 'X' &&
phrases[i + 1].first == 'x' && phrases[i + 1].second <= 1 &&
(phrases[i].second + phrases[i + 1].second) % 4 == 0){
phrases[i].second += phrases[i + 1].second;
phrases.erase(phrases.begin() + i + 1);
}
}
double ans = 0.0;
for (char label = 'A'; label <= max_label; ++label) {
double avg_len = 0.0;
double num = 0.0;
for (i = 0; i < phrases.size(); ++i)
if (phrases[i].first == label) {avg_len += double(phrases[i].second); num += 1.0;}
avg_len /= num;
int std = 4;
if (avg_len > 14.0) std = 16;
else if (avg_len > 11.0) std = 12;
else if (avg_len > 6.0) std = 8;