spiffyscore

Check-in [f653a4fa10]
Login
Overview
SHA1:f653a4fa1046734d8b1cdf93e96149f2f5bab309
Date: 2011-09-22 18:30:17
User: brian
Comment:Added back in support for subsections
Timelines: family | ancestors | descendants | both | feature/abc
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2011-10-11
15:19
[29231e328a] Closed-Leaf: Played around a little with melodies and instruments (user: brian, tags: feature/abc)
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)
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Modified cfg.py from [fb69d16646] to [0cb9a22d0b].

    11     11   import tree
    12     12   
    13     13   random.seed(time.time())
    14     14   
    15     15   def main():
    16     16       composition = {
    17     17           "verse1": {
    18         -            "melody": {  # Instrument 'melody'
    19         -                "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
    20         -                "octave": 8,
    21         -                "duration": 10,
    22         -                "grammars": {  # Notes for this instrument to use in this piece
    23         -                    "u": ["C G/2 G/2 G/2 C B, F' C F C B F (w)"],
    24         -                    "w": ["E/4 A/4 D/4 G/4 F/4 F/4 B2 (u)"],
           18  +            "intro": {
           19  +                "melody": {  # Instrument 'melody'
           20  +                    "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
           21  +                    "octave": 8,
           22  +                    "duration": 10,
           23  +                    "grammars": {  # Notes for this instrument to use in this piece
           24  +                        "u": ["G/2 G/2 G/4 G/4 A/4 A/4 A/2 G G A A A3 (w)"],
           25  +                        "w": ["E E F F G/2 G/2 G3 (u)"],
           26  +                    },
           27  +                },
           28  +            },
           29  +            "body": {
           30  +                "melody": {  # Instrument 'melody'
           31  +                    "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
           32  +                    "octave": 8,
           33  +                    "duration": 10,
           34  +                    "grammars": {  # Notes for this instrument to use in this piece
           35  +                        "u": ["C G/2 G/2 G/2 C B, F' C F C B F (w)"],
           36  +                        "w": ["E/4 A/4 D/4 G/4 F/4 F/4 B2 (u)"],
           37  +                    },
    25     38                   },
    26     39               },
    27     40           },
    28     41           "verse2": {
    29         -            "melody": {  # Instrument 'melody'
    30         -                "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
    31         -                "octave": 8,
    32         -                "duration": 10,
    33         -                "grammars": {  # Notes for this instrument to use in this piece
    34         -                    "u": ["C C C C F/2 F/2 F/2 (u)", "D D G/2 A/2 D D (u)"],
           42  +            "body": {
           43  +                "melody": {  # Instrument 'melody'
           44  +                    "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
           45  +                    "octave": 8,
           46  +                    "duration": 10,
           47  +                    "grammars": {  # Notes for this instrument to use in this piece
           48  +                        "u": ["C C C C F/2 F/2 F/2 (u)", "D D G/2 A/2 D D (u)"],
           49  +                    },
    35     50                   },
    36         -            },
    37         -            "harmony": {  # Instrument 'melody'
    38         -                "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
    39         -                "octave": 8,
    40         -                "duration": 10,
    41         -                "grammars": {  # Notes for this instrument to use in this piece
    42         -                    "u": ["C C C C F/2 F/2 F/2 (u)", "D D G/2 A/2 D D (u)"],
           51  +                "harmony": {  # Instrument 'melody'
           52  +                    "score_line": "i2 %(time)f %(duration)f 7000 %(octave)d.%(note)s 2",
           53  +                    "octave": 8,
           54  +                    "duration": 10,
           55  +                    "grammars": {  # Notes for this instrument to use in this piece
           56  +                        "u": ["C C C C F/2 F/2 F/2 (u)", "D D G/2 A/2 D D (u)"],
           57  +                    },
    43     58                   },
    44     59               },
    45     60           },
    46     61       }
    47     62       print '''f1 0 512 10 1
    48     63   f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08
    49     64   f3 0 1025 10 1
    50         -t 0 60
           65  +t 0 100
    51     66       '''
    52     67   
    53     68       section_start = 0
    54     69       for section in ["verse1", "verse2"]:
           70  +        print "; Section " + section
           71  +        subsection_start = section_start
    55     72           section = composition[section]
    56         -#        for subsection in section
    57         -        instrs = []
    58         -        for instr in section.values():
    59         -            sync = None
    60         -            max_time = instr["duration"]
    61         -            instr_score = render_instr(instr, sync, max_time)
    62         -            instrs.append(instr_score)
    63         -            for line in generate_csound_score(instr_score, instr["score_line"], section_start):
    64         -                print line
    65         -        longest_score = max(instrs, key=lambda i: score_len(i))
    66         -        section_start += score_len(longest_score)
           73  +        for subsection in ["intro", "body", "outro"]:
           74  +            try:
           75  +                print "; Subsection " + subsection
           76  +                subsection = section[subsection]
           77  +                instrs = []
           78  +                for instr in subsection:
           79  +                    print ";Instrument " + instr
           80  +                    instr = subsection[instr]
           81  +                    sync = None
           82  +                    max_time = instr["duration"]
           83  +                    instr_score = render_instr(instr, sync, max_time)
           84  +                    instrs.append(instr_score)
           85  +                    for line in generate_csound_score(instr_score, instr["score_line"], subsection_start):
           86  +                        print line
           87  +                longest_score = max(instrs, key=lambda i: score_len(i))
           88  +                subsection_start += score_len(longest_score)
           89  +                section_start += score_len(longest_score)
           90  +            except KeyError:
           91  +                pass
    67     92           
    68     93   
    69     94   
    70     95   def render_instr(instr, sync, max_time):
    71     96       grammars = instr["grammars"]
    72     97       for g in instr["grammars"]:
    73     98           for i in range(len(grammars[g])):
................................................................................
    98    123       options = []
    99    124       for g in range(len(grammars[node])):
   100    125           if score_len(grammars[node][g]) <= time_remaining:
   101    126               options.append(grammars[node][g])
   102    127       if len(options) == 0:
   103    128           raise ValueError("No available grammars that will fit in the score")
   104    129       if sync:
   105         -        
          130  +        pass
   106    131       else:
   107    132           phrase = random.choice(options)
   108    133       score = score[:node_index-1] + phrase + score[node_index+1:]
   109    134       return score
   110    135   
   111    136   
   112    137   def score_len(score):

Modified test.sco from [ec3cdf0cc7] to [b5abda4892].

     1      1   f1 0 512 10 1
     2      2   f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08
     3      3   f3 0 1025 10 1
     4         -t 0 60
            4  +t 0 100
     5      5       
     6         -i2 0.000000 0.250000 7000 8.04 2
     7         -i2 0.250000 0.250000 7000 8.09 2
     8         -i2 0.500000 0.250000 7000 8.02 2
     9         -i2 0.750000 0.250000 7000 8.07 2
    10         -i2 1.000000 0.250000 7000 8.05 2
    11         -i2 1.250000 0.250000 7000 8.05 2
    12         -i2 1.500000 2.000000 7000 8.11 2
    13         -i2 3.500000 1.000000 7000 8.02 2
    14         -i2 4.500000 1.000000 7000 8.02 2
    15         -i2 5.500000 0.500000 7000 8.07 2
    16         -i2 6.000000 0.500000 7000 8.09 2
    17         -i2 6.500000 1.000000 7000 8.02 2
    18         -i2 7.500000 1.000000 7000 8.02 2
    19         -i2 8.500000 1.000000 7000 8.02 2
    20         -i2 9.500000 0.500000 7000 8.07 2
    21         -i2 10.000000 0.500000 7000 8.09 2
    22         -i2 10.500000 1.000000 7000 8.02 2
    23         -i2 11.500000 1.000000 7000 8.02 2
    24         -i2 3.500000 1.000000 7000 8.02 2
    25         -i2 4.500000 1.000000 7000 8.02 2
    26         -i2 5.500000 0.500000 7000 8.07 2
    27         -i2 6.000000 0.500000 7000 8.09 2
    28         -i2 6.500000 1.000000 7000 8.02 2
    29         -i2 7.500000 1.000000 7000 8.02 2
    30         -i2 8.500000 1.000000 7000 8.02 2
    31         -i2 9.500000 0.500000 7000 8.07 2
    32         -i2 10.000000 0.500000 7000 8.09 2
    33         -i2 10.500000 1.000000 7000 8.02 2
    34         -i2 11.500000 1.000000 7000 8.02 2
            6  +; Section verse1
            7  +; Subsection intro
            8  +;Instrument melody
            9  +i2 0.000000 1.000000 7000 8.04 2
           10  +i2 1.000000 1.000000 7000 8.04 2
           11  +i2 2.000000 1.000000 7000 8.05 2
           12  +i2 3.000000 1.000000 7000 8.05 2
           13  +i2 4.000000 0.500000 7000 8.07 2
           14  +i2 4.500000 0.500000 7000 8.07 2
           15  +i2 5.000000 3.000000 7000 8.07 2
           16  +; Subsection body
           17  +;Instrument melody
           18  +i2 8.000000 0.250000 7000 8.04 2
           19  +i2 8.250000 0.250000 7000 8.09 2
           20  +i2 8.500000 0.250000 7000 8.02 2
           21  +i2 8.750000 0.250000 7000 8.07 2
           22  +i2 9.000000 0.250000 7000 8.05 2
           23  +i2 9.250000 0.250000 7000 8.05 2
           24  +i2 9.500000 2.000000 7000 8.11 2
           25  +; Subsection outro
           26  +; Section verse2
           27  +; Subsection intro
           28  +; Subsection body
           29  +;Instrument melody
           30  +i2 11.500000 1.000000 7000 8.00 2
           31  +i2 12.500000 1.000000 7000 8.00 2
           32  +i2 13.500000 1.000000 7000 8.00 2
           33  +i2 14.500000 1.000000 7000 8.00 2
           34  +i2 15.500000 0.500000 7000 8.05 2
           35  +i2 16.000000 0.500000 7000 8.05 2
           36  +i2 16.500000 0.500000 7000 8.05 2
           37  +;Instrument harmony
           38  +i2 11.500000 1.000000 7000 8.00 2
           39  +i2 12.500000 1.000000 7000 8.00 2
           40  +i2 13.500000 1.000000 7000 8.00 2
           41  +i2 14.500000 1.000000 7000 8.00 2
           42  +i2 15.500000 0.500000 7000 8.05 2
           43  +i2 16.000000 0.500000 7000 8.05 2
           44  +i2 16.500000 0.500000 7000 8.05 2
           45  +; Subsection outro