-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_measures.py
538 lines (464 loc) · 20.4 KB
/
generate_measures.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
from email import message
import io
import os
import sys
import argparse
import json
import edges as edges
import cytoscape_graph as cg
from math import isnan
import networkx as nx
import re
from pydash import flatten_deep, flatten, flat_map
from numpy import random, linalg, min, max, array, isnan, asarray
from song_library import SongPart
# parser = argparse.ArgumentParser(
# description='generate measure files from a graph')
# parser.add_argument(
# '--graph', metavar='string', nargs='+', type='string',
# help='a graphml file to load the notes from')
# args = parser.parse_args()
def load_from_json():
file = open("music_rhythm_edges.json")
myEdges = edges.edges_from_dict(json.loads(file.read()))
g = nx.DiGraph()
# Loop through edge JSON objects to get the data
for edge in enumerate(myEdges):
(_id, edge_data) = edge
d = edge_data.data
# Add the edges from the JSON file
g.add_edge(u_of_edge=d.from_label, v_of_edge=d.to_label,
weight=d.weight, source=d.source, target=d.target)
# Regular expression to split out the beats and duration
x = re.compile('M1_4\/4_V1_B([0-9]\.[0-9]+)_(.*)')
for idx, attrs in g.nodes.items():
# Find all matches in the label string
fa = x.findall(idx)
# Unbox the beat and duration from the matches
beat, duration = fa[0]
# Set the custom attributes in the node
g.nodes[idx]['beat'] = beat
g.nodes[idx]['duration'] = duration
# Assign a random weight because they are all 0 for the moment
for u, v in enumerate(g.edges.data(True, None, None)):
# Unbox the source and target from the edge
source, target, attrs = v
# Randomly assign a floating point value fothe the weight
g[source][target]['weight'] = random.uniform(-1, 1)
# Get the list of outgoing edges
for u in enumerate(g.nodes.keys()):
_id, label = u
successors = g.successors(label)
for s in successors:
ed = g[label][s]
# Write out to a GraphML file for other tools
nx.write_graphml(g, 'g.graphml')
# Tidy up the file handle
file.close()
def load_from_cytoscape():
file = open("js_utils/graph.json")
graph = cg.cytoscape_graph_from_dict(json.loads(file.read()))
g = nx.DiGraph()
for node in graph.elements.nodes:
g.add_node(node.data.label,
id=node.data.id,
beat=node.data.beat,
duration=node.data.duration,
y=node.position.y,
x=node.position.x)
for edge in graph.elements.edges:
g.add_edge(u_of_edge=edge.data.source_label,
v_of_edge=edge.data.target_label,
weight=edge.data.weight,
tie=edge.data.tie)
# Write out to a GraphML file for other tools
nx.write_graphml(g, 'g.graphml')
file.close()
def generate_measures(measures_len):
g: nx.DiGraph = nx.read_graphml('g.graphml')
# Build a spot for multiple measures
song_part = []
for i in range(measures_len):
# Holder for measures
measure = []
# start label is M1_4/4_V1_B0.0_NILS
def randomPath(label):
# Skip the NILS measure, it is the origin of the graph
if(label != 'M1_4/4_V1_B0.0_NILS'):
# look up the duration label and get a number
dur = len_beats(g.nodes[label]['duration'])
flat_dur = flatten_deep(array(dur, dtype=object).flat)
# Add the node to the measure container
measure.append(flat_dur)
# Get the list of outgoing edges and their weights
ssors = g.successors(label)
# Container of available node choices
labelChoices = []
# Paired container of the random weights
choiceWeights = []
# Loop through each successor (inbound edge)
for ss in ssors:
# Get the weights for the selected successor
nextWeight = g.edges[label, ss]['weight']
# If the weight is -1, don't add the successor
if nextWeight > -0.999:
# Add the node to the values array
labelChoices.append(ss)
# Add the weight to the choices weights
choiceWeights.append(float(nextWeight))
# There has to be more than one choice to randomize
if(len(labelChoices) > 1):
arr = array(choiceWeights, dtype=float)
# Normalize the -1 to 1 range to 0 to 1
def NormalizeData(data):
return (data - min(data)) / (max(data) - min(data))
# Normalize our weights to a range of [0, 1]
normWeights = NormalizeData(arr)
# Rescale the weights to sum to 1
scaledWeights = normWeights / normWeights.sum()
# Print scaledWeights for debugging
# print(scaledWeights)
array_sum = sum(scaledWeights)
array_has_nan = isnan(array_sum)
if array_has_nan:
return '0.'
# Randomly select the next note based on their
# normalized and scaled weights
rr = random.choice(a=labelChoices, size=1,
replace=False, p=scaledWeights)
# Get the label of the first (of one) choice
picked = str(rr[0])
return randomPath(picked)
elif (len(labelChoices) == 1):
# ^ If there are no options, choose the only option
# We want to stop endless loops here
if(labelChoices[0] == 'M1_4/4_V1_B0.0_NILS'):
return
# Recursively call the next choice in the list of edges
return randomPath(labelChoices[0])
else:
# If there are no further nodes, we end here as well
return
# Kick off the navigation with the source node
randomPath('M1_4/4_V1_B0.0_NILS')
# Flatten the list because of triplets
l = flatten_deep(array(measure, dtype=object).flat)
# Do a beats conversion (1 whole note is 4 beats)
multiplied = [element * 4 for element in l]
measureTotal = sum([abs(element) for element in multiplied])
# If the sum is less than 4, append a rest for the difference
if(measureTotal < 4.0):
multiplied.append((4.0 - measureTotal) * -1)
song_part.append(multiplied)
return song_part
def rebalance_graph(
wn_weight=0.1, hn_weight=0.1, qn_weight=0.1, en_weight=0.1, sn_weight=0.1,
wr_weight=0.1, hr_weight=0.1, qr_weight=0.1, er_weight=0.1, sr_weight=0.1,
dhn_weight=0.1, dqn_weight=0.1, den_weight=0.1, dsn_weight=0.1,
dhr_weight=0.1, dqr_weight=0.1, der_weight=0.1, dsr_weight=0.1,
triplets=0.1
):
"""
Rebalance the weights of the music graph
Parameters
----------
wn_weight: float
Whole Note Probability from -1 to +1
wn_weight
hn_weight: float
Half Note Probability from -1 to +1
qn_weight: float
Quarter Note Probability from -1 to +1
en_weight: float
Eighth Note Probability from -1 to +1
sn_weight: float
Sixteenth Note Probability from -1 to +1
wr_weight: float
Whole Rest Probability from -1 to +1
hr_weight: float
Half Rest Probability from -1 to +1
qr_weight: float
Quarter Rest Probability from -1 to +1
er_weight: float
Eighth Rest Probability from -1 to +1
sr_weight: float
Sixteenth Rest Probability from -1 to +1
dhn_weight: float
Dotted Half Note Probability from -1 to +1
dqn_weight: float
Dotted Quarter Note Probability from -1 to +1
den_weight: float
Dotted Eighth Note Probability from -1 to +1
dsn_weight: float
Dotted Sixteenth Note Probability from -1 to +1
dhr_weight: float
Dotted Half Rest Probability from -1 to +1
dqr_weight: float
Dotted Quarter Rest Probability from -1 to +1
der_weight: float
Dotted Eighth Rest Probability from -1 to +1
dsr_weight: float
Dotted Sixteenth Rest Probability from -1 to +1
triplets: float
Triplet Probability from -1 to +1
"""
g: nx.DiGraph = nx.read_graphml('g.graphml')
for u, v, w in g.edges.data('weight'):
dur = g.nodes[v]['duration']
if dur == '1N':
g.edges[u, v]['weight'] = wn_weight
if dur == '1/2N':
g.edges[u, v]['weight'] = hn_weight
if dur == '1/4N':
g.edges[u, v]['weight'] = qn_weight
if dur == '1/8N':
g.edges[u, v]['weight'] = en_weight
if dur == '1/16N':
g.edges[u, v]['weight'] = sn_weight
if dur == '1R':
g.edges[u, v]['weight'] = wr_weight
if dur == '1/2R':
g.edges[u, v]['weight'] = hr_weight
if dur == '1/4R':
g.edges[u, v]['weight'] = qr_weight
if dur == '1/8R':
g.edges[u, v]['weight'] = er_weight
if dur == '1/16R':
g.edges[u, v]['weight'] = sr_weight
if dur == '1/2.N':
g.edges[u, v]['weight'] = dhn_weight
if dur == '1/4.N':
g.edges[u, v]['weight'] = dqn_weight
if dur == '1/8.N':
g.edges[u, v]['weight'] = den_weight
if dur == '1/16.N':
g.edges[u, v]['weight'] = dsn_weight
if dur == '1/2.R':
g.edges[u, v]['weight'] = dhr_weight
if dur == '1/4.R':
g.edges[u, v]['weight'] = dqr_weight
if dur == '1/8.R':
g.edges[u, v]['weight'] = der_weight
if dur == '1/16.R':
g.edges[u, v]['weight'] = dsr_weight
if dur != 0 and 'T' in dur:
if 'R' in dur:
g.edges[u, v]['weight'] = triplets
else:
g.edges[u, v]['weight'] = triplets + 0.001
nx.write_graphml(g, 'g.graphml')
def len_beats(duration):
if(duration == '1N'):
return [1]
if(duration == '1/2N'):
return [(1/2)]
if(duration == '1/2.N'):
return [(1/2)+(1/4)]
if(duration == '1/4N'):
return [(1/4)]
if(duration == '1/4TN'):
return [(((1/4) * 2)/3)]
if(duration == '1/4T+1/4TN'):
return [(((1/4) * 2)/3), (((1/4) * 2)/3)]
if(duration == '1/4.N'):
return [(1/4)+(1/8)]
if(duration == '1/8N'):
return [(1/8)]
if(duration == '1/8.N'):
return [(1/8)+(1/16)]
if(duration == '1/8TN'):
return [((0.125 * 2)/3)]
if(duration == '1/8T+1/8TN'):
return [((0.125 * 2)/3), ((0.125 * 2)/3)]
if(duration == '1/16N'):
return [(1/16)]
if(duration == '1/16.N'):
return [(1/16)+(1/32)]
if(duration == '1R'):
return [-1]
if(duration == '1/2R'):
return [-(1/2)]
if(duration == '1/2.R'):
return [((1/2)+(1/4)) * -1]
if(duration == '1/4R'):
return [-(1/4)]
if(duration == '1/4TR'):
return [(((1/4)*2)/3) * -1]
if(duration == '1/4T+1/4TR'):
return [(((1/4)*2)/3) * -1, (((1/4)*2)/3) * -1]
if(duration == '1/4.R'):
return [((1/4)+(1/8) * -1)]
if(duration == '1/8R'):
return [-(1/8)]
if(duration == '1/8.R'):
return [(((1/8)+(1/16)) * -1)]
if(duration == '1/8TR'):
return [((((1/8) * 2)/3) * -1)]
if(duration == '1/8T+1/8TR'):
return [((((1/8) * 2)/3) * -1), ((((1/8) * 2)/3) * -1)]
if(duration == '1/16R'):
return [(1/16) * -1]
if(duration == '1/16.R'):
return [((1/16)+(1/32) * -1)]
else:
return [0]
if __name__ == '__main__':
load_from_cytoscape()
def write_files(pattern_name: str = 'default'):
if not os.path.exists('s:\\dev\\song_patterns\\' + pattern_name):
os.makedirs('s:\\dev\\song_patterns\\' + pattern_name)
# region Generate files
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\measure_4.json', 'w') as outfile1:
out4 = generate_measures(4)
json.dump(out4, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\verse_4.json', 'w') as outfile1:
out4 = generate_measures(4)
json.dump(out4, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\chorus_4.json', 'w') as outfile1:
out4 = generate_measures(4)
json.dump(out4, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\preverse_4.json', 'w') as outfile1:
out4 = generate_measures(4)
json.dump(out4, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\prechorus_4.json', 'w') as outfile1:
out4 = generate_measures(4)
json.dump(out4, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\measure_8.json', 'w') as outfile1:
out8 = generate_measures(8)
json.dump(out8, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\verse_8.json', 'w') as outfile1:
out8 = generate_measures(8)
json.dump(out8, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\chorus_8.json', 'w') as outfile1:
out8 = generate_measures(8)
json.dump(out8, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\preverse_8.json', 'w') as outfile1:
out8 = generate_measures(8)
json.dump(out8, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\prechorus_8.json', 'w') as outfile1:
out8 = generate_measures(8)
json.dump(out8, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\measure_16.json', 'w') as outfile1:
out16 = generate_measures(16)
json.dump(out16, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\verse_16.json', 'w') as outfile2:
out16 = generate_measures(16)
json.dump(out16, outfile2)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\chorus_16.json', 'w') as outfile2:
out16 = generate_measures(16)
json.dump(out16, outfile2)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\preverse_16.json', 'w') as outfile2:
out16 = generate_measures(16)
json.dump(out16, outfile2)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\prechorus_16.json', 'w') as outfile2:
out16 = generate_measures(16)
json.dump(out16, outfile2)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\measure_32.json', 'w') as outfile1:
out32 = generate_measures(32)
json.dump(out32, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\verse_32.json', 'w') as outfile3:
out64 = generate_measures(32)
json.dump(out64, outfile3)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\chorus_32.json', 'w') as outfile3:
out64 = generate_measures(32)
json.dump(out64, outfile3)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\preverse_32.json', 'w') as outfile3:
out64 = generate_measures(32)
json.dump(out64, outfile3)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\prechorus_32.json', 'w') as outfile3:
out64 = generate_measures(32)
json.dump(out64, outfile3)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\measure_64.json', 'w') as outfile1:
out64 = generate_measures(64)
json.dump(out64, outfile1)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\verse_64.json', 'w') as outfile4:
out64 = generate_measures(64)
json.dump(out64, outfile4)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\chorus_64.json', 'w') as outfile4:
out64 = generate_measures(64)
json.dump(out64, outfile4)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\preverse_64.json', 'w') as outfile4:
out64 = generate_measures(64)
json.dump(out64, outfile4)
with open('s:\\dev\\song_patterns\\' + pattern_name + '\\prechorus_64.json', 'w') as outfile4:
out64 = generate_measures(64)
json.dump(out64, outfile4)
# endregion
rebalance_graph(
wn_weight=-1.0, hn_weight=-1.0, qn_weight=0.51, en_weight=-0.51, sn_weight=-0.91,
wr_weight=-1.0, hr_weight=-1.0, qr_weight=0.52, er_weight=-0.52, sr_weight=-0.92,
dhn_weight=-0.53, dqn_weight=0.253, den_weight=0.53, dsn_weight=-0.53,
dhr_weight=-1.0, dqr_weight=-0.253, der_weight=0.754, dsr_weight=-0.754,
triplets=-1.0
)
write_files()
rebalance_graph(
wn_weight=-1, hn_weight=-1, qn_weight=0.51, en_weight=-0.51, sn_weight=-0.91,
wr_weight=-1, hr_weight=-1, qr_weight=0.52, er_weight=-0.52, sr_weight=-0.92,
dhn_weight=-0.53, dqn_weight=0.253, den_weight=0.53, dsn_weight=-0.53,
dhr_weight=-1, dqr_weight=-0.253, der_weight=0.75, dsr_weight=-0.754, triplets=-1
)
write_files('slow')
rebalance_graph(
wn_weight=0.5, hn_weight=0.666, qn_weight=-0.5, en_weight=-0.51, sn_weight=-0.91,
wr_weight=-1, hr_weight=-1, qr_weight=-0.666, er_weight=-0.52, sr_weight=-
0.92, dhn_weight=-0.53, dqn_weight=-0.333, den_weight=0.53, dsn_weight=0.125,
dhr_weight=-1, dqr_weight=-0.253, der_weight=0.75, dsr_weight=-0.754, triplets=-1
)
write_files('quarters')
rebalance_graph(
wn_weight=-1, hn_weight=-1, qn_weight=1, en_weight=-1, sn_weight=-1, wr_weight=-1,
hr_weight=-1, qr_weight=0.333, er_weight=-1, sr_weight=-1, dhn_weight=-1,
dqn_weight=-1, den_weight=-1, dsn_weight=-1, dhr_weight=-1, dqr_weight=-1,
der_weight=-1, dsr_weight=-1, triplets=-1
)
write_files('eighths')
rebalance_graph(
wn_weight=-1, hn_weight=-1, qn_weight=-1, en_weight=1, sn_weight=-1, wr_weight=-1,
hr_weight=-1, qr_weight=-1, er_weight=0.333,
sr_weight=-1, dhn_weight=-1, dqn_weight=-1, den_weight=-1, dsn_weight=-1,
dhr_weight=-1, dqr_weight=-1, der_weight=-1, dsr_weight=-1, triplets=-1
)
write_files('sixteenths')
rebalance_graph(
wn_weight=-1, hn_weight=-1, qn_weight=-1, en_weight=-1, sn_weight=1, wr_weight=-1,
hr_weight=-1, qr_weight=-1, er_weight=-1,
sr_weight=0.333, dhn_weight=-1, dqn_weight=-1, den_weight=-1, dsn_weight=-1,
dhr_weight=-1, dqr_weight=-1, der_weight=-1, dsr_weight=-1, triplets=-1
)
write_files('8_and_16')
rebalance_graph(
wn_weight=-1, hn_weight=-1, qn_weight=0.666, en_weight=0.333, sn_weight=-1, wr_weight=-1,
hr_weight=-1, qr_weight=-1, er_weight=-0.666,
sr_weight=-0.666, dhn_weight=-1, dqn_weight=-1, den_weight=-1, dsn_weight=-1,
dhr_weight=-1, dqr_weight=-1, der_weight=-1, dsr_weight=-1, triplets=-1
)
write_files('4_and_8')
rebalance_graph(
wn_weight=-1, hn_weight=-1, qn_weight=1, en_weight=1, sn_weight=-1, wr_weight=-1,
hr_weight=-1, qr_weight=0.333, er_weight=0.333,
sr_weight=-1, dhn_weight=-1, dqn_weight=-1, den_weight=-1, dsn_weight=-1,
dhr_weight=-1, dqr_weight=-1, der_weight=-1, dsr_weight=-1, triplets=-1
)
write_files('moar_dots')
rebalance_graph(
wn_weight=-1, hn_weight=-1, qn_weight=-1, en_weight=-1, sn_weight=-1, wr_weight=-1,
hr_weight=-1, qr_weight=-1, er_weight=-1,
sr_weight=-1, dhn_weight=-1, dqn_weight=-1, den_weight=1, dsn_weight=1,
dhr_weight=-1, dqr_weight=-1, der_weight=0.25, dsr_weight=0.25, triplets=-1
)
write_files('moar_dots2')
rebalance_graph(
wn_weight=-1, hn_weight=-1, qn_weight=-1, en_weight=-1, sn_weight=-1, wr_weight=-1,
hr_weight=-1, qr_weight=-1, er_weight=-1, sr_weight=-1,
dhn_weight=-0.25, dqn_weight=1, den_weight=1, dsn_weight=-0.25, dhr_weight=-0.25,
dqr_weight=0.25, der_weight=0.25, dsr_weight=-0.25, triplets=-1
)
write_files('fuzzy_dots')
rebalance_graph(
wn_weight=0.5, hn_weight=0.666, qn_weight=0.75, en_weight=0.125, sn_weight=0.125,
wr_weight=-0.8, hr_weight=-0.333, qr_weight=-0.333, er_weight=0.333,
sr_weight=0.25, dhn_weight=-1, dqn_weight=-1, den_weight=-1, dsn_weight=-1,
dhr_weight=-1, dqr_weight=-1, der_weight=-1, dsr_weight=-1, triplets=-1
)
write_files('fuzzy')