Differences From Artifact [b052479df1466350]:
- Executable file
cfg.py
- 2011-09-22 17:44:23 - part of checkin [1560bc634c] on branch feature/abc - Added back in section ordering support (user: brian) [annotate]
To Artifact [fb69d166468e17a9]:
- Executable file
cfg.py
- 2011-09-22 18:04:07 - part of checkin [b2ce9522e9] on branch feature/abc - Fixed bug that prevented more than two sections from working properly (user: brian) [annotate]
46 } 46 }
47 print '''f1 0 512 10 1 47 print '''f1 0 512 10 1
48 f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08 48 f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08
49 f3 0 1025 10 1 49 f3 0 1025 10 1
50 t 0 60 50 t 0 60
51 ''' 51 '''
52 52
53 start = 0 | 53 section_start = 0
54 for section in ["verse1", "verse2"]: 54 for section in ["verse1", "verse2"]:
55 section = composition[section] 55 section = composition[section]
56 # for subsection in section 56 # for subsection in section
57 instrs = [] 57 instrs = []
58 for instr in section.values(): 58 for instr in section.values():
59 sync = None 59 sync = None
60 max_time = instr["duration"] 60 max_time = instr["duration"]
61 instr_score = render_instr(instr, sync, max_time) 61 instr_score = render_instr(instr, sync, max_time)
62 instrs.append(instr_score) 62 instrs.append(instr_score)
63 for line in generate_csound_score(instr_score, instr["score_line"], | 63 for line in generate_csound_score(instr_score, instr["score_line"],
64 print line 64 print line
65 longest_score = max(instrs, key=lambda i: score_len(i)) 65 longest_score = max(instrs, key=lambda i: score_len(i))
66 start = score_len(longest_score) | 66 section_start += score_len(longest_score)
67 67
68 68
69 69
70 def render_instr(instr, sync, max_time): 70 def render_instr(instr, sync, max_time):
71 grammars = instr["grammars"] 71 grammars = instr["grammars"]
72 for g in instr["grammars"]: 72 for g in instr["grammars"]:
73 for i in range(len(grammars[g])): 73 for i in range(len(grammars[g])):
................................................................................................................................................................................
74 grammars[g][i] = parse.parse(grammars[g][i]) 74 grammars[g][i] = parse.parse(grammars[g][i])
75 init_node = random.choice(instr["grammars"].keys()) 75 init_node = random.choice(instr["grammars"].keys())
76 init_score = random.choice(instr["grammars"][init_node]) 76 init_score = random.choice(instr["grammars"][init_node])
77 score = init_score 77 score = init_score
78 while True: 78 while True:
79 time_remaining = max_time - score_len(score) 79 time_remaining = max_time - score_len(score)
80 try: 80 try:
81 score = choose_node(score, grammars, time_remaining) | 81 score = choose_node(score, grammars, time_remaining, sync)
82 except ValueError: 82 except ValueError:
83 break 83 break
84 return score 84 return score
85 85
86 86
87 def choose_node(score, grammars, time_remaining): | 87 def choose_node(score, grammars, time_remaining, sync):
88 if time_remaining <= 0: 88 if time_remaining <= 0:
89 raise ValueError("No time remaining in the score") 89 raise ValueError("No time remaining in the score")
90 node = None 90 node = None
91 node_index = None 91 node_index = None
92 for item in range(len(score)): 92 for item in range(len(score)):
93 if isinstance(score[item], tree.Tree): 93 if isinstance(score[item], tree.Tree):
94 node = score[item].name 94 node = score[item].name
................................................................................................................................................................................
97 raise ValueError("No more nodes to fill in") 97 raise ValueError("No more nodes to fill in")
98 options = [] 98 options = []
99 for g in range(len(grammars[node])): 99 for g in range(len(grammars[node])):
100 if score_len(grammars[node][g]) <= time_remaining: 100 if score_len(grammars[node][g]) <= time_remaining:
101 options.append(grammars[node][g]) 101 options.append(grammars[node][g])
102 if len(options) == 0: 102 if len(options) == 0:
103 raise ValueError("No available grammars that will fit in the score") 103 raise ValueError("No available grammars that will fit in the score")
> 104 if sync:
> 105
> 106 else:
104 phrase = random.choice(options) | 107 phrase = random.choice(options)
105 score = score[:node_index-1] + phrase + score[node_index+1:] 108 score = score[:node_index-1] + phrase + score[node_index+1:]
106 return score 109 return score
107 110
108 <
109 <
110 <
111 <
112 <
113 # movement_start = 0 <
114 # progression = "verse1 verse2" <
115 # for comp_name in progression.split(): <
116 # # We need an arbitrary grammar from this instrument to start the score <
117 # max_instr = 0 <
118 # for instr_name, instr in composition[comp_name].iteritems(): <
119 # for grammar in instr["grammars"]: <
120 # for g in range(len(instr["grammars"][grammar])): <
121 # instr["grammars"][grammar][g] = parse.parse(instr["grammars <
122 # g = random.choice(instr["grammars"].keys()) <
123 # ins_score = random.choice(instr["grammars"][g]) <
124 ## ins_score = instr["grammars"][g] <
125 # score_complete = False <
126 # while score_complete is False: <
127 # if score_len(ins_score) >= instr["duration"]: <
128 # score_complete = True <
129 # break <
130 # for i in range(len(ins_score)): <
131 # if isinstance(ins_score[i], tree.Tree): <
132 # unrolled_score = select_node(instr["grammars"][ins_scor <
133 # new_score = ins_score[:i-1] + unrolled_score + ins_scor <
134 # ins_score = new_score <
135 # if i == len(ins_score): <
136 # score_complete = True <
137 # break <
138 # <
139 # <
140 # ins_score = [n for n in ins_score if not isinstance(n, tree.Tree)] <
141 # composition[comp_name][instr_name]["score"] = ins_score <
142 # <
143 # if score_len(ins_score) > max_instr: <
144 # max_instr = score_len(ins_score) <
145 # for line in generate_csound_score(composition[comp_name][instr_name <
146 # print line <
147 # <
148 # movement_start += max_instr <
149 <
150 111
151 def score_len(score): 112 def score_len(score):
152 total = 0 113 total = 0
153 for n in score: 114 for n in score:
154 if not isinstance(n, tree.Tree): 115 if not isinstance(n, tree.Tree):
155 total += n.duration 116 total += n.duration
156 return total 117 return total
157 118
158 def select_node(grammar): <
159 return random.choice(grammar) <
160 <
161 <
162 def generate_score(score, grammars): <
163 while 1: <
164 found_substitution = False <
165 for key,value in grammars.iteritems(): <
166 if score.find(key) != -1: <
167 found_substitution = True <
168 while score.find(key) != -1: <
169 score = score.replace(key, random.choice(grammars[key]), 1) <
170 # print scoe <
171 if len(score.split()) > 2000: <
172 for k in grammars.keys(): <
173 score = score.replace(k, "") <
174 return score <
175 if found_substitution is False: <
176 break <
177 return score <
178 <
179 119
180 def generate_csound_score(score, score_line, t): 120 def generate_csound_score(score, score_line, t):
181 csound_note_values = { 121 csound_note_values = {
182 "C": "00", 122 "C": "00",
183 "C#": "01", 123 "C#": "01",
184 "D": "02", 124 "D": "02",
185 "D#": "03", 125 "D#": "03",