-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
structures.py
5677 lines (5244 loc) · 220 KB
/
structures.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
from copy import deepcopy as copy
from fractions import Fraction
if __name__ == 'musicpy.structures':
from . import database
else:
import database
class note:
def __init__(self, name, num=4, duration=0.25, volume=100, channel=None):
if name not in database.standard:
raise ValueError(
f"Invalid note name '{name}', accepted note names are {list(database.standard.keys())}"
)
self.name = name
self.num = num
self.duration = duration
volume = int(volume)
if volume > 127:
volume = 127
self.volume = volume
self.channel = channel
@property
def degree(self):
return database.standard[self.name] + 12 * (self.num + 1)
@degree.setter
def degree(self, value):
self.name = database.standard_reverse[value % 12]
self.num = (value // 12) - 1
def __repr__(self):
return f'{self.name}{self.num}'
def __eq__(self, other):
return type(
other
) is note and self.name == other.name and self.num == other.num
def __matmul__(self, other):
return self.name == other.name
def setvolume(self, vol):
vol = int(vol)
if vol > 127:
vol = 127
self.volume = vol
def set(self, duration=None, volume=None, channel=None):
if duration is None:
duration = copy(self.duration)
if volume is None:
volume = copy(self.volume)
if channel is None:
channel = copy(self.channel)
return note(self.name, self.num, duration, volume, channel)
def __mod__(self, obj):
return self.set(*obj)
def join(self, other, ind, interval):
if isinstance(other, str):
other = mp.to_note(other)
if isinstance(other, note):
return chord([copy(self), copy(other)], interval=interval)
if isinstance(other, chord):
temp = copy(other)
temp.insert(ind, copy(self))
temp.interval.insert(ind, interval)
return temp
def up(self, unit=1):
return mp.degree_to_note(self.degree + unit, self.duration,
self.volume, self.channel)
def down(self, unit=1):
return self.up(-unit)
def __pos__(self):
return self.up()
def __neg__(self):
return self.down()
def __invert__(self):
name = self.name
if name in database.standard_dict:
if '#' in name:
return self.reset(name=database.reverse_standard_dict[name])
else:
return self.reset(name=database.standard_dict[name])
elif name in database.reverse_standard_dict:
return self.reset(name=database.reverse_standard_dict[name])
else:
return self.reset(name=name)
def __add__(self, obj):
if isinstance(obj, int):
return self.up(obj)
if not isinstance(obj, note):
obj = mp.to_note(obj)
return chord([copy(self), copy(obj)])
def __sub__(self, obj):
if isinstance(obj, int):
return self.down(obj)
def __call__(self, obj=''):
return mp.C(self.name + obj, self.num)
def with_interval(self, interval):
result = chord([copy(self), self + interval])
return result
def getchord_by_interval(start,
interval1,
duration=0.25,
interval=0,
cummulative=True):
return mp.getchord_by_interval(start, interval1, duration, interval,
cummulative)
def dotted(self, num=1):
temp = copy(self)
temp.duration = temp.duration * sum([(1 / 2)**i
for i in range(num + 1)])
return temp
def reset_octave(self, num):
temp = copy(self)
temp.num = num
return temp
def reset_pitch(self, name):
temp = copy(self)
temp.name = name
return temp
def reset_name(self, name):
temp = mp.to_note(name)
temp.duration = self.duration
temp.volume = self.volume
temp.channel = self.channel
return temp
def set_channel(self, channel):
self.channel = channel
def with_channel(self, channel):
temp = copy(self)
temp.channel = channel
return temp
class chord:
''' This class can contain a chord with many notes played simultaneously and either has intervals, the default interval is 0.'''
def __init__(self,
notes,
duration=None,
interval=None,
rootpitch=4,
other_messages=[],
start_time=None):
standardize_msg = False
if isinstance(notes, str):
notes = notes.replace(' ', '').split(',')
if all(not any(i.isdigit() for i in j) for j in notes
if (not j.startswith('tempo')) and (
not j.startswith('pitch'))):
standardize_msg = True
elif isinstance(notes, list) and all(
not isinstance(i, note)
for i in notes) and all(not any(j.isdigit() for j in i)
for i in notes if isinstance(i, str)):
standardize_msg = True
notes_msg = _read_notes(notes, rootpitch)
notes, current_intervals, current_start_time = notes_msg
if current_intervals and not interval:
interval = current_intervals
if standardize_msg and notes:
root = notes[0]
notels = [root]
last = None
for i in range(1, len(notes)):
last_note = notels[i - 1]
if isinstance(last_note, note):
last = last_note
current_note = notes[i]
if not isinstance(current_note, note):
notels.append(current_note)
else:
if last is not None:
current = note(current_note.name, last.num)
if database.standard[
current.name] <= database.standard[last.name]:
current = note(current.name, last.num + 1)
else:
current = current_note
notels.append(current)
notes = notels
self.notes = notes
# interval between each two notes one-by-one
self.interval = [0 for i in range(len(notes))]
if interval is not None:
self.change_interval(interval)
if duration is not None:
if isinstance(duration, (int, float)):
for t in self.notes:
t.duration = duration
else:
for k in range(len(duration)):
self.notes[k].duration = duration[k]
if start_time is None:
self.start_time = current_start_time
else:
self.start_time = start_time
self.other_messages = other_messages
def get_duration(self):
return [i.duration for i in self.notes]
def get_volume(self):
return [i.volume for i in self.notes]
def get_degree(self):
return [i.degree for i in self]
def names(self):
return [i.name for i in self if isinstance(i, note)]
def __eq__(self, other):
return type(
other
) is chord and self.notes == other.notes and self.interval == other.interval
def split(self, return_type, get_time=False, sort=False):
temp = copy(self)
inds = [
i for i in range(len(temp))
if isinstance(temp.notes[i], return_type)
]
notes = [temp.notes[i] for i in inds]
intervals = [temp.interval[i] for i in inds]
if get_time and return_type != note:
no_time = [k for k in inds if temp.notes[k].start_time is None]
for each in no_time:
current_time = temp[:each].bars()
current = temp.notes[each]
current.start_time = current_time
if sort:
notes.sort(key=lambda s: s.start_time)
return chord(notes, interval=intervals, start_time=temp.start_time)
def get_msg(self, types):
return [i for i in self.other_messages if i.type == types]
def cut(self, ind1=0, ind2=None, start_time=0, return_inds=False):
# get parts of notes between two bars
temp = copy(self)
start_offset = start_time - ind1
if start_offset < 0:
start_offset = 0
ind1 -= start_time
if ind1 < 0:
ind1 = 0
if ind2 is not None:
ind2 -= start_time
if ind2 <= 0:
ind2 = 0
else:
ind2 = temp.bars(mode=0)
changes = []
for i in range(len(temp.notes)):
each = temp.notes[i]
if isinstance(each, (tempo, pitch_bend)):
if each.start_time is None:
each.start_time = temp[:i].bars(mode=0)
each.start_time -= (ind1 + start_time - start_offset)
if 0 <= each.start_time < ind2 - ind1:
changes.append(each)
temp = temp.only_notes()
current_bar = 0
notes = temp.notes
intervals = temp.interval
length = len(notes)
start_ind = 0
to_ind = length
find_start = False
if ind1 == 0:
find_start = True
actual_start_time = 0
for i in range(length):
current_note = notes[i]
if isinstance(current_note, note):
current_bar += intervals[i]
if (not find_start) and current_bar >= ind1:
start_ind = i + 1
find_start = True
actual_start_time = current_bar - ind1
if ind2 is None:
break
elif ind2 and current_bar >= ind2:
to_ind = i + 1
break
if not find_start:
start_ind = to_ind
if return_inds:
return start_ind, to_ind
result = temp[start_ind:to_ind]
result += chord(changes)
result.other_messages = [
i for i in result.other_messages if ind1 <= i.start_time < ind2
]
result.start_time = actual_start_time
return result
def cut_time(self,
bpm,
time1=0,
time2=None,
start_time=0,
return_inds=False,
normalize_tempo=False):
if normalize_tempo:
temp = copy(self)
temp.normalize_tempo(bpm)
return temp.cut_time(bpm, time1, time2, start_time, return_inds)
time1 -= start_time
if time1 < 0:
return chord([]) if not return_inds else (0, 0)
if time2 is not None:
time2 -= start_time
if time2 <= 0:
return chord([]) if not return_inds else (0, 0)
current_bar = 0
notes = self.notes
intervals = self.interval
length = len(notes)
start_ind = 0
to_ind = length
find_start = False
if time1 == 0:
find_start = True
actual_start_time = 0
start_length = (time1 / 4) * bpm / 60
for i in range(length):
current_note = notes[i]
if isinstance(current_note, note):
current_bar += intervals[i]
if (not find_start) and (60 / bpm) * current_bar * 4 >= time1:
start_ind = i + 1
find_start = True
actual_start_time = current_bar - start_length
if time2 is None:
break
elif time2 and (60 / bpm) * current_bar * 4 >= time2:
to_ind = i + 1
break
if not find_start:
start_ind = to_ind
if return_inds:
return start_ind, to_ind
result = self[start_ind:to_ind]
result.start_time = actual_start_time
return result
def last_note_standardize(self):
for i in range(len(self.notes) - 1, -1, -1):
current = self.notes[i]
if isinstance(current, note):
self.interval[i] = current.duration
break
def bars(self, start_time=0, mode=1, audio_mode=0):
if mode == 0:
max_length = sum(self.interval)
elif mode == 1:
temp = self.only_notes(audio_mode=audio_mode)
current_durations = temp.get_duration()
if not current_durations:
return 0
current_intervals = temp.interval
max_length = current_durations[0]
current_length = 0
for i in range(1, len(temp)):
current_duration = current_durations[i]
last_interval = current_intervals[i - 1]
current_length += last_interval + current_duration
if current_length > max_length:
max_length = current_length
current_length -= current_duration
return start_time + max_length
def firstnbars(self, n, start_time=0):
return self.cut(0, n, start_time)
def get_bar(self, n, start_time=0):
return self.cut(n, n + 1, start_time)
def split_bars(self, start_time=0):
bars_length = int(self.bars(start_time))
result = []
for i in range(bars_length):
result.append(self.cut(i, i + 1, start_time))
return result
def count(self, note1, mode='name'):
if isinstance(note1, str):
if any(i.isdigit() for i in note1):
mode = 'note'
note1 = mp.to_note(note1)
if mode == 'name':
return self.names().count(note1.name)
elif mode == 'note':
return self.notes.count(note1)
def standard_notation(self):
temp = copy(self)
for each in temp.notes:
if isinstance(each, note) and each.name in database.standard_dict:
each.name = database.standard_dict[each.name]
return temp
def most_appear(self, choices=None, mode='name', as_standard=False):
test_obj = self
if as_standard:
test_obj = self.standard_notation()
if not choices:
return max([i for i in database.standard2],
key=lambda s: test_obj.count(s))
else:
choices = [
mp.to_note(i) if isinstance(i, str) else i for i in choices
]
if mode == 'name':
return max([i.name for i in choices],
key=lambda s: test_obj.count(s))
elif mode == 'note':
return max(choices,
key=lambda s: test_obj.count(s, mode='note'))
def count_appear(self, choices=None, as_standard=True, sort=False):
test_obj = self
if as_standard:
test_obj = self.standard_notation()
if not choices:
choices = copy(database.standard2) if as_standard else copy(
database.standard)
else:
choices = [
mp.to_note(i).name if isinstance(i, str) else i.name
for i in choices
]
result = {i: test_obj.count(i) for i in choices}
if sort:
result = [[i, result[i]] for i in result]
result.sort(key=lambda s: s[1], reverse=True)
return result
def eval_time(self,
bpm,
ind1=None,
ind2=None,
mode='seconds',
start_time=0,
normalize_tempo=False,
audio_mode=0):
if normalize_tempo:
temp = copy(self)
temp.normalize_tempo(bpm)
return temp.eval_time(bpm,
ind1,
ind2,
start_time,
mode=mode,
audio_mode=audio_mode)
if ind1 is None:
whole_bars = self.bars(start_time, audio_mode=audio_mode)
else:
if ind2 is None:
ind2 = self.bars(start_time, audio_mode=audio_mode)
whole_bars = ind2 - ind1
result = (60 / bpm) * whole_bars * 4
if mode == 'seconds':
result = round(result, 3)
return f'{result}s'
elif mode == 'hms':
hours = int(result / 3600)
minutes = int((result - 3600 * hours) / 60)
seconds = round(result - 3600 * hours - 60 * minutes, 3)
if hours:
return f'{hours} hours, {minutes} minutes, {seconds} seconds'
else:
return f'{minutes} minutes, {seconds} seconds'
elif mode == 'number':
return result
def count_bars(self, ind1, ind2, bars_range=True):
bars_length = self[ind1:ind2].bars()
if bars_range:
start = self[:ind1].bars()
return [start, start + bars_length]
else:
return bars_length
def clear_pitch_bend(self, value=0, cond=None):
length = len(self)
whole_notes = self.notes
if cond is None:
if value == 0:
inds = [
i for i in range(length)
if not (isinstance(whole_notes[i], pitch_bend)
and whole_notes[i].value == 0)
]
elif value == 'all':
inds = [
i for i in range(length)
if not isinstance(whole_notes[i], pitch_bend)
]
else:
inds = [
i for i in range(length)
if not isinstance(whole_notes[i], pitch_bend)
or not cond(whole_notes[i])
]
self.notes = [whole_notes[k] for k in inds]
self.interval = [self.interval[k] for k in inds]
def clear_tempo(self, cond=None):
length = len(self)
whole_notes = self.notes
if cond is None:
inds = [
i for i in range(length)
if not isinstance(whole_notes[i], tempo)
]
else:
inds = [
i for i in range(length)
if (not isinstance(whole_notes[i], tempo)) or (
not cond(whole_notes[i]))
]
self.notes = [whole_notes[k] for k in inds]
self.interval = [self.interval[k] for k in inds]
def __mod__(self, alist):
if isinstance(alist, (list, tuple)):
return self.set(*alist)
elif isinstance(alist, (str, note)):
return self.on(alist)
def standardize(self):
temp = self.only_notes()
notenames = temp.names()
intervals = temp.interval
durations = temp.get_duration()
names_standard = [
database.standard_dict[i] if i in database.standard_dict else i
for i in notenames
]
names_offrep = []
new_duration = []
new_interval = []
for i in range(len(names_standard)):
current = names_standard[i]
if current not in names_offrep:
if current is not None:
names_offrep.append(current)
else:
names_offrep.append(temp.notes[i])
new_interval.append(intervals[i])
new_duration.append(durations[i])
temp.notes = chord(names_offrep,
rootpitch=temp[0].num,
duration=new_duration).notes
temp.interval = new_interval
return temp
def sortchord(self):
temp = self.copy()
temp.notes.sort(key=lambda x: x.degree)
return temp
def set(self, duration=None, interval=None, volume=None, ind='all'):
if interval is None:
interval = copy(self.interval)
result = chord(copy(self.notes),
duration,
interval,
start_time=copy(self.start_time))
if volume is not None:
result.setvolume(volume, ind)
return result
def special_set(self,
duration=None,
interval=None,
volume=None,
ind='all'):
if interval is None:
interval = copy(self.interval)
result = chord(copy(self.notes),
duration,
interval,
start_time=copy(self.start_time))
result.interval = [
0 if hasattr(self.notes[i], 'keep_same_time')
and self.notes[i].keep_same_time else result.interval[i]
for i in range(len(self))
]
result.setvolume(volume)
if volume is not None:
result.setvolume(volume, ind)
return result
def change_interval(self, newinterval):
if isinstance(newinterval, (int, float)):
self.interval = [newinterval for i in range(len(self.notes))]
else:
if len(newinterval) == len(self.interval):
self.interval = newinterval
else:
raise ValueError(
'please ensure the intervals between notes has the same numbers of the notes'
)
def __repr__(self):
return f'{self.notes} with interval {self.interval}'
def __contains__(self, note1):
if not isinstance(note1, note):
note1 = mp.to_note(note1)
if note1.name in database.standard_dict:
note1.name = database.standard_dict[note1.name]
return note1 in self.same_accidentals().notes
def __add__(self, obj):
if isinstance(obj, (int, list)):
return self.up(obj)
elif isinstance(obj, tuple):
if isinstance(obj[0], chord):
return self | obj
else:
return self.up(*obj)
elif isinstance(obj, rest):
return self.rest(obj.get_duration())
temp = copy(self)
if isinstance(obj, note):
temp.notes.append(copy(obj))
temp.interval.append(temp.interval[-1])
elif isinstance(obj, str):
return temp + mp.to_note(obj)
elif isinstance(obj, chord):
temp |= obj
return temp
def __radd__(self, obj):
if isinstance(obj, (rest, float)):
temp = copy(self)
temp.start_time += (obj if not isinstance(obj, rest) else
obj.get_duration())
return temp
elif isinstance(obj, int):
return self + obj
def __ror__(self, obj):
if isinstance(obj, (int, float, rest)):
temp = copy(self)
temp.start_time += (obj if not isinstance(obj, rest) else
obj.get_duration())
return temp
def __pos__(self):
return self.up()
def __neg__(self):
return self.down()
def __invert__(self):
return self.reverse()
def __or__(self, obj):
if isinstance(obj, (int, float)):
return self.rest(obj)
elif isinstance(obj, str):
obj = mp.trans(obj)
elif isinstance(obj, tuple):
first = obj[0]
start = obj[1] if len(obj) == 2 else 0
if isinstance(first, int):
temp = copy(self)
for k in range(first - 1):
temp |= (self, start)
return temp
elif isinstance(first, rest):
return self.rest(first.get_duration(),
ind=obj[1] if len(obj) == 2 else None)
else:
return self.add(first, start=start, mode='after')
elif isinstance(obj, list):
return self.rest(*obj)
elif isinstance(obj, rest):
return self.rest(obj.get_duration())
return self.add(obj, mode='after')
def __xor__(self, obj):
if isinstance(obj, int):
return self.inversion_highest(obj)
if isinstance(obj, note):
name = obj.name
else:
name = obj
notenames = self.names()
if name in notenames and name != notenames[-1]:
return self.inversion_highest(notenames.index(name))
else:
return self + obj
def __truediv__(self, obj):
if isinstance(obj, int):
if obj > 0:
return self.inversion(obj)
else:
return self.inversion_highest(-obj)
elif isinstance(obj, list):
return self.sort(obj)
else:
if not isinstance(obj, chord):
if isinstance(obj, str):
obj = mp.trans_note(obj)
notenames = self.names()
if obj.name not in database.standard2:
obj.name = database.standard_dict[obj.name]
if obj.name in notenames and obj.name != notenames[0]:
return self.inversion(notenames.index(obj.name))
return self.on(obj)
def __and__(self, obj):
if isinstance(obj, tuple):
if len(obj) == 2:
first = obj[0]
if isinstance(first, int):
temp = copy(self)
for k in range(first - 1):
temp &= (self, (k + 1) * obj[1])
return temp
else:
return self.add(obj[0], start=obj[1], mode='head')
else:
return
elif isinstance(obj, int):
return self & (obj, 0)
else:
return self.add(obj, mode='head')
def __matmul__(self, obj):
if isinstance(obj, list):
return self.get(obj)
elif isinstance(obj, int):
return self.inv(obj)
elif isinstance(obj, str):
return self.inv(self.names().index(obj))
else:
if isinstance(obj, tuple):
return mp.alg.negative_harmony(obj[0], self, *obj[1:])
else:
return mp.alg.negative_harmony(obj, self)
def negative_harmony(self, *args, **kwargs):
return mp.alg.negative_harmony(current_chord=self, *args, **kwargs)
def __call__(self, obj):
# deal with the chord's sharp or flat notes, or to omit some notes
# of the chord
temp = copy(self)
commands = obj.split(',')
for each in commands:
each = each.replace(' ', '')
first = each[0]
if first in ['#', 'b']:
degree = each[1:]
if degree in database.degree_match:
degree_ls = database.degree_match[degree]
found = False
for i in degree_ls:
current_note = temp[0].up(i)
if current_note in temp:
ind = temp.notes.index(current_note)
temp.notes[ind] = temp.notes[ind].up(
) if first == '#' else temp.notes[ind].down()
found = True
break
if not found:
temp += temp[0].up(
degree_ls[0]).up() if first == '#' else temp[0].up(
degree_ls[0]).down()
else:
if degree in database.standard:
if degree in database.standard_dict:
degree = database.standard_dict[degree]
self_names = [
i if i not in database.standard_dict else
database.standard_dict[i] for i in temp.names()
]
if degree in self_names:
ind = temp.names().index(degree)
temp.notes[ind] = temp.notes[ind].up(
) if first == '#' else temp.notes[ind].down()
elif each.startswith('omit') or each.startswith('no'):
degree = each[4:] if each.startswith('omit') else each[2:]
if degree in database.degree_match:
degree_ls = database.degree_match[degree]
for i in degree_ls:
current_note = temp[0].up(i)
if current_note in temp:
ind = temp.notes.index(current_note)
del temp.notes[ind]
del temp.interval[ind]
break
else:
if degree in database.standard:
if degree in database.standard_dict:
degree = database.standard_dict[degree]
self_names = [
i if i not in database.standard_dict else
database.standard_dict[i] for i in temp.names()
]
if degree in self_names:
temp = temp.omit(degree)
elif each.startswith('sus'):
num = each[3:]
if num.isdigit():
num = int(num)
else:
num = 4
temp.notes = temp.sus(num).notes
elif each.startswith('add'):
degree = each[3:]
if degree in database.degree_match:
degree_ls = database.degree_match[degree]
temp += temp[0].up(degree_ls[0])
return temp
def get(self, ls):
temp = copy(self)
result = []
result_interval = []
for each in ls:
if isinstance(each, int):
result.append(temp[each - 1])
result_interval.append(temp.interval[each - 1])
elif isinstance(each, float):
num, pitch = [int(j) for j in str(each).split('.')]
if num > 0:
current_note = temp[num - 1] + pitch * database.octave
else:
current_note = temp[-num - 1] - pitch * database.octave
result.append(current_note)
result_interval.append(temp.interval[abs(num) - 1])
return chord(result,
interval=result_interval,
start_time=temp.start_time)
def pop(self, ind=None):
if ind is None:
result = self.notes.pop()
self.interval.pop()
else:
result = self.notes.pop(ind)
self.interval.pop(ind)
return result
def __sub__(self, obj):
if isinstance(obj, (int, list)):
return self.down(obj)
if isinstance(obj, tuple):
return self.down(*obj)
if not isinstance(obj, note):
obj = mp.to_note(obj)
temp = copy(self)
if obj in temp:
ind = temp.notes.index(obj)
del temp.notes[ind]
del temp.interval[ind]
return temp
def __mul__(self, num):
if isinstance(num, tuple):
return self | num
else:
temp = copy(self)
for i in range(num - 1):
temp |= self
return temp
def __rmul__(self, num):
return self * num
def reverse(self, start=None, end=None, cut=False, start_time=0):
temp = copy(self)
if start is None:
temp2 = temp.only_notes()
length = len(temp2)
bar_length = temp2.bars()
changes = []
for i in range(len(temp.notes)):
each = temp.notes[i]
if isinstance(each, (tempo, pitch_bend)):
if each.start_time is None:
each.start_time = temp[:i].bars()
else:
each.start_time -= start_time
each.start_time = bar_length - each.start_time
changes.append(each)
if temp2.notes:
last_interval = temp2.interval[-1]
end_events = []
current_start_time = 0
for i in range(len(temp2.notes)):
current_note = temp2.notes[i]
current_end_time = current_start_time + current_note.duration
current_end_event = (current_note, current_end_time, i)
end_events.append(current_end_event)
current_start_time += temp2.interval[i]
end_events.sort(key=lambda s: (s[1], s[2]), reverse=True)
new_notes = [i[0] for i in end_events]
new_interval = [
end_events[j][1] - end_events[j + 1][1]
for j in range(len(end_events) - 1)
]
new_interval.append(last_interval)
temp2.notes = new_notes
temp2.interval = new_interval
temp2 += chord(changes)
return temp2
else:
if end is None:
result = temp[:start] + temp[start:].reverse(
start_time=start_time)
return result[start:] if cut else result
else:
result = temp[:start] + temp[start:end].reverse(
start_time=start_time) + temp[end:]
return result[start:end] if cut else result
def reverse_chord(self, start_time=0):
temp = copy(self)
bar_length = temp.bars()
temp.notes = temp.notes[::-1]
temp.interval = temp.interval[::-1]
if temp.interval:
temp.interval.append(temp.interval.pop(0))
for i in temp.notes:
if isinstance(i, (tempo, pitch_bend)):
if i.start_time is None:
i.start_time = temp[:i].bars()
else:
i.start_time -= start_time
i.start_time = bar_length - i.start_time
return temp
def intervalof(self, cummulative=True, translate=False):
degrees = self.get_degree()
if not cummulative:
N = len(degrees)
result = [degrees[i] - degrees[i - 1] for i in range(1, N)]
else:
root = degrees[0]
others = degrees[1:]
result = [i - root for i in others]
if not translate:
return result
return [database.INTERVAL[x % database.octave] for x in result]
def add(self, note1=None, mode='after', start=0, duration=0.25):
if len(self) == 0:
result = copy(note1)
if start != 0:
result.start_time += start
return result
temp = copy(self)
if isinstance(note1, int):
temp += temp[0].up(note1)
return temp
if len(note1) == 0:
return temp
if mode == 'tail':
note1 = copy(note1)
temp.notes += note1.notes
temp.interval += note1.interval
temp.other_messages += note1.other_messages
return temp
elif mode == 'head':
note1 = copy(note1)