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