spiffyscore

Check-in [b2ce9522e9]
Login
Overview
SHA1:b2ce9522e9d8cad38398a713e26d4d8fbdb43aa5
Date: 2011-09-22 18:04:07
User: brian
Comment:Fixed bug that prevented more than two sections from working properly
Timelines: family | ancestors | descendants | both | feature/abc
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2011-09-22
18:30
[f653a4fa10] Added back in support for subsections (user: brian, tags: feature/abc)
18:04
[b2ce9522e9] Fixed bug that prevented more than two sections from working properly (user: brian, tags: feature/abc)
17:58
[4b32d1483b] Fixed tempo for realz this time (user: brian, tags: feature/abc)
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Modified cfg.py from [b052479df1] to [fb69d16646].

    46     46       }
    47     47       print '''f1 0 512 10 1
    48     48   f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08
    49     49   f3 0 1025 10 1
    50     50   t 0 60
    51     51       '''
    52     52   
    53         -    start = 0
           53  +    section_start = 0
    54     54       for section in ["verse1", "verse2"]:
    55     55           section = composition[section]
    56     56   #        for subsection in section
    57     57           instrs = []
    58     58           for instr in section.values():
    59     59               sync = None
    60     60               max_time = instr["duration"]
    61     61               instr_score = render_instr(instr, sync, max_time)
    62     62               instrs.append(instr_score)
    63         -            for line in generate_csound_score(instr_score, instr["score_line"], start):
           63  +            for line in generate_csound_score(instr_score, instr["score_line"], section_start):
    64     64                   print line
    65     65           longest_score = max(instrs, key=lambda i: score_len(i))
    66         -        start = score_len(longest_score)
           66  +        section_start += score_len(longest_score)
    67     67           
    68     68   
    69     69   
    70     70   def render_instr(instr, sync, max_time):
    71     71       grammars = instr["grammars"]
    72     72       for g in instr["grammars"]:
    73     73           for i in range(len(grammars[g])):
................................................................................
    74     74               grammars[g][i] = parse.parse(grammars[g][i])
    75     75       init_node = random.choice(instr["grammars"].keys())
    76     76       init_score = random.choice(instr["grammars"][init_node])
    77     77       score = init_score
    78     78       while True:
    79     79           time_remaining = max_time - score_len(score)
    80     80           try:
    81         -            score = choose_node(score, grammars, time_remaining)
           81  +            score = choose_node(score, grammars, time_remaining, sync)
    82     82           except ValueError:
    83     83               break
    84     84       return score
    85     85   
    86     86   
    87         -def choose_node(score, grammars, time_remaining):
           87  +def choose_node(score, grammars, time_remaining, sync):
    88     88       if time_remaining <= 0:
    89     89           raise ValueError("No time remaining in the score")
    90     90       node = None
    91     91       node_index = None
    92     92       for item in range(len(score)):
    93     93           if isinstance(score[item], tree.Tree):
    94     94               node = score[item].name
................................................................................
    97     97           raise ValueError("No more nodes to fill in")
    98     98       options = []
    99     99       for g in range(len(grammars[node])):
   100    100           if score_len(grammars[node][g]) <= time_remaining:
   101    101               options.append(grammars[node][g])
   102    102       if len(options) == 0:
   103    103           raise ValueError("No available grammars that will fit in the score")
   104         -    phrase = random.choice(options)
          104  +    if sync:
          105  +        
          106  +    else:
          107  +        phrase = random.choice(options)
   105    108       score = score[:node_index-1] + phrase + score[node_index+1:]
   106    109       return score
   107    110   
   108         -            
   109         -            
   110         -
   111         -
   112         -
   113         -#    movement_start = 0
   114         -#    progression = "verse1 verse2"
   115         -#    for comp_name in progression.split():
   116         -#        # We need an arbitrary grammar from this instrument to start the score with
   117         -#        max_instr =  0
   118         -#        for instr_name, instr in composition[comp_name].iteritems():
   119         -#            for grammar in instr["grammars"]:
   120         -#                for g in range(len(instr["grammars"][grammar])):
   121         -#                    instr["grammars"][grammar][g] = parse.parse(instr["grammars"][grammar][g], default_octave=instr["octave"])
   122         -#            g = random.choice(instr["grammars"].keys())
   123         -#            ins_score = random.choice(instr["grammars"][g])
   124         -##            ins_score = instr["grammars"][g]
   125         -#            score_complete = False
   126         -#            while score_complete is False:
   127         -#                if score_len(ins_score) >= instr["duration"]:
   128         -#                    score_complete = True
   129         -#                    break
   130         -#                for i in range(len(ins_score)):
   131         -#                    if isinstance(ins_score[i], tree.Tree):
   132         -#                        unrolled_score = select_node(instr["grammars"][ins_score[i].name])
   133         -#                        new_score = ins_score[:i-1] + unrolled_score + ins_score[i+1:]
   134         -#                        ins_score = new_score
   135         -#                    if i == len(ins_score):
   136         -#                        score_complete = True
   137         -#                        break
   138         -#            
   139         -#
   140         -#            ins_score = [n for n in ins_score if not isinstance(n, tree.Tree)]
   141         -#            composition[comp_name][instr_name]["score"] = ins_score
   142         -#
   143         -#            if score_len(ins_score) > max_instr:
   144         -#                max_instr = score_len(ins_score)
   145         -#            for line in generate_csound_score(composition[comp_name][instr_name]["score"], instr["score_line"], movement_start):
   146         -#                print line
   147         -#
   148         -#        movement_start += max_instr
   149         -
   150    111   
   151    112   def score_len(score):
   152    113       total = 0
   153    114       for n in score:
   154    115           if not isinstance(n, tree.Tree):
   155    116               total += n.duration
   156    117       return total
   157    118   
   158         -def select_node(grammar):
   159         -    return random.choice(grammar)
   160         -            
   161         -
   162         -def generate_score(score, grammars):
   163         -    while 1:
   164         -        found_substitution = False
   165         -        for key,value in grammars.iteritems():
   166         -            if score.find(key) != -1:
   167         -                found_substitution = True
   168         -                while score.find(key) != -1:
   169         -                    score = score.replace(key, random.choice(grammars[key]), 1)
   170         -#                    print scoe
   171         -                    if len(score.split()) > 2000:
   172         -                        for k in grammars.keys():
   173         -                            score = score.replace(k, "")
   174         -                        return score
   175         -        if found_substitution is False:
   176         -            break
   177         -    return score
   178         -
   179    119   
   180    120   def generate_csound_score(score, score_line, t):
   181    121       csound_note_values = {
   182    122           "C": "00",
   183    123           "C#": "01",
   184    124           "D": "02",
   185    125           "D#": "03",