Differences From Artifact [5ac9fc32526a68ed]:
- Executable file
spiffyscore.py
- 2011-11-16 00:14:47 - part of checkin [872ae93059] on branch develop - If I move from csound to midi I should actually rename function with 'csound' in the name (user: brian) [annotate]
To Artifact [87c5508f415d5e6f]:
- Executable file
spiffyscore.py
- 2011-11-17 19:55:50 - part of checkin [f8034c7dfb] on branch develop - Removed leftover csound function lines that got printed. Also disabled ipdb so I get concise stacktraces. Also made program crash when your score pointed to a non-existant node. Previously that just generated an invalid MIDI file (somehow...) (user: brian) [annotate]
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 from __future__ import division 3 from __future__ import division
4 import ipdb | 4 #import ipdb
5 import os 5 import os
6 import random 6 import random
7 import sys 7 import sys
8 import time 8 import time
9 9
10 from midiutil.MidiFile import MIDIFile as midifile 10 from midiutil.MidiFile import MIDIFile as midifile
11 import parse 11 import parse
................................................................................................................................................................................
18 mymidi = midifile(15) 18 mymidi = midifile(15)
19 19
20 def main(): 20 def main():
21 composition = { 21 composition = {
22 "intro": { 22 "intro": {
23 "body": { 23 "body": {
24 "lead_instr": { # Instrument 'melody' 24 "lead_instr": { # Instrument 'melody'
25 "channel": 6, | 25 "channel": 8,
26 "octave": 4, | 26 "octave": 5,
27 "duration": 30, | 27 "duration": 60,
28 "grammars": { # Notes for this instrument to use in this pi 28 "grammars": { # Notes for this instrument to use in this pi
29 "u": ["A/2, B/2, C/2 D/2 (u)", "D2' D2' D2' D2' (x)"], | 29 "u": ["C2' B2 A3 D3 B C' D C2' z (u)", "C2' C2' C2' C2'
30 "v": ["C/2 C/2 C/2 C/2 (w)"], | 30 "v": ["G2 F2 E2 F2 D5 (u)", "B/4 C/4' B/4 A/4 D2 z"],
31 "w": ["E/2 F/2 E/2 F/2 (u)"], <
32 "x": ["z4 (v)"], 31 "x": ["z4 (v)"],
33 }, 32 },
34 }, 33 },
35 "follow_instr": { # Instrument 'bass' 34 "follow_instr": { # Instrument 'bass'
36 "channel": 4, 35 "channel": 4,
37 "sync": "lead_instr", 36 "sync": "lead_instr",
38 "octave": 2, 37 "octave": 2,
39 "duration": 30, | 38 "duration": 60,
40 "grammars": { # Notes for this instrument to use in this pi 39 "grammars": { # Notes for this instrument to use in this pi
41 "u": ["E F G E (v)"], | 40 "u": ["C/2 C/2 C/2 z/2 (u)"],
42 "v": ["G A A A (e)", "G A A A (v)"], <
43 "e": ["B A G A (u)"], <
44 "x": ["z4 (e)"], <
> 41 },
> 42 },
> 43 },
> 44 },
> 45 "section1": {
> 46 "body": {
> 47 "lead_instr": { # Instrument 'melody'
> 48 "channel": 6,
> 49 "octave": 5,
> 50 "duration": 60,
> 51 "grammars": { # Notes for this instrument to use in this pi
> 52 "u": ["C E A F G z (u)", "C E A F G z (v)"],
> 53 "v": ["A/2 D/2 G/2 C/2 | F/2 B/2 E/2 z/2 | (u)"],
> 54 },
> 55 },
> 56 "follow_instr": { # Instrument 'bass'
> 57 "channel": 4,
> 58 "sync": "lead_instr",
> 59 "octave": 2,
> 60 "duration": 60,
> 61 "grammars": { # Notes for this instrument to use in this pi
> 62 "u": ["C/2 C/2 C/2 z/2 (u)"],
45 }, 63 },
46 }, 64 },
47 }, 65 },
48 }, 66 },
49 } 67 }
50 print '''f1 0 512 10 1 <
51 f2 0 8192 10 .24 .64 .88 .76 .06 .5 .34 .08 <
52 f3 0 1025 10 1 <
53 t 0 60 <
54 ''' <
55 68
56 section_start = 0 69 section_start = 0
57 for section in ["intro"]: | 70 for section in ["intro", "section1"]:
58 print "; Section " + section | 71 print "Section " + section
59 subsection_start = section_start 72 subsection_start = section_start
60 section = composition[section] 73 section = composition[section]
61 for subsection in ["intro", "body", "outro"]: 74 for subsection in ["intro", "body", "outro"]:
62 try: 75 try:
63 print "; Subsection " + subsection | 76 print "Subsection " + subsection
64 subsection = section[subsection] 77 subsection = section[subsection]
65 78
66 unordered_instrs = [] 79 unordered_instrs = []
67 for instr in subsection: 80 for instr in subsection:
68 subsection[instr]["name"] = instr 81 subsection[instr]["name"] = instr
69 if not "sync" in subsection[instr].keys(): 82 if not "sync" in subsection[instr].keys():
70 subsection[instr]["sync"] = None 83 subsection[instr]["sync"] = None
................................................................................................................................................................................
72 ordered_instrs = topsort.topsort(unordered_instrs) 85 ordered_instrs = topsort.topsort(unordered_instrs)
73 ordered_instrs.remove(None) # None used as a placeholder for so 86 ordered_instrs.remove(None) # None used as a placeholder for so
74 87
75 instrs = [] 88 instrs = []
76 syncs = {} 89 syncs = {}
77 track = 0 90 track = 0
78 for instr in ordered_instrs: 91 for instr in ordered_instrs:
79 print ";Instrument " + instr | 92 print "Instrument " + instr
80 instr = subsection[instr] 93 instr = subsection[instr]
81 max_time = instr["duration"] 94 max_time = instr["duration"]
82 instr_score, syncs = render_instr(instr, syncs, max_time) 95 instr_score, syncs = render_instr(instr, syncs, max_time)
83 instrs.append(instr_score) 96 instrs.append(instr_score)
84 midify_instr_score(instr_score, track, instr["channel"], sub 97 midify_instr_score(instr_score, track, instr["channel"], sub
85 longest_score = max(instrs, key=lambda i: score_len(i)) 98 longest_score = max(instrs, key=lambda i: score_len(i))
86 subsection_start += score_len(longest_score) 99 subsection_start += score_len(longest_score)
................................................................................................................................................................................
136 grammar = sync_node 149 grammar = sync_node
137 else: 150 else:
138 grammar = random.choice(time_filtered_grammars.keys()) 151 grammar = random.choice(time_filtered_grammars.keys())
139 if score is None: 152 if score is None:
140 grammar = random.choice(time_filtered_grammars.keys()) 153 grammar = random.choice(time_filtered_grammars.keys())
141 elif instr["sync"] is None: 154 elif instr["sync"] is None:
142 grammar = get_next_node(score); 155 grammar = get_next_node(score);
> 156 if grammar not in instr["grammars"].keys():
> 157 raise Exception("You tried to direct a grammar to a node that doesn'
143 phrases = time_filtered_grammars[grammar] 158 phrases = time_filtered_grammars[grammar]
144 if instr["name"] not in syncs.keys(): 159 if instr["name"] not in syncs.keys():
145 syncs[instr["name"]] = [] 160 syncs[instr["name"]] = []
146 syncs[instr["name"]].append({"node": grammar, "time": current_time}) 161 syncs[instr["name"]].append({"node": grammar, "time": current_time})
147 162
148 return random.choice(phrases), syncs 163 return random.choice(phrases), syncs
149 164