-
Notifications
You must be signed in to change notification settings - Fork 0
/
music_annotator.py
1481 lines (1251 loc) · 57.2 KB
/
music_annotator.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 sys
import codecs
import re
from PyQt5.QtWidgets import (QApplication, QWidget, QMainWindow, QAction, QComboBox, QVBoxLayout, QHBoxLayout, QLabel,
QLineEdit, QTextBrowser, QSpacerItem, QSizePolicy, QPushButton, QFileDialog, QMenu, QMessageBox,
QToolButton, QSplitter, QCompleter, QDialog, QDialogButtonBox, QGridLayout, QCheckBox, QColorDialog)
from PyQt5.QtGui import QIcon,QColor,QFont,QCursor,QPixmap,QClipboard
from PyQt5.QtCore import QSize, Qt, QTimer, QEvent
from PersistenceUtils import *
FLAT = '\u266D'
SHARP = '\u266F'
DRAWBEND1 = '\u0438'
DRAWBEND2 = '\u0439'
DRAWBEND3 = '\u043A'
BLOWBEND1 = '\u043C'
BLOWBEND2 = '\u043D'
OVERBLOW = '\u043B'
OVERDRAW = '\u0440'
DRAW = '\u043E'
BLOW = '\u043F'
DEFAULT_PLACEMENT = 'below' # 'below' or 'above'
PREFER_FLAT = False
PREFER_SHARP = False
last_pressed = False
def warndlg(title,mssg):
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowIcon(QIcon("warning_orange.png"))
msg.setText(mssg)
#msg.setInformativeText("This is additional information")
msg.setWindowTitle(title)
#msg.setDetailedText(details)
msg.setStandardButtons(QMessageBox.Ok) # msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
retval = msg.exec_()
#print("value of pressed warning dialog button:", retval)
return retval
def get_label(string, label):
pattern = fr'.*<{label}>\s*(.+)\s*</{label}>.*'
m = re.search(pattern, string)
if m != None:
value = m.group(1)
return value
else:
return ''
def replace_text(string, text):
pattern = r'(.*<text>\s*)(.+)(\s*</text>.*)'
new = fr'\1 {text}\3'
output = re.sub(pattern, new, string)
output = output.replace('<text> ','<text>')
output = output.replace('<text>','<text font-family="KobyMusic">')
return output
def note_semitones(full_note):
# full_note is NOTE + OCTAVE + <optional: #/b>
# example: 'C4', 'B5b', 'F6#'
steps_dct = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
alter_dct = {'': 0, '#': 1, 'b': -1}
step = full_note[0]
octave = int(full_note[1])
if len(full_note) > 2:
alter = full_note[2]
else:
alter = ''
alter = alter_dct[alter]
semitones = octave*12 + steps_dct[step] + alter
return semitones
def semitones_to_note(semitones):
# full_note is STEP + OCTAVE + <optional: #/b>
# example: 'C4', 'B5b', 'F6#'
steps_dct = {0: 'C', 2: 'D', 4: 'E', 5: 'F', 7: 'G', 9: 'A', 11: 'B'}
alter_dct = {0: '', 1: '#', -1: 'b'}
clean_notes = list(steps_dct.keys())
octave = semitones // 12
inside_octave = semitones - octave*12
if inside_octave in clean_notes:
alter = 0
else:
alter = 1
inside_octave -= 1
full_note = steps_dct[inside_octave] + str(octave) + alter_dct[alter]
return full_note
def soa_to_semitones(soa):
# soa = (step, octave, alter)
# example: 'B5b' = ('B', '5', '-1')
steps_dct = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
step = soa[0]
octave = int(soa[1])
if soa[2]:
alter = int(soa[2])
else:
alter = 0
semitones = octave*12 + steps_dct[step] + alter
return semitones
def semitones_to_soa(semitones):
# full_note is STEP + OCTAVE + <optional: #/b>
# example: 'C4', 'B5b', 'F6#'
steps_dct = {0: 'C', 2: 'D', 4: 'E', 5: 'F', 7: 'G', 9: 'A', 11: 'B'}
alter_dct = {0: '', 1: '1', -1: '-1'}
clean_notes = list(steps_dct.keys())
octave = semitones // 12
inside_octave = semitones - octave*12
if inside_octave in clean_notes:
alter = 0
else:
alter = 1
inside_octave -= 1
step = steps_dct[inside_octave]
octave = str(octave)
alter = alter_dct[alter]
return (step, octave, alter)
def note_to_text_ChromaticHarmonica16(step, octave, alter, returnAllOptions=False):
global last_pressed
extra = {'': '', '1': '#', '-1': 'b'}
note = step + octave + extra[alter]
dct = {'C3': 'L1', 'D3': '(L1)','E3':'L2', 'F3': ['(L2)','#L2'], 'G3': 'L3', 'A3': '(L3)', 'B3': '(L4)',
'C4': '1', 'D4': '(1)', 'E4':'2', 'F4': ['(2)','#2'], 'G4': '3', 'A4': '(3)', 'B4': '(4)',
'C5': ['4','5','(#4)'], 'D5': '(5)', 'E5':'6', 'F5': ['(6)','#6'], 'G5': '7', 'A5': '(7)', 'B5': '(8)',
'C6': ['8','9','(#8)'], 'D6': '(9)', 'E6':'10', 'F6': ['(10)','#10'], 'G6': '11', 'A6': '(11)', 'B6': '(12)',
'C7': ['12','(#12)'],
'D3b': '#L1', 'E3b': '(#L1)', 'F3b': 'L2', 'G3b': '(#L2)', 'A3b': '#L3', 'B3b': '(#L3)',
'C3#': '#L1', 'D3#': '(#L1)', 'E3#': ['(L2)', '#L2'], 'F3#': '(#L2)', 'G3#': '#L3', 'A3#': '(#L3)',
'D4b': '#1', 'E4b': '(#1)', 'F4b': '2', 'G4b': '(#2)', 'A4b': '#3', 'B4b': '(#3)',
'C4#': '#1', 'D4#': '(#1)', 'E4#': ['(2)','#2'], 'F4#': '(#2)', 'G4#': '#3', 'A4#': '(#3)',
'D5b': ['#4','#5'], 'E5b': '(#5)', 'F5b': '6', 'G5b': '(#6)', 'A5b': '#7', 'B5b': '(#7)',
'C5#': ['#4','#5'], 'D5#': '(#5)', 'E5#': ['(6)','#6'], 'F5#': '(#6)', 'G5#': '#7', 'A5#': '(#7)',
'D6b': ['#8','#9'], 'E6b': '(#9)', 'F6b': '10', 'G6b': '(#10)', 'A6b': '#11', 'B6b': '(#11)',
'C6#': ['#8','#9'], 'D6#': '(#9)', 'E6#': ['(10)','#10'], 'F6#': '(#10)', 'G6#': '#11', 'A6#': '(#11)',
}
name = dct.get(note, '?')
if returnAllOptions == False:
if type(name) == list:
if last_pressed: # prefer pressed '#'
for opt in name:
if '#' in opt:
name = opt
break
else: # prefer not pressed '#'
for opt in name:
if '#' not in opt:
name = opt
break
if type(name) == list:
name = name[-1]
if type(name) == list:
name = '\n'.join(name)
else: # returnAllOptions == True
if type(name) == list:
name = '\n'.join(name)
if '#' in name:
last_pressed = True
else:
last_pressed = False
# if name.startswith('('):
# name += '\n\u2193'
# else:
# name += '\n\u2191'
return name
def note_to_text_ChromaticHarmonica12(step, octave, alter, returnAllOptions=False):
global last_pressed
extra = {'': '', '1': '#', '-1': 'b'}
note = step + octave + extra[alter]
dct = {'C4': '1', 'D4': '(1)', 'E4':'2', 'F4': ['(2)','#2'], 'G4': '3', 'A4': '(3)', 'B4': '(4)',
'C5': ['4','5','(#4)'], 'D5': '(5)', 'E5':'6', 'F5': ['(6)','#6'], 'G5': '7', 'A5': '(7)', 'B5': '(8)',
'C6': ['8','9','(#8)'], 'D6': '(9)', 'E6':'10', 'F6': ['(10)','#10'], 'G6': '11', 'A6': '(11)', 'B6': '(12)',
'C7': ['12','(#12)'],
'D4b': '#1', 'E4b': '(#1)', 'F4b': '2', 'G4b': '(#2)', 'A4b': '#3', 'B4b': '(#3)',
'C4#': '#1', 'D4#': '(#1)', 'E4#': ['(2)','#2'], 'F4#': '(#2)', 'G4#': '#3', 'A4#': '(#3)',
'D5b': ['#4','#5'], 'E5b': '(#5)', 'F5b': '6', 'G5b': '(#6)', 'A5b': '#7', 'B5b': '(#7)',
'C5#': ['#4','#5'], 'D5#': '(#5)', 'E5#': ['(6)','#6'], 'F5#': '(#6)', 'G5#': '#7', 'A5#': '(#7)',
'D6b': ['#8','#9'], 'E6b': '(#9)', 'F6b': '10', 'G6b': '(#10)', 'A6b': '#11', 'B6b': '(#11)',
'C6#': ['#8','#9'], 'D6#': '(#9)', 'E6#': ['(10)','#10'], 'F6#': '(#10)', 'G6#': '#11', 'A6#': '(#11)',
}
name = dct.get(note, '?')
if returnAllOptions == False:
if type(name) == list:
if last_pressed: # prefer pressed '#'
for opt in name:
if '#' in opt:
name = opt
break
else: # prefer not pressed '#'
for opt in name:
if '#' not in opt:
name = opt
break
if type(name) == list:
name = name[-1]
if type(name) == list:
name = '\n'.join(name)
else: # returnAllOptions == True
if type(name) == list:
name = '\n'.join(name)
if '#' in name:
last_pressed = True
else:
last_pressed = False
# if name.startswith('('):
# name += '\n\u2193'
# else:
# name += '\n\u2191'
return name
def note_to_text_ChromaticHarmonica10(step, octave, alter, returnAllOptions=False):
global last_pressed
extra = {'': '', '1': '#', '-1': 'b'}
note = step + octave + extra[alter]
dct = {'C4': '1', 'D4': '(1)', 'E4':'2', 'F4': ['(2)','#2'], 'G4': '3', 'A4': '(3)', 'B4': '(4)',
'C5': ['4','5','(#4)'], 'D5': '(5)', 'E5':'6', 'F5': ['(6)','#6'], 'G5': '7', 'A5': '(7)', 'B5': '(8)',
'C6': ['8','(#8)'], 'D6': '(9)', 'E6':'9', 'F6': ['(10)','#9'], 'G6': '10',
'C7': ['12','(#12)'],
'D4b': '#1', 'E4b': '(#1)', 'F4b': '2', 'G4b': '(#2)', 'A4b': '#3', 'B4b': '(#3)',
'C4#': '#1', 'D4#': '(#1)', 'E4#': ['(2)','#2'], 'F4#': '(#2)', 'G4#': '#3', 'A4#': '(#3)',
'D5b': ['#4','#5'], 'E5b': '(#5)', 'F5b': '6', 'G5b': '(#6)', 'A5b': '#7', 'B5b': '(#7)',
'C5#': ['#4','#5'], 'D5#': '(#5)', 'E5#': ['(6)','#6'], 'F5#': '(#6)', 'G5#': '#7', 'A5#': '(#7)',
'D6b': ['#8'], 'E6b': '(#9)', 'F6b': '9', 'G6b': '(#10)', 'A6b': '#10',
'C6#': ['#8'], 'D6#': '(#9)', 'E6#': ['(10)','#9'], 'F6#': '(#10)', 'G6#': '#10',
}
name = dct.get(note, '?')
if returnAllOptions == False:
if type(name) == list:
if last_pressed: # prefer pressed '#'
for opt in name:
if '#' in opt:
name = opt
break
else: # prefer not pressed '#'
for opt in name:
if '#' not in opt:
name = opt
break
if type(name) == list:
name = name[-1]
if type(name) == list:
name = '\n'.join(name)
else: # returnAllOptions == True
if type(name) == list:
name = '\n'.join(name)
if '#' in name:
last_pressed = True
else:
last_pressed = False
# if name.startswith('('):
# name += '\n\u2193'
# else:
# name += '\n\u2191'
return name
def note_to_text_DiatonicHarmonicaC(step, octave, alter, octaveShift=0):
global last_pressed
extra = {'': '', '1': '#', '-1': 'b'}
note = step + octave + extra[alter]
note = step + str(int(octave)+octaveShift) + extra[alter]
dct = {"C4": "1", "D4": "(1)", "E4":"2", "F4": "(2'')", "G4": "3", "A4": "(3'')", "B4": "(3)", # "G4": ["(2)","3"]
"C5": "4", "D5": "(4)", "E5":"5", "F5": "(5)", "G5": "6", "A5": "(6)", "B5": "(7)",
"C6": "7", "D6": "(8)", "E6":"8", "F6": "(9)", "G6": "9", "A6": "(10)", "B6": "10'",
"C7": "10",
"D4b": "(1')", "E4b": "1@", "F4b": "2", "G4b": "(2')", "A4b": "(3''')", "B4b": "(3')", "C5b": "(4)",
"C4#": "(1')", "D4#": "1@", "E4#": "(2'')", "F4#": "(2')", "G4#": "(3''')", "A4#": "(3')", "B4#": "4",
"D5b": "(4')", "E5b": "4@", "F5b": "5", "G5b": "5@", "A5b": "(6')", "B5b": "6@",
"C5#": "(4')", "D5#": "4@", "E5#": "(5)", "F5#": "5@", "G5#": "(6')", "A5#": "6@",
"D6b": "(7@)", "E6b": "8'", "F6b": "8", "G6b": "9'", "A6b": "(9@)", "B6b": "10''",
"C6#": "(7@)", "D6#": "8'", "E6#": "(9)", "F6#": "9'", "G6#": "(9@)", "A6#": "10''",
"D7b": "(10@)",
"C7#": "(10@)",
}
name = dct.get(note, '?')
if type(name) == list:
#name = name[0]
name = '\n'.join(name)
if name.startswith('('): # draw notes
direction = 'draw'
else:
direction = 'blow'
bends = name.count("'")
overblowdraw = name.count("@")
name = name.replace("'", "")
name = name.replace("@", "")
name = name.replace("(", "")
name = name.replace(")", "")
draw_arrows = [DRAW, DRAWBEND1, DRAWBEND2, DRAWBEND3]
blow_arrows = [BLOW, BLOWBEND1, BLOWBEND2]
if overblowdraw > 0:
if direction == 'draw':
arrow = OVERDRAW
else:
arrow = OVERBLOW
else:
if direction == 'draw':
arrow = draw_arrows[bends]
else:
arrow = blow_arrows[bends]
name = arrow + '\n' + name
return name
def note_to_text_Recorder(step, octave, alter, octaveShift=0, addHebrew=False):
notes = ['לה', 'סי', 'דו', 'רה', 'מי', 'פה', 'סול']
octaveShift -= 1 # by default Recorder sheet is displayed at a lower octave, e.g. C5 on XML file is displayed as C4 on page
step_index = ord(step) - ord('A')
if addHebrew:
name = notes[step_index]
if alter == '1':
name = '#' + name
#name = name + '#'
elif alter == '-1':
name = 'b' + name
#name = name + 'b'
else:
name = ''
extra = {'': '', '1': '#', '-1': 'b'}
note = step + octave + extra[alter]
note = step + str(int(octave)+octaveShift) + extra[alter]
dct = {"C4": "11111133", "D4": "11111130", "E4": "11111100", "F4": "11111033", "G4": "11110000", "A4": "11100000", "B4": "11000000",
"C5": "10100000", "D5": "00100000", "E5": "21111100", "F5": "21111030", "G5": "21110000", "A5": "21100000", "B5": "21101100",
"C6": "21001100", "D6": "21011030",
"D4b": "11111131", "E4b": "11111110", "F4b": "11111100", "G4b": "11110130", "A4b": "11101130", "B4b": "11011000", "C5b": "11000000",
"C4#": "11111131", "D4#": "11111110", "E4#": "11111033", "F4#": "11110130", "G4#": "11101130", "A4#": "11011000", "B4#": "10100000",
"D5b": "01100000", "E5b": "00111130", "F5b": "21111100", "G5b": "21110100", "A5b": "21101000", "B5b": "21101130", "C6b": "21101100",
"C5#": "01100000", "D5#": "00111130", "E5#": "21111030", "F5#": "21110100", "G5#": "21101000", "A5#": "21101130", "B5#": "21001100",
"D6b": "21211233", "E6b": "20110130", "F6b": "?", "G6b": "?", "A6b": "?", "B6b": "?",
"C6#": "21211233", "D6#": "20110130", "E6#": "?", "F6#": "?", "G6#": "?", "A6#": "?",
}
holes_ids = dct.get(note, '?')
BG = '\u045D\n'
TABLE = [['\u0456', '\u0457', '\u0458', 'x'],
['\u0453', '\u0454', '\u0455', 'x'],
['\u0453', '\u0454', '\u0455', 'x'],
['\u0456', '\u0457', '\u0458', 'x'],
['\u0453', '\u0454', '\u0455', 'x'],
['\u0453', '\u0454', '\u0455', 'x'],
['\u0459', '\u045A', '\u045B', '\u045C'],
['\u0459', '\u045A', '\u045B', '\u045C']]
if holes_ids == '?':
return '?'
ids = list(map(int, list(holes_ids)))
output = ''
for k in range(len(ids)):
LUT = TABLE[k]
i = ids[k]
char = LUT[i]
output += char + '\n'
name = name + '\n' + BG + output
name = name.replace('b',FLAT)
name = name.replace('#',SHARP)
if name.startswith('\n'):
name = name[1:]
return name
def note_to_text_trumpet(step, octave, alter, addHebrew=False):
notes = ['לה', 'סי', 'דו', 'רה', 'מי', 'פה', 'סול']
step_index = ord(step) - ord('A')
if addHebrew:
name = notes[step_index]
if alter == '1':
name = '#' + name
#name = name + '#'
elif alter == '-1':
name = 'b' + name
#name = name + 'b'
elif alter == '':
pass
else:
warndlg('ERROR',f'note alteration of "{alter}" is not supported!')
else:
name = ''
extra = {'': '', '1': '#', '-1': 'b'}
note = step + octave + extra[alter]
dct = { 'F3#': 7, 'G3b': 7, 'G3': 5, 'G3#': 3, 'A3b': 3, 'A3': 6, 'A3#': 4, 'B3b': 4, 'B3': 2,
'C4': 0, 'C4#': 7, 'D4b': 7, 'D4': 5, 'D4#': 3, 'E4b': 3, 'E4': 6, 'F4': 4, 'F4#': 2, 'G4b': 2, 'G4': 0, 'G4#': 3, 'A4b': 3, 'A4': 6, 'A4#': 4, 'B4b': 4, 'B4': 2,
'C5': 0, 'C5#': 6, 'D5b': 6, 'D5': 4, 'D5#': 2, 'E5b': 2, 'E5': 0, 'F5': 4, 'F5#': 2, 'G5b': 2, 'G5': 0, 'G5#': 3, 'A5b': 3, 'A5': 6, 'A5#': 4, 'B5b': 4, 'B5': 2,
'C6': 0, 'C6#': 6, 'D6b': 6, 'D6': 4, 'D6#': 2, 'E6b': 2, 'E6': 0, 'F6': 4, 'F6#': 2, 'G6b': 2, 'G6': 0,
}
# signs3 = ['\n\u0470', '\n\u0473'] # on my font
# signs2 = ['\n\u046F', '\n\u0472']
# signs1 = ['\n\u046E', '\n\u0471']
signs3 = ['\n\u2462', '\n\u2778']
signs2 = ['\n\u2461', '\n\u2777']
signs1 = ['\n\u2460', '\n\u2776']
code = dct.get(note, None)
if code is not None:
bit2 = (code >> 2) & 0x1
bit1 = (code >> 1) & 0x1
bit0 = code & 0x1
chars = signs3[bit0] + signs2[bit1] + signs1[bit2]
name += chars
else:
name += '\n' + note + '\n?'
name = name.replace('b',FLAT)
name = name.replace('#',SHARP)
if name.startswith('\n'):
name = name[1:]
return name
def note_to_text_baritone(step, octave, alter, addHebrew=False):
notes = ['לה', 'סי', 'דו', 'רה', 'מי', 'פה', 'סול']
step_index = ord(step) - ord('A')
if addHebrew:
name = notes[step_index]
if alter == '1':
name = '#' + name
#name = name + '#'
elif alter == '-1':
name = 'b' + name
#name = name + 'b'
else:
name = ''
extra = {'': '', '1': '#', '-1': 'b'}
note = step + octave + extra[alter]
dct = {
'E2': 7, 'F2': 5, 'F2#': 3, 'G2b': 3, 'G2': 6, 'G2#': 4, 'A2b': 4, 'A2': 2, 'A2#': 0, 'B2b': 0, 'B2': 7,
'C3': 5, 'C3#': 3, 'D3b': 3, 'D3': 6, 'D3#': 4, 'E3b': 4, 'E3': 2, 'F3': 0, 'F3#': 3, 'G3b': 3, 'G3': 6, 'G3#': 4, 'A3b': 4, 'A3': 2, 'A3#': 0, 'B3b': 0, 'B3': 6,
'C4': 4, 'C4#': 2, 'D4b': 2, 'D4': 0, 'D4#': 4, 'E4b': 4, 'E4': 2, 'F4': 0, 'F4#': 3, 'G4b': 3, 'G4': 6, 'G4#': 4, 'A4b': 4, 'A4': 2, 'A4#': 0, 'B4b': 0,
}
# signs3 = ['\n\u0470', '\n\u0473'] # on my font
# signs2 = ['\n\u046F', '\n\u0472']
# signs1 = ['\n\u046E', '\n\u0471']
signs3 = ['\n\u2462', '\n\u2778']
signs2 = ['\n\u2461', '\n\u2777']
signs1 = ['\n\u2460', '\n\u2776']
code = dct.get(note, None)
if code is not None:
bit2 = (code >> 2) & 0x1
bit1 = (code >> 1) & 0x1
bit0 = code & 0x1
chars = signs3[bit0] + signs2[bit1] + signs1[bit2]
name += chars
else:
name += '\n' + note + '\n?'
name = name.replace('b',FLAT)
name = name.replace('#',SHARP)
if name.startswith('\n'):
name = name[1:]
return name
def change_notes_according_to_preference(step, alter):
if PREFER_FLAT == True and PREFER_SHARP == True:
warndlg('ERROR', 'Both PREFER_FLAT and PREFER_SHARP are True! Change at least one of them to False')
if PREFER_FLAT:
# prefer Bb instead of A#
if step == 'A' and alter == '1':
(step, alter) = ('B', '-1')
# prefer Ab instead of G#
if step == 'G' and alter == '1':
(step, alter) = ('A', '-1')
# prefer Gb instead of F#
if step == 'F' and alter == '1':
(step, alter) = ('G', '-1')
# prefer Eb instead of D#
if step == 'D' and alter == '1':
(step, alter) = ('E', '-1')
# prefer Db instead of C#
if step == 'C' and alter == '1':
(step, alter) = ('D', '-1')
if PREFER_SHARP:
# prefer C# instead of Db
if step == 'D' and alter == '-1':
(step, alter) = ('C', '1')
# prefer D# instead of Eb
if step == 'E' and alter == '-1':
(step, alter) = ('D', '1')
# prefer F# instead of Gb
if step == 'G' and alter == '-1':
(step, alter) = ('F', '1')
# prefer G# instead of Ab
if step == 'A' and alter == '-1':
(step, alter) = ('G', '1')
# prefer A# instead of Bb
if step == 'B' and alter == '-1':
(step, alter) = ('A', '1')
return (step, alter)
def note_to_text_tuba(step, octave, alter, addHebrew=False):
notes = ['לה', 'סי', 'דו', 'רה', 'מי', 'פה', 'סול']
step_index = ord(step) - ord('A')
if addHebrew:
name = notes[step_index]
if alter == '1':
name = '#' + name
#name = name + '#'
elif alter == '-1':
name = 'b' + name
#name = name + 'b'
else:
name = ''
extra = {'': '', '1': '#', '-1': 'b'}
note = step + octave + extra[alter]
dct = {
'E1': 7, 'F1': 5, 'F1#': 3, 'G1b': 3, 'G1': 6, 'G1#': 4, 'A1b': 4, 'A1': 2, 'A1#': 0, 'B1b': 0, 'B1': 7,
'C2': 5, 'C2#': 3, 'D2b': 3, 'D2': 6, 'D2#': 4, 'E2b': 4, 'E2': 2, 'F2': 0, 'F2#': 3, 'G2b': 3, 'G2': 6, 'G2#': 4, 'A2b': 4, 'A2': 2, 'A2#': 0, 'B2b': 0, 'B2': 6,
'C3': 4, 'C3#': 2, 'D3b': 2, 'D3': 0, 'D3#': 4, 'E3b': 4, 'E3': 2, 'F3': 0, 'F3#': 3, 'G3b': 3, 'G3': 6, 'G3#': 4, 'A3b': 4, 'A3': 2, 'A3#': 0, 'B3b': 0,
}
# signs3 = ['\n\u0470', '\n\u0473'] # on my font
# signs2 = ['\n\u046F', '\n\u0472']
# signs1 = ['\n\u046E', '\n\u0471']
signs3 = ['\n\u2462', '\n\u2778']
signs2 = ['\n\u2461', '\n\u2777']
signs1 = ['\n\u2460', '\n\u2776']
code = dct.get(note, None)
if code is not None:
bit2 = (code >> 2) & 0x1
bit1 = (code >> 1) & 0x1
bit0 = code & 0x1
chars = signs3[bit0] + signs2[bit1] + signs1[bit2]
name += chars
else:
name += '\n' + note + '\n?'
name = name.replace('b',FLAT)
name = name.replace('#',SHARP)
if name.startswith('\n'):
name = name[1:]
return name
def note_to_text_english(step, octave, alter):
notes = ['לה', 'סי', 'דו', 'רה', 'מי', 'פה', 'סול']
step_index = ord(step) - ord('A')
name = notes[step_index]
if alter == '1':
name = '#' + name
#name = name + '#'
elif alter == '-1':
name = 'b' + name
#name = name + 'b'
extra = {'': '', '1': '#', '-1': 'b'}
note = step + octave + extra[alter]
dct = {
}
#signs = ['\n\u26AA', '\n\u26AB']
signs3 = ['\n\u2462', '\n\u2778']
signs2 = ['\n\u2461', '\n\u2777']
signs1 = ['\n\u2460', '\n\u2776']
code = dct.get(note, None)
if code is not None:
bit2 = (code >> 2) & 0x1
bit1 = (code >> 1) & 0x1
bit0 = code & 0x1
chars = signs3[bit0] + signs2[bit1] + signs1[bit2]
name += chars
else:
name += '\n' + note
name = name.replace('b',FLAT)
name = name.replace('#',SHARP)
return name
def note_to_text_heb(step, octave, alter):
notes = ['לה', 'סי' ,'דו', 'רה', 'מי', 'פה', 'סול']
step_index = ord(step) - ord('A')
name = notes[step_index]
if alter == '1':
name = '#' + name
elif alter == '-1':
name = 'b' + name
name = name.replace('b', FLAT)
name = name.replace('#', SHARP)
return name
def test_semitones_conversion():
notes1 = ['C4', 'D4', 'E4', 'F4', 'G4', 'A4', 'B4']
notes2 = ['C4#', 'D4#', 'E4#', 'F4#', 'G4#', 'A4#', 'B4#']
notes3 = ['C4b', 'D4b', 'E4b', 'F4b', 'G4b', 'A4b', 'B4b']
notes = notes1 + notes2 + notes3
for note in notes:
semitones = note_semitones(note)
note_new = semitones_to_note(semitones)
if note_new != note:
print(f'ERROR: {note_new} != {note}, semitones = {semitones}')
def soa_shift(soa, semitonesOffset):
step, octave, alter = soa
if semitonesOffset != 0:
semitones = soa_to_semitones(soa)
semitones += semitonesOffset
soa = semitones_to_soa(semitones)
if semitonesOffset == 12:
soa = (step, str(int(octave)+1), alter)
if semitonesOffset == 24:
soa = (step, str(int(octave)+2), alter)
if semitonesOffset == -12:
soa = (step, str(int(octave)-1), alter)
if semitonesOffset == -24:
soa = (step, str(int(octave)-2), alter)
return soa
def count_alterations(all_notes):
button_count = 0
bend_count = 0
overbend_count = 0
impossibles_count = 0
for note in all_notes:
if (DRAWBEND1 in note) or (DRAWBEND2 in note) or (DRAWBEND3 in note) or (BLOWBEND1 in note) or (BLOWBEND2 in note):
bend_count += 1
if OVERBLOW in note:
overbend_count += 1
if "#" in note:
button_count += 1
if "?" in note:
impossibles_count += 1
return (button_count, bend_count, overbend_count, impossibles_count)
def fix_notes_split(input_list):
output = list()
for i in range(len(input_list)):
string = input_list[i]
if string.startswith(' ') or string.startswith('>') or (i == 0):
output.append(string)
else:
output[-1] = output[-1] + 'note' + string
return output
def add_text_to_notes(in_xml_file, out_xml_file='', mode=0, semitonesShift=0):
try:
with codecs.open(in_xml_file, "r", "utf-8") as fi:
lines = fi.readlines()
except Exception as e:
err = str(e)
warndlg('ERROR in opening file', f'ERROR: Error in MusicXML file!\nInput file may not be supported or wrong format selected')
return [],[]
single_line = ''.join(lines)
splt = single_line.split('note')
splt = fix_notes_split(splt)
prev_note = None
N = len(splt)
all_notes = list()
all_text = list()
for i in range(N):
string = splt[i]
if 'pitch' in string:
step = get_label(string, 'step')
octave = get_label(string, 'octave')
alter = get_label(string, 'alter')
voice = get_label(string, 'voice')
(step, octave, alter) = soa_shift((step, octave, alter), semitonesShift)
current_note = step + octave + alter
if DEFAULT_PLACEMENT == 'above':
if voice == '1': # place additional voice "above", where default voice text is "below"
placement = 'placement="above" '
else:
placement = 'placement="below" '
else:
if voice == '1': # place additional voice "above", where default voice text is "below"
placement = 'placement="below" '
else:
placement = 'placement="above" '
if 'text' not in string:
if '\n' in string:
lyric = f' <lyric number="1" {placement}color="#000000">\n <syllabic>single</syllabic>\n <text>NONE</text>\n </lyric>'
sss1 = string.split('\n')
end = sss1.pop()
sss1.append(lyric)
sss1.append(end)
string = '\n'.join(sss1)
else:
lyric = f'<lyric number="1" {placement}color="#000000"><syllabic>single</syllabic><text>NONE</text></lyric>'
string = string[0:-2] + lyric + string[-2:]
(step, alter) = change_notes_according_to_preference(step, alter)
if mode == 0:
new_text = note_to_text_heb(step, octave, alter)
elif mode == 1:
new_text = note_to_text_ChromaticHarmonica10(step, octave, alter, returnAllOptions=False)
elif mode == 2:
new_text = note_to_text_ChromaticHarmonica12(step, octave, alter, returnAllOptions=False)
elif mode == 3:
new_text = note_to_text_ChromaticHarmonica16(step, octave, alter, returnAllOptions=False)
elif mode == 4:
new_text = note_to_text_DiatonicHarmonicaC(step, octave, alter, 0)
elif mode == 5:
new_text = note_to_text_trumpet(step, octave, alter, addHebrew=False)
elif mode == 6:
new_text = note_to_text_baritone(step, octave, alter, addHebrew=False)
elif mode == 7:
new_text = note_to_text_tuba(step, octave, alter, addHebrew=False)
elif mode == 8:
new_text = note_to_text_Recorder(step, octave, alter, 0, addHebrew=False)
elif mode == 9:
new_text = note_to_text_english(step, octave, alter)
else:
warndlg('ERROR', 'Text mode not supported!')
if '\n' in new_text:
text = '[' + new_text.replace('\n',',') + ']'
all_text.append(text)
else:
all_text.append(new_text)
if '?' in new_text:
string = string.replace('color="#000000"', 'color="#FF0000"')
newstr = replace_text(string, new_text)
if 'tie type="stop"' not in string or prev_note != current_note: # don't write note text if this is a 'tie' to previous note, and previous note is identical
splt[i] = newstr
prev_note = current_note
all_notes.append(f'{step}{octave}{alter}')
#print(f'{step}{octave}{alter}', end=',')
output = 'note'.join(splt)
if out_xml_file:
with codecs.open(out_xml_file, "w+", "utf-8") as fo:
fo.write(output)
return all_notes,all_text
def main_process():
#test_semitones_conversion()
mode = 1 # heb, chromatic10, chromatic12, diatonicC
all_notes,all_text = add_text_to_notes(my_xml_file, outfile, mode, 0)
print(','.join(all_text))
print(f'Alterations counts: (button, bend, overbend, impossible) = {count_alterations(all_notes)}')
class DndLineEdit(QLineEdit):
def __init__(self, parent):
super(DndLineEdit, self).__init__(parent)
self.parent = parent
def dragEnterEvent(self, event):
data = event.mimeData()
urls = data.urls()
if urls and urls[0].scheme() == 'file':
event.acceptProposedAction()
def dragMoveEvent(self, event):
data = event.mimeData()
urls = data.urls()
if urls and urls[0].scheme() == 'file':
event.acceptProposedAction()
# register a callback function when file is dropped
def setDropFcn(self,dropFcnName):
self.dropFcn = dropFcnName
def dropEvent(self, event):
data = event.mimeData()
urls = data.urls()
if urls and urls[0].scheme() == 'file':
filepath = str(urls[0].path())[1:]
# any file type here - call external dropFcn() registered earlier
self.dropFcn(filepath)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self)
self.input_file = ''
self.output_file = ''
self.semitones_shift = 0
self.initUI()
def closeEvent(self, event):
#print("User has clicked the red x on the main window")
event.accept()
def initUI(self):
self.setWindowIcon(QIcon("google-earth-icon.png"))
self.title = 'MusicXML Auto Annotator'
self.version = 'v0.8'
defaultGeometry = (600, 200, 900, 200)
self.left, self.top, self.width, self.height = defaultGeometry
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
defaultDpi = 100
btnHeight = 25
btnWidth = 70
iconSize = btnHeight - 6
titleWidth = 130
bigBtnWidth = 250
txtHeight = 19
font1 = QFont()
font1.setPointSize(defaultDpi // 10)
font1red = QFont()
font1red.setPointSize(defaultDpi // 10)
#font1red.
font2 = QFont()
font2.setPointSize(defaultDpi // 10)
font2.setUnderline(True)
font = QFont()
font.setPointSize(defaultDpi // 10)
#font.setFamily("Courier New")
self.input_file_title = QLabel('Input MusicXML File:')
self.input_file_title.setFont(font1)
self.input_file_title.setFixedWidth(titleWidth)
self.input_file_edit = DndLineEdit(self)
self.input_file_edit.setText(str(self.input_file))
#self.input_file_edit.editingFinished.connect(self.input_file_changed)
self.input_file_edit.setDropFcn(self.handleDropFileInput)
#self.input_file_edit.returnPressed.connect(self.input_file_changed)
self.input_file_edit.setFont(font)
self.input_file_edit.setMinimumWidth(bigBtnWidth)
#self.browse_input_file = QPushButton(QIcon('download-icon.png'),'Browse')
self.browse_input_file = QPushButton('Browse')
self.browse_input_file.setFixedHeight(btnHeight)
self.browse_input_file.setFixedWidth(btnWidth)
self.browse_input_file.setCheckable(False)
self.browse_input_file.setFont(font1)
self.browse_input_file.clicked.connect(self.select_input_file)
self.output_file_title = QLabel('Output MusicXML File:')
self.output_file_title.setFont(font1)
self.output_file_title.setFixedWidth(titleWidth)
self.output_file_edit = QLineEdit()
self.output_file_edit.setText(str(self.output_file))
#self.output_file_edit.editingFinished.connect(self.output_file_changed)
#self.output_file_edit.returnPressed.connect(self.output_file_changed)
self.output_file_edit.setFont(font)
self.output_file_edit.setMinimumWidth(bigBtnWidth)
#self.browse_output_file = QPushButton(QIcon('download-icon.png'),'Browse')
self.browse_output_file = QPushButton('Browse')
self.browse_output_file.setFixedHeight(btnHeight)
self.browse_output_file.setFixedWidth(btnWidth)
self.browse_output_file.setCheckable(False)
self.browse_output_file.setFont(font1)
self.browse_output_file.clicked.connect(self.select_output_file)
self.options_title = QLabel(' ')
self.options_title.setFont(font1)
self.semitones_shift_title = QLabel('Semitones Shift:')
self.semitones_shift_title.setFont(font1)
self.semitones_shift_title.setFixedWidth(titleWidth)
self.semitones_shift_edit = QLineEdit()
self.semitones_shift_edit.setText(str(self.semitones_shift))
self.semitones_shift_edit.editingFinished.connect(self.semitones_shift_changed)
self.semitones_shift_edit.returnPressed.connect(self.semitones_shift_changed)
self.semitones_shift_edit.setFont(font)
self.semitones_shift_edit.setMinimumWidth(bigBtnWidth)
self.text_mode_title = QLabel('Text Mode:')
self.text_mode_title.setFont(font1)
self.text_mode_title.setFixedWidth(titleWidth)
self.text_mode_combo = QComboBox(self)
items = ['Hebrew Names','Chromatic Harmonica 10','Chromatic Harmonica 12','Chromatic Harmonica 16','Diatonic Harmonica (C)', 'Trumpet+Hebrew', 'Baritone+Hebrew', 'Tuba+Hebrew', 'Recorder+Hebrew', 'English+Hebrew'] # first one is the default one
self.out_postfix = ['Hebrew','Chromatic10','Chromatic12','Chromatic16','DiatonicC','Trumpet','Baritone','Tuba','Recorder','English'] # name extension for outfile
self.text_mode_combo.addItems(items)
self.text_mode_combo.activated.connect(self.combo_activated)
self.text_mode_combo.setFont(font1)
selected_mode = 0
self.text_mode_type = selected_mode
#index = self.text_mode_combo.findText(selected_mode, Qt.MatchFixedString)
#if index >= 0:
# self.text_mode_combo.setCurrentIndex(index)
self.summary0_title = QLabel('Summary:')
self.summary0_title.setFont(font2)
self.summary1_title = QLabel('')
self.summary1_title.setFont(font1)
#self.summary1_title.setFixedHeight(txtHeight)
self.summary1_text = QLabel('')
self.summary1_text.setFont(font1)
#self.summary1_text.setFixedHeight(txtHeight)
self.summary2_title = QLabel('')
self.summary2_title.setFont(font1)
#self.summary2_title.setFixedHeight(txtHeight)
self.summary2_text = QLabel('')
self.summary2_text.setFont(font1red)
#self.summary2_text.setFixedHeight(txtHeight)
self.summary3_title = QLabel('')
self.summary3_title.setFont(font1)