-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMusicGene.py
97 lines (75 loc) · 2.36 KB
/
MusicGene.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
import pdb
import random
from Gene import Gene
from mingus.containers import *
from mingus.core import *
from mingus.midi import fluidsynth
from evaluators import *
from crossovers import *
from mutators import *
EXPECTED_CONTOUR = [1,1,2,1]
def convert_to_track(track):
""" Convert our notation of a track to a mingus one """
assert(not isinstance(track, Track))
track_ = Track()
for notes, duration in track:
track_.add_notes(notes, duration)
return track_
class MusicGene(Gene):
def __init__(self, track):
self.track = track
Gene.__init__(self)
self.bachian = False
def __str__(self):
str_ = "MusicGene [B=%d]: "%self.bachian + "fitness " + str(self.fitness) + '\n'
str_ += str(self.track)
return str_
def __repr__(self):
return str(self)
def print_track(self, track):
return str(self.track)
def get_fitness(self):
val = 0
val += rhythm_fluctuation_evaluator(self.track)
val += pitch_class_fluctuation_evaluator(self.track)
val += dissonant_note_evaluator(self.track)
val += numericEvaluation(self.track)
fitness = val
return fitness
def mate(self, other):
children = single_pt_crossover(self, other)
return children
def mutate(self):
algo = random.random()
if algo < 0.2:
child = MusicGene(one_note_mutator(self.track))
else:
child = MusicGene(permute_duration_mutator(self.track))
return child
def play(self):
fluidsynth.play_Track(self.track)
class CompositionGene(Gene):
def __init__(self, track):
self.track = track
Gene.__init__(self)
def __str__(self):
str_ = "CompositionGene: " + "fitness " + str(self.fitness) + '\n'
str_ += str(self.track)
return str_
def __repr__(self):
return str(self)
def print_track(self, track):
return str(self.track)
def get_fitness(self):
val = 0
val += rhythmicContinuity(self.track)
val += rhythmicContour(self.track, EXPECTED_CONTOUR)
fitness = val
return fitness
def mate(self, other):
return []
#return interleaved_single_pt_crossover(self, other)
def mutate(self):
return self
def play(self):
fluidsynth.play_Track(self.track)