-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmutators.py
85 lines (72 loc) · 1.82 KB
/
mutators.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
from mingus.core import *
from mingus.containers import *
import random
def one_note_mutator(track):
"""
This function will pick up one random note
from the track and replace it with a another note
which is inturn created by transposing it, or
similar
"""
if len(track.bars) ==0:
return -1
notes =[]
while len(notes)==0:
#Count the no of notes in the damn thing
#Pick up a random bar
bar_no = random.randint(0,len(track.bars)-1)
#Pick up a random note in that bar
note_no = random.randint(0,len(track.bars[bar_no])-1)
#Select the note and mutate it
beat, duration, notes = track.bars[bar_no][note_no]
#print bar_no
#print note_no
#print notes[0].name
#mutate note
if random.randint(0,1)%2==0:
track.bars[bar_no][note_no][2][0].transpose("3")
else:
track.bars[bar_no][note_no][2][0].transpose("-3")
#print track.bars[bar_no][note_no][2][0].name
return track
def transpose_bar_mutator(track):
"""
Find a random bar, transpose it up by a
perfect fifth
"""
bar=[]
while len(bar)==0:
bar_no=random.randint(0,len(track.bars)-1)
bar = track.bars[bar_no]
#print bar_no
#transpose the bar up by a perfect fifth
if random.randint(0,1)==0:
track.bars[bar_no].transpose("5")
else:
track.bars[bar_no].transpose("-5")
return track
def permute_duration_mutator(track):
"""
Permute the durations in the melody.
I will do this in a bar, so as to preserve the
sum of durations =1 property
"""
track2=Track()
for bar in track.bars:
#randomly pick up a duration and
x1=[]
i=0
for beat,duration, notes in bar:
x1.append(i)
i+=1
x2 = x1[:]
random.shuffle(x2)
i=0
bar_ = Bar()
for beat,duration, notes in bar:
bar_.set_meter(bar.meter)
# print "---" + str(x2[i]) + "--" + str(bar[x2[i]][1])
bar_.place_notes(notes, bar[x2[i]][1])
i+=1
track2.add_bar(bar_)
return track2