spiffyscore
Check-in [4f5bc3936f]
Not logged in
Overview
SHA1 Hash:4f5bc3936f3fe8c21b65e90aaf431418ce7ded7e
Date: 2011-10-20 18:37:19
User: brian
Comment:Syncing now works
Timelines: family | ancestors | refactor
Other Links: files | file ages | manifest
Tags And Properties
Changes
hide diffs unified diffs patch

Modified cfg.py from [368187a13fa4afec] to [6468589fdee69e24].

1 #!/usr/bin/env python 1 #!/usr/bin/env python 2 2 3 from __future__ import division 3 from __future__ import division 4 import os | 4 import ipdb 5 import ipdb | 5 import os 6 import random 6 import random 7 import sys 7 import sys 8 import time 8 import time 9 9 10 import parse 10 import parse 11 import topsort 11 import topsort 12 import yaml 12 import yaml ................................................................................................................................................................................ 142 for subsection in ["intro", "body", "outro"]: 142 for subsection in ["intro", "body", "outro"]: 143 try: 143 try: 144 print "; Subsection " + subsection 144 print "; Subsection " + subsection 145 subsection = section[subsection] 145 subsection = section[subsection] 146 146 147 unordered_instrs = [] 147 unordered_instrs = [] 148 for instr in subsection: 148 for instr in subsection: > 149 subsection[instr]["name"] = instr 149 if not "sync" in subsection[instr].keys(): 150 if not "sync" in subsection[instr].keys(): 150 subsection[instr]["sync"] = None 151 subsection[instr]["sync"] = None 151 unordered_instrs.append([subsection[instr]["sync"], instr]) 152 unordered_instrs.append([subsection[instr]["sync"], instr]) 152 ordered_instrs = topsort.topsort(unordered_instrs) 153 ordered_instrs = topsort.topsort(unordered_instrs) 153 ordered_instrs.remove(None) # None used as a placeholder for so 154 ordered_instrs.remove(None) # None used as a placeholder for so 154 155 155 instrs = [] 156 instrs = [] 156 syncs = {} 157 syncs = {} 157 for instr in ordered_instrs: 158 for instr in ordered_instrs: 158 print ";Instrument " + instr 159 print ";Instrument " + instr 159 instr = subsection[instr] 160 instr = subsection[instr] 160 # ipdb.set_trace() < 161 max_time = instr["duration"] 161 max_time = instr["duration"] 162 instr_score, syncs = render_instr(instr, syncs, max_time) 162 instr_score, syncs = render_instr(instr, syncs, max_time) 163 instrs.append(instr_score) 163 instrs.append(instr_score) 164 for line in generate_csound_score(instr_score, instr["score_ 164 for line in generate_csound_score(instr_score, instr["score_ 165 print line 165 print line 166 longest_score = max(instrs, key=lambda i: score_len(i)) 166 longest_score = max(instrs, key=lambda i: score_len(i)) 167 subsection_start += score_len(longest_score) 167 subsection_start += score_len(longest_score) ................................................................................................................................................................................ 170 pass 170 pass 171 171 172 172 173 def render_instr(instr, syncs, max_time): 173 def render_instr(instr, syncs, max_time): 174 for g in instr["grammars"]: 174 for g in instr["grammars"]: 175 for i in range(len(instr["grammars"][g])): 175 for i in range(len(instr["grammars"][g])): 176 instr["grammars"][g][i] = parse.parse(instr["grammars"][g][i]) 176 instr["grammars"][g][i] = parse.parse(instr["grammars"][g][i]) 177 score = [] | 177 178 while True: | 178 score= [] 179 # ipdb.set_trace() | 179 try: 180 time_remaining = max_time - score_len(score) | 180 score, syncs = choose_phrase(instr, syncs, 0, max_time) 181 try: | 181 182 score, syncs = choose_node(score, instr, time_remaining, syncs) | 182 while True: 183 except ValueError: | 183 score_index_to_replace = None 184 break | 184 for item in range(len(score)): # Optimize this by caching the index 185 return (score, syncs) | 185 if isinstance(score[item], tree.Tree): 186 | 186 score_index_to_replace = item 187 | 187 if score_index_to_replace is None: 188 def choose_node(score, instr, time_remaining, syncs): | 188 raise ValueError("No more nodes to fill in") 189 # ipdb.set_trace() | 189 190 grammars = instr["grammars"] | 190 time_remaining = max_time - score_len(score) 191 if time_remaining <= 0: | 191 new_phrase, syncs = choose_phrase(instr, syncs, score_len(score), ti 192 raise ValueError("No time remaining in the score") | 192 score = score[:node_index-1] + new_phrase + score[node_index+1:] 193 | 193 194 if len(score) == 0: | 194 except ValueError: 195 options = get_node_choices(instr, syncs, score_len(score)) | 195 return (score, syncs) 196 node = random.choice(options) | 196 197 phrase = random.choice(instr["grammars"][node]) | 197 198 | 198 def choose_phrase(instr, syncs, current_time, time_remaining): 199 # Find the next node in the score that needs choosing | 199 '''Filters grammars for ones that match the sync option, and phrases that fi 200 node = None | 200 time_filtered_grammars = {} 201 node_index = None | 201 for grammar in instr["grammars"]: 202 for item in range(len(score)): | 202 time_filtered_grammars[grammar] = get_phrases_that_fit(instr["grammars"] 203 if isinstance(score[item], tree.Tree): | 203 if len(time_filtered_grammars.keys()) == 0: 204 node = score[item].name | 204 raise ValueError("No available grammars that will fit in the score") 205 node_index = item | 205 206 if node is None: | 206 grammar = None 207 raise ValueError("No more nodes to fill in") | 207 # if instr["name"] == "follow_instr": 208 | 208 # ipdb.set_trace() 209 options = get_node_choices(instr, syncs, score_len(score)) | 209 if instr["sync"] is not None: 210 node = random.choice(options) | 210 guiding_instr = instr["sync"] 211 phrase = random.choice(instr["grammars"][node]) | 211 sync_node = get_sync_node_at_time(syncs[guiding_instr], current_time) 212 score = score[:node_index-1] + phrase + score[node_index+1:] | 212 if sync_node in time_filtered_grammars.keys(): 213 return score | 213 grammar = sync_node 214 | 214 if grammar is None: 215 | 215 grammar = random.choice(time_filtered_grammars.keys()) 216 def get_node_choices(instr, syncs, current_time): | 216 phrases = time_filtered_grammars[grammar] 217 # If this instrument should follow another, choose a grammar node from the c | 217 if instr["name"] not in syncs.keys(): 218 # ipdb.set_trace() | 218 syncs[instr["name"]] = [] 219 grammars = instr["grammars"] | 219 syncs[instr["name"]].append({"node": grammar, "time": current_time}) 220 options = [] | 220 221 if instr["sync"] is not None: | 221 return random.choice(phrases), syncs 222 guiding_instr = instr["sync"] | 222 223 sync_node = get_sync_node_at_time(syncs[guiding_instr], current_time) | 223 224 if sync_node in instr["grammars"].keys(): | 224 def get_phrases_that_fit(grammar, time_remaining): 225 options.append(sync_node) | 225 valid_phrases = [] 226 else: | 226 for phrase in grammar: 227 for g in range(len(grammars[node])): | 227 if score_len(phrase) <= time_remaining: 228 if score_len(grammars[node][g]) <= time_remaining: | 228 valid_phrases.append(phrase) 229 options.append(grammars[node][g]) | 229 return valid_phrases 230 else: | 230 231 for g in range(len(grammars[node])): | 231 232 if score_len(grammars[node][g]) <= time_remaining: | 232 def get_sync_node_at_time(syncs, t): 233 options.append(grammars[node][g]) | 233 for s in range(len(syncs)): 234 if len(options) == 0: | 234 if syncs[s]["time"] >= t: 235 raise ValueError("No available grammars that will fit in the score") < 236 return options < 237 < 238 < 239 def get_sync_node_at_time(syncs, t): < 240 for s in len(syncs): < 241 if syncs[s]["time"] > t: < 242 return syncs[s]["node"] 235 return syncs[s]["node"] 243 236 244 237 245 def score_len(score): 238 def score_len(score): 246 total = 0 239 total = 0 247 for n in score: 240 for n in score: 248 if not isinstance(n, tree.Tree): 241 if not isinstance(n, tree.Tree):