-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposer.py
265 lines (189 loc) · 7.99 KB
/
composer.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
import suffix_tree
from music_notes import MusicSequence, Note
import os
import markov
import random
from fractions import Fraction
import json
import suffix_tree
from math import pow
from constants import NOTES
import constants
class Composer:
def __init__(self, files):
self.train(files)
def train(self, files):
self.order = 1 # first order markov chain....do not change from 1
self.chains = [markov.MarkovChain()]
self.seqs = []
# parse music
for name,file in files.iteritems():
fp = open(file, "r")
self.seqs.append(MusicSequence(file))
fp.close()
patterns = [] # uncompressed patterns
compressed = []
# extract patterns
for seq in self.seqs:
abs_str = seq.to_abs_value_string()
stree = suffix_tree.SuffixTree(abs_str)
tree_patterns = stree.get_patterns(3)
for p in tree_patterns:
durations = []
for note in seq[p[0]-1:p[0]+p[1]+1]:
durations.append(note.duration)
try:
patterns.append({"seq": seq, "start_index": p[0],"depth": p[1],"start_note": seq[p[0]],"end_note": seq[p[0]+p[1]+1], "durations": durations})
except IndexError:
patterns.append({"seq": seq, "start_index": p[0],"depth": p[1],"start_note": seq[p[0]],"end_note": seq[p[0]+p[1]], "durations": durations})
patterns = self.compress_patterns(patterns)
for seq in self.seqs:
self.add_to_markov_chain(self.chains[0], seq, 1)
for pattern in patterns:
self.insert_pattern(pattern)
def insert_pattern(self, pattern):
# create state from pattern
seq = pattern["seq"]
key = []
for note in seq[pattern["start_index"]:pattern["start_index"]+pattern["depth"]]:
key.append((note.pitch, note.octave, note.duration))
key = tuple(key)
try:
self.chains[0][key]
except KeyError:
self.chains[0][key] = State()
# get ending note
# add ending note to pattern state
try:
end_note = seq[pattern["start_index"]+pattern["depth"]]
self.chains[0][key].add_state((end_note.pitch, end_note.octave, end_note.duration))
except KeyError:
pass
# get prior note
# find prior note in chain
# weight pattern and add to prior note state
# add state to chain
try:
prior_note = seq[pattern["start_index"]-1]
for i in range(len(key)):
self.chains[0][(prior_note.pitch, prior_note.octave, prior_note.duration)].add_state(key)
except KeyError:
pass
def compress_patterns(self, patterns):
comps = {}
deletions = []
for seq in self.seqs:
abs_str = seq.to_abs_value_string()
for x in [p for p in patterns if p["seq"] == seq]:
try:
comps[abs_str[x["start_index"]:x["start_index"]+x["depth"]]] +=1
except KeyError:
comps[abs_str[x["start_index"]:x["start_index"]+x["depth"]]] = 1
for k,c in comps.iteritems():
if c < 2 or k=='':
deletions.append(k)
for k in deletions:
del comps[k]
final = []
for seq in self.seqs:
abs_str = seq.to_abs_value_string()
final += [p for p in patterns if p["seq"] == seq and abs_str[p["start_index"]:p["start_index"]+p["depth"]] in comps.keys()]
return final
def add_to_markov_chain(self, chain, seq, order):
if order > len(seq):
order = len(seq)
for i in range(0, len(seq) - (order + 1)):
pitches = []
octaves = []
durations = []
for j in range(0, order):
pitches.append(seq[i+j].pitch)
octaves.append(seq[i+j].octave)
durations.append(seq[i+j].duration)
next_pitch = seq[i+order].pitch
next_octave = seq[i+order].octave
next_duration = seq[i+order].duration
key = []
for k in range(0,len(pitches)):
key.append((pitches[k],octaves[k],durations[k]))
key = tuple(key)
try:
chain[key[0]]
except KeyError:
chain[key[0]] = State()
chain[key[0]].add_state((next_pitch, next_octave, next_duration))
return chain
def write(self, number_of_notes):
# get rand starting note or starting pattern (random state)
def rand_start_state():
seq = self.seqs[random.randint(0, len(self.seqs)-1)]
note = seq[0]
key = (note.pitch, note.octave, note.duration)
return key
def next_note(order):
order -= 1
new_state = self.chains[order].next()
try:
new_state[0]
return new_state
except TypeError:
print new_state
exit("fail")
return next_note(order)
start = rand_start_state()
states = [tuple([tuple([start[0],start[1],start[2]])])]
piece = []
self.chains[0].set_current_state(states[0][0])
# create beginning of sentence
for note in states[len(states) -1]:
piece.append(list(note))
piece = piece[1:]
leng = 0
while True:
new_note = next_note(self.order)
self.chains[0].set_current_state(new_note)
piece.append(list(new_note))
if type(new_note[0]) == type("string") or type(new_note[0]) == type(u'unicode'):
leng += 1
else:
leng += len(new_note)
if leng > number_of_notes:
break
# flatten piece
flattened = []
for notes in piece:
if type(notes[0]) == type("string") or type(notes[0]) == type(u'unicode'):
flattened.append(notes)
else:
print type(notes[0])
for note in notes:
flattened.append(list(note))
return flattened
def measurify(self, piece, top, bot):
multiplier = bot / 4
for note in piece:
note[2] *= multiplier
leftover = None
measures = []
count = Fraction(0)
i = 0
for note in piece:
try:
measures[i]
except IndexError:
measures.append([])
if leftover != None:
measures[i].append((leftover[0], leftover[1], float(leftover[2].numerator) / leftover[2].denominator))
count += leftover[2]
count += note[2]
if count > Fraction(top / bot):
leftover = (note[0], note[1], count % 1)
measures[i].append((note[0], note[1], float((note[2] - leftover[2]).numerator) / (note[2] - leftover[2]).denominator))
count = 1
else:
measures[i].append((note[0], note[1], float(note[2].numerator) / note[2].denominator))
leftover = None
count = count % 1
if count == 0:
i += 1
return measures