-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.py
60 lines (52 loc) · 1.62 KB
/
generator.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
import random
from music21 import *
# Note: Durations are *reversed* i.e., 1 represents a 16th-note
# 2 represents an 8th-note, 4 represents a quarter note, etc.
durations = [1, 2, 4, 8, 16]
switch = {1: 16,
2: 8,
4: 4,
8: 2,
16: 1}
bottom_map = {2: 8,
4: 4,
8: 2}
pitches_nats = ["C", "D", "E", "F", "G", "A", "B",
"c", "d", "e", "f", "g", "a", "b",
"c'", "d'", "e'", "f'", "g'", "a'", "b'",
"c''", "d''", "e''", "f''", "g''", "a''", "b''",
"r", "r"]
accidentals = ["", "", "", "", "", "", "", "#", "#", "##", "-", "-", "--"]
def generate_music_string(time_sig, num_measures=1):
result = "tinyNotation: " + time_sig + " "
time_sig = time_sig.split("/")
beats = []
top = int(time_sig[0])
bottom = int(time_sig[1])
while sum(beats) != top * bottom_map[bottom] * num_measures:
new_beat = random.sample(durations, 1)[0]
if sum(beats) + new_beat > top * bottom_map[bottom] * num_measures:
continue
else:
beats.append(new_beat)
beats = [switch[x] for x in beats]
for beat in beats:
pitch = random.sample(pitches_nats, 1)[0]
acc = random.sample(accidentals, 1)[0]
result += pitch
if pitch != "r":
result += acc
result += str(beat)
result += " "
result_split = result[:-1].split(' ')[:34]
return ' '.join(result_split)
if __name__ == '__main__':
music_strings = []
for i in range(1):
music_str = generate_music_string("4/4")
converter.parse(music_str).write("musicxml.png", fp="./data/image" + str(i))
music_strings.append(music_str)
f = open("music_strings_test.txt","w")
for line in music_strings:
f.write(line + "\n")
f.close()