spiffyscore

Check-in [3023a967c2]
Login
Overview
SHA1:3023a967c2f9c9a8f36a7dbc13d6b05a0beafa6a
Date: 2011-12-04 20:52:22
User: brian
Comment:Made room for arbitrary-note chording whenever I figure out the CFY to represent iTSYntax to use for it
Timelines: family | ancestors | develop
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2011-12-04
20:52
[3023a967c2] Leaf: Made room for arbitrary-note chording whenever I figure out the CFY to represent iTSYntax to use for it (user: brian, tags: develop)
2011-11-27
02:56
[8f28834102] Fixed chord support (user: brian, tags: develop)
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Modified parse.py from [0884d30f89] to [2894767aa0].

   104    104           p[0] = new_note
   105    105   
   106    106   
   107    107       def p_chord(p):
   108    108           '''chord : BRACKET note BRACKET
   109    109                    | BRACKET note CHORD_TYPE BRACKET
   110    110           '''
   111         -        root_note = p[2].value
   112         -        chorded_notes = []
   113    111   
   114         -        for offset in [0, 4, 7]:
   115         -            chorded_notes.append(Note(root_note+offset, octave=p[2].octave))
          112  +        if len(p) > 3:
          113  +            chorded_notes = []
          114  +            root_note = p[2].value
          115  +            for offset in [0, 4, 7]:
          116  +                chorded_notes.append(Note(root_note+offset, octave=p[2].octave))
          117  +            chord = Chord(notes=chorded_notes)
          118  +        else:  # This won'k work until the grammar is modified to rocognise multiple notes botween brackets
          119  +            chord = p[1:-2]
          120  +                
   116    121   
   117         -        p[0] = Chord(notes=chorded_notes)
          122  +        p[0] = chord
   118    123   
   119    124   
   120    125       def p_note_syncopate(p):
   121    126           ''' note : note SYNCOPATE
   122    127           '''
   123    128           note.syncopate = p[2]
   124    129   

Modified spiffyscore.py from [11e8d97d43] to [1f857f673d].

     1      1   #!/usr/bin/env python
     2      2   
     3      3   from __future__ import division
     4      4   import ipdb
     5      5   import os
            6  +from pprint import pprint
     6      7   import random
     7      8   import sys
     8      9   import time
     9     10   
    10     11   from midiutil.MidiFile import MIDIFile as midifile
    11     12   import parse
    12     13   import topsort
    13     14   import yaml
    14     15   
    15     16   import tree
    16     17   
    17     18   random.seed(time.time())
    18         -mymidi = midifile(15)
           19  +mymidi = midifile(15, deinterleave=True)
    19     20   
    20     21   def main():
    21     22       composition = {
    22     23           "intro": {
    23         -            "body": {
    24         -                "percusion": {
    25         -                    "channel": 14,  # Orchestra kit
    26         -                    "octave": 4,
    27         -                    "duration": 60,
    28         -                    "grammars": {
    29         -                        "u": ["A ^A (u)"]
    30         -                    }
    31         -                },
           24  +            "intro": {
    32     25                   "pan_flute": {
    33     26                       "channel": 8,
    34     27                       "octave": 5,
    35     28                       "duration": 60,
    36     29                       "vol_offset": -15,
    37     30                       "grammars": {  # Notes for this instrument to use in this piece
    38         -                        "u": ["[C2'] C2' | [A3] A3 (u)"],
    39         -#                        "u": ["[C2'] [B2] | [A3] D3 || B | C' | D | C2' C2' | z | (u)", "C2' C2' | C2' C2' | (x)"],
    40         -#                        "v": ["G2 F2 | E2 F2 | D5 (u)", "B/4 C/4' B/4 A/4 | D2 D2 | z | (u)"],
    41         -#                        "x": ["z4 | (v)"],
           31  +                        "u": ["C2' B2 | A3 D3 || B | C' | D | C2' C2' | z | (u)", "C2' C2' | C2' C2' | (x)"],
           32  +                        "v": ["G2 F2 | E2 F2 | D5 (u)", "B/4 C/4' B/4 A/4 | D2 D2 | z | (u)"],
           33  +                        "x": ["z8 | (v)"],
    42     34                       },
           35  +                },
           36  +                "taisho_koto": {
           37  +                    "channel": 11,
           38  +                    "octave": 5,
           39  +                    "duration": 60,
           40  +                    "sync": "pan_flute",
           41  +                    "grammars": {  # Notes for this instrument to use in this piece
           42  +                        "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)"],
           43  +                        "v": ["z4 (v)"],
           44  +                        "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)"],
           45  +                    },
           46  +                },
           47  +                "vibraphone": {
           48  +                    "channel": 14,
           49  +                    "vol_offset": 27,
           50  +                    "octave": 5,
           51  +                    "sync": "pan_flute",
           52  +                    "duration": 60,
           53  +                    "grammars": {  # Notes for this instrument to use in this piece
           54  +                        "u": ["z4 (u)"],
           55  +                        "v": ["G2 F2 | E2 F2 | D5 (u)", "B/4 C/4' B/4 A/4 | D2 D2 | z | (u)"],
           56  +                        "x": ["z4 | (v)"],
           57  +                    },
           58  +                },
           59  +                "percusion": {
           60  +                    "channel": 9,  # Orchestra kit
           61  +                    "octave": 4,
           62  +                    "duration": 60,
           63  +                    "grammars": {
           64  +                        "u": ["A ^A (u)"]
           65  +                    }
    43     66                   },
    44     67                   "bass": {
    45     68                       "channel": 4,
    46     69                       "sync": "pan_flute",
    47     70                       "octave": 2,
    48     71                       "duration": 60,
    49     72                       "grammars": {  # Notes for this instrument to use in this piece
    50         -                        "u": ["C/2 C/2 C/2 z/2 (u)"],
           73  +                        "u": ["C/2 C/2 C (u)"],
           74  +                    },
           75  +                },
           76  +                "horn_timbre1": { 
           77  +                    "channel": 13,  # 'Atmosphere'
           78  +                    "octave": 2,
           79  +                    "duration": 60,
           80  +                    "vol_offset": -15,
           81  +                    "grammars": {  # Notes for this instrument to use in this piece
           82  +                        "u": ["[C]4 [D]4 (u)"],
           83  +                    },
           84  +                },
           85  +            },
           86  +            "body": {
           87  +                "percusion": {
           88  +                    "channel": 9,  # Orchestra kit
           89  +                    "octave": 4,
           90  +                    "duration": 60,
           91  +                    "grammars": {
           92  +                        "u": ["A ^A (u)"]
           93  +                    }
           94  +                },
           95  +                "vibraphone": {
           96  +                    "channel": 14,
           97  +                    "vol_offset": 27,
           98  +                    "octave": 5,
           99  +                    "duration": 60,
          100  +                    "grammars": {  # Notes for this instrument to use in this piece
          101  +                        "u": ["C2' B2 | A3 D3 || B | C' | D | C2' C2' | z | (u)", "C2' C2' | C2' C2' | (x)"],
          102  +                        "v": ["G2 F2 | E2 F2 | D5 (u)", "B/4 C/4' B/4 A/4 | D2 D2 | z | (u)"],
          103  +                        "x": ["z4 | (v)"],
          104  +                    },
          105  +                },
          106  +                "bass": {
          107  +                    "channel": 4,
          108  +                    "sync": "vibraphone",
          109  +                    "octave": 2,
          110  +                    "duration": 60,
          111  +                    "grammars": {  # Notes for this instrument to use in this piece
          112  +                        "u": ["C/2 C/2 C (u)"],
    51    113                       },
    52    114                   },
    53    115                   "horn_timbre1": { 
    54    116                       "channel": 13,  # 'Atmosphere'
    55    117                       "octave": 2,
    56    118                       "duration": 60,
          119  +                    "vol_offset": -15,
    57    120                       "grammars": {  # Notes for this instrument to use in this piece
    58         -                        "u": ["C4 D4 (u)"],
    59         -                    },
    60         -                },
    61         -                "horn_timbre2": {  
    62         -                    "channel": 13,
    63         -                    "octave": 2,
    64         -                    "duration": 60,
    65         -                    "grammars": {  # Notes for this instrument to use in this piece
    66         -                        "u": ["G4 A4 (u)"],
          121  +                        "u": ["[C]4 [D]4 (u)"],
    67    122                       },
    68    123                   },
    69    124               },
    70    125           },
    71    126           "section1": {
          127  +            "intro": {
          128  +                "reverse_cymbal": {
          129  +                    "channel": 5,
          130  +                    "octave": 3,
          131  +                    "duration": 3,
          132  +                    "grammars": {
          133  +                        "u": ["B3"]
          134  +                    }
          135  +                },
          136  +                "bass": {
          137  +                    "channel": 4,
          138  +                    "octave": 2,
          139  +                    "duration": 3,
          140  +                    "grammars": {  # Notes for this instrument to use in this piece
          141  +                        "u": ["C/2 C/2 C (u)"],
          142  +                    },
          143  +                },
          144  +                "percusion": {
          145  +                    "channel": 9,  # Orchestra kit
          146  +                    "octave": 4,
          147  +                    "duration": 3,
          148  +                    "vol_offset": -10,
          149  +                    "grammars": {
          150  +                        "u": ["E,, | z4 | (u)"]
          151  +                    }
          152  +                },
          153  +                "atmosphere1": {
          154  +                    "channel": 13,
          155  +                    "octave": 2,
          156  +                    "duration": 3,
          157  +                    "vol_offset": -15,
          158  +                    "grammars": {  # Notes for this instrument to use in this piece
          159  +                        "u": ["[C]4 (u)"],
          160  +                    },
          161  +                },
          162  +            },
    72    163               "body": {
    73    164                   "guitar": {  # Instrument 'melody'
    74         -                    "channel": 6,
    75         -                    "octave": 5,
          165  +                    "channel": 11,
          166  +                    "octave": 4,
    76    167                       "duration": 60,
    77    168                       "grammars": {  # Notes for this instrument to use in this piece
    78    169                           "u": ["C | E | A | F | G | z | (u)", "C | E | A | F | G | z | (v)"],
    79    170                           "v": ["A/2 D/2 | G/2 C/2 | F/2 B/2 | E/2 | z/2 | (u)"],
    80    171                       },
    81    172                   },
    82    173                   "bass": {  # Instrument 'bass'
................................................................................
    84    175                       "sync": "guitar",
    85    176                       "octave": 2,
    86    177                       "duration": 60,
    87    178                       "grammars": {  # Notes for this instrument to use in this piece
    88    179                           "u": ["C/2 C/2 | C/2 z/2 | (u)"],
    89    180                       },
    90    181                   },
    91         -                "horn_timbre1": {
          182  +                "percusion": {
          183  +                    "channel": 9,  # Orchestra kit
          184  +                    "octave": 4,
          185  +                    "duration": 60,
          186  +                    "vol_offset": -10,
          187  +                    "grammars": {
          188  +                        "u": ["^G,, | ^A,, | ^G,,/2 ^A,,/2 | E,, | z4 | (u)", "E,, | z4 | (u)"]
          189  +                    }
          190  +                },
          191  +                "atmosphere1": {
    92    192                       "channel": 13,
    93    193                       "octave": 2,
          194  +                    "duration": 60,
          195  +                    "vol_offset": -15,
          196  +                    "grammars": {  # Notes for this instrument to use in this piece
          197  +                        "u": ["[C]4 [D]4 (u)"],
          198  +                    },
          199  +                },
          200  +            },
          201  +        },
          202  +        "blackmore1": {
          203  +            "intro": {
          204  +                "guitar": {
          205  +                    "channel": 6,
          206  +                    "octave": 3,
    94    207                       "duration": 60,
    95    208                       "grammars": {  # Notes for this instrument to use in this piece
    96         -                        "u": ["C4 D4 (u)"],
          209  +                        "u": ["C3 | C/4 D/4 E | C/4 C | (u)"],
    97    210                       },
    98    211                   },
    99         -                "horn_timbre2": { 
          212  +                "contrabass": {
   100    213                       "channel": 13,
   101    214                       "octave": 2,
   102    215                       "duration": 60,
          216  +                    "vol_offset": -45,
          217  +                    "grammars": {  # Notes for this instrument to use in this piece
          218  +                        "u": ["[C]4 [D]4 (u)"],
          219  +                    },
          220  +                },
          221  +                "choir": {
          222  +                    "channel": 7,
          223  +                    "octave": 3,
          224  +                    "duration": 60,
          225  +                    "vol_offset": -35,
   103    226                       "grammars": {  # Notes for this instrument to use in this piece
   104         -                        "u": ["G4 A4 (u)"],
          227  +                        "u": ["C2 G2 F2 B,2 C3 (u)"],
   105    228                       },
   106    229                   },
   107    230               },
   108    231           },
   109    232       }
   110    233   
   111    234       section_start = 0
   112         -    for section_name in ["intro", "section1"]:
          235  +    for section_name in ["intro", "section1", "blackmore1"]:
   113    236           print "Section " + section_name
   114    237           subsection_start = section_start
   115    238           section = composition[section_name]
   116    239           for subsection in ["intro", "body", "outro"]:
   117    240               if not section.has_key(subsection):
   118    241                   continue
   119    242               print "\tSubsection " + subsection
................................................................................
   142    265                   max_time = instr["duration"]
   143    266                   instr_score, syncs = render_instr(instr, syncs, max_time)
   144    267                   instrs.append(instr_score)
   145    268   
   146    269                   volume = 100
   147    270                   if instr.has_key("vol_offset"):
   148    271                       volume += instr["vol_offset"]
          272  +                mymidi.addTrackName(track, 0, instr["name"])
   149    273                   midify_instr_score(instr_score, track, instr["channel"], subsection_start, volume=volume)
          274  +                track += 1
   150    275               longest_score = max(instrs, key=lambda i: score_len(i))
   151    276               subsection_start += score_len(longest_score)
   152    277               section_start += score_len(longest_score)
   153         -            track += 1
   154    278       with open("out.mid", "wb") as outfile:
   155    279           mymidi.writeFile(outfile)
   156    280   
   157    281   
   158    282   
   159    283   def render_instr(instr, syncs, max_time):
   160    284       for g in instr["grammars"]:
   161    285           for i in range(len(instr["grammars"][g])):
   162         -            instr["grammars"][g][i] = parse.parse(instr["grammars"][g][i], default_octave=instr["octave"])
          286  +            try:
          287  +                instr["grammars"][g][i] = parse.parse(instr["grammars"][g][i], default_octave=instr["octave"])
          288  +            except topsort.CycleError:
          289  +                print "Your syncs created a loop! Fix it."
          290  +                sys.exit(1)
   163    291   
   164    292       score= []
   165    293       try:
   166    294           score, syncs = choose_phrase(instr, syncs, 0, max_time, None)
   167    295   
   168    296           while True:
   169    297               score_index_to_replace = None
................................................................................
   188    316            fitting_phrases = get_phrases_that_fit(instr["grammars"][grammar], time_remaining)
   189    317            if len(fitting_phrases) > 0:
   190    318               time_filtered_grammars[grammar] = fitting_phrases
   191    319       if len(time_filtered_grammars.keys()) == 0:
   192    320           raise ValueError("No available grammars that will fit in the score")
   193    321   
   194    322       grammar = None
          323  +#    if instr["name"] == "taisho_koto":
          324  +#        ipdb.set_trace()
          325  +
   195    326       if instr["sync"] is not None:
   196    327           guiding_instr = instr["sync"]
   197    328           sync_node = get_sync_node_at_time(syncs[guiding_instr], current_time)
   198    329           if sync_node in time_filtered_grammars.keys():
   199    330               grammar = sync_node
   200    331           else:
   201    332               grammar = random.choice(time_filtered_grammars.keys())