spiffyscore

Diff
Login

Diff

Differences From Artifact [11e8d97d43]:

To Artifact [1f857f673d]:


1
2
3
4
5

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40























41
42








43
44
45
46
47
48
49
50
51
52
53
54
55
56

57










58

59









60










61
62
63
64

65
66
67
68
69
70
71




































72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90









91
92
93
94

95
96
97
98



99









100
101
102

103
104









105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python

from __future__ import division
import ipdb
import os

import random
import sys
import time

from midiutil.MidiFile import MIDIFile as midifile
import parse
import topsort
import yaml

import tree

random.seed(time.time())
mymidi = midifile(15)

def main():
    composition = {
        "intro": {
            "body": {
                "percusion": {
                    "channel": 14,  # Orchestra kit
                    "octave": 4,
                    "duration": 60,
                    "grammars": {
                        "u": ["A ^A (u)"]
                    }
                },
                "pan_flute": {
                    "channel": 8,
                    "octave": 5,
                    "duration": 60,
                    "vol_offset": -15,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["[C2'] C2' | [A3] A3 (u)"],
#                        "u": ["[C2'] [B2] | [A3] D3 || B | C' | D | C2' C2' | z | (u)", "C2' C2' | C2' C2' | (x)"],
#                        "v": ["G2 F2 | E2 F2 | D5 (u)", "B/4 C/4' B/4 A/4 | D2 D2 | z | (u)"],























#                        "x": ["z4 | (v)"],
                    },








                },
                "bass": {
                    "channel": 4,
                    "sync": "pan_flute",
                    "octave": 2,
                    "duration": 60,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C/2 C/2 C/2 z/2 (u)"],
                    },
                },
                "horn_timbre1": { 
                    "channel": 13,  # 'Atmosphere'
                    "octave": 2,
                    "duration": 60,

                    "grammars": {  # Notes for this instrument to use in this piece










                        "u": ["C4 D4 (u)"],

                    },









                },










                "horn_timbre2": {  
                    "channel": 13,
                    "octave": 2,
                    "duration": 60,

                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["G4 A4 (u)"],
                    },
                },
            },
        },
        "section1": {




































            "body": {
                "guitar": {  # Instrument 'melody'
                    "channel": 6,
                    "octave": 5,
                    "duration": 60,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C | E | A | F | G | z | (u)", "C | E | A | F | G | z | (v)"],
                        "v": ["A/2 D/2 | G/2 C/2 | F/2 B/2 | E/2 | z/2 | (u)"],
                    },
                },
                "bass": {  # Instrument 'bass'
                    "channel": 4,
                    "sync": "guitar",
                    "octave": 2,
                    "duration": 60,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C/2 C/2 | C/2 z/2 | (u)"],
                    },
                },









                "horn_timbre1": {
                    "channel": 13,
                    "octave": 2,
                    "duration": 60,

                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C4 D4 (u)"],
                    },
                },



                "horn_timbre2": { 









                    "channel": 13,
                    "octave": 2,
                    "duration": 60,

                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["G4 A4 (u)"],









                    },
                },
            },
        },
    }

    section_start = 0
    for section_name in ["intro", "section1"]:
        print "Section " + section_name
        subsection_start = section_start
        section = composition[section_name]
        for subsection in ["intro", "body", "outro"]:
            if not section.has_key(subsection):
                continue
            print "\tSubsection " + subsection





>












|




|
<
<
<
<
<
<
<
<






<
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

>
>
>
>
>
>
>
>







|






>

>
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|
|


>

|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|
|















>
>
>
>
>
>
>
>
>
|



>

|


>
>
>
|
>
>
>
>
>
>
>
>
>



>

|
>
>
>
>
>
>
>
>
>







|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24








25
26
27
28
29
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python

from __future__ import division
import ipdb
import os
from pprint import pprint
import random
import sys
import time

from midiutil.MidiFile import MIDIFile as midifile
import parse
import topsort
import yaml

import tree

random.seed(time.time())
mymidi = midifile(15, deinterleave=True)

def main():
    composition = {
        "intro": {
            "intro": {








                "pan_flute": {
                    "channel": 8,
                    "octave": 5,
                    "duration": 60,
                    "vol_offset": -15,
                    "grammars": {  # Notes for this instrument to use in this piece

                        "u": ["C2' B2 | A3 D3 || B | C' | D | C2' C2' | z | (u)", "C2' C2' | C2' C2' | (x)"],
                        "v": ["G2 F2 | E2 F2 | D5 (u)", "B/4 C/4' B/4 A/4 | D2 D2 | z | (u)"],
                        "x": ["z8 | (v)"],
                    },
                },
                "taisho_koto": {
                    "channel": 11,
                    "octave": 5,
                    "duration": 60,
                    "sync": "pan_flute",
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C/2 F/2 | D/2 z/2 | z4 | (u)", "C/2 F/2 | G/2 F/2 | (u)", "A/2 F/2 z/2 C/2 | z | (u)"],
                        "v": ["z4 (v)"],
                        "x": ["C/2 C/2 | F/2 G/2 | F/2, G/2 | z/2 C/2 | (x)", "C/2 F/2 | D/2 C/4 z/4 | F/2 G/2 A/2 C/2 | (x)"],
                    },
                },
                "vibraphone": {
                    "channel": 14,
                    "vol_offset": 27,
                    "octave": 5,
                    "sync": "pan_flute",
                    "duration": 60,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["z4 (u)"],
                        "v": ["G2 F2 | E2 F2 | D5 (u)", "B/4 C/4' B/4 A/4 | D2 D2 | z | (u)"],
                        "x": ["z4 | (v)"],
                    },
                },
                "percusion": {
                    "channel": 9,  # Orchestra kit
                    "octave": 4,
                    "duration": 60,
                    "grammars": {
                        "u": ["A ^A (u)"]
                    }
                },
                "bass": {
                    "channel": 4,
                    "sync": "pan_flute",
                    "octave": 2,
                    "duration": 60,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C/2 C/2 C (u)"],
                    },
                },
                "horn_timbre1": { 
                    "channel": 13,  # 'Atmosphere'
                    "octave": 2,
                    "duration": 60,
                    "vol_offset": -15,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["[C]4 [D]4 (u)"],
                    },
                },
            },
            "body": {
                "percusion": {
                    "channel": 9,  # Orchestra kit
                    "octave": 4,
                    "duration": 60,
                    "grammars": {
                        "u": ["A ^A (u)"]
                    }
                },
                "vibraphone": {
                    "channel": 14,
                    "vol_offset": 27,
                    "octave": 5,
                    "duration": 60,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C2' B2 | A3 D3 || B | C' | D | C2' C2' | z | (u)", "C2' C2' | C2' C2' | (x)"],
                        "v": ["G2 F2 | E2 F2 | D5 (u)", "B/4 C/4' B/4 A/4 | D2 D2 | z | (u)"],
                        "x": ["z4 | (v)"],
                    },
                },
                "bass": {
                    "channel": 4,
                    "sync": "vibraphone",
                    "octave": 2,
                    "duration": 60,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C/2 C/2 C (u)"],
                    },
                },
                "horn_timbre1": { 
                    "channel": 13,  # 'Atmosphere'
                    "octave": 2,
                    "duration": 60,
                    "vol_offset": -15,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["[C]4 [D]4 (u)"],
                    },
                },
            },
        },
        "section1": {
            "intro": {
                "reverse_cymbal": {
                    "channel": 5,
                    "octave": 3,
                    "duration": 3,
                    "grammars": {
                        "u": ["B3"]
                    }
                },
                "bass": {
                    "channel": 4,
                    "octave": 2,
                    "duration": 3,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C/2 C/2 C (u)"],
                    },
                },
                "percusion": {
                    "channel": 9,  # Orchestra kit
                    "octave": 4,
                    "duration": 3,
                    "vol_offset": -10,
                    "grammars": {
                        "u": ["E,, | z4 | (u)"]
                    }
                },
                "atmosphere1": {
                    "channel": 13,
                    "octave": 2,
                    "duration": 3,
                    "vol_offset": -15,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["[C]4 (u)"],
                    },
                },
            },
            "body": {
                "guitar": {  # Instrument 'melody'
                    "channel": 11,
                    "octave": 4,
                    "duration": 60,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C | E | A | F | G | z | (u)", "C | E | A | F | G | z | (v)"],
                        "v": ["A/2 D/2 | G/2 C/2 | F/2 B/2 | E/2 | z/2 | (u)"],
                    },
                },
                "bass": {  # Instrument 'bass'
                    "channel": 4,
                    "sync": "guitar",
                    "octave": 2,
                    "duration": 60,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C/2 C/2 | C/2 z/2 | (u)"],
                    },
                },
                "percusion": {
                    "channel": 9,  # Orchestra kit
                    "octave": 4,
                    "duration": 60,
                    "vol_offset": -10,
                    "grammars": {
                        "u": ["^G,, | ^A,, | ^G,,/2 ^A,,/2 | E,, | z4 | (u)", "E,, | z4 | (u)"]
                    }
                },
                "atmosphere1": {
                    "channel": 13,
                    "octave": 2,
                    "duration": 60,
                    "vol_offset": -15,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["[C]4 [D]4 (u)"],
                    },
                },
            },
        },
        "blackmore1": {
            "intro": {
                "guitar": {
                    "channel": 6,
                    "octave": 3,
                    "duration": 60,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C3 | C/4 D/4 E | C/4 C | (u)"],
                    },
                },
                "contrabass": {
                    "channel": 13,
                    "octave": 2,
                    "duration": 60,
                    "vol_offset": -45,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["[C]4 [D]4 (u)"],
                    },
                },
                "choir": {
                    "channel": 7,
                    "octave": 3,
                    "duration": 60,
                    "vol_offset": -35,
                    "grammars": {  # Notes for this instrument to use in this piece
                        "u": ["C2 G2 F2 B,2 C3 (u)"],
                    },
                },
            },
        },
    }

    section_start = 0
    for section_name in ["intro", "section1", "blackmore1"]:
        print "Section " + section_name
        subsection_start = section_start
        section = composition[section_name]
        for subsection in ["intro", "body", "outro"]:
            if not section.has_key(subsection):
                continue
            print "\tSubsection " + subsection
142
143
144
145
146
147
148

149

150
151
152
153
154
155
156
157
158
159
160
161

162



163
164
165
166
167
168
169
                max_time = instr["duration"]
                instr_score, syncs = render_instr(instr, syncs, max_time)
                instrs.append(instr_score)

                volume = 100
                if instr.has_key("vol_offset"):
                    volume += instr["vol_offset"]

                midify_instr_score(instr_score, track, instr["channel"], subsection_start, volume=volume)

            longest_score = max(instrs, key=lambda i: score_len(i))
            subsection_start += score_len(longest_score)
            section_start += score_len(longest_score)
            track += 1
    with open("out.mid", "wb") as outfile:
        mymidi.writeFile(outfile)



def render_instr(instr, syncs, max_time):
    for g in instr["grammars"]:
        for i in range(len(instr["grammars"][g])):

            instr["grammars"][g][i] = parse.parse(instr["grammars"][g][i], default_octave=instr["octave"])




    score= []
    try:
        score, syncs = choose_phrase(instr, syncs, 0, max_time, None)

        while True:
            score_index_to_replace = None







>

>



<








>
|
>
>
>







265
266
267
268
269
270
271
272
273
274
275
276
277

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
                max_time = instr["duration"]
                instr_score, syncs = render_instr(instr, syncs, max_time)
                instrs.append(instr_score)

                volume = 100
                if instr.has_key("vol_offset"):
                    volume += instr["vol_offset"]
                mymidi.addTrackName(track, 0, instr["name"])
                midify_instr_score(instr_score, track, instr["channel"], subsection_start, volume=volume)
                track += 1
            longest_score = max(instrs, key=lambda i: score_len(i))
            subsection_start += score_len(longest_score)
            section_start += score_len(longest_score)

    with open("out.mid", "wb") as outfile:
        mymidi.writeFile(outfile)



def render_instr(instr, syncs, max_time):
    for g in instr["grammars"]:
        for i in range(len(instr["grammars"][g])):
            try:
                instr["grammars"][g][i] = parse.parse(instr["grammars"][g][i], default_octave=instr["octave"])
            except topsort.CycleError:
                print "Your syncs created a loop! Fix it."
                sys.exit(1)

    score= []
    try:
        score, syncs = choose_phrase(instr, syncs, 0, max_time, None)

        while True:
            score_index_to_replace = None
188
189
190
191
192
193
194



195
196
197
198
199
200
201
         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["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
        else:
            grammar = random.choice(time_filtered_grammars.keys())







>
>
>







316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
         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"] == "taisho_koto":
#        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
        else:
            grammar = random.choice(time_filtered_grammars.keys())