forked from Rainbow-Dreamer/musicpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructures.py
1664 lines (1461 loc) · 55.9 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
try:
from .database import *
except:
from database import *
class note:
def __init__(self, name, num, duration=0.25, volume=100):
self.name = name
self.num = num
self.degree = standard[name] + 12 * (num + 1)
self.duration = duration
self.volume = volume
def __str__(self):
return f'{self.name}{self.num}'
__repr__ = __str__
def __eq__(self, other):
return isinstance(
other, note) and self.name == other.name and self.num == other.num
def __matmul__(self, other):
return self.name == other.name
def setvolume(self, vol):
self.volume = vol
def set(self, duration=0.25, volume=100):
return note(self.name, self.num, duration, volume)
def __mod__(self, obj):
return self.set(*obj)
def join(self, other, ind, interval):
if type(other) == str:
other = toNote(other)
if type(other) == note:
return chord([copy(self), copy(other)], interval=interval)
if type(other) == chord:
temp = copy(other)
temp.insert(ind, copy(self))
temp.interval.insert(ind - 1, interval)
return temp
def up(self, unit=1, duration=None, volume=None):
if duration is None:
duration = self.duration
if volume is None:
volume = self.volume
return degree_to_note(self.degree + unit, duration, volume)
def down(self, unit=1, duration=None, volume=None):
if duration is None:
duration = self.duration
if volume is None:
volume = self.volume
return degree_to_note(self.degree - unit, duration, volume)
def __pos__(self):
return self.up()
def __neg__(self):
return self.down()
def __invert__(self):
name = self.name
if name in standard_dict:
return note(standard_dict[name], self.num)
elif name in reverse_standard_dict:
return note(reverse_standard_dict[name], self.num)
else:
return note(name, self.num)
def play(self, *args, **kwargs):
import musicpy
musicpy.play(self, *args, **kwargs)
def __add__(self, obj):
if isinstance(obj, int):
return self.up(obj)
if not isinstance(obj, note):
obj = toNote(obj)
return chord([copy(self), copy(obj)])
def __sub__(self, obj):
if isinstance(obj, int):
return self.down(obj)
def toNote(notename, duration=0.25, volume=100, pitch=4):
if any(all(i in notename for i in j) for j in ['()', '[]', '{}']):
split_symbol = '(' if '(' in notename else (
'[' if '[' in notename else '{')
notename, info = notename.split(split_symbol)
info = info[:-1].split(';')
if len(info) == 1:
duration = info[0]
else:
duration, volume = info[0], eval(info[1])
if duration[0] == '.':
duration = 1 / eval(duration[1:])
else:
duration = eval(duration)
return toNote(notename, duration, volume)
else:
num_text = ''.join([x for x in notename if x.isdigit()])
if not num_text.isdigit():
num = pitch
else:
num = int(num_text)
name = ''.join([x for x in notename if not x.isdigit()])
return note(name, num, duration, volume)
def trans_note(notename, duration=0.25, volume=100, pitch=4):
num = ''.join([x for x in notename if x.isdigit()])
if not num:
num = pitch
else:
num = eval(num)
name = ''.join([x for x in notename if not x.isdigit()])
return note(name, num, duration, volume)
def degrees_to_chord(ls, duration=0.25, interval=0):
return chord([degree_to_note(i) for i in ls],
duration=duration,
interval=interval)
def degree_to_note(degree, duration=0.25, volume=100):
name = standard_reverse[degree % 12]
num = (degree // 12) - 1
return note(name, num, duration, volume)
def read_notes(note_ls, rootpitch=4):
intervals = []
notes_result = []
for each in note_ls:
if isinstance(each, note):
notes_result.append(each)
else:
if any(all(i in each for i in j) for j in ['()', '[]', '{}']):
split_symbol = '(' if '(' in each else (
'[' if '[' in each else '{')
notename, info = each.split(split_symbol)
volume = 100
info = info[:-1].split(';')
info_len = len(info)
if info_len == 1:
duration = info[0]
else:
if info_len == 2:
duration, interval = info
else:
duration, interval, volume = info
volume = eval(volume)
if interval[0] == '.':
interval = 1 / eval(interval[1:])
else:
interval = eval(interval)
intervals.append(interval)
if duration[0] == '.':
duration = 1 / eval(duration[1:])
else:
duration = eval(duration)
notes_result.append(
toNote(notename, duration, volume, rootpitch))
else:
notes_result.append(toNote(each, pitch=rootpitch))
if len(intervals) != len(notes_result):
intervals = []
return notes_result, intervals
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):
standardize_msg = False
if type(notes) == str:
notes = notes.replace(' ', '').split(',')
if all(not any(i.isdigit() for i in j) for j in notes):
standardize_msg = True
elif type(notes) == list and all(
type(i) == str and (not any(j.isdigit() for j in i))
for i in notes):
standardize_msg = True
notes_msg = read_notes(notes, rootpitch)
notes, current_intervals = notes_msg
if current_intervals:
interval = current_intervals
#notes = [x if isinstance(x, note) else toNote(x) for x in notes]
if standardize_msg and notes:
root = notes[0]
notels = [root]
for i in range(1, len(notes)):
last = notels[i - 1]
current = note(notes[i].name, last.num)
if standard[current.name] <= standard[last.name]:
current = note(current.name, last.num + 1)
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.changeInterval(interval)
if duration is not None:
if isinstance(duration, int) or isinstance(duration, float):
for t in self.notes:
t.duration = duration
else:
for k in range(len(duration)):
self.notes[k].duration = duration[k]
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]
def __eq__(self, other):
return isinstance(
other, chord
) and self.notes == other.notes and self.interval == other.interval
def addnote(self, newnote):
if isinstance(newnote, note):
self.notes.append(newnote)
self.interval.append(self.interval[-1])
else:
self.notes.append(toNote(newnote))
self.interval.append(self.interval[-1])
def split(self):
return self.notes
def __mod__(self, alist):
types = type(alist)
if types in [list, tuple]:
return self.set(*alist)
elif types == int:
temp = copy(self)
for i in range(alist - 1):
temp //= self
return temp
def standardize(self):
temp = self.copy()
notenames = temp.names()
intervals = temp.interval
durations = temp.get_duration()
names_offrep = []
new_duration = []
new_interval = []
for i in range(len(notenames)):
current = notenames[i]
if current not in names_offrep:
if current in standard_dict:
current = standard_dict[current]
names_offrep.append(current)
new_interval.append(intervals[i])
new_duration.append(durations[i])
temp.notes = chord(names_offrep,
rootpitch=temp[1].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):
return chord(copy(self.notes), duration, interval)
def changeInterval(self, newinterval):
if isinstance(newinterval, int) or isinstance(newinterval, float):
self.interval = [newinterval for i in range(len(self.notes))]
else:
if len(newinterval) == len(self.interval):
self.interval = newinterval
else:
return 'please ensure the intervals between notes has the same numbers of the notes'
def __str__(self):
return f'{self.notes} with interval {self.interval}'
__repr__ = __str__
def __contains__(self, note1):
if not isinstance(note1, note):
note1 = toNote(note1)
return note1 in self.notes
def __add__(self, obj):
if isinstance(obj, int) or isinstance(obj, list):
return self.up(obj)
if isinstance(obj, tuple):
return self.up(*obj)
temp = copy(self)
if isinstance(obj, note):
temp.notes.append(copy(obj))
temp.interval.append(temp.interval[-1])
elif isinstance(obj, str):
try:
return temp.__add__(toNote(obj))
except:
obj_num = temp[-1].num
if obj in standard:
if standard[obj] <= standard[temp[-1].name]:
obj_num += 1
return temp.__add__(note(obj, obj_num))
elif isinstance(obj, chord):
obj = copy(obj)
temp.notes += obj.notes
temp.interval += obj.interval
return temp
def __pos__(self):
return self.up()
def __neg__(self):
return self.down()
def __invert__(self):
return self.reverse()
def __floordiv__(self, obj):
types = type(obj)
if types == int or types == float:
return self.period(obj)
elif types == str:
import musicpy
obj = musicpy.trans(obj)
elif types == tuple:
import musicpy
obj = musicpy.trans(*obj)
return self.add(obj, mode='after')
def __or__(self, other):
return self // other
def __xor__(self, obj):
if type(obj) == int:
return self.inversion_highest(obj)
if type(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) + 1)
else:
return self + obj
def __truediv__(self, obj):
types = type(obj)
if types == int:
if obj > 0:
return self.inversion(obj)
else:
return self.inversion_highest(-obj)
elif types == list:
return self.sort(obj)
else:
if types != chord:
if types != note:
obj = trans_note(obj)
notenames = self.names()
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 type(obj) == tuple:
if len(obj) == 2:
return self.add(obj[0], start=obj[1], mode='head')
else:
return
return self.add(obj, mode='head')
def __matmul__(self, obj):
types = type(obj)
if types == list:
return self.get(obj)
elif types == int:
return self.inv(obj)
elif types == str:
return self.inv(self.names().index(obj))
else:
import musicpy
if types == tuple:
return musicpy.negative_harmony(obj[0], self, *obj[1:])
else:
return musicpy.negative_harmony(obj, self)
def negative_harmony(self, *args, **kwargs):
import musicpy
return musicpy.negative_harmony(a=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 degree_match:
degree_ls = degree_match[degree]
found = False
for i in degree_ls:
current_note = temp[1].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[1].up(
degree_ls[0]).up() if first == '#' else temp[1].up(
degree_ls[0]).down()
else:
if degree in standard:
if degree in standard_dict:
degree = standard_dict[degree]
self_names = [
i if i not in standard_dict else 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 degree_match:
degree_ls = degree_match[degree]
for i in degree_ls:
current_note = temp[1].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 standard:
if degree in standard_dict:
degree = standard_dict[degree]
self_names = [
i if i not in standard_dict else standard_dict[i]
for i in temp.names()
]
if degree in self_names:
temp = temp.omit(degree, 2)
return temp
def detect(self, *args, **kwargs):
import musicpy
return musicpy.detect(self, *args, **kwargs)
def get(self, ls):
result = []
result_interval = []
for each in ls:
if isinstance(each, int):
result.append(self[each])
result_interval.append(self.interval[each - 1])
elif isinstance(each, float):
num, pitch = [int(j) for j in str(each).split('.')]
if num > 0:
current_note = self[num] + pitch * octave
else:
current_note = self[-num] - pitch * octave
result.append(current_note)
result_interval.append(self.interval[num - 1])
return chord(result, interval=result_interval)
def pop(self, ind=None):
if ind is None:
result = self.notes.pop()
self.interval.pop()
else:
if ind > 0:
ind -= 1
result = self.notes.pop(ind)
self.interval.pop(ind)
return result
def __sub__(self, obj):
if isinstance(obj, int) or isinstance(obj, list):
return self.down(obj)
if isinstance(obj, tuple):
return self.up(*obj)
if not isinstance(obj, note):
obj = toNote(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):
temp = copy(self)
unit = copy(temp)
for i in range(num - 1):
temp += unit
return temp
def reverse(self, end=None, start=0, cut=False):
temp = copy(self)
if end is None:
temp.notes = temp.notes[::-1]
temp.interval = temp.interval[::-1]
else:
if cut:
temp.notes = temp.notes[start:end][::-1]
temp.interval = temp.interval[start:end][::-1]
else:
temp.notes = temp.notes[:start] + temp.notes[
start:end][::-1] + temp.notes[end:]
temp.interval = temp.interval[:start] + temp.interval[
start:end][::-1] + temp.interval[end:]
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 [INTERVAL[x % octave][0] for x in result]
def add(self,
note1=None,
interval=None,
mode='tail',
start=0,
duration=0.25):
temp = copy(self)
if type(note1) == int:
temp += temp[1].up(note1)
return temp
if mode == 'tail':
if interval is not None:
return temp + degree_to_note(temp.notes[0].degree + interval)
else:
return temp + note1
elif mode == 'head':
note1 = copy(note1)
if isinstance(note1, chord):
inter = note1.interval
else:
if isinstance(note1, str):
note1 = chord([toNote(note1, duration=duration)])
elif isinstance(note1, note):
note1 = chord([note1])
elif isinstance(note1, list):
note1 = chord(note1)
if isinstance(interval, int):
inter = [interval for i in range(len(note1))]
else:
inter = interval if interval else [0]
# calculate the absolute distances of all of the notes of the chord to add and self,
# and then sort them, make differences between each two distances
distance = []
if start != 0:
note1.notes.insert(0, temp.notes[0])
note1.interval.insert(0, start)
for i in range(len(temp.notes)):
dis = sum(temp.interval[:i])
distance.append((dis, temp.notes[i]))
for j in range(len(note1.notes)):
dis = sum(inter[:j])
if start == 0 or j != 0:
distance.append((dis, note1.notes[j]))
distance = sorted(distance, key=lambda x: x[0])
newinterval = [
distance[x][0] - distance[x - 1][0]
for x in range(1, len(distance))
] + [distance[-1][1].duration]
newnotes = [distance[x][1] for x in range(len(distance))]
newinterval = [int(t) if int(t) == t else t for t in newinterval]
return chord(newnotes, interval=newinterval)
elif mode == 'after':
if self.interval[-1] == 0:
times = self.notes[-1].duration
return self.period(times) + note1
else:
return self + note1
def inversion(self, num=1):
# return chord's [first, second, ... (num)] inversion chord
if num not in range(1, len(self.notes)):
return 'the number of inversion is out of range of the notes in this chord'
else:
temp = copy(self)
for i in range(num):
temp.notes.append(temp.notes.pop(0) + octave)
return temp
def inv(self, num=1):
temp = self.copy()
if type(num) == str:
return self @ num
if num not in range(1, len(self.notes)):
return 'the number of inversion is out of range of the notes in this chord'
while temp[num + 1].degree >= temp[num].degree:
temp[num + 1] = temp[num + 1].down(octave)
temp.insert(1, temp.pop(num + 1))
return temp
def sort(self, indlist, rootpitch=None):
temp = self.copy()
names = [temp[i].name for i in indlist]
if rootpitch is None:
rootpitch = temp[indlist[0]].num
elif rootpitch == 'same':
rootpitch = temp[1].num
new_interval = [temp.interval[i - 1] for i in indlist]
new_duration = [temp[i].duration for i in indlist]
return chord(names,
rootpitch=rootpitch,
interval=new_interval,
duration=new_duration)
def voicing(self, rootpitch=None):
if rootpitch is None:
rootpitch = self[1].num
duration, interval = [i.duration for i in self.notes], self.interval
notenames = self.names()
return [
chord(x, rootpitch=rootpitch).set(duration, interval)
for x in perm(notenames)
]
def inversion_highest(self, ind):
if ind in range(1, len(self)):
temp = self.copy()
while temp[ind].degree < temp[-1].degree:
temp[ind] = temp[ind].up(octave)
temp.notes.append(temp.notes.pop(ind - 1))
return temp
def inoctave(self):
temp = self.copy()
root = self[1].degree
for i in range(2, len(temp) + 1):
while temp[i].degree - root > octave:
temp[i] = temp[i].down(octave)
temp.notes.sort(key=lambda x: x.degree)
return temp
def on(self, root, duration=0.25, interval=None, each=0):
temp = copy(self)
if each == 0:
if type(root) == chord:
return root + self
if type(root) != note:
root = toNote(root)
root.duration = duration
temp.notes.insert(0, root)
if interval is not None:
temp.interval.insert(0, interval)
else:
temp.interval.insert(0, self.interval[0])
return temp
else:
if type(root) == chord:
root = list(root)
else:
root = [toNote(i) for i in root]
return [self.on(x, duration, interval) for x in root]
def up(self, unit=1, ind=None, ind2=None):
temp = copy(self)
if type(unit) != int:
for k in range(len(unit)):
temp.notes[k] = temp.notes[k].up(unit[k])
return temp
if type(ind) != int and ind is not None:
temp.notes = [
temp.notes[i - 1].up(unit) if i in ind else temp.notes[i - 1]
for i in range(1,
len(temp.notes) + 1)
]
return temp
if ind2 is None:
if ind is None:
temp.notes = [
degree_to_note(temp[i].degree + unit, temp[i].duration,
temp[i].volume)
for i in range(1,
len(temp) + 1)
]
else:
temp[ind] = degree_to_note(temp[ind].degree + unit,
temp[ind].duration,
temp[ind].volume)
else:
temp.notes = [
degree_to_note(temp[i].degree + unit, temp[i].duration,
temp[i].volume) if ind <= i < ind2 else temp[i]
for i in range(1,
len(temp) + 1)
]
return temp
def down(self, unit=1, ind=None, ind2=None):
if type(unit) != int:
unit = [-i for i in unit]
return self.up(unit, ind, ind2)
return self.up(-unit, ind, ind2)
def drop(self, ind, mode=0):
# if mode is 0, then drop notes by index,
# if mode is 1, then drop notes by the names of notes,
# if mode is 2, then drop notes by only name (ignoring pitch)
if mode == 0:
if type(ind) == list:
return self.drop([self[i] for i in ind], mode=1)
else:
return self.drop(self[ind], mode=1)
elif mode == 1:
temp = copy(self)
if type(ind) == list:
ind = [toNote(x) if type(x) != note else x for x in ind]
for each in ind:
if each in temp.notes:
current = temp.notes.index(each)
del temp.notes[current]
del temp.interval[current]
else:
if type(ind) != note:
ind = toNote(ind)
if ind in temp.notes:
current = temp.notes.index(ind)
del temp.notes[current]
del temp.interval[current]
elif mode == 2:
temp = copy(self)
if type(ind) == list:
for each in ind:
self_notenames = temp.names()
if each in self_notenames:
current = self_notenames.index(each)
del temp.notes[current]
del temp.interval[current]
else:
self_notenames = temp.names()
if ind in self_notenames:
current = self_notenames.index(ind)
del temp.notes[current]
del temp.interval[current]
return temp
omit = drop
def copy(self):
return copy(self)
def __setitem__(self, ind, value):
if type(value) == str:
value = toNote(value)
self.notes[ind - 1] = value
def __delitem__(self, ind):
del self.notes[ind - 1]
def index(self, value):
if type(value) == str:
try:
value = toNote(value)
if value not in self:
return -1
return self.notes.index(value) + 1
except:
note_names = self.names()
if value not in note_names:
return -1
return note_names.index(value) + 1
def remove(self, note1):
if type(note1) == str:
note1 = toNote(note1)
if note1 in self:
inds = self.notes.index(note1)
self.notes.remove(note1)
del self.interval[inds]
def append(self, value, interval=None):
if type(value) == str:
value = toNote(value)
self.notes.append(value)
if interval is None:
interval = self.interval[-1]
self.interval.append(interval)
def delete(self, ind):
del self.notes[ind - 1]
del self.interval[ind - 1]
def insert(self, ind, value, interval=None):
if type(value) == str:
value = toNote(value)
self.notes.insert(ind - 1, value)
if interval is None:
interval = self.interval[-1]
self.interval.insert(ind - 1, interval)
def drops(self, ind):
temp = self.copy()
dropnote = temp.notes.pop(-ind).down(octave)
dropinterval = temp.interval.pop(-ind)
temp.notes.insert(0, dropnote)
temp.interval.insert(0, dropinterval)
return temp
def period(self, length, ind=-1):
temp = copy(self)
temp.interval[ind] += length
return temp
def modulation(self, old_scale, new_scale):
# change notes (including both of melody and chords) in the given piece
# of music from a given scale to another given scale, and return
# the new changing piece of music.
temp = copy(self)
number = len(new_scale.getScale())
transdict = {
old_scale[i].name: new_scale[i].name
for i in range(1, number + 1)
}
for k in range(len(temp)):
if temp[k + 1].name in transdict:
current = temp.notes[k]
temp.notes[k] = toNote(
f'{transdict[current.name]}{current.num}',
current.duration, current.volume)
return temp
def __getitem__(self, ind):
if isinstance(ind, slice):
start = ind.start if ind.start is None else (
ind.start - 1 if ind.start > 0 else len(self) + ind.start)
stop = ind.stop if ind.stop is None else (
ind.stop - 1 if ind.stop > 0 else len(self) + ind.stop)
return self.__getslice__(start, stop)
temp = copy(self)
if ind != 0:
if ind > 0:
ind -= 1
return temp.notes[ind]
def __iter__(self):
for i in self.notes:
yield i
def __getslice__(self, i, j):
temp = copy(self)
return chord(temp.notes[i:j], interval=temp.interval[i:j])
def __len__(self):
return len(self.notes)
def setvolume(self, ind, vol):
if type(ind) == int:
self.notes[ind - 1].setvolume(vol)
elif type(ind) == list:
if type(vol) == list:
for i in range(len(ind)):
current = ind[i]
self.notes[current - 1].setvolume(vol[i])
elif type(vol) == int:
for i in range(len(ind)):
current = ind[i]
self.notes[current - 1].setvolume(vol)
elif ind == 'all':
if type(vol) == list:
for i in range(len(vol)):
self.notes[i].setvolume(vol[i])
elif type(vol) == int:
for each in self.notes:
each.setvolume(vol)
def move(self, x):
# x could be a dict or list of (index, move_steps)
temp = self.copy()
if type(x) == dict:
for i in x:
temp.notes[i - 1] = temp.notes[i - 1].up(x[i])
return temp
if type(x) == list:
for i in x:
temp.notes[i[0] - 1] = temp.notes[i[0] - 1].up(i[1])
return temp
def extend(self,
distance,
duration=None,
intervals=None,
volume=None,
modes='tail'):
# extend a chord with notes has one or multiple given distances
# with the first note in the chord
temp = copy(self)
if duration is None:
duration = temp[-1].duration
if intervals is None:
intervals = temp.interval[-1]
if volume is None:
volume = temp[-1].volume
if isinstance(distance, int):
temp = temp.add(degree_to_note(temp[1].degree + distance, duration,
volume),
mode=modes)
temp.interval[-1] = intervals
else:
if not isinstance(duration, list):
duration = [duration for x in range(len(distance))]
if not isinstance(intervals, list):
intervals = [intervals for y in range(len(distance))]
if not isinstance(volume, list):
volume = [volume for y in range(len(distance))]
for k in range(len(distance)):
temp = temp.add(degree_to_note(temp[1].degree + distance[k],
duration[k], volume[k]),
mode=modes)
temp.interval[-1] = intervals[k]
return temp
def play(self, *args, **kwargs):
import musicpy
musicpy.play(self, *args, **kwargs)
def split_melody(self, *args, **kwargs):
import musicpy
return musicpy.split_melody(self, *args, **kwargs)
def split_chord(self, *args, **kwargs):
import musicpy
return musicpy.split_chord(self, *args, **kwargs)
def split_all(self, *args, **kwargs):
import musicpy
return musicpy.split_all(self, *args, **kwargs)
def detect_scale(self, *args, **kwargs):
import musicpy
return musicpy.detect_scale(self, *args, **kwargs)
def chord_analysis(self, *args, **kwargs):
import musicpy
return musicpy.chord_analysis(self, *args, **kwargs)
def clear_at(self, duration=0, interval=None, volume=None):
temp = copy(self)
i = 1
while i <= len(temp):
current = temp[i]
if duration is not None:
if current.duration <= duration:
temp.delete(i)
continue
if interval is not None:
if temp.interval[i] <= interval:
temp.delete(i)
continue
if volume is not None:
if current.volume <= volume:
temp.delete(i)
continue
i += 1
return temp
def retrograde(self, rhythm=False):
result = self.reverse()
if not rhythm: