Changes In Branch feature/abc Through [7a6ab725ee] Excluding Merge-Ins
This is equivalent to a diff from 843dabad43 to 7a6ab725ee
2011-09-15
| ||
19:19 | Movements work again check-in: a9b80ad75f user: brian tags: feature/abc | |
19:09 | Program prints stuff out again check-in: 7a6ab725ee user: brian tags: feature/abc | |
15:28 | Got the program to run through again. Now without the roman numerals check-in: 33ddc6fee4 user: brian tags: feature/abc | |
2011-09-13
| ||
18:29 | Create new branch named "feature/abc" check-in: 3833b59ec2 user: brian tags: feature/abc | |
2011-02-10
| ||
22:34 | Added lexical support for parens instead of quotes for chords, cleaned up the yacc parser, added lex tokens for syncopation check-in: 702d933446 user: brian@linux-85dd.site tags: master | |
2010-11-30
| ||
17:33 | This is what I submitted as my final piece for MUS 306 check-in: 843dabad43 user: spiffytech@gmail.com tags: master | |
06:22 | Added a timbre instrument, doubled the length of the piece, added intros and outro check-in: de7c009e4d user: spiffytech@gmail.com tags: master | |
Modified cfg.py from [019c552e42] to [6f8ab10545].
1 1 #!/usr/bin/env python 2 2 3 3 from __future__ import division 4 4 import os 5 +import pdb 5 6 import random 6 7 import sys 7 8 import time 8 -random.seed(time.time()) 9 9 import parse 10 10 11 +import tree 12 + 13 +random.seed(time.time()) 14 + 11 15 def main(): 12 16 key = "A" 13 17 bps = 60/60 14 18 tempo = 1/bps 15 19 max_duration = 1 16 20 17 21 composition = { 18 - "intro1": { 19 - "rhythm": { 20 - "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6", 21 - "octave": 7, 22 - "duration": 4, 23 - "grammars": { 24 - "u": ['"I" "ii"/4 "ii"/4 "IV"/2 "V"2 "IV" "ii" x u', '"I" "vii" "III" y u', '"I" "v" "IV" u u'], 25 - "w": ['"i" "VII"2 "VI"/4 "V"/4 "i"/4 "VII"2 "VI" "V" w u'], 26 - "x": ['"III/2" "VI"/2 "III"/2 "vii"2 "i"2 "V" u'], 27 - "y": ['"I" "vi"2 "IV" "V" y y u'], 28 - }, 29 - "score": "u x u y x w u", 30 - }, 31 - }, 32 - "intro2": { 33 - "rhythm": { 34 - "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6", 35 - "octave": 7, 36 - "duration": 8, 37 - "grammars": { 38 - "u": ['"I" "ii"/4 "ii"/4 "IV"/2 "V"2 "IV" "ii" x u', '"I" "vii" "III" y u', '"I" "v" "IV" u u'], 39 - "w": ['"i" "VII"2 "VI"/4 "V"/4 "i"/4 "VII"2 "VI" "V" w u'], 40 - "x": ['"III/2" "VI"/2 "III"/2 "vii"2 "i"2 "V" u'], 41 - "y": ['"I" "vi"2 "IV" "V" y y u'], 42 - }, 43 - "score": "u x u y x w u", 44 - }, 45 - "timbre": { 46 - "score_line": "i3 %(time)f %(duration)f 2000 %(octave)d.%(note)s 2 3 5 3", 47 - "octave": 6, 48 - "duration": 8, 49 - "grammars": { # Notes for this instrument to use in this piece 50 - "u": ['I2 VII2 V2 VI2 I2 IV/2 V/2 III2 u', 'I2 IV2 V2 IV I V2 III2 II2 u'] 51 - }, 52 - "score": "u u u u u", 53 - }, 54 - }, 55 22 "overview": { 56 23 "melody": { # Instrument 'melody' 57 24 "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2", 58 25 "octave": 8, 59 26 "duration": 40, 60 27 "grammars": { # Notes for this instrument to use in this piece 61 - "u": ["I V/2 V/2 V/2 I VII, IV' I IV I VII IV"], 62 - "w": ['III/4 VI/4 II/4 V/4 VI/4 IV/4 VII2'], 63 - "x": ['I/2 I/2 VI IV/2 V2 z I/2 I/2 VI IV/2 V'], 28 + "u": ["C G/2 G/2 G/2 C B, F' C F C B F (u)"], 29 +# "w": ['E/4 A/4 D/4 G/4 F/4 F/4 B2'], 64 30 }, 65 - "score": "u u w w w w w x x", 66 - }, 67 - "rhythm": { 68 - "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6", 69 - "octave": 7, 70 - "duration": 44, 71 - "grammars": { 72 - "u": ['"I" "ii"/4 "ii"/4 "IV"/2 "V"2 "IV" "ii" x u', '"I" "vii" "III" y u', '"I" "v" "IV" u u'], 73 - "w": ['"i" "VII"2 "VI"/4 "V"/4 "i"/4 "VII"2 "VI" "V" w u'], 74 - "x": ['"III/2" "VI"/2 "III"/2 "vii"2 "i"2 "V" u'], 75 - "y": ['"I" "vi"2 "IV" "V" y y u'], 76 - }, 77 - "score": "u x u y x w u", 78 - }, 79 - "timbre": { 80 - "score_line": "i3 %(time)f %(duration)f 2000 %(octave)d.%(note)s 2 3 5 3", 81 - "octave": 6, 82 - "duration": 44, 83 - "grammars": { # Notes for this instrument to use in this piece 84 - "u": ['I2 VII2 V2 VI2 I2 IV/2 V/2 III2 u', 'I2 IV2 V2 IV I V2 III2 II2 u'] 85 - }, 86 - "score": "u u u u u", 87 - }, 88 - }, 89 - "a": { # Movement block 'a' for reuse throughout the piece 90 - "melody": { # Instrument 'melody' 91 - "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2", 92 - "octave": 8, 93 - "duration": 40, 94 - "grammars": { # Notes for this instrument to use in this piece 95 - "u": ["I V/2 V/2 V/2 I VII, IV' x", "I IV w", "I VII IV u"], 96 - "w": ['VII I IV V VI VII I IV V VI VII u'], 97 - "x": ['VI/4 III/4 II/4 I/4 w'], 98 - }, 99 - "score": "u", 100 - }, 101 - "rhythm": { 102 - "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6", 103 - "octave": 7, 104 - "duration": 48, 105 - "grammars": { 106 - "u": ['"I" "ii"/4 "ii"/4 "IV"/2 "V"2 "IV" "ii" x u', '"I" "vii" "III" y u', '"I" "v" "IV" u u'], 107 - "w": ['"i" "VII"2 "VI"/4 "V"/4 "i"/4 "VII"2 "VI" "V" w u'], 108 - "x": ['"III/2" "VI"/2 "III"/2 "vii"2 "i"2 "V" u'], 109 - "y": ['"I" "vi"2 "IV" "V" y y u'], 110 - }, 111 - "score": "u x u y x w u", 112 - }, 113 - "timbre": { 114 - "score_line": "i3 %(time)f %(duration)f 2000 %(octave)d.%(note)s 2 3 5 3", 115 - "octave": 6, 116 - "duration": 42, 117 - "grammars": { # Notes for this instrument to use in this piece 118 - "u": ['I2 VII2 V2 VI2 I2 IV/2 V/2 III2 u', 'I2 IV2 V2 IV I V2 III2 II2 u'] 119 - }, 120 - "score": "u u u u u", 121 - }, 122 - }, 123 - "b": { # Movement block 'a' for reuse throughout the piece 124 - "melody": { # Instrument 'melody' 125 - "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2", 126 - "octave": 8, 127 - "duration": 40, 128 - "grammars": { # Notes for this instrument to use in this piece 129 - "u": ['I VII V III u', "y"], 130 - "w": ['VII I IV V VI u', 'w'], 131 - "x": ['VI/4 III/4 II/4 I/4 w', 'x x'], 132 - "y": ["III/4 VI/4 II/4 V/4 VI/4 IV/4 VII2"], 133 - }, 134 - "score": "w w x x w", 135 - }, 136 - "rhythm": { 137 - "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6", 138 - "octave": 7, 139 - "duration": 44, 140 - "grammars": { 141 - "u": ['"I" "V" "vi" "iii" "IV" "I" "IV" "V" u u', "y"], 142 - "y": ['"I" "vi"2 "IV" "V" y y u'], 143 - }, 144 - "score": "u u y y u", 145 - }, 146 - "timbre": { 147 - "score_line": "i3 %(time)f %(duration)f 2000 %(octave)d.%(note)s 2 3 5 3", 148 - "octave": 6, 149 - "duration": 40, 150 - "grammars": { # Notes for this instrument to use in this piece 151 - "u": ['I2 VII2 V2 VI2 I2 IV/2 V/2 III2 u', 'I2 IV2 V2 IV I V2 III2 II2 u'] 152 - }, 153 - "score": "u u u u u", 154 - }, 155 - }, 156 - "zelda1": { # Movement block 'a' for reuse throughout the piece 157 - "melody": { # Instrument 'melody' 158 - "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2", 159 - "octave": 8, 160 - "duration": 50, 161 - "grammars": { # Notes for this instrument to use in this piece 162 - "u": ['I VI/2 VI/2 IV/2 I VI/2 VI/2 IV/2 I VI/2 VI/2 IV/2 w', "y"], 163 - "w": ['VII/2 I/2 II/2 V/2 u'], 164 - "x": ['VI/4 III/4 II/4 I/4 w'], 165 - "y": ["III/4 VI/4 II/4 V/4 VI/4 IV/4 VII2 x"], 166 - }, 167 - "score": "w", 168 - }, 169 - "rhythm": { 170 - "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6", 171 - "octave": 7, 172 - "duration": 54, 173 - "grammars": { 174 - "u": ['"I"/2 "V"/2 "vi"/2 "iii"/2 "IV"/2 "I"/2 "IV"/2 "V"/2 u u', "y"], 175 - "y": ['"I"/2 "vi" "IV"/2 "V"/2 y y u'], 176 - }, 177 - "score": "u u y y u", 178 - }, 179 - "timbre": { 180 - "score_line": "i3 %(time)f %(duration)f 2000 %(octave)d.%(note)s 2 3 5 3", 181 - "octave": 6, 182 - "duration": 54, 183 - "grammars": { # Notes for this instrument to use in this piece 184 - "u": ['I2 VII2 V2 VI2 I2 IV/2 V/2 III2 u', 'I2 IV2 V2 IV I V2 III2 II2 u'] 185 - }, 186 - "score": "u u u u u", 187 - }, 188 - }, 189 - "zelda2": { # Movement block 'a' for reuse throughout the piece 190 - "melody": { # Instrument 'melody' 191 - "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2", 192 - "octave": 8, 193 - "duration": 40, 194 - "grammars": { # Notes for this instrument to use in this piece 195 - "u": ['I/2 I/2 VI IV/2 V2 z I/2 I/2 VI IV/2 V u', "w"], 196 - "w": ['III/2 II/2 IV/2 III/2 V/2 IV/2 VI/2 V/2 w', 'u', 'x'], 197 - "x": ['VII3 z2 V/4 IV/4 V/4 IV/4 V/4 VI/4 II/4 II/4 II/4 I/4 II/2 u', 'w'], 198 - }, 199 - "score": "u", 200 - }, 201 - "rhythm": { 202 - "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6", 203 - "octave": 7, 204 - "duration": 44, 205 - "grammars": { 206 - "u": ['"I"/2 "V"/2 "vi"/2 "iii"/2 "IV"/2 "I"/2 "IV"/2 "V"/2 u u', "y"], 207 - "y": ['"I"/2 "vi" "IV"/2 "V"/2 y y u'], 208 - }, 209 - "score": "u u y y u", 210 - }, 211 - "timbre": { 212 - "score_line": "i3 %(time)f %(duration)f 2000 %(octave)d.%(note)s 2 3 5 3", 213 - "octave": 6, 214 - "duration": 44, 215 - "grammars": { # Notes for this instrument to use in this piece 216 - "u": ['I2 VII2 V2 VI2 I2 IV/2 V/2 III2 u', 'I2 IV2 V2 IV I V2 III2 II2 u'] 217 - }, 218 - "score": "u u u u u", 219 - }, 220 - }, 221 - "timbre_solo": { # Movement block 'a' for reuse throughout the piece 222 - "rhythm": { 223 - "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6", 224 - "octave": 7, 225 - "duration": 30, 226 - "grammars": { 227 - "u": ['"I"/2 "V"/2 "vi"/2 "iii"/2 "IV"/2 "I"/2 "IV"/2 "V"/2 u u', "y"], 228 - "y": ['"I"/2 "vi" "IV"/2 "V"/2 y y u'], 229 - }, 230 - "score": "u u y y u", 231 - }, 232 - "timbre": { 233 - "score_line": "i3 %(time)f %(duration)f 2000 %(octave)d.%(note)s 2 3 5 3", 234 - "octave": 6, 235 - "duration": 30, 236 - "grammars": { # Notes for this instrument to use in this piece 237 - "u": ['I VI/2 VI/2 IV/2 u', "y"], 238 - "w": ['VII/2 I/2 II/2 V/2 u', 'w w', 'u x'], 239 - "x": ['VI/4 III/4 II/4 I/4 w', 'x x', 'x u'], 240 - "y": ["III/4 VI/4 II/4 V/4 VI/4 IV/4 VII2 w", 'y y'], 241 - }, 242 - "score": "u", 243 - }, 244 - }, 245 - "outro1": { # Movement block 'a' for reuse throughout the piece 246 - "melody": { # Instrument 'melody' 247 - "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2", 248 - "octave": 8, 249 - "duration": 20, 250 - "grammars": { # Notes for this instrument to use in this piece 251 - "u": ['I VII V III u', "y"], 252 - "w": ['VII I IV V VI u', 'w w'], 253 - "x": ['VI/4 III/4 II/4 I/4 w', 'x x'], 254 - "y": ["III/4 VI/4 II/4 V/4 VI/4 IV/4 VII2"], 255 - }, 256 - "score": "w w x x w", 257 - }, 258 - "rhythm": { 259 - "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6", 260 - "octave": 7, 261 - "duration": 26, 262 - "grammars": { 263 - "u": ['"I" "V" "vi" "iii" "IV" "I" "IV" "V" u u', "y"], 264 - "y": ['"I" "vi"2 "IV" "V" y y u'], 265 - }, 266 - "score": "u u y y u", 267 - }, 268 - "timbre": { 269 - "score_line": "i3 %(time)f %(duration)f 2000 %(octave)d.%(note)s 2 3 5 3", 270 - "octave": 6, 271 - "duration": 26, 272 - "grammars": { # Notes for this instrument to use in this piece 273 - "u": ['I2 VII2 V2 VI2 I2 IV/2 V/2 III2 u', 'I2 IV2 V2 IV I V2 III2 II2 u'] 274 - }, 275 - "score": "u u u u u", 276 - }, 277 - }, 278 - "outro2": { 279 - "rhythm": { 280 - "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6", 281 - "octave": 7, 282 - "duration": 4, 283 - "grammars": { 284 - "u": ['"I" "V" "vi" "iii" "IV" "I" "IV" "V" u u', "y"], 285 - "y": ['"I" "vi"2 "IV" "V" y y u'], 286 - }, 287 - "score": "u u y y u", 31 + "score": "u u", 288 32 }, 289 33 }, 290 34 } 291 - 292 - max_t = 0 # max time encountered so far. Used for movement timing 293 - progression = "intro1 intro2 overview a timbre_solo zelda1 b zelda2 outro1 outro2" 294 -# progression = "zelda1" 295 - for comp_name in progression.split(): 296 - comp_start_time = max_t 297 - for instr_name, instr in composition[comp_name].iteritems(): 298 - generated_score = generate_score(instr["score"], instr["grammars"]) # Fill in the scores by generating them based on the grammars 299 -# print generated_score 300 - score = parse.parse(generated_score, default_octave=instr["octave"]) # Return Node/Chord objects 301 - 302 - # Generate timestamps for the notes 303 - t = comp_start_time 304 - for note in range(len(score)): 305 - score[note].time = t 306 - score[note].duration *= tempo 307 - t += score[note].duration 308 -# print "time difference =", t-comp_start_time 309 -# print "instrument duration =",composition[comp_name][instr_name]["duration"] 310 - if (t-comp_start_time) > float(composition[comp_name][instr_name]["duration"]): 311 -# print "here" 312 - score = score[:note] 313 - break 314 - max_t = t if t > max_t else max_t 315 - composition[comp_name][instr_name]["score"] = score 316 - 317 - # Must be done after all note times keyed in, else you can't coordinate melodies with the rhythm chords 318 35 print '''f1 0 512 10 1 319 36 f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08 320 37 f3 0 1025 10 1 321 38 ''' 39 + movement_start = 0 40 + 41 + 42 + max_t = 0 # max time encountered so far. Used for movement timing 43 + progression = "overview" 322 44 for comp_name in progression.split(): 323 - print "; Movement:", comp_name 45 + comp_start_time = max_t 46 + # We need an arbitrary grammar from this instrument to start the score with 47 + max_instr = 0 48 + movement_start += max_instr 324 49 for instr_name, instr in composition[comp_name].iteritems(): 325 - composition[comp_name][instr_name]["score"] = transliterate_score(composition[comp_name][instr_name]["score"], key) 326 -# print "\nMovement %s instrument %s" % (comp_name, instr_name) 327 -# print composition[comp_name][instr_name]["score"] 328 - final_score = generate_csound_score(composition[comp_name][instr_name]["score"], composition[comp_name][instr_name]["score_line"]) 329 - for line in final_score: 330 - print line 50 + for grammar in instr["grammars"]: 51 + for g in range(len(instr["grammars"][grammar])): 52 + instr["grammars"][grammar][g] = parse.parse(instr["grammars"][grammar][g], default_octave=instr["octave"]) 53 + g = random.choice(instr["grammars"].keys()) 54 + ins_score = random.choice(instr["grammars"][g]) 55 +# ins_score = instr["grammars"][g] 56 + score_complete = False 57 + while score_complete is False: 58 + if score_len(ins_score) >= 50: 59 + score_complete = True 60 + break 61 + for i in range(len(ins_score)): 62 + if isinstance(ins_score[i], tree.Tree): 63 + unrolled_score = select_node(instr["grammars"][ins_score[i].name]) 64 + new_score = ins_score[:i-1] + unrolled_score + ins_score[i+i:] 65 + ins_score = new_score 66 + if i == len(ins_score): 67 + score_complete = True 68 + break 331 69 332 70 333 -def make_scale(key): 334 - notes = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"] 335 - scale = [key] 336 - pos = notes.index(key) 337 - progression = [2,2,1,2,2,2,1] 338 - for p in progression: 339 - pos = (pos + p) % 12 340 - scale.append(notes[pos]) 341 - return scale 71 + ins_score = [n for n in ins_score if not isinstance(n, tree.Tree)] 72 + composition[comp_name][instr_name]["score"] = ins_score 73 + 74 + # Generate timestamps for the notes 75 +# t = comp_start_time 76 +# for note in range(len(ins_score)): 77 +# ins_score[note].time = t 78 +# ins_score[note].duration *= tempo 79 +# t += score[note].duration 80 +## print "time difference =", t-comp_start_time 81 +## print "instrument duration =",composition[comp_name][instr_name]["duration"] 82 +# if (t-comp_start_time) > float(composition[comp_name][instr_name]["duration"]): 83 +## print "here" 84 +# ins_score = ins_score[:note] 85 +# break 86 +# max_t = t if t > max_t else max_t 87 +# composition[comp_name][instr_name]["score"] = ins_score 88 + 89 + # Must be done after all note times keyed in, else you can't coordinate melodies with the rhythm chords 90 +# for comp_name in progression.split(): 91 +# print "; Movement:", comp_name 92 +# for instr_name, instr in composition[comp_name].iteritems(): 93 +## print "\nMovement %s instrument %s" % (comp_name, instr_name) 94 +## print composition[comp_name][instr_name]["score"] 95 +# final_score = generate_csound_score(composition[comp_name][instr_name]["score"], composition[comp_name][instr_name]["score_line"]) 96 +# for line in final_score: 97 +# print line 98 + 99 + if score_len(ins_score) > max_instr: 100 + max_instr = ins_score 101 + for line in generate_csound_score(composition[comp_name][instr_name]["score"], instr["score_line"], movement_start): 102 + print line 103 + 104 + 105 +def score_len(score): 106 + total = 0 107 + for n in score: 108 + if not isinstance(n, tree.Tree): 109 + total += n.duration 110 + return total 342 111 112 +def select_node(grammar): 113 + return random.choice(grammar) 114 + 343 115 344 116 def generate_score(score, grammars): 117 + pdb.set_trace() 345 118 while 1: 346 119 found_substitution = False 347 120 for key,value in grammars.iteritems(): 348 121 if score.find(key) != -1: 349 122 found_substitution = True 350 123 while score.find(key) != -1: 351 124 score = score.replace(key, random.choice(grammars[key]), 1) ................................................................................ 354 127 for k in grammars.keys(): 355 128 score = score.replace(k, "") 356 129 return score 357 130 if found_substitution is False: 358 131 break 359 132 return score 360 133 361 -def transliterate_score(score, key): 362 - scale = make_scale(key) 363 - scale_conversion = { 364 - "I": 1, 365 - "II": 2, 366 - "III": 3, 367 - "IV": 4, 368 - "V": 5, 369 - "VI": 6, 370 - "VII": 7, 371 - "VIII": 8, 372 - } 373 - keyed_score = [] 374 - for i in range(len(score)): 375 - if isinstance(score[i], parse.Note): 376 - score[i].value = scale[scale_conversion[score[i].value]-1] 377 - elif isinstance(score[i], parse.Chord): 378 - chord = [] 379 - root_note_index = scale.index(key) + scale_conversion[score[i].value] 380 - chord.append(scale[root_note_index]) 381 - chord.append(scale[(root_note_index+3) % 8]) 382 - if score[i].chord_type == "m": # Minor chords, flat the 5th 383 - chord.append(scale[(root_note_index+4) % 8]) 384 - else: 385 - chord.append(scale[(root_note_index+5) % 8]) 386 - score[i].chord = chord 387 - elif isinstance(score[i], parse.Rest): 388 - pass 389 - return score 390 134 391 - 392 -def generate_csound_score(score, score_line): 135 +def generate_csound_score(score, score_line, t): 393 136 csound_note_values = { 394 137 "C": "00", 395 138 "C#": "01", 396 139 "D": "02", 397 140 "D#": "03", 398 141 "E": "04", 399 142 "F": "05", ................................................................................ 401 144 "G": "07", 402 145 "G#": "08", 403 146 "A": "09", 404 147 "A#": "10", 405 148 "B": "11", 406 149 } 407 150 csound_score = [] 151 + pdb.set_trace() 408 152 for token in score: 409 153 if isinstance(token, parse.Chord): # Chords 410 154 for note in token.chord: 411 155 note = csound_note_values[note] 412 - csound_score.append(score_line % {"time": token.time, "octave": token.octave, "note": note, "duration": token.duration}) 156 + csound_score.append(score_line % {"time": t, "octave": token.octave, "note": note, "duration": token.duration}) 413 157 elif isinstance(token, parse.Note): # Individual notes 414 158 note = csound_note_values[token.value] 415 - csound_score.append(score_line % {"time": token.time, "octave": token.octave, "note": note, "duration": token.duration}) 159 + csound_score.append(score_line % {"time": t, "octave": token.octave, "note": note, "duration": token.duration}) 160 + t += token.duration 416 161 return csound_score 417 162 418 163 419 164 if __name__ == "__main__": main()
Modified parse.py from [9c47a4ff24] to [8755d61a44].
1 1 #!/usr/bin/env python 2 + 3 +import tree 2 4 3 5 from ply import lex, yacc 4 6 class Note(): 5 7 def __init__(self, value, duration=.25, octave=8): 6 8 self.value = value 7 9 self.duration = duration 8 10 self.octave = octave ................................................................................ 33 35 "NOTE_LENGTH", 34 36 "BASENOTE", 35 37 "ACCIDENTAL", 36 38 "REST", 37 39 "OCTAVE", 38 40 "CHORD_TYPE", 39 41 "QUOTE", 42 + "NODE", 40 43 ) 41 44 42 45 t_ignore = " |" 43 46 44 - #t_BASENOTE = r"[A-Ga-g]" 45 - t_BASENOTE = r"I+V?|VI*|i+v?|vi*" 47 + t_BASENOTE = r"[A-Ga-g]" 48 +# t_BASENOTE = r"I+V?|VI*|i+v?|vi*" 46 49 t_ACCIDENTAL = r"\^{1,2}|_{1,2}|=" 47 50 t_REST = r"z" 48 51 t_OCTAVE = r"'+|,+" 49 52 t_CHORD_TYPE = r"m|7|m7|0|o|\+|mb5|sus|sus4|maj7|mmaj7|7sus4|dim|dim7|7b5|m7b5|6|b6|m6|mb6|46|maj9|9|add9|7b9|m9" 50 53 t_QUOTE = '"' 54 + t_NODE = r"\([a-zA-Z0-9_-]+\)" 51 55 52 56 def t_NOTE_LENGTH(t): 53 57 r"/?\d+" 54 58 multiplier = float(t.value.strip("/")) 55 59 if t.value.startswith("/"): 56 60 multiplier = 1/multiplier 57 61 t.value = multiplier ................................................................................ 74 78 # Parse (yacc) 75 79 76 80 77 81 def p_pitch_list(p): 78 82 '''score : score note 79 83 | score chord 80 84 | score rest 85 + | score node 81 86 ''' 82 87 p[0] = p[1] + [p[2]] 83 88 84 89 def p_score(p): 85 90 '''score : note 86 91 | chord 87 92 | rest 93 + | node 88 94 ''' 89 95 p[0] = [p[1]] 90 96 91 97 92 98 def p_note(p): 93 99 '''note : pitch 94 100 ''' ................................................................................ 143 149 def p_rest(p): 144 150 ''' rest : REST 145 151 | REST NOTE_LENGTH 146 152 ''' 147 153 p[0] = Rest() 148 154 if len(p) > 2: 149 155 p[0].duration = p[2] 156 + 157 + def p_node(p): 158 + '''node : NODE 159 + ''' 160 + p[0] = tree.Tree(p[1].strip("(").strip(")")) 161 + 150 162 151 163 def p_error(p): 164 + print p 152 165 raise Exception("Syntax error at '%s' of element type %s" % (p.value, p.type)) 153 166 154 167 yacc.yacc() 155 168 156 169 #print yacc.parse("GFG B'AB,, | g/2fg gab | GFG BAB | d2A AFD") 157 170 return yacc.parse(score)
Added tree.py version [29c78902c8].
1 +import pdb 2 + 3 +class Tree(): 4 + def __init__(self, name): 5 + self.nodes = [] 6 + self.name = name 7 + 8 + def traverse_depth_first(self): 9 + all_nodes = [] 10 + for node in self.nodes: 11 + if isinstance(node, Node): 12 + all_nodes.append(node) 13 + elif isinstance(node, Tree): 14 +# pdb.set_trace() 15 + all_nodes.extend(node.traverse_depth_first()) 16 + return all_nodes 17 + 18 + def __str__(self): 19 + return str(self.__unicode__()) 20 + def __unicode__(self): 21 + return self.nodes 22 + 23 + 24 +class Node(): 25 + def __init__(self, parent, data): 26 + self.parent = parent 27 + self.data = data 28 + 29 + def __str__(self): 30 + return str(self.__unicode__()) 31 + def __unicode__(self): 32 + return self.data