spiffyscore

Diff
Login

Differences From Artifact [6468589fde]:

To Artifact [47205bbb94]:


106
107
108
109
110
111
112
113
114


115
116
117
118
119
120
121
122
123
124


125
126
127
128
129
130
131
106
107
108
109
110
111
112


113
114
115
116
117
118
119
120
121
122


123
124
125
126
127
128
129
130
131







-
-
+
+








-
-
+
+







        "sync_test": {
            "body": {
                "lead_instr": {  # Instrument 'melody'
                    "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6",
                    "octave": 8,
                    "duration": 30,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["D/4 D/4 D/4 D/4"],
                        "v": ["C/4 C/4 C/4 C/4"],
                        "u": ["D/4 D/4 D/4 D/4 (u)"],
                        "v": ["C/4 C/4 C/4 C/4 (v)"],
                    },
                },
                "follow_instr": {  # Instrument 'melody'
                    "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 1",
                    "sync": "lead_instr",
                    "octave": 8,
                    "duration": 30,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["D/4 D/4 D/4 D/4"],
                        "v": ["C/4 C/4 C/4 C/4"],
                        "u": ["D/4 D/4 D/4 D/4 (u)"],
                        "v": ["C/4 C/4 C/4 C/4 (v)"],
                    },
                },
            },
        },
    }
    print '''f1 0 512 10 1
f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08
185
186
187
188
189
190
191
192

193
194
195
196
197
198
199
200
201
202



203
204
205
206
207
208
209
210
211
212
213
214
215
185
186
187
188
189
190
191

192
193
194
195
196
197
198
199
200
201

202
203
204
205
206
207
208


209
210
211
212
213
214
215







-
+









-
+
+
+




-
-







                if isinstance(score[item], tree.Tree):
                    score_index_to_replace = item
            if score_index_to_replace is None:
                raise ValueError("No more nodes to fill in")

            time_remaining = max_time - score_len(score)
            new_phrase, syncs = choose_phrase(instr, syncs, score_len(score), time_remaining)
            score = score[:node_index-1] + new_phrase + score[node_index+1:]
            score = score[:score_index_to_replace-1] + new_phrase + score[score_index_to_replace+1:]

    except ValueError:
        return (score, syncs)


def choose_phrase(instr, syncs, current_time, time_remaining):
    '''Filters grammars for ones that match the sync option, and phrases that fit the time remaining in the score'''
    time_filtered_grammars = {}
    for grammar in instr["grammars"]:
        time_filtered_grammars[grammar] = get_phrases_that_fit(instr["grammars"][grammar], time_remaining)
         fitting_phrases = get_phrases_that_fit(instr["grammars"][grammar], time_remaining)
         if len(fitting_phrases) > 0:
            time_filtered_grammars[grammar] = fitting_phrases
    if len(time_filtered_grammars.keys()) == 0:
        raise ValueError("No available grammars that will fit in the score")

    grammar = None
#    if instr["name"] == "follow_instr":
#        ipdb.set_trace()
    if instr["sync"] is not None:
        guiding_instr = instr["sync"]
        sync_node = get_sync_node_at_time(syncs[guiding_instr], current_time)
        if sync_node in time_filtered_grammars.keys():
            grammar = sync_node
    if grammar is None:
        grammar = random.choice(time_filtered_grammars.keys())