spiffyscore

Check-in [7a6ab725ee]
Login
Overview
Comment:Program prints stuff out again
Timelines: family | ancestors | descendants | both | feature/abc
Files: files | file ages | folders
SHA1: 7a6ab725ee01d778ec16889b12b144f93629d769
User & Date: brian on 2011-09-15 19:09:43
Other Links: branch diff | manifest | tags
Context
2011-09-15
19:19
Movements work again check-in: a9b80ad75f user: brian tags: feature/abc
19:09
Program prints stuff out again check-in: 7a6ab725ee user: brian tags: feature/abc
15:28
Got the program to run through again. Now without the roman numerals check-in: 33ddc6fee4 user: brian tags: feature/abc
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Modified cfg.py from [d386f64212] to [6f8ab10545].

     1      1   #!/usr/bin/env python
     2      2   
     3      3   from __future__ import division
     4      4   import os
            5  +import pdb
     5      6   import random
     6      7   import sys
     7      8   import time
     8         -random.seed(time.time())
     9      9   import parse
    10     10   
           11  +import tree
           12  +
           13  +random.seed(time.time())
           14  +
    11     15   def main():
    12     16       key = "A"
    13     17       bps = 60/60
    14     18       tempo = 1/bps
    15     19       max_duration = 1
    16     20   
    17     21       composition = {
    18     22           "overview": {
    19     23               "melody": {  # Instrument 'melody'
    20     24                   "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
    21     25                   "octave": 8,
    22     26                   "duration": 40,
    23     27                   "grammars": {  # Notes for this instrument to use in this piece
    24         -                    "u": ["C G/2 G/2 G/2 C B, F' C F C B F"],
           28  +                    "u": ["C G/2 G/2 G/2 C B, F' C F C B F (u)"],
    25     29   #                    "w": ['E/4 A/4 D/4 G/4 F/4 F/4 B2'],
    26     30                   },
    27     31                   "score": "u u",
    28     32               },
    29     33           },
    30     34       }
    31         -
    32         -    max_t = 0  # max time encountered so far. Used for movement timing
    33         -    progression = "overview"
    34         -#    progression = "zelda1"
    35         -    for comp_name in progression.split():
    36         -        comp_start_time = max_t
    37         -        for instr_name, instr in composition[comp_name].iteritems():
    38         -            generated_score = generate_score(instr["score"], instr["grammars"])  # Fill in the scores by generating them based on the grammars
    39         -#            print generated_score
    40         -            print generated_score
    41         -            score = parse.parse(generated_score, default_octave=instr["octave"])  # Return Node/Chord objects
    42         -
    43         -            # Generate timestamps for the notes 
    44         -            t = comp_start_time
    45         -            for note in range(len(score)):
    46         -                score[note].time = t
    47         -                score[note].duration *= tempo
    48         -                t += score[note].duration
    49         -#                print "time difference =", t-comp_start_time
    50         -#                print "instrument duration =",composition[comp_name][instr_name]["duration"]
    51         -                if (t-comp_start_time) > float(composition[comp_name][instr_name]["duration"]):
    52         -#                    print "here"
    53         -                    score = score[:note]
    54         -                    break
    55         -                max_t = t if t > max_t else max_t
    56         -            composition[comp_name][instr_name]["score"] = score
    57         -
    58         -    # Must be done after all note times keyed in, else you can't coordinate melodies with the rhythm chords
    59     35       print '''f1  0  512  10  1
    60     36               f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08
    61     37               f3 0 1025 10 1
    62     38       '''
           39  +    movement_start = 0
           40  +
           41  +
           42  +    max_t = 0  # max time encountered so far. Used for movement timing
           43  +    progression = "overview"
    63     44       for comp_name in progression.split():
    64         -        print "; Movement:", comp_name
           45  +        comp_start_time = max_t
           46  +        # We need an arbitrary grammar from this instrument to start the score with
           47  +        max_instr =  0
           48  +        movement_start += max_instr
    65     49           for instr_name, instr in composition[comp_name].iteritems():
    66         -#            composition[comp_name][instr_name]["score"] = transliterate_score(composition[comp_name][instr_name]["score"], key)
    67         -#            print "\nMovement %s instrument %s" % (comp_name, instr_name)
    68         -#            print composition[comp_name][instr_name]["score"] 
    69         -            final_score = generate_csound_score(composition[comp_name][instr_name]["score"], composition[comp_name][instr_name]["score_line"])
    70         -            for line in final_score:
    71         -                print line
           50  +            for grammar in instr["grammars"]:
           51  +                for g in range(len(instr["grammars"][grammar])):
           52  +                    instr["grammars"][grammar][g] = parse.parse(instr["grammars"][grammar][g], default_octave=instr["octave"])
           53  +            g = random.choice(instr["grammars"].keys())
           54  +            ins_score = random.choice(instr["grammars"][g])
           55  +#            ins_score = instr["grammars"][g]
           56  +            score_complete = False
           57  +            while score_complete is False:
           58  +                if score_len(ins_score) >= 50:
           59  +                    score_complete = True
           60  +                    break
           61  +                for i in range(len(ins_score)):
           62  +                    if isinstance(ins_score[i], tree.Tree):
           63  +                        unrolled_score = select_node(instr["grammars"][ins_score[i].name])
           64  +                        new_score = ins_score[:i-1] + unrolled_score + ins_score[i+i:]
           65  +                        ins_score = new_score
           66  +                    if i == len(ins_score):
           67  +                        score_complete = True
           68  +                        break
    72     69               
    73     70   
    74         -def make_scale(key):
    75         -    notes = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]
    76         -    scale = [key]
    77         -    pos = notes.index(key)
    78         -    progression = [2,2,1,2,2,2,1]
    79         -    for p in progression:
    80         -        pos = (pos + p) % 12
    81         -        scale.append(notes[pos])
    82         -    return scale
           71  +            ins_score = [n for n in ins_score if not isinstance(n, tree.Tree)]
           72  +            composition[comp_name][instr_name]["score"] = ins_score
           73  +
           74  +            # Generate timestamps for the notes 
           75  +#            t = comp_start_time
           76  +#            for note in range(len(ins_score)):
           77  +#                ins_score[note].time = t
           78  +#                ins_score[note].duration *= tempo
           79  +#                t += score[note].duration
           80  +##                print "time difference =", t-comp_start_time
           81  +##                print "instrument duration =",composition[comp_name][instr_name]["duration"]
           82  +#                if (t-comp_start_time) > float(composition[comp_name][instr_name]["duration"]):
           83  +##                    print "here"
           84  +#                    ins_score = ins_score[:note]
           85  +#                    break
           86  +#                max_t = t if t > max_t else max_t
           87  +#            composition[comp_name][instr_name]["score"] = ins_score
           88  +
           89  +    # Must be done after all note times keyed in, else you can't coordinate melodies with the rhythm chords
           90  +#    for comp_name in progression.split():
           91  +#        print "; Movement:", comp_name
           92  +#        for instr_name, instr in composition[comp_name].iteritems():
           93  +##            print "\nMovement %s instrument %s" % (comp_name, instr_name)
           94  +##            print composition[comp_name][instr_name]["score"] 
           95  +#            final_score = generate_csound_score(composition[comp_name][instr_name]["score"], composition[comp_name][instr_name]["score_line"])
           96  +#            for line in final_score:
           97  +#                print line
           98  +
           99  +            if score_len(ins_score) > max_instr:
          100  +                max_instr = ins_score
          101  +            for line in generate_csound_score(composition[comp_name][instr_name]["score"], instr["score_line"], movement_start):
          102  +                print line
          103  +
          104  +
          105  +def score_len(score):
          106  +    total = 0
          107  +    for n in score:
          108  +        if not isinstance(n, tree.Tree):
          109  +            total += n.duration
          110  +    return total
    83    111   
          112  +def select_node(grammar):
          113  +    return random.choice(grammar)
          114  +            
    84    115   
    85    116   def generate_score(score, grammars):
          117  +    pdb.set_trace()
    86    118       while 1:
    87    119           found_substitution = False
    88    120           for key,value in grammars.iteritems():
    89    121               if score.find(key) != -1:
    90    122                   found_substitution = True
    91    123                   while score.find(key) != -1:
    92    124                       score = score.replace(key, random.choice(grammars[key]), 1)
................................................................................
    95    127                           for k in grammars.keys():
    96    128                               score = score.replace(k, "")
    97    129                           return score
    98    130           if found_substitution is False:
    99    131               break
   100    132       return score
   101    133   
   102         -def transliterate_score(score, key):
   103         -    scale = make_scale(key)
   104         -    scale_conversion = {
   105         -        "I": 1,
   106         -        "II": 2,
   107         -        "III": 3,
   108         -        "IV": 4,
   109         -        "V": 5,
   110         -        "VI": 6,
   111         -        "VII": 7,
   112         -        "VIII": 8,
   113         -    }
   114         -    keyed_score = []
   115         -    for i in range(len(score)):
   116         -        if isinstance(score[i], parse.Note):
   117         -            score[i].value = scale[scale_conversion[score[i].value]-1]
   118         -        elif isinstance(score[i], parse.Chord):
   119         -            chord = []
   120         -            root_note_index = scale.index(key) + scale_conversion[score[i].value]
   121         -            chord.append(scale[root_note_index])
   122         -            chord.append(scale[(root_note_index+3) % 8])
   123         -            if score[i].chord_type == "m":  # Minor chords, flat the 5th
   124         -                chord.append(scale[(root_note_index+4) % 8])
   125         -            else:
   126         -                chord.append(scale[(root_note_index+5) % 8])
   127         -            score[i].chord = chord
   128         -        elif isinstance(score[i], parse.Rest):
   129         -            pass
   130         -    return score
   131    134   
   132         -
   133         -def generate_csound_score(score, score_line):
          135  +def generate_csound_score(score, score_line, t):
   134    136       csound_note_values = {
   135    137           "C": "00",
   136    138           "C#": "01",
   137    139           "D": "02",
   138    140           "D#": "03",
   139    141           "E": "04",
   140    142           "F": "05",
................................................................................
   142    144           "G": "07",
   143    145           "G#": "08",
   144    146           "A": "09",
   145    147           "A#": "10",
   146    148           "B": "11",
   147    149       }
   148    150       csound_score = []
          151  +    pdb.set_trace()
   149    152       for token in score:
   150    153           if isinstance(token, parse.Chord):  # Chords
   151    154               for note in token.chord: 
   152    155                   note = csound_note_values[note]
   153         -                csound_score.append(score_line % {"time": token.time, "octave": token.octave, "note": note, "duration": token.duration})
          156  +                csound_score.append(score_line % {"time": t, "octave": token.octave, "note": note, "duration": token.duration})
   154    157           elif isinstance(token, parse.Note):  # Individual notes
   155    158               note = csound_note_values[token.value]
   156         -            csound_score.append(score_line % {"time": token.time, "octave": token.octave, "note": note, "duration": token.duration})
          159  +            csound_score.append(score_line % {"time": t, "octave": token.octave, "note": note, "duration": token.duration})
          160  +        t += token.duration
   157    161       return csound_score
   158    162   
   159    163   
   160    164   if __name__ == "__main__": main()