spiffyscore
Check-in [3cac4f013d]
Not logged in
Overview
SHA1 Hash:3cac4f013df3b3a7be932583f18353a04078c6c5
Date: 2011-09-22 17:04:24
User: brian
Comment:Finally broke the program out into functions. It's now much cleaner and easier to underntand.
Timelines: family | ancestors | descendants | both | feature/abc
Other Links: files | file ages | manifest
Tags And Properties
Changes
hide diffs unified diffs patch

Modified cfg.py from [eefff49c8fc33785] to [77ab4fec9becbad0].

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()

Modified test.sco from [5e21d18eb05a5ddb] to [4da964b4dce982b8].

1 f1 0 512 10 1 1 f1 0 512 10 1 2 f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08 2 f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08 3 f3 0 1025 10 1 3 f3 0 1025 10 1 4 t 0 60 4 t 0 60 5 5 6 i2 0.000000 0.250000 7000 8.04 2 | 6 i2 0.000000 0.250000 7000 8.00 2 7 i2 0.250000 0.250000 7000 8.09 2 < 8 i2 0.500000 0.250000 7000 8.02 2 < 9 i2 0.750000 0.250000 7000 8.07 2 < 10 i2 1.000000 0.250000 7000 8.05 2 < 11 i2 1.250000 0.250000 7000 8.05 2 < 12 i2 1.500000 0.250000 7000 8.00 2 < 13 i2 1.750000 0.500000 7000 8.07 2 | 7 i2 0.250000 0.500000 7000 8.07 2 14 i2 2.250000 0.500000 7000 8.07 2 | 8 i2 0.750000 0.500000 7000 8.07 2 15 i2 2.750000 0.500000 7000 8.07 2 | 9 i2 1.250000 0.500000 7000 8.07 2 16 i2 3.250000 0.250000 7000 8.00 2 | 10 i2 1.750000 0.250000 7000 8.00 2 17 i2 3.500000 0.250000 7000 7.11 2 | 11 i2 2.000000 0.250000 7000 7.11 2 18 i2 3.750000 0.250000 7000 9.05 2 | 12 i2 2.250000 0.250000 7000 9.05 2 19 i2 4.000000 0.250000 7000 8.00 2 | 13 i2 2.500000 0.250000 7000 8.00 2 20 i2 4.250000 0.250000 7000 8.05 2 | 14 i2 2.750000 0.250000 7000 8.05 2 21 i2 4.500000 0.250000 7000 8.00 2 | 15 i2 3.000000 0.250000 7000 8.00 2 22 i2 4.750000 0.250000 7000 8.11 2 | 16 i2 3.250000 0.250000 7000 8.11 2 23 i2 5.000000 0.250000 7000 8.04 2 | 17 i2 3.500000 0.250000 7000 8.04 2 24 i2 5.250000 0.250000 7000 8.09 2 | 18 i2 3.750000 0.250000 7000 8.09 2 25 i2 5.500000 0.250000 7000 8.02 2 | 19 i2 4.000000 0.250000 7000 8.02 2 26 i2 5.750000 0.250000 7000 8.07 2 | 20 i2 4.250000 0.250000 7000 8.07 2 27 i2 6.000000 0.250000 7000 8.05 2 | 21 i2 4.500000 0.250000 7000 8.05 2 28 i2 6.250000 0.250000 7000 8.05 2 | 22 i2 4.750000 0.250000 7000 8.05 2 29 i2 6.500000 0.250000 7000 8.00 2 | 23 i2 5.000000 0.250000 7000 8.00 2 30 i2 6.750000 0.500000 7000 8.07 2 | 24 i2 5.250000 0.500000 7000 8.07 2 31 i2 7.250000 0.500000 7000 8.07 2 | 25 i2 5.750000 0.500000 7000 8.07 2 32 i2 7.750000 0.500000 7000 8.07 2 | 26 i2 6.250000 0.500000 7000 8.07 2 33 i2 8.250000 0.250000 7000 8.00 2 | 27 i2 6.750000 0.250000 7000 8.00 2 34 i2 8.500000 0.250000 7000 7.11 2 | 28 i2 7.000000 0.250000 7000 7.11 2 35 i2 8.750000 0.250000 7000 9.05 2 | 29 i2 7.250000 0.250000 7000 9.05 2 36 i2 9.000000 0.250000 7000 8.00 2 | 30 i2 7.500000 0.250000 7000 8.00 2 37 i2 9.250000 0.250000 7000 8.05 2 | 31 i2 7.750000 0.250000 7000 8.05 2 38 i2 9.500000 0.250000 7000 8.00 2 | 32 i2 8.000000 0.250000 7000 8.00 2 39 i2 9.750000 0.250000 7000 8.11 2 | 33 i2 8.250000 0.250000 7000 8.11 2 40 i2 10.000000 0.250000 7000 8.05 2 | 34 i2 8.500000 0.250000 7000 8.04 2 41 i2 10.250000 0.250000 7000 8.00 2 | 35 i2 8.750000 0.250000 7000 8.09 2 42 i2 10.500000 0.250000 7000 8.00 2 | 36 i2 9.000000 0.250000 7000 8.02 2 43 i2 10.750000 0.250000 7000 8.00 2 | 37 i2 9.250000 0.250000 7000 8.07 2 44 i2 11.000000 0.250000 7000 8.00 2 | 38 i2 9.500000 0.250000 7000 8.05 2 45 i2 11.250000 0.500000 7000 8.05 2 | 39 i2 9.750000 0.250000 7000 8.05 2 46 i2 11.750000 0.500000 7000 8.05 2 | 40 i2 10.000000 2.000000 7000 8.11 2 > 41 i2 12.000000 0.250000 7000 8.00 2 47 i2 12.250000 0.250000 7000 8.00 2 42 i2 12.250000 0.250000 7000 8.00 2 48 i2 12.500000 0.250000 7000 8.00 2 43 i2 12.500000 0.250000 7000 8.00 2 49 i2 12.750000 0.250000 7000 8.00 2 44 i2 12.750000 0.250000 7000 8.00 2 50 i2 13.000000 0.250000 7000 8.00 2 | 45 i2 13.000000 0.500000 7000 8.05 2 51 i2 13.250000 0.500000 7000 8.05 2 | 46 i2 13.500000 0.500000 7000 8.05 2 52 i2 13.750000 0.500000 7000 8.05 2 | 47 i2 14.000000 0.250000 7000 8.00 2 53 i2 14.250000 0.250000 7000 8.00 2 48 i2 14.250000 0.250000 7000 8.00 2 54 i2 14.500000 0.250000 7000 8.00 2 49 i2 14.500000 0.250000 7000 8.00 2 55 i2 14.750000 0.250000 7000 8.00 2 50 i2 14.750000 0.250000 7000 8.00 2 56 i2 15.000000 0.250000 7000 8.00 2 | 51 i2 15.000000 0.500000 7000 8.05 2 57 i2 15.250000 0.500000 7000 8.05 2 | 52 i2 15.500000 0.500000 7000 8.05 2 58 i2 15.750000 0.500000 7000 8.05 2 | 53 i2 16.000000 0.250000 7000 8.00 2 59 i2 16.250000 0.250000 7000 8.00 2 54 i2 16.250000 0.250000 7000 8.00 2 60 i2 16.500000 0.250000 7000 8.00 2 55 i2 16.500000 0.250000 7000 8.00 2 61 i2 16.750000 0.250000 7000 8.00 2 56 i2 16.750000 0.250000 7000 8.00 2 62 i2 17.000000 0.250000 7000 8.00 2 | 57 i2 17.000000 0.500000 7000 8.05 2 63 i2 17.250000 0.500000 7000 8.05 2 | 58 i2 17.500000 0.500000 7000 8.05 2 64 i2 17.750000 0.500000 7000 8.05 2 | 59 i2 18.000000 0.250000 7000 8.00 2 65 i2 18.250000 0.250000 7000 8.00 2 60 i2 18.250000 0.250000 7000 8.00 2 66 i2 18.500000 0.250000 7000 8.00 2 61 i2 18.500000 0.250000 7000 8.00 2 67 i2 18.750000 0.250000 7000 8.00 2 62 i2 18.750000 0.250000 7000 8.00 2 > 63 i2 19.000000 0.500000 7000 8.05 2 > 64 i2 19.500000 0.500000 7000 8.05 2 68 i2 19.000000 0.250000 7000 8.00 2 | 65 i2 20.000000 0.250000 7000 8.00 2 > 66 i2 20.250000 0.250000 7000 8.00 2 > 67 i2 20.500000 0.250000 7000 8.00 2 > 68 i2 20.750000 0.250000 7000 8.00 2 69 i2 19.250000 0.500000 7000 8.05 2 | 69 i2 21.000000 0.500000 7000 8.05 2 70 i2 19.750000 0.500000 7000 8.05 2 | 70 i2 21.500000 0.500000 7000 8.05 2 71 i2 20.250000 0.500000 7000 8.05 2 | 71 i2 22.000000 0.500000 7000 8.05 2