-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1802 lines (1684 loc) · 69.9 KB
/
main.py
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
import os
import sys
import threading
import time
import re
import mido
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtCore import QPointF, QLineF, QTimer, Qt
from PySide6.QtGui import QGuiApplication, QColor, QPen, QTextCharFormat, QBrush, QTextCursor, QIcon
from PySide6.QtWidgets import QGraphicsRectItem, QGraphicsLineItem
from mido import Message
import mido.backends.rtmidi
from qt_material import apply_stylesheet
import graphic_view
import midiTex
import program_table
import help
kill_flag = []
default_volume = 72
note_mapping = {
'a': 60 - 3,
'b': 60 - 1,
'c': 60,
'd': 60 + 2,
'e': 60 + 4,
'f': 60 + 5,
'g': 60 + 7,
}
color_set = [[0, 188, 212], [139, 195, 74], [255, 64, 129], [255, 196, 1], [255, 61, 0], [0, 150, 136],
[0, 229, 255], [224, 64, 251], [0, 150, 136], [255, 23, 68]]
tunes = ['c', 'd', 'e', 'f', 'g', 'a', 'b']
notes = ['c', '#c', 'd', '#d', 'e', 'f', '#f', 'g', '#g', 'a', '#a', 'b']
notes_h = {
'C': 0,
'C#': 1,
'D': 2,
'D#': 3,
'E': 4,
'F': 5,
'F#': 6,
'G': 7,
'G#': 8,
'A': 9,
'A#': 10,
'B': 11
}
pitch2note = {
2: 2, 3: 4, 4: 5, 5: 7, 6: 9, 7: 11, 8: 12, 9: 14, 11: 17, 13: 21
}
program_names = {
0: 'Acoustic Grand Piano',
1: 'Bright Acoustic Piano',
2: 'Electric Grand Piano',
3: 'Honky-tonk Piano',
4: 'Electric Piano 1(Rhodes Piano)',
5: 'Electric Piano 2(Chorused Piano)',
6: 'Harpsichord',
7: 'Clavi',
8: 'Celesta',
9: 'Glockenspiel',
10: 'Music box',
11: 'Vibraphone',
12: 'Marimba',
13: 'Xylophone',
14: 'Tubular Bells',
15: 'Dulcimer',
16: 'Drawbar Organ(Hammond Organ)',
17: 'Percussive Organ',
18: 'Rock Organ',
19: 'Church Organ',
20: 'Reed Organ',
21: 'Accordion',
22: 'Harmonica',
23: 'Tango Accordion',
24: 'Acoustic Guitar(nylon)',
25: 'Acoustic Guitar(steel)',
26: 'Electric Guitar(jazz)',
27: 'Electric Guitar(clean)',
28: 'Electric Guitar(muted)',
29: 'Overdriven Guitar',
30: 'Distortion Guitar',
31: 'Guitar harmonics',
32: 'Acoustic Bass',
33: 'Electric Bass(finger)',
34: 'Electric Bass(pick)',
35: 'Fretless Bass',
36: 'Slap Bass 1',
37: 'Slap Bass 2',
38: 'Synth Bass 1',
39: 'Synth Bass 2',
40: 'Violin',
41: 'Viola',
42: 'Cello',
43: 'Contrabass',
44: 'Tremolo Strings',
45: 'Pizzicato Strings',
46: 'Orchestral Harp',
47: 'Timpani',
48: 'String Ensemble 1',
49: 'String Ensemble 2',
50: 'Synth Strings 1',
51: 'Synth Strings 2',
52: 'Choir Aahs',
53: 'Voice Oohs',
54: 'Synth Voice',
55: 'Orchestra Hit',
56: 'Trumpet',
57: 'Trombone',
58: 'Tuba',
59: 'Muted Trumpet',
60: 'French Horn',
61: 'Brass Section',
62: 'Synth Brass 1',
63: 'Synth Brass 2',
64: 'Soprano Sax',
65: 'Alto Sax',
66: 'Tenor Sax',
67: 'Baritone Sax',
68: 'Oboe',
69: 'English Horn',
70: 'Bassoon',
71: 'Clarinet',
72: 'Piccolo',
73: 'Flute',
74: 'Recorder',
75: 'Pan Flute',
76: 'Blown Bottle',
77: 'Shakuhachi',
78: 'Whistle',
79: 'Ocarina',
80: 'Lead 1(square)',
81: 'Lead 2(sawtooth)',
82: 'Lead 3(calliope)',
83: 'Lead 4(chiff)',
84: 'Lead 5(charang)',
85: 'Lead 6(voice)',
86: 'Lead 7(fifths)',
87: 'Lead 8(bass + lead)',
88: 'Pad 1(new age)',
89: 'Pad 2(warm)',
90: 'Pad 3(polysynth)',
91: 'Pad 4(choir)',
92: 'Pad 5(bowed)',
93: 'Pad 6(metallic)',
94: 'Pad 7(halo)',
95: 'Pad 8(sweep)',
96: 'FX 1(rain)',
97: 'FX 2(soundtrack)',
98: 'FX 3(crystal)',
99: 'FX 4(atmosphere)',
100: 'FX 5(light)',
101: 'FX 6(goblins)',
102: 'FX 7(echoes)',
103: 'FX 8(sci-fi)',
104: 'Sitar',
105: 'Banjo',
106: 'Shamisen',
107: 'Koto',
108: 'Kalimba',
109: 'Bagpipe',
110: 'Fiddle',
111: 'Shanai',
112: 'Tinkle Bell',
113: 'Agogo',
114: 'Steel Drums',
115: 'Woodblock',
116: 'Taiko Drum',
117: 'Melodic Tom',
118: 'Synth Drum',
119: 'Reverse Cymbal',
120: 'Guitar Fret Noise',
121: 'Breath Noise',
122: 'Seashore',
123: 'Bird Tweet',
124: 'Telephone',
125: 'Helicopter',
126: 'Applause',
127: 'Gunshot'
}
program_chinese = ["大钢琴(声学钢琴)",
"明亮的钢琴",
"电钢琴",
"酒吧钢琴",
"柔和的电钢琴",
"加合唱效果的电钢琴",
"羽管键琴(拨弦古钢琴)",
"科拉维科特琴(击弦古钢琴)",
"钢片琴",
"钟琴",
"八音盒",
"颤音琴",
"马林巴琴",
"木琴",
"管钟",
"大扬琴",
"击杆风琴",
"打击式风琴",
"摇滚风琴",
"教堂风琴",
"簧管风琴",
"手风琴",
"口琴",
"探戈手风琴",
"尼龙弦吉他",
"钢弦吉他",
"爵士电吉他",
"清音电吉他",
"闷音电吉他",
"加驱动效果的电吉他",
"加失真效果的电吉他",
"吉他和音",
"大贝司(声学贝司)",
"电贝司(指弹)",
"电贝司(拨片)",
"无品贝司",
"掌击贝司",
"掌击贝司",
"电子合成贝司",
"电子合成贝司",
"小提琴",
"中提琴",
"大提琴",
"低音大提琴",
"弦乐群颤音",
"弦乐群拨弦",
"竖琴",
"定音鼓",
"弦乐合奏",
"弦乐合奏",
"合成弦乐合奏",
"合成弦乐合奏",
"人声合唱'啊'",
"人声'嘟'",
"合成人声",
"管弦乐敲击齐奏",
"小号",
"长号",
"大号",
"加弱音器小号",
"法国号(圆号)",
"铜管组(铜管乐器合奏音色)",
"合成铜管音色",
"合成铜管音色",
"高音萨克斯风",
"次中音萨克斯风",
"中音萨克斯风",
"低音萨克斯风",
"双簧管",
"英国管",
"巴松(大管)",
"单簧管(黑管)",
"短笛",
"长笛",
"竖笛",
"排箫",
"芦笛",
"日本尺八",
"口哨声",
"奥卡雷那",
"合成主音(方波)",
"合成主音(锯齿波)",
"合成主音",
"合成主音",
"合成主音",
"合成主音(人声)",
"合成主音(平行五度)",
"合成主音(贝司加主音)",
"合成音色(新世纪)",
"合成音色(温暖)",
"合成音色",
"合成音色(合唱)",
"合成音色",
"合成音色(金属声)",
"合成音色(光环)",
"合成音色",
"合成效果雨声",
"合成效果音轨",
"合成效果水晶",
"合成效果大气",
"合成效果明亮",
"合成效果鬼怪",
"合成效果回声",
"合成效果科幻",
"西塔尔(印度)",
"班卓琴(美洲)",
"三昧线(日本)",
"十三弦筝(日本)",
"卡林巴",
"风笛",
"民族提琴",
"山奈",
"叮当铃",
"Agogo",
"钢鼓",
"木鱼",
"太鼓",
"通通鼓",
"合成鼓",
"铜钹",
"吉他换把杂音",
"呼吸声",
"海浪声",
"鸟鸣",
"电话铃",
"直升机",
"鼓掌声",
"枪声"]
program_class = ['钢琴类', '色彩打击乐器类', '风琴类', '吉他类', '贝斯类', '弦乐类', '合奏/合唱类', '铜管类', '簧管类',
'笛类', '合成主音类', '合成音色类', '合成效果类', '民间乐器类', '打击乐器类', '声音效果类']
def extract_ch(text):
pattern = r'ch\((.*?)\)'
matches = re.finditer(pattern, text)
result = []
for match in matches:
content = match.group(1)
start = match.start()
end = match.end()
result.append((content, start, end))
return result
def extract_ar(text):
pattern = r'ar(\d+)\((.*?)\)'
matches = re.finditer(pattern, text)
result = []
for match in matches:
content = match.group(2)
start = match.start()
end = match.end()
result.append((content, start, end, match.group(1)))
return result
# def is_list_of_int(arr):
# if type(arr) == list:
# return all(isinstance(i, int) for i in arr)
# else:
# return False
def fit_in_range(num, start, end):
# Check if the number is within the range and fit it by mod
if num < start or num > end:
# If not, add or subtract 12 until it is
while num < start or num > end:
if num < start:
num += 12
elif num > end:
num -= 12
return num
# def add_node(to_track, note, velocity=72, offset=0, times=480):
# """添加单个音符,note以60形式表示"""
# to_track.append(Message('note_on', note=note, velocity=velocity, time=offset))
# to_track.append(Message('note_off', note=note, velocity=velocity, time=offset + times))
# return
# def get_chord_set(chord, transposition, descend=False):
# """解析字符串形式表示的和弦为note数组"""
# chord_set = []
# chord_subset = []
# duration_set = []
# velocity_set = []
# duration = ''
# duration_next = None
# velocity = ''
# velocity_next = None
# chord_mode = False
# duration_mode = False
# velocity_mode = False
# unison = False
# new_mode = False
# delay_mode = False
# for c in chord:
# if c == '$':
# new_mode = True
# continue
# if c == '*':
# if len(chord_set) > 0:
# if type(chord_set[-1]) == int:
# chord_set.append(chord_set[-1])
# else:
# chord_set.append(chord_set[-1][-1])
# else:
# chord_set.append(55 + transposition)
# duration_set.append(duration_next)
# duration_next = None
# velocity_set.append(0)
# continue
# if c == '-':
# unison = True
# continue
# if c == '{':
# chord_mode = True
# chord_subset = []
# continue
# if c == '}':
# if len(chord_set) > 0 and not new_mode:
# if type(chord_set[-1]) == int:
# refer = chord_set[-1]
# else:
# refer = chord_set[-1][-1]
# if unison:
# fix = fit_in_range(chord_subset[-1], refer - 6, refer + 6) - chord_subset[-1]
# unison = False
# elif descend:
# fix = fit_in_range(chord_subset[-1], refer - 12, refer - 1) - chord_subset[-1]
# else:
# fix = fit_in_range(chord_subset[-1], refer + 1, refer + 12) - chord_subset[-1]
# chord_subset = [i + fix for i in chord_subset]
# if new_mode:
# new_mode = False
# chord_set.append(chord_subset)
# duration_set.append(duration_next)
# velocity_set.append(velocity_next)
# duration_next = None
# velocity_next = None
# chord_mode = False
# continue
# if c == '[':
# duration_mode = True
# duration = ''
# continue
# if c == ']':
# duration_mode = False
# duration_next = float(duration)
# continue
# if duration_mode:
# duration = duration + c
# continue
# if c == '(':
# velocity_mode = True
# velocity = ''
# continue
# if c == ')':
# velocity_mode = False
# velocity_next = int(velocity)
# continue
# if velocity_mode:
# velocity = velocity + c
# continue
# if chord_mode:
# if 96 < ord(c) < 104:
# chord_subset.append(note_mapping[c] + transposition)
# elif c == '#':
# chord_subset[-1] = chord_subset[-1] + 1
# elif c == '^':
# chord_subset[-1] = chord_subset[-1] - 1
# if c != ' ' and len(chord_subset) > 1:
# chord_subset[-1] = fit_in_range(chord_subset[-1], chord_subset[-2] + 1, chord_subset[-2] + 12)
# continue
# if 96 < ord(c) < 104:
# chord_set.append(note_mapping[c] + transposition)
# duration_set.append(duration_next)
# velocity_set.append(velocity_next)
# duration_next = None
# velocity_next = None
# elif c == '#':
# chord_set[-1] = chord_set[-1] + 1
# elif c == '^':
# chord_set[-1] = chord_set[-1] - 1
# elif c == '<':
# chord_set.append(['delay_on'])
# duration_set.append(0)
# velocity_set.append(0)
# delay_mode = True
# elif c == '>':
# chord_set.append(['delay_off'])
# duration_set.append(0)
# velocity_set.append(0)
# delay_mode = True
#
# if c != ' ' and len(chord_set) > 1 and not new_mode and not delay_mode:
# if type(chord_set[-2]) == int:
# refer = chord_set[-2]
# else:
# refer = chord_set[-2][-1]
# if unison:
# chord_set[-1] = fit_in_range(chord_set[-1], refer - 6, refer + 6)
# unison = False
# elif descend:
# chord_set[-1] = fit_in_range(chord_set[-1], refer - 12, refer - 1)
# else:
# chord_set[-1] = fit_in_range(chord_set[-1], refer + 1, refer + 12)
#
# if delay_mode:
# if new_mode or len(chord_set) < 2:
# chord_set[-1].append(55 + transposition)
# else:
# if type(chord_set[-2]) == int:
# refer = chord_set[-2]
# else:
# refer = chord_set[-2][-1]
# chord_set[-1].append(refer)
# delay_mode = False
#
# if new_mode:
# new_mode = False
# return [chord_set, duration_set, velocity_set]
# def add_chord_set(to_track, chord_set, velocity=72, offset=0, times=480, arpeggio_time=0, to_channel=0):
# """添加用note数组表示的和弦组,支持琶音模式"""
# to_track.append(Message('note_on', note=chord_set[0], velocity=velocity, time=offset, channel=to_channel))
# for note in chord_set[1:]:
# to_track.append(Message('note_on', note=note, velocity=velocity, time=arpeggio_time, channel=to_channel))
# for note in chord_set:
# if to_track[-1].type == 'note_off':
# to_track.append(Message('note_off', note=note, velocity=velocity, time=0, channel=to_channel))
# else:
# to_track.append(
# Message('note_off', note=note, velocity=velocity, time=times - (len(chord_set) - 1) * arpeggio_time,
# channel=to_channel))
# return
# def add_chord(to_track, chord, velocity=96, offset=0, time=480, arpeggio=0, transposition=0):
# """添加和弦"""
# chord_set = get_chord_set(chord, transposition)
# add_chord_set(to_track, chord_set, velocity, offset, time, arpeggio)
#
# def add_seq(to_track, chord, velocity=72, offset=0, times=120, transposition=0, descend=False, last_note=None,
# arpeggio_time=0, to_channel=0):
# """添加单一升降序列,支持和弦及琶音"""
# if chord == '':
# return
# seq_set = get_chord_set(chord, transposition, descend)
# chord_set = seq_set[0]
# duration_set = seq_set[1]
# velocity_set = seq_set[2]
# times_set = []
# for i in range(0, len(duration_set)):
# if duration_set[i] is None:
# times_set.append(times)
# else:
# times_set.append(int(duration_set[i] * 480))
# if velocity_set[i] is None:
# velocity_set[i] = velocity
#
# # 通过移动处理和弦转折情形的衔接
# if last_note is not None and chord[0] != '$':
# # 计算修正值
# n = 0
# while type(chord_set[n]) != int and not is_list_of_int(chord_set[n]):
# n += 1
# if type(chord_set[n]) == int:
# if descend:
# fix = fit_in_range(chord_set[n], last_note - 12, last_note - 1) - chord_set[n]
# else:
# fix = fit_in_range(chord_set[n], last_note + 1, last_note + 12) - chord_set[n]
# else:
# if descend:
# fix = fit_in_range(chord_set[n][-1], last_note - 12, last_note - 1) - chord_set[n][-1]
# else:
# fix = fit_in_range(chord_set[n][-1], last_note + 1, last_note + 12) - chord_set[n][-1]
# # 进行修正
# for i, chord in enumerate(chord_set):
# if type(chord_set[i]) == int:
# chord_set[i] = chord + fix
# elif is_list_of_int(chord_set[i]):
# chord_set[i] = [j + fix for j in chord_set[i]]
#
# if type(chord_set[0]) == int:
# to_track.append(
# Message('note_on', note=chord_set[0], velocity=velocity_set[0], time=offset, channel=to_channel))
# to_track.append(
# Message('note_off', note=chord_set[0], velocity=velocity_set[0], time=times_set[0], channel=to_channel))
# elif is_list_of_int(chord_set[0]):
# add_chord_set(to_track, chord_set[0], velocity_set[0], offset, times_set[0], arpeggio_time, to_channel)
# elif chord_set[0][0] == 'delay_on':
# to_track.append(Message('control_change', channel=to_channel, control=64, value=127))
# elif chord_set[0][0] == 'delay_off':
# to_track.append(Message('control_change', channel=to_channel, control=64, value=0))
# for i in range(1, len(chord_set)):
# if type(chord_set[i]) == int:
# to_track.append(Message('note_on', note=chord_set[i], velocity=velocity_set[i], time=0, channel=to_channel))
# to_track.append(
# Message('note_off', note=chord_set[i], velocity=velocity_set[i], time=times_set[i], channel=to_channel))
# elif is_list_of_int(chord_set[i]):
# add_chord_set(to_track, chord_set[i], velocity_set[i], 0, times_set[i], arpeggio_time, to_channel)
# elif chord_set[i][0] == 'delay_on':
# to_track.append(Message('control_change', channel=to_channel, control=64, value=127))
# elif chord_set[i][0] == 'delay_off':
# to_track.append(Message('control_change', channel=to_channel, control=64, value=0))
# n = -1
# while type(chord_set[n]) != int and not is_list_of_int(chord_set[n]):
# n -= 1
# if type(chord_set[-1]) == int:
# return chord_set[-1]
# elif is_list_of_int(chord_set[-1]):
# return chord_set[-1][-1]
#
# def sharp(messages):
# if type(messages[0]) == str:
# messages[1] += 1
# else:
# for i, message in enumerate(messages):
# messages[i] = sharp(message)
# return messages
#
#
# def flat(messages):
# if type(messages[0]) == str:
# messages[1] -= 1
# else:
# for i, message in enumerate(messages):
# messages[i] = sharp(message)
# return messages
def make_list(messages):
message_list = []
for message in messages:
if len(message) == 0:
continue
if type(message[0]) == str:
message_list.append(message)
else:
for sub_message in make_list(message):
message_list.append(sub_message)
return message_list
def add_branch(branch_type, notes_name, refer, default_time, default_velocity, descend, unison, arp_time, default_transposition,
the_time):
message_box = []
depth = 0
chord_mode = False
branch_text = []
time_to_add = default_time
velocity_to_add = default_velocity
transposition = default_transposition
if branch_type == 0:
refer += transposition
default_refer = refer
duration_mode = False
velocity_mode = False
duration_text = ''
velocity_text = ''
for note_num, note in enumerate(notes_name):
if chord_mode:
if note == '}':
chord_mode = False
# 执行子串
message_box.append([])
branch_text.reverse()
result_save = []
for i, branch in enumerate(branch_text[1:]):
result = add_branch(1, branch, refer, time_to_add, velocity_to_add, descend, unison,
0, transposition, the_time + (len(branch_text) - 2 - i) * arp_time)
message_box[-1].append(result[0])
refer = result[1]
descend = True
unison = False
if i == 0:
result_save = result
refer = result_save[1]
descend = result_save[2]
the_time = result_save[3]
time_to_add = default_time
velocity_to_add = default_velocity
transposition = default_transposition
unison = False
else:
branch_text[-1] += note
if 96 < ord(note) < 104 or note == '!':
branch_text.append('')
continue
if depth > 0:
if note == ')':
depth -= 1
if depth == 0:
# 执行子串
message_box.append([])
result = []
for i, branch in enumerate(branch_text):
if i == 0:
result = add_branch(1, branch, refer, time_to_add, velocity_to_add, descend, unison,
arp_time, transposition, the_time)
message_box[-1].append(result[0])
else:
message_box[-1].append(
add_branch(2, branch, refer, time_to_add, velocity_to_add, descend, unison, arp_time,
transposition, the_time))
refer = result[1]
descend = result[2]
the_time = result[3]
time_to_add = default_time
velocity_to_add = default_velocity
transposition = default_transposition
unison = False
continue
if note == '|' and depth == 1:
branch_text.append('')
else:
branch_text[-1] += note
if note == '(':
depth += 1
continue
if velocity_mode:
if note.isdigit() or note == '.':
velocity_text += note
else:
velocity_mode = False
velocity_to_add = int(velocity_text)
if 96 < ord(note) < 104:
if unison:
note_to_add = fit_in_range(note_mapping[note] + transposition, refer - 6, refer + 6)
unison = False
elif descend:
note_to_add = fit_in_range(note_mapping[note] + transposition, refer - 12, refer - 1)
else:
note_to_add = fit_in_range(note_mapping[note] + transposition, refer + 1, refer + 12)
message_box.append([])
message_box[-1].append(['note_on', note_to_add, velocity_to_add, the_time])
the_time += time_to_add
time_to_add = default_time
velocity_to_add = default_velocity
transposition = default_transposition
message_box[-1].append(['note_off', note_to_add, velocity_to_add, the_time])
refer = note_to_add
continue
if note == ';':
descend = not descend
continue
if note == '#':
transposition += 1
continue
if note == '^':
transposition -= 1
continue
if note == '$':
refer = default_refer
unison = True
continue
if note == '!':
if unison:
note_to_add = fit_in_range(refer, refer - 6, refer + 6)
unison = False
elif descend:
note_to_add = fit_in_range(refer, refer - 12, refer - 1)
else:
note_to_add = fit_in_range(refer, refer + 1, refer + 12)
message_box.append([])
message_box[-1].append(['note_on', note_to_add, 0, 0])
message_box[-1].append(['note_off', note_to_add, 0, 0])
refer = note_to_add
continue
if note == '*':
message_box.append([])
message_box[-1].append(['note_on', refer, 0, the_time])
the_time += time_to_add
time_to_add = default_time
message_box[-1].append(['note_off', refer, 0, the_time])
continue
if note == '-':
unison = True
continue
if note == '[':
duration_mode = True
duration_text = ''
continue
if note == ']':
duration_mode = False
time_to_add = int(480 * float(duration_text))
continue
if note == '{':
chord_mode = True
branch_text = ['']
continue
if duration_mode:
duration_text += note
continue
if note == '\\':
velocity_mode = True
velocity_text = ''
continue
if note == '<':
message_box.append(['control_change', 64, 127, the_time])
continue
if note == '>':
message_box.append(['control_change', 64, 0, the_time])
continue
if note == '(':
depth += 1
branch_text = ['']
continue
if branch_type == 0:
message_list = make_list(message_box)
sorted_list = sorted(message_list, key=lambda x: (x[3], ord(x[0][6]), x[2]))
print('s',sorted_list)
for i in range(len(sorted_list) - 1, 0, -1):
sorted_list[i][3] = sorted_list[i][3] - sorted_list[i - 1][3]
print('p', sorted_list)
return sorted_list
elif branch_type == 1:
return message_box, refer, descend, the_time
elif branch_type == 2:
return message_box
def add_line(to_mid, track_num, notes_name, velocity=default_volume, offset=0, duration=0.5, transposition=0,
arpeggio_time=0):
to_track = to_mid.tracks[track_num]
default_time = int(480 * duration)
message_list = add_branch(0, notes_name, 62, default_time, velocity, False, True, arpeggio_time, transposition,
offset)
for message in message_list:
if message[0] == 'control_change':
to_track.append(
Message('control_change', control=message[1], value=message[2], time=message[3], channel=track_num))
else:
to_track.append(
Message(message[0], note=message[1], velocity=message[2], time=message[3], channel=track_num))
# def add(to_mid, track_num, notes_name, velocity=default_volume, offset=0, duration=0.5, transposition=0,
# arpeggio_time=0):
# """添加通用音乐记号,包括和弦和琶音"""
# to_track = to_mid.tracks[track_num]
# times = int(480 * duration)
# chords = notes_name.split(';')
# last_note = add_seq(to_track, chords[0], velocity, offset, times, transposition, arpeggio_time=arpeggio_time,
# to_channel=track_num)
# if chords[0] == '':
# next_offset = offset
# else:
# next_offset = 0
# flag = 1
# for i, chord in enumerate(chords[1:]):
# if flag == 0:
# last_note = add_seq(to_track, chord, velocity, next_offset, times, transposition, last_note=last_note,
# arpeggio_time=arpeggio_time, to_channel=track_num)
# flag = 1
# else:
# last_note = add_seq(to_track, chord, velocity, next_offset, times, transposition, True, last_note=last_note,
# arpeggio_time=arpeggio_time, to_channel=track_num)
# flag = 0
# if chord != '':
# next_offset = 0
def set_track(to_midi, program=0, name=None, bmp=120, offset=0):
"""向文件中添加一个音轨"""
tempo = 60000000 // bmp
if name is None or name == '':
name = program_names[program]
new_track = mido.MidiTrack()
new_track.append(mido.MetaMessage('track_name', name=name, time=0))
new_track.append(mido.MetaMessage('set_tempo', tempo=tempo, time=0))
new_track.append(Message('program_change', channel=len(to_midi.tracks), program=program, time=offset))
to_midi.tracks.append(new_track)
def get_choir(choir_string):
"""解析和弦术语"""
add_point = choir_string.find("add")
on_point = choir_string.find("on")
if add_point == -1 and on_point == -1:
add_note = None
on_note = None
name = choir_string
else:
if add_point == -1:
name = choir_string[:on_point]
add_note = None
on_note = choir_string[on_point + 2:]
elif on_point == -1:
name = choir_string[:add_point]
on_note = None
add_note = choir_string[add_point + 3:]
else:
if add_point < on_point:
name = choir_string[:add_point]
add_note = choir_string[add_point + 3:on_point]
on_note = choir_string[on_point + 2:]
else:
name = choir_string[:on_point]
add_note = choir_string[add_point + 3:]
on_note = choir_string[on_point + 2:add_point]
name = name.replace(' ', '')
if on_note is not None:
on_note = on_note.replace(' ', '')
if add_note is not None:
add_note = add_note.replace(' ', '').split('\\')
if len(name) == 1:
root = notes_h[name]
choir_type = 'maj'
elif name[-1] == '#':
root = notes_h[name[0]] + 1
choir_type = 'maj'
elif name[-1] == 'b':
root = notes_h[name[0]] - 1
choir_type = 'maj'
else:
if name[1] == '#':
root = notes_h[name[0]] + 1
elif name[1] == 'b':
root = notes_h[name[0]] - 1
else:
root = notes_h[name[0]]
if name[-3:] == 'min' or name[-3:] == 'dim' or name[-3:] == 'aug' or name[-3:] == 'mM7':
choir_type = name[-3:]
elif name[-4:] == 'sus2' or name[-4:] == 'sus4' or name[-4:] == 'maj7' or name[-4:] == 'min7':
choir_type = name[-4:]
elif name[-1] == '7' or name[-1] == '8':
choir_type = name[-1]
else:
choir_type = None
if choir_type == 'maj':
choir_set = [root, root + 4, root + 7]
elif choir_type == 'min':
choir_set = [root, root + 3, root + 7]
elif choir_type == 'dim':
choir_set = [root, root + 3, root + 6]
elif choir_type == 'aug':
choir_set = [root, root + 4, root + 8]
elif choir_type == 'sus2':
choir_set = [root, root + 2, root + 7]
elif choir_type == 'sus4':
choir_set = [root, root + 5, root + 7]
elif choir_type == '7':
choir_set = [root, root + 4, root + 7, root + 10]
elif choir_type == 'maj7':
choir_set = [root, root + 4, root + 7, root + 11]
elif choir_type == 'min7':
choir_set = [root, root + 3, root + 7, root + 10]
elif choir_type == 'mM7':
choir_set = [root, root + 3, root + 7, root + 11]
elif choir_type == '8':
choir_set = [root, root + 12]
else:
choir_set = [root]
if add_note is not None:
for note in add_note:
buff = 0
while note[0] == '#':
buff = buff + 1
note = note[1:]
while note[0] == 'b':
buff = buff - 1
note = note[1:]
choir_set.append(pitch2note[int(note)] + buff + root)
choir_set.sort()
note_string = ''
for note in choir_set:
note_string = note_string + notes[note % 12] + ' '
if on_note is not None:
start_point = note_string.find(on_note.lower())
return note_string[start_point:] + note_string[:start_point]
else:
return note_string
def choir(choir_string, duration=4):
"""使用和弦术语生成对应的midiTex,支持序列输入,用;分隔"""
choir_string_set = choir_string.split(';')
note_string = ''
for string in choir_string_set:
note_string = note_string + '[' + str(duration) + ']' + '{' + get_choir(string) + '}-'
note_string = note_string
return note_string
def arpeggio(root_string, note_num=8, total_duration=4.0):
"""使用和弦术语生成对应的琶音伴奏midiTex,支持序列输入,用;分隔"""
arpeggio_string = ''
if note_num % 2 == 0:
duration = str(total_duration / note_num)
else:
duration = str(total_duration / note_num)
duration = '[' + duration + ']'
roots = root_string.split(';')
for root in roots:
note_set = get_choir(root)[:-1].split(' ')
arpeggio_string = arpeggio_string + '$' + duration + note_set[0] + duration + note_set[2]
if note_num % 2 == 0:
i = 0
for i in range(3, note_num // 2 + 2):
arpeggio_string = arpeggio_string + duration + note_set[i % len(note_set)]
last_i = i
arpeggio_string = arpeggio_string + ';'
for i in range(note_num // 2 + 2, note_num + 1):
last_i = last_i - 1
arpeggio_string = arpeggio_string + duration + note_set[last_i % len(note_set)]
else:
i = 3