spiffyscore

Diff
Login

Differences From Artifact [390f3e8450]:

To Artifact [caa7e9ffa7]:


1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16

17
18
19
20
21
22
23
#!/usr/bin/env python

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


import parse
import topsort
import yaml

import tree

random.seed(time.time())


def main():
    composition = {
        "fm_test": {
            "intro": {
                "melody": {  # Instrument 'melody'
                    "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2 6 5 1",









>







>







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
#!/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 = {
        "fm_test": {
            "intro": {
                "melody": {  # Instrument 'melody'
                    "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2 6 5 1",
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
                        "u": ["C/4 C/4 C/4 C/4"],
                    },
                },
            },
        },
        "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",
##                    "score_line": "i5 %(time)f %(duration)f 7000 %(octave)d.%(note)s",
#                    "octave": 7,
#                    "duration": 30,
#                    "grammars": {  # Notes for this instrument to use in this piece
##                        "u": ["A ^A B C ^C D ^D E F ^F G ^G"],
#                        "u": ["A/2, B/2, C/2 D/2 (u)", "D/2' D/2' D/2' D/2' (v)"],
#                        "v": ["C/2 C/2 C/2 C/2 (w)"],
#                        "w": ["E/2 F/2 E/2 F/2 (u)"],

#                    },
#                },
                "follow_instr": {  # Instrument 'melody'
#                    "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 1",
                    "score_line": "i3 %(time)f %(duration)f 7000 %(octave)d.%(note)s",
#                    "sync": "lead_instr",
                    "octave": 2,
                    "duration": 30,
                    "grammars": {  # Notes for this instrument to use in this piece
#                        "u": ["A ^A B C ^C D ^D E F ^F G ^G"],
                        "u": ["E F G E (v)"],
                        "v": ["G A A A (e)", "G A A A (v)"],
                        "e": ["B A G A (u)"],

                    },
                },
#                "instr2": {  # Instrument 'melody'
#                    "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6",
#                    "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s",
#                    "octave": 5,
#                    "duration": 30,







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

|
<
|







>







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
                        "u": ["C/4 C/4 C/4 C/4"],
                    },
                },
            },
        },
        "sync_test": {
            "body": {
                "lead_instr": {  # Instrument 'melody'
                    "channel": 6,

                    "octave": 4,
                    "duration": 30,
                    "grammars": {  # Notes for this instrument to use in this piece
#                        "u": ["A ^A B C ^C D ^D E F ^F G ^G"],
                        "u": ["A/2, B/2, C/2 D/2 (u)", "D/2' D/2' D/2' D/2' (x)"],
                        "v": ["C/2 C/2 C/2 C/2 (w)"],
                        "w": ["E/2 F/2 E/2 F/2 (u)"],
                        "x": ["z4 (v)"],
                    },
                },
                "follow_instr": {  # Instrument 'melody'
                    "channel": 0,

                    "sync": "lead_instr",
                    "octave": 2,
                    "duration": 30,
                    "grammars": {  # Notes for this instrument to use in this piece
#                        "u": ["A ^A B C ^C D ^D E F ^F G ^G"],
                        "u": ["E F G E (v)"],
                        "v": ["G A A A (e)", "G A A A (v)"],
                        "e": ["B A G A (u)"],
                        "x": ["z4 (e)"],
                    },
                },
#                "instr2": {  # Instrument 'melody'
#                    "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6",
#                    "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s",
#                    "octave": 5,
#                    "duration": 30,
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
                        subsection[instr]["sync"] = None
                    unordered_instrs.append([subsection[instr]["sync"], instr])
                ordered_instrs = topsort.topsort(unordered_instrs)
                ordered_instrs.remove(None)  # None used as a placeholder for sort order for instruments with no sync setting

                instrs = []
                syncs = {}

                for instr in ordered_instrs:
                    print ";Instrument " + instr
                    instr = subsection[instr]
                    max_time = instr["duration"]
                    instr_score, syncs = render_instr(instr, syncs, max_time)
                    instrs.append(instr_score)
                    for line in generate_csound_score(instr_score, instr["score_line"], subsection_start):
                        print line
                longest_score = max(instrs, key=lambda i: score_len(i))
                subsection_start += score_len(longest_score)
                section_start += score_len(longest_score)

            except KeyError:
                pass





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"])








>






|
<



>


>
>
>







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
                        subsection[instr]["sync"] = None
                    unordered_instrs.append([subsection[instr]["sync"], instr])
                ordered_instrs = topsort.topsort(unordered_instrs)
                ordered_instrs.remove(None)  # None used as a placeholder for sort order for instruments with no sync setting

                instrs = []
                syncs = {}
                track = 0
                for instr in ordered_instrs:
                    print ";Instrument " + instr
                    instr = subsection[instr]
                    max_time = instr["duration"]
                    instr_score, syncs = render_instr(instr, syncs, max_time)
                    instrs.append(instr_score)
                    generate_csound_score(instr_score, track, instr["channel"], subsection_start)

                longest_score = max(instrs, key=lambda i: score_len(i))
                subsection_start += score_len(longest_score)
                section_start += score_len(longest_score)
                track += 1
            except KeyError:
                pass
    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"])

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

298
299
300

301
302
303
304
    total = 0
    for n in score:
        if not isinstance(n, tree.Tree):
            total += n.duration
    return total






def generate_csound_score(score, score_line, t):
    csound_note_values = {
        "C": "00",
        "C#": "01", "Db": "01",
        "D": "02",
        "D#": "03", "Eb": "03",
        "E": "04",
        "F": "05",
        "F#": "06", "Gb": "06",
        "G": "07",
        "G#": "08", "Ab": "08",
        "A": "09",
        "A#": "10", "Bb": "10",
        "B": "11",
    }
    csound_score = []
    for token in score:
        if isinstance(token, parse.Chord):  # Chords
            for note in token.chord: 
                note = csound_note_values[note]
                csound_score.append(score_line % {"time": t, "octave": token.octave, "note": note, "duration": token.duration})
                csound_score.append(score_line % {"time": t, "octave": token.octave, "note": note, "duration": token.duration})

        elif isinstance(token, parse.Note):  # Individual notes
            note = csound_note_values[token.value]
            csound_score.append(score_line % {"time": t, "octave": token.octave, "note": note, "duration": token.duration})

        elif isinstance(token, tree.Tree):
            continue
        t += token.duration

    return csound_score


if __name__ == "__main__": main() 







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



|
<
<
>

|
<
>



>
|



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
298
299
300
301
302
    total = 0
    for n in score:
        if not isinstance(n, tree.Tree):
            total += n.duration
    return total


def get_midi_note(octave, note):
    return note + 12 * (octave+1)


def generate_csound_score(score, track, channel, t):
    # Assume get_midi_note()




    global mymidi









    for token in score:
        if isinstance(token, parse.Chord):  # Chords
            for note in token.chord: 
                note = get_midi_note(token.octave, note)


                mymidi.addNote(track=track, channel=channel,pitch=note, time=t, duration=token.duration, volume=100)
        elif isinstance(token, parse.Note):  # Individual notes
            note = get_midi_note(token.octave, token.value)

            mymidi.addNote(track=track, channel=channel,pitch=note, time=t, duration=token.duration, volume=100)
        elif isinstance(token, tree.Tree):
            continue
        t += token.duration

    return []


if __name__ == "__main__": main()