spiffyscore
File Annotation
Not logged in
6bfc2449ff 2010-11-04 spiffytech@gm: #!/usr/bin/env python
6bfc2449ff 2010-11-04 spiffytech@gm: 
5a35ffdd27 2010-11-17 spiffytech@gm: from __future__ import division
6bfc2449ff 2010-11-04 spiffytech@gm: import os
6bfc2449ff 2010-11-04 spiffytech@gm: import random
6bfc2449ff 2010-11-04 spiffytech@gm: import sys
6bfc2449ff 2010-11-04 spiffytech@gm: import time
6bfc2449ff 2010-11-04 spiffytech@gm: random.seed(time.time())
9bd31df856 2010-11-16 spiffytech@gm: import parse
6a17d4d36a 2010-11-12 spiffytech@gm: 
6a17d4d36a 2010-11-12 spiffytech@gm: def main():
729263ecd0 2010-11-13 spiffytech@gm:     key = "A"
e6bf0dbf82 2010-11-18 spiffytech@gm:     bps = 60/60
5a35ffdd27 2010-11-17 spiffytech@gm:     tempo = 1/bps
aa88358397 2010-11-17 spiffytech@gm:     max_duration = 1
7c7ce6adb8 2010-11-17 spiffytech@gm: 
7c7ce6adb8 2010-11-17 spiffytech@gm:     composition = {
7c7ce6adb8 2010-11-17 spiffytech@gm:         "a": {  # Movement block 'a' for reuse throughout the piece
4ab39b23ec 2010-11-29 spiffytech@gm: #            "melody": {  # Instrument 'melody'
4ab39b23ec 2010-11-29 spiffytech@gm: #                "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
4ab39b23ec 2010-11-29 spiffytech@gm: #                "octave": 8,
4ab39b23ec 2010-11-29 spiffytech@gm: #                "duration": 40,
4ab39b23ec 2010-11-29 spiffytech@gm: #                "grammars": {  # Notes for this instrument to use in this piece
4ab39b23ec 2010-11-29 spiffytech@gm: #                    "u": ["I V/2 V/2 V/2 I VII, IV' x u", "I IV w w", "I VII IV u u"],
4ab39b23ec 2010-11-29 spiffytech@gm: #                    "w": ['VII I IV V VI u', 'w w'],
4ab39b23ec 2010-11-29 spiffytech@gm: #                    "x": ['VI/4 III/4 II/4 I/4 w', 'x x'],
4ab39b23ec 2010-11-29 spiffytech@gm: #                },
4ab39b23ec 2010-11-29 spiffytech@gm: #                "score": "u",
4ab39b23ec 2010-11-29 spiffytech@gm: #            },
4ab39b23ec 2010-11-29 spiffytech@gm: #            "rhythm": {
4ab39b23ec 2010-11-29 spiffytech@gm: #                "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6",
4ab39b23ec 2010-11-29 spiffytech@gm: #                "octave": 7,
4ab39b23ec 2010-11-29 spiffytech@gm: #                "duration": 50,
4ab39b23ec 2010-11-29 spiffytech@gm: #                "grammars": {
4ab39b23ec 2010-11-29 spiffytech@gm: #                    "u": ['"I" "ii"/4 "ii"/4 "IV"/2 "V"2 "IV" "ii" x u', '"I" "vii" "III" y u', '"I" "v" "IV" u u'],
4ab39b23ec 2010-11-29 spiffytech@gm: #                    "w": ['"i" "VII"2 "VI"/4 "V"/4 "i"/4 "VII"2 "VI" "V" w u'],
4ab39b23ec 2010-11-29 spiffytech@gm: #                    "x": ['"III/2" "VI"/2 "III"/2 "vii"2 "i"2 "V" u'],
4ab39b23ec 2010-11-29 spiffytech@gm: #                    "y": ['"I" "vi"2 "IV" "V" y y u'],
4ab39b23ec 2010-11-29 spiffytech@gm: #                },
4ab39b23ec 2010-11-29 spiffytech@gm: #                "score": "u x u y x w u",
4ab39b23ec 2010-11-29 spiffytech@gm: #            },
4ab39b23ec 2010-11-29 spiffytech@gm:             "timbre": {
4ab39b23ec 2010-11-29 spiffytech@gm:                 "score_line": "i3 %(time)f %(duration)f 5000 %(octave)d.%(note)s 2 3 5 3",
4ab39b23ec 2010-11-29 spiffytech@gm:                 "octave": 5,
d9af5998b9 2010-11-18 spiffytech@gm:                 "duration": 40,
db1df2f460 2010-11-17 spiffytech@gm:                 "grammars": {  # Notes for this instrument to use in this piece
4ab39b23ec 2010-11-29 spiffytech@gm:                     "u": ['I2 I2 V VI I/2 IV/2 "V"2 "I"2']
db1df2f460 2010-11-17 spiffytech@gm:                 },
4ab39b23ec 2010-11-29 spiffytech@gm:                 "score": "u u u u u",
db1df2f460 2010-11-17 spiffytech@gm:             },
e6bf0dbf82 2010-11-18 spiffytech@gm:         },
1987470dfc 2010-11-18 spiffytech@gm:         "b": {  # Movement block 'a' for reuse throughout the piece
1987470dfc 2010-11-18 spiffytech@gm:             "melody": {  # Instrument 'melody'
1987470dfc 2010-11-18 spiffytech@gm:                 "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
1987470dfc 2010-11-18 spiffytech@gm:                 "octave": 8,
d9af5998b9 2010-11-18 spiffytech@gm:                 "duration": 20,
d9af5998b9 2010-11-18 spiffytech@gm:                 "grammars": {  # Notes for this instrument to use in this piece
d9af5998b9 2010-11-18 spiffytech@gm:                     "u": ['I VII V III u', "y"],
d9af5998b9 2010-11-18 spiffytech@gm:                     "w": ['VII I IV V VI u', 'w w'],
d9af5998b9 2010-11-18 spiffytech@gm:                     "x": ['VI/4 III/4 II/4 I/4 w', 'x x'],
d9af5998b9 2010-11-18 spiffytech@gm:                     "y": ["III/4 VI/4 II/4 V/4 VI/4 IV/4 VII2"],
d9af5998b9 2010-11-18 spiffytech@gm:                 },
d9af5998b9 2010-11-18 spiffytech@gm:                 "score": "w w x x w",
d9af5998b9 2010-11-18 spiffytech@gm:             },
d9af5998b9 2010-11-18 spiffytech@gm:             "rhythm": {
d9af5998b9 2010-11-18 spiffytech@gm:                 "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6",
d9af5998b9 2010-11-18 spiffytech@gm:                 "octave": 7,
d9af5998b9 2010-11-18 spiffytech@gm:                 "duration": 24,
d9af5998b9 2010-11-18 spiffytech@gm:                 "grammars": {
d9af5998b9 2010-11-18 spiffytech@gm:                     "u": ['"I" "V" "vi" "iii" "IV" "I" "IV" "V" u u', "y"],
d9af5998b9 2010-11-18 spiffytech@gm:                     "y": ['"I" "vi"2 "IV" "V" y y u'],
d9af5998b9 2010-11-18 spiffytech@gm:                 },
d9af5998b9 2010-11-18 spiffytech@gm:                 "score": "u u y y u",
d9af5998b9 2010-11-18 spiffytech@gm:             },
d9af5998b9 2010-11-18 spiffytech@gm:         },
d9af5998b9 2010-11-18 spiffytech@gm:         "c": {  # Movement block 'a' for reuse throughout the piece
d9af5998b9 2010-11-18 spiffytech@gm:             "melody": {  # Instrument 'melody'
d9af5998b9 2010-11-18 spiffytech@gm:                 "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
d9af5998b9 2010-11-18 spiffytech@gm:                 "octave": 8,
d9af5998b9 2010-11-18 spiffytech@gm:                 "duration": 20,
d9af5998b9 2010-11-18 spiffytech@gm:                 "grammars": {  # Notes for this instrument to use in this piece
d9af5998b9 2010-11-18 spiffytech@gm:                     "u": ['I VI/2 VI/2 IV/2 u', "y"],
d9af5998b9 2010-11-18 spiffytech@gm:                     "w": ['VII/2 I/2 II/2 V/2 u', 'w w'],
d9af5998b9 2010-11-18 spiffytech@gm:                     "x": ['VI/4 III/4 II/4 I/4 w', 'x x'],
d9af5998b9 2010-11-18 spiffytech@gm:                     "y": ["III/4 VI/4 II/4 V/4 VI/4 IV/4 VII2"],
d9af5998b9 2010-11-18 spiffytech@gm:                 },
d9af5998b9 2010-11-18 spiffytech@gm:                 "score": "w w x x w",
d9af5998b9 2010-11-18 spiffytech@gm:             },
d9af5998b9 2010-11-18 spiffytech@gm:             "rhythm": {
d9af5998b9 2010-11-18 spiffytech@gm:                 "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6",
d9af5998b9 2010-11-18 spiffytech@gm:                 "octave": 7,
d9af5998b9 2010-11-18 spiffytech@gm:                 "duration": 20,
d9af5998b9 2010-11-18 spiffytech@gm:                 "grammars": {
d9af5998b9 2010-11-18 spiffytech@gm:                     "u": ['"I"/2 "V"/2 "vi"/2 "iii"/2 "IV"/2 "I"/2 "IV"/2 "V"/2 u u', "y"],
d9af5998b9 2010-11-18 spiffytech@gm:                     "y": ['"I"/2 "vi" "IV"/2 "V"/2 y y u'],
d9af5998b9 2010-11-18 spiffytech@gm:                 },
d9af5998b9 2010-11-18 spiffytech@gm:                 "score": "u u y y u",
d9af5998b9 2010-11-18 spiffytech@gm:             },
d9af5998b9 2010-11-18 spiffytech@gm:         },
d9af5998b9 2010-11-18 spiffytech@gm:         "d": {  # Movement block 'a' for reuse throughout the piece
d9af5998b9 2010-11-18 spiffytech@gm:             "melody": {  # Instrument 'melody'
d9af5998b9 2010-11-18 spiffytech@gm:                 "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
d9af5998b9 2010-11-18 spiffytech@gm:                 "octave": 8,
01b7d73285 2010-11-18 spiffytech@gm:                 "duration": 20,
1987470dfc 2010-11-18 spiffytech@gm:                 "grammars": {  # Notes for this instrument to use in this piece
e16a1f5203 2010-11-18 spiffytech@gm:                     "u": ['I VII V III u', "y"],
e16a1f5203 2010-11-18 spiffytech@gm:                     "w": ['VII I IV V VI u', 'w w'],
e16a1f5203 2010-11-18 spiffytech@gm:                     "x": ['VI/4 III/4 II/4 I/4 w', 'x x'],
e16a1f5203 2010-11-18 spiffytech@gm:                     "y": ["III/4 VI/4 II/4 V/4 VI/4 IV/4 VII2"],
1987470dfc 2010-11-18 spiffytech@gm:                 },
1987470dfc 2010-11-18 spiffytech@gm:                 "score": "w w x x w",
1987470dfc 2010-11-18 spiffytech@gm:             },
1987470dfc 2010-11-18 spiffytech@gm:             "rhythm": {
1987470dfc 2010-11-18 spiffytech@gm:                 "score_line": "i1 %(time)f %(duration)f 7000 %(octave)d.%(note)s %(octave)d.%(note)s 0 6",
1987470dfc 2010-11-18 spiffytech@gm:                 "octave": 7,
d9af5998b9 2010-11-18 spiffytech@gm:                 "duration": 24,
1987470dfc 2010-11-18 spiffytech@gm:                 "grammars": {
e16a1f5203 2010-11-18 spiffytech@gm:                     "u": ['"I" "V" "vi" "iii" "IV" "I" "IV" "V" u u', "y"],
e16a1f5203 2010-11-18 spiffytech@gm:                     "y": ['"I" "vi"2 "IV" "V" y y u'],
1987470dfc 2010-11-18 spiffytech@gm:                 },
1987470dfc 2010-11-18 spiffytech@gm:                 "score": "u u y y u",
1987470dfc 2010-11-18 spiffytech@gm:             },
1987470dfc 2010-11-18 spiffytech@gm:         },
5aa14570f1 2010-11-13 spiffytech@gm:     }
7c7ce6adb8 2010-11-17 spiffytech@gm: 
db1df2f460 2010-11-17 spiffytech@gm:     max_t = 0  # max time encountered so far. Used for movement timing
d9af5998b9 2010-11-18 spiffytech@gm:     progression = "a b c d"
db1df2f460 2010-11-17 spiffytech@gm:     for comp_name in progression.split():
d9af5998b9 2010-11-18 spiffytech@gm:         comp_start_time = max_t
db1df2f460 2010-11-17 spiffytech@gm:         for instr_name, instr in composition[comp_name].iteritems():
7c7ce6adb8 2010-11-17 spiffytech@gm:             generated_score = generate_score(instr["score"], instr["grammars"])  # Fill in the scores by generating them based on the grammars
1987470dfc 2010-11-18 spiffytech@gm: #            print generated_score
e6bf0dbf82 2010-11-18 spiffytech@gm:             score = parse.parse(generated_score, default_octave=instr["octave"])  # Return Node/Chord objects
5aa14570f1 2010-11-13 spiffytech@gm: 
7c7ce6adb8 2010-11-17 spiffytech@gm:             # Generate timestamps for the notes
d9af5998b9 2010-11-18 spiffytech@gm:             t = comp_start_time
7c7ce6adb8 2010-11-17 spiffytech@gm:             for note in range(len(score)):
7c7ce6adb8 2010-11-17 spiffytech@gm:                 score[note].time = t
5a35ffdd27 2010-11-17 spiffytech@gm:                 score[note].duration *= tempo
7c7ce6adb8 2010-11-17 spiffytech@gm:                 t += score[note].duration
d9af5998b9 2010-11-18 spiffytech@gm: #                print "time difference =", t-comp_start_time
d9af5998b9 2010-11-18 spiffytech@gm: #                print "instrument duration =",composition[comp_name][instr_name]["duration"]
d9af5998b9 2010-11-18 spiffytech@gm:                 if (t-comp_start_time) > float(composition[comp_name][instr_name]["duration"]):
d9af5998b9 2010-11-18 spiffytech@gm: #                    print "here"
d9af5998b9 2010-11-18 spiffytech@gm:                     score = score[:note]
d9af5998b9 2010-11-18 spiffytech@gm:                     break
db1df2f460 2010-11-17 spiffytech@gm:                 max_t = t if t > max_t else max_t
7c7ce6adb8 2010-11-17 spiffytech@gm:             composition[comp_name][instr_name]["score"] = score
6a17d4d36a 2010-11-12 spiffytech@gm: 
db1df2f460 2010-11-17 spiffytech@gm:     # Must be done after all note times keyed in, else you can't coordinate melodies with the rhythm chords
1987470dfc 2010-11-18 spiffytech@gm:     print '''f1  0  512  10  1
1987470dfc 2010-11-18 spiffytech@gm:             f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08
4ab39b23ec 2010-11-29 spiffytech@gm:             f3 0 1025 10 1
1987470dfc 2010-11-18 spiffytech@gm:     '''
db1df2f460 2010-11-17 spiffytech@gm:     for comp_name in progression.split():
db1df2f460 2010-11-17 spiffytech@gm:         for instr_name, instr in composition[comp_name].iteritems():
7c7ce6adb8 2010-11-17 spiffytech@gm:             composition[comp_name][instr_name]["score"] = transliterate_score(composition[comp_name][instr_name]["score"], key)
7c7ce6adb8 2010-11-17 spiffytech@gm: #            print "\nMovement %s instrument %s" % (comp_name, instr_name)
7c7ce6adb8 2010-11-17 spiffytech@gm: #            print composition[comp_name][instr_name]["score"]
b8d35587a4 2010-11-17 spiffytech@gm:             final_score = generate_csound_score(composition[comp_name][instr_name]["score"], composition[comp_name][instr_name]["score_line"])
7c7ce6adb8 2010-11-17 spiffytech@gm:             for line in final_score:
7c7ce6adb8 2010-11-17 spiffytech@gm:                 print line
7c7ce6adb8 2010-11-17 spiffytech@gm: 
338933c1a8 2010-11-12 spiffytech@gm: 
6a17d4d36a 2010-11-12 spiffytech@gm: def make_scale(key):
6a17d4d36a 2010-11-12 spiffytech@gm:     notes = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]
6a17d4d36a 2010-11-12 spiffytech@gm:     scale = [key]
6a17d4d36a 2010-11-12 spiffytech@gm:     pos = notes.index(key)
6a17d4d36a 2010-11-12 spiffytech@gm:     progression = [2,2,1,2,2,2,1]
6a17d4d36a 2010-11-12 spiffytech@gm:     for p in progression:
6a17d4d36a 2010-11-12 spiffytech@gm:         pos = (pos + p) % 12
6a17d4d36a 2010-11-12 spiffytech@gm:         scale.append(notes[pos])
6a17d4d36a 2010-11-12 spiffytech@gm:     return scale
6a17d4d36a 2010-11-12 spiffytech@gm: 
338933c1a8 2010-11-12 spiffytech@gm: 
5aa14570f1 2010-11-13 spiffytech@gm: def generate_score(score, grammars):
6a17d4d36a 2010-11-12 spiffytech@gm:     while 1:
6a17d4d36a 2010-11-12 spiffytech@gm:         found_substitution = False
6a17d4d36a 2010-11-12 spiffytech@gm:         for key,value in grammars.iteritems():
6a17d4d36a 2010-11-12 spiffytech@gm:             if score.find(key) != -1:
6a17d4d36a 2010-11-12 spiffytech@gm:                 found_substitution = True
6a17d4d36a 2010-11-12 spiffytech@gm:                 while score.find(key) != -1:
6a17d4d36a 2010-11-12 spiffytech@gm:                     score = score.replace(key, random.choice(grammars[key]), 1)
d9af5998b9 2010-11-18 spiffytech@gm: #                    print scoe
d9af5998b9 2010-11-18 spiffytech@gm:                     if len(score.split()) > 2000:
1987470dfc 2010-11-18 spiffytech@gm:                         for k in grammars.keys():
1987470dfc 2010-11-18 spiffytech@gm:                             score = score.replace(k, "")
9bd31df856 2010-11-16 spiffytech@gm:                         return score
6a17d4d36a 2010-11-12 spiffytech@gm:         if found_substitution is False:
6a17d4d36a 2010-11-12 spiffytech@gm:             break
6a17d4d36a 2010-11-12 spiffytech@gm:     return score
6bfc2449ff 2010-11-04 spiffytech@gm: 
7c7ce6adb8 2010-11-17 spiffytech@gm: def transliterate_score(score, key):
6a17d4d36a 2010-11-12 spiffytech@gm:     scale = make_scale(key)
6a17d4d36a 2010-11-12 spiffytech@gm:     scale_conversion = {
6a17d4d36a 2010-11-12 spiffytech@gm:         "I": 1,
6a17d4d36a 2010-11-12 spiffytech@gm:         "II": 2,
6a17d4d36a 2010-11-12 spiffytech@gm:         "III": 3,
6a17d4d36a 2010-11-12 spiffytech@gm:         "IV": 4,
6a17d4d36a 2010-11-12 spiffytech@gm:         "V": 5,
6a17d4d36a 2010-11-12 spiffytech@gm:         "VI": 6,
6a17d4d36a 2010-11-12 spiffytech@gm:         "VII": 7,
6a17d4d36a 2010-11-12 spiffytech@gm:         "VIII": 8,
6a17d4d36a 2010-11-12 spiffytech@gm:     }
6a17d4d36a 2010-11-12 spiffytech@gm:     keyed_score = []
7c7ce6adb8 2010-11-17 spiffytech@gm:     for i in range(len(score)):
7c7ce6adb8 2010-11-17 spiffytech@gm:         if isinstance(score[i], parse.Note):
9bd31df856 2010-11-16 spiffytech@gm:             score[i].value = scale[scale_conversion[score[i].value]-1]
aa88358397 2010-11-17 spiffytech@gm:         elif isinstance(score[i], parse.Chord):
5aa14570f1 2010-11-13 spiffytech@gm:             chord = []
9bd31df856 2010-11-16 spiffytech@gm:             root_note_index = scale.index(key) + scale_conversion[score[i].value]
5aa14570f1 2010-11-13 spiffytech@gm:             chord.append(scale[root_note_index])
1987470dfc 2010-11-18 spiffytech@gm:             chord.append(scale[(root_note_index+3) % 8])
1987470dfc 2010-11-18 spiffytech@gm:             if score[i].chord_type == "m":  # Minor chords, flat the 5th
1987470dfc 2010-11-18 spiffytech@gm:                 chord.append(scale[(root_note_index+4) % 8])
f02f66468f 2010-11-17 spiffytech@gm:             else:
1987470dfc 2010-11-18 spiffytech@gm:                 chord.append(scale[(root_note_index+5) % 8])
9bd31df856 2010-11-16 spiffytech@gm:             score[i].chord = chord
aa88358397 2010-11-17 spiffytech@gm:         elif isinstance(score[i], parse.Rest):
aa88358397 2010-11-17 spiffytech@gm:             pass
9bd31df856 2010-11-16 spiffytech@gm:     return score
6a17d4d36a 2010-11-12 spiffytech@gm: 
6bfc2449ff 2010-11-04 spiffytech@gm: 
b8d35587a4 2010-11-17 spiffytech@gm: def generate_csound_score(score, score_line):
6a17d4d36a 2010-11-12 spiffytech@gm:     csound_note_values = {
6a17d4d36a 2010-11-12 spiffytech@gm:         "C": "00",
6a17d4d36a 2010-11-12 spiffytech@gm:         "C#": "01",
6a17d4d36a 2010-11-12 spiffytech@gm:         "D": "02",
6a17d4d36a 2010-11-12 spiffytech@gm:         "D#": "03",
6a17d4d36a 2010-11-12 spiffytech@gm:         "E": "04",
6a17d4d36a 2010-11-12 spiffytech@gm:         "F": "05",
6a17d4d36a 2010-11-12 spiffytech@gm:         "F#": "06",
6a17d4d36a 2010-11-12 spiffytech@gm:         "G": "07",
3cd7a5dcd0 2010-11-12 spiffytech@gm:         "G#": "08",
6a17d4d36a 2010-11-12 spiffytech@gm:         "A": "09",
6a17d4d36a 2010-11-12 spiffytech@gm:         "A#": "10",
6a17d4d36a 2010-11-12 spiffytech@gm:         "B": "11",
6a17d4d36a 2010-11-12 spiffytech@gm:     }
6a17d4d36a 2010-11-12 spiffytech@gm:     csound_score = []
6a17d4d36a 2010-11-12 spiffytech@gm:     for token in score:
9bd31df856 2010-11-16 spiffytech@gm:         if isinstance(token, parse.Chord):  # Chords
9bd31df856 2010-11-16 spiffytech@gm:             for note in token.chord:
5aa14570f1 2010-11-13 spiffytech@gm:                 note = csound_note_values[note]
4ab39b23ec 2010-11-29 spiffytech@gm:                 csound_score.append(score_line % {"time": token.time, "octave": token.octave, "note": note, "duration": token.duration})
aa88358397 2010-11-17 spiffytech@gm:         elif isinstance(token, parse.Note):  # Individual notes
9bd31df856 2010-11-16 spiffytech@gm:             note = csound_note_values[token.value]
e6bf0dbf82 2010-11-18 spiffytech@gm:             csound_score.append(score_line % {"time": token.time, "octave": token.octave, "note": note, "duration": token.duration})
6a17d4d36a 2010-11-12 spiffytech@gm:     return csound_score
338933c1a8 2010-11-12 spiffytech@gm: 
6bfc2449ff 2010-11-04 spiffytech@gm: 
6a17d4d36a 2010-11-12 spiffytech@gm: if __name__ == "__main__": main()