spiffyscore

Check-in [a9e60898b7]
Login
Overview
SHA1:a9e60898b7e8bd115700dcf702f918dff815757d
Date: 2011-10-20 19:56:48
User: brian
Comment:Program now chooses consistent nodes based on what's specified in the grammar. A couple other bugs are still present.
Timelines: family | ancestors | descendants | both | develop
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2011-10-20
20:01
[2ae36c6e68] Program no longer cuts out a note from the score each time it adds a new phrase to the score (user: brian, tags: develop)
19:56
[a9e60898b7] Program now chooses consistent nodes based on what's specified in the grammar. A couple other bugs are still present. (user: brian, tags: develop)
19:21
[601339a475] Fixed the issue with the score being cut short. Now the instruments are changing nodes without my say-so. Alos, they're not always printing complete nodes. (user: brian, tags: develop)
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Modified cfg.py from [47205bbb94] to [9313c52f09].

   106    106           "sync_test": {
   107    107               "body": {
   108    108                   "lead_instr": {  # Instrument 'melody'
   109    109                       "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6",
   110    110                       "octave": 8,
   111    111                       "duration": 30,
   112    112                       "grammars": {  # Notes for this instrument to use in this piece
   113         -                        "u": ["D/4 D/4 D/4 D/4 (u)"],
   114         -                        "v": ["C/4 C/4 C/4 C/4 (v)"],
          113  +                        "u": ["D/4 D/4 D/4 D/4 (v)"],
          114  +                        "v": ["C/4 C/4 C/4 C/4 (u)"],
   115    115                       },
   116    116                   },
   117    117                   "follow_instr": {  # Instrument 'melody'
   118    118                       "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 1",
   119    119                       "sync": "lead_instr",
   120    120                       "octave": 8,
   121    121                       "duration": 30,
................................................................................
   173    173   def render_instr(instr, syncs, max_time):
   174    174       for g in instr["grammars"]:
   175    175           for i in range(len(instr["grammars"][g])):
   176    176               instr["grammars"][g][i] = parse.parse(instr["grammars"][g][i])
   177    177   
   178    178       score= []
   179    179       try:
   180         -        score, syncs = choose_phrase(instr, syncs, 0, max_time)
          180  +        score, syncs = choose_phrase(instr, syncs, 0, max_time, None)
   181    181   
   182    182           while True:
   183    183               score_index_to_replace = None
   184    184               for item in range(len(score)):  # Optimize this by caching the index of the last node I replaced and startng there
   185         -                if isinstance(score[item], tree.Tree):
          185  +                if isinstance(score[item], tree.Tree):  # Also, make this use the find_next_node() function (or whatever I called it)
   186    186                       score_index_to_replace = item
   187    187               if score_index_to_replace is None:
   188    188                   raise ValueError("No more nodes to fill in")
   189    189   
   190    190               time_remaining = max_time - score_len(score)
   191         -            new_phrase, syncs = choose_phrase(instr, syncs, score_len(score), time_remaining)
          191  +            new_phrase, syncs = choose_phrase(instr, syncs, score_len(score), time_remaining, score)
   192    192               score = score[:score_index_to_replace-1] + new_phrase + score[score_index_to_replace+1:]
   193    193   
   194    194       except ValueError:
   195    195           return (score, syncs)
   196    196   
   197    197   
   198         -def choose_phrase(instr, syncs, current_time, time_remaining):
          198  +def choose_phrase(instr, syncs, current_time, time_remaining, score):
   199    199       '''Filters grammars for ones that match the sync option, and phrases that fit the time remaining in the score'''
   200    200       time_filtered_grammars = {}
   201    201       for grammar in instr["grammars"]:
   202    202            fitting_phrases = get_phrases_that_fit(instr["grammars"][grammar], time_remaining)
   203    203            if len(fitting_phrases) > 0:
   204    204               time_filtered_grammars[grammar] = fitting_phrases
   205    205       if len(time_filtered_grammars.keys()) == 0:
................................................................................
   207    207   
   208    208       grammar = None
   209    209       if instr["sync"] is not None:
   210    210           guiding_instr = instr["sync"]
   211    211           sync_node = get_sync_node_at_time(syncs[guiding_instr], current_time)
   212    212           if sync_node in time_filtered_grammars.keys():
   213    213               grammar = sync_node
   214         -    if grammar is None:
          214  +        else:
          215  +            grammar = random.choice(time_filtered_grammars.keys())
          216  +    if score is None:
   215    217           grammar = random.choice(time_filtered_grammars.keys())
          218  +    elif instr["sync"] is None:
          219  +        grammar = get_next_node(score);
   216    220       phrases = time_filtered_grammars[grammar]
   217    221       if instr["name"] not in syncs.keys():
   218    222           syncs[instr["name"]] = []
   219    223       syncs[instr["name"]].append({"node": grammar, "time": current_time})
   220    224   
   221    225       return random.choice(phrases), syncs
   222    226   
................................................................................
   230    234   
   231    235   
   232    236   def get_sync_node_at_time(syncs, t):
   233    237       for s in range(len(syncs)):
   234    238           if syncs[s]["time"] >= t:
   235    239               return syncs[s]["node"]
   236    240   
          241  +def get_next_node(score):
          242  +    for token in score:
          243  +        if isinstance(token, tree.Tree):
          244  +            return token.name
          245  +
   237    246   
   238    247   def score_len(score):
   239    248       total = 0
   240    249       for n in score:
   241    250           if not isinstance(n, tree.Tree):
   242    251               total += n.duration
   243    252       return total