Differences From Artifact [eefff49c8fc33785]:
- Executable file
cfg.py
- 2011-09-15 19:19:45 - part of checkin [a9b80ad75f] on branch feature/abc - Movements work again (user: brian) [annotate]
To Artifact [77ab4fec9becbad0]:
- Executable file
cfg.py
- 2011-09-22 17:04:24 - part of checkin [3cac4f013d] on branch feature/abc - Finally broke the program out into functions. It's now much cleaner and easier to underntand. (user: brian) [annotate]
9 import parse 9 import parse
10 10
11 import tree 11 import tree
12 12
13 random.seed(time.time()) 13 random.seed(time.time())
14 14
15 def main(): 15 def main():
16 key = "A" <
17 bps = 60/60 <
18 tempo = 1/bps <
19 max_duration = 1 <
20 <
21 composition = { 16 composition = {
22 "verse1": { 17 "verse1": {
23 "melody": { # Instrument 'melody' 18 "melody": { # Instrument 'melody'
24 "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 19 "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s
25 "octave": 8, 20 "octave": 8,
26 "duration": 40, | 21 "duration": 10,
27 "grammars": { # Notes for this instrument to use in this piece 22 "grammars": { # Notes for this instrument to use in this piece
28 "u": ["C G/2 G/2 G/2 C B, F' C F C B F (w)"], 23 "u": ["C G/2 G/2 G/2 C B, F' C F C B F (w)"],
29 "w": ['E/4 A/4 D/4 G/4 F/4 F/4 B2 (u)'], | 24 "w": ["E/4 A/4 D/4 G/4 F/4 F/4 B2 (u)"],
30 }, 25 },
31 "score": "u u", <
32 }, 26 },
33 }, 27 },
34 "verse2": { 28 "verse2": {
35 "melody": { # Instrument 'melody' 29 "melody": { # Instrument 'melody'
36 "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 30 "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s
37 "octave": 8, 31 "octave": 8,
38 "duration": 40, | 32 "duration": 10,
39 "grammars": { # Notes for this instrument to use in this piece 33 "grammars": { # Notes for this instrument to use in this piece
40 "u": ["C C C C F/2 F/2 F/2 (u)"], | 34 "u": ["C C C C F/2 F/2 F/2 (u)", "D D G/2 A/2 D D (u)"],
41 }, 35 },
42 "score": "u u", <
43 }, 36 },
44 }, 37 },
45 } 38 }
46 print '''f1 0 512 10 1 | 39 print '''f1 0 512 10 1
47 f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08 | 40 f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08
48 f3 0 1025 10 1 | 41 f3 0 1025 10 1
49 t 0 60 | 42 t 0 60
50 ''' 43 '''
> 44
> 45 start = 0
> 46 for section in composition.values():
> 47 # for subsection in section
> 48 instrs = []
> 49 for instr in section.values():
> 50 sync = None
> 51 max_time = instr["duration"]
> 52 instr_score = render_instr(instr, sync, max_time)
> 53 instrs.append(instr_score)
> 54 for line in generate_csound_score(instr_score, instr["score_line"],
> 55 print line
> 56 longest_score = max(instrs, key=lambda i: score_len(i))
> 57 start = score_len(longest_score)
> 58
> 59
> 60
> 61 def render_instr(instr, sync, max_time):
> 62 grammars = instr["grammars"]
> 63 for g in instr["grammars"]:
> 64 for i in range(len(grammars[g])):
> 65 grammars[g][i] = parse.parse(grammars[g][i])
> 66 init_node = random.choice(instr["grammars"].keys())
> 67 init_score = random.choice(instr["grammars"][init_node])
> 68 score = init_score
> 69 while True:
> 70 time_remaining = max_time - score_len(score)
> 71 try:
> 72 score = choose_node(score, grammars, time_remaining)
> 73 except ValueError:
> 74 break
> 75 return score
> 76
> 77
> 78 def choose_node(score, grammars, time_remaining):
> 79 if time_remaining <= 0:
> 80 raise ValueError("No time remaining in the score")
> 81 node = None
> 82 node_index = None
> 83 for item in range(len(score)):
> 84 if isinstance(score[item], tree.Tree):
> 85 node = score[item].name
> 86 node_index = item
> 87 if node is None:
> 88 raise ValueError("No more nodes to fill in")
> 89 options = []
> 90 for g in range(len(grammars[node])):
> 91 if score_len(grammars[node][g]) <= time_remaining:
> 92 options.append(grammars[node][g])
> 93 if len(options) == 0:
> 94 raise ValueError("No available grammars that will fit in the score")
> 95 phrase = random.choice(options)
> 96 score = score[:node_index-1] + phrase + score[node_index+1:]
> 97 return score
> 98
> 99
> 100
> 101
> 102
> 103
51 movement_start = 0 | 104 # movement_start = 0
52 progression = "verse1 verse2" | 105 # progression = "verse1 verse2"
53 for comp_name in progression.split(): | 106 # for comp_name in progression.split():
54 # We need an arbitrary grammar from this instrument to start the score w | 107 # # We need an arbitrary grammar from this instrument to start the score
55 max_instr = 0 | 108 # max_instr = 0
56 for instr_name, instr in composition[comp_name].iteritems(): | 109 # for instr_name, instr in composition[comp_name].iteritems():
57 for grammar in instr["grammars"]: | 110 # for grammar in instr["grammars"]:
58 for g in range(len(instr["grammars"][grammar])): | 111 # for g in range(len(instr["grammars"][grammar])):
59 instr["grammars"][grammar][g] = parse.parse(instr["grammars" | 112 # instr["grammars"][grammar][g] = parse.parse(instr["grammars
60 g = random.choice(instr["grammars"].keys()) | 113 # g = random.choice(instr["grammars"].keys())
61 ins_score = random.choice(instr["grammars"][g]) | 114 # ins_score = random.choice(instr["grammars"][g])
62 # ins_score = instr["grammars"][g] | 115 ## ins_score = instr["grammars"][g]
63 score_complete = False | 116 # score_complete = False
64 while score_complete is False: | 117 # while score_complete is False:
65 if score_len(ins_score) >= 10: | 118 # if score_len(ins_score) >= instr["duration"]:
66 score_complete = True | 119 # score_complete = True
67 break <
> 120 # break
68 for i in range(len(ins_score)): | 121 # for i in range(len(ins_score)):
69 if isinstance(ins_score[i], tree.Tree): | 122 # if isinstance(ins_score[i], tree.Tree):
70 unrolled_score = select_node(instr["grammars"][ins_score | 123 # unrolled_score = select_node(instr["grammars"][ins_scor
71 new_score = ins_score[:i-1] + unrolled_score + ins_score | 124 # new_score = ins_score[:i-1] + unrolled_score + ins_scor
72 ins_score = new_score | 125 # ins_score = new_score
73 if i == len(ins_score): | 126 # if i == len(ins_score):
74 score_complete = True | 127 # score_complete = True
75 break <
> 128 # break
76 | 129 #
77 | 130 #
78 ins_score = [n for n in ins_score if not isinstance(n, tree.Tree)] | 131 # ins_score = [n for n in ins_score if not isinstance(n, tree.Tree)]
79 composition[comp_name][instr_name]["score"] = ins_score | 132 # composition[comp_name][instr_name]["score"] = ins_score
80 | 133 #
81 if score_len(ins_score) > max_instr: | 134 # if score_len(ins_score) > max_instr:
82 max_instr = score_len(ins_score) | 135 # max_instr = score_len(ins_score)
83 for line in generate_csound_score(composition[comp_name][instr_name] | 136 # for line in generate_csound_score(composition[comp_name][instr_name
84 print line | 137 # print line
85 | 138 #
86 movement_start += max_instr | 139 # movement_start += max_instr
87 140
88 141
89 def score_len(score): 142 def score_len(score):
90 total = 0 143 total = 0
91 for n in score: 144 for n in score:
92 if not isinstance(n, tree.Tree): 145 if not isinstance(n, tree.Tree):
93 total += n.duration 146 total += n.duration
................................................................................................................................................................................
94 return total 147 return total
95 148
96 def select_node(grammar): 149 def select_node(grammar):
97 return random.choice(grammar) 150 return random.choice(grammar)
98 151
99 152
100 def generate_score(score, grammars): 153 def generate_score(score, grammars):
101 pdb.set_trace() <
102 while 1: 154 while 1:
103 found_substitution = False 155 found_substitution = False
104 for key,value in grammars.iteritems(): 156 for key,value in grammars.iteritems():
105 if score.find(key) != -1: 157 if score.find(key) != -1:
106 found_substitution = True 158 found_substitution = True
107 while score.find(key) != -1: 159 while score.find(key) != -1:
108 score = score.replace(key, random.choice(grammars[key]), 1) 160 score = score.replace(key, random.choice(grammars[key]), 1)
................................................................................................................................................................................
136 if isinstance(token, parse.Chord): # Chords 188 if isinstance(token, parse.Chord): # Chords
137 for note in token.chord: 189 for note in token.chord:
138 note = csound_note_values[note] 190 note = csound_note_values[note]
139 csound_score.append(score_line % {"time": t, "octave": token.oct 191 csound_score.append(score_line % {"time": t, "octave": token.oct
140 elif isinstance(token, parse.Note): # Individual notes 192 elif isinstance(token, parse.Note): # Individual notes
141 note = csound_note_values[token.value] 193 note = csound_note_values[token.value]
142 csound_score.append(score_line % {"time": t, "octave": token.octave, 194 csound_score.append(score_line % {"time": t, "octave": token.octave,
> 195 elif isinstance(token, tree.Tree):
> 196 continue
143 t += token.duration 197 t += token.duration
144 return csound_score 198 return csound_score
145 199
146 200
147 if __name__ == "__main__": main() 201 if __name__ == "__main__": main()