Differences From Artifact [d8090f9de3883f3f]:
- Executable file
cfg.py
- 2010-11-12 20:30:17 - part of checkin [e425f3d6c2] on branch ply - Now generates scores until it makes one 50 notes ling (user: spiffytech@gmail.com) [annotate]
To Artifact [43fd8f5bb3819142]:
- Executable file
cfg.py
- 2010-11-13 00:12:38 - part of checkin [5aa14570f1] on branch ply - Now handles chords (user: spiffytech@gmail.com) [annotate]
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import os 3 import os
4 import random 4 import random
5 import sys 5 import sys
6 import time 6 import time
7 random.seed(time.time()) 7 random.seed(time.time())
8 8
> 9 def main():
> 10 key = "C"
9 grammars = { | 11 note_grammars = {
10 "u": ["I V I IV u", "I IV", "I VII IV" , "e"], | 12 "u": ["I V I IV u", "I IV", "I VII IV" , "e"],
11 "e": [""], | 13 "e": [""],
12 } | 14 }
> 15 chord_grammars = {
> 16 "u": ["I IV V IV I", "e"],
> 17 "e": [""]
13 | 18 }
> 19 compose_piece(key, note_grammars)
> 20 compose_piece(key, chord_grammars, chords=True)
14 21
15 def main(): <
16 key = "G#" <
> 22 def compose_piece(key, grammars, chords=False):
17 score = "" 23 score = ""
18 while len(score.split()) < 50: | 24 while len(score.split()) < 15:
19 score = "u u u" 25 score = "u u u"
20 score = generate_score(score) | 26 score = generate_score(score, grammars)
21 score = keyify_score(score, key) | 27 score = transliterate_score(score, key, chords)
22 score = generate_csound_score(score) 28 score = generate_csound_score(score)
23 print "f1 0 256 10 1 0 3 ; sine wave function table" 29 print "f1 0 256 10 1 0 3 ; sine wave function table"
24 for line in score: 30 for line in score:
25 print line 31 print line
26 32
27 33
28 def make_scale(key): 34 def make_scale(key):
................................................................................................................................................................................
32 progression = [2,2,1,2,2,2,1] 38 progression = [2,2,1,2,2,2,1]
33 for p in progression: 39 for p in progression:
34 pos = (pos + p) % 12 40 pos = (pos + p) % 12
35 scale.append(notes[pos]) 41 scale.append(notes[pos])
36 return scale 42 return scale
37 43
38 44
39 def generate_score(score): | 45 def generate_score(score, grammars):
40 while 1: 46 while 1:
41 found_substitution = False 47 found_substitution = False
42 for key,value in grammars.iteritems(): 48 for key,value in grammars.iteritems():
43 if score.find(key) != -1: 49 if score.find(key) != -1:
44 found_substitution = True 50 found_substitution = True
45 while score.find(key) != -1: 51 while score.find(key) != -1:
46 score = score.replace(key, random.choice(grammars[key]), 1) 52 score = score.replace(key, random.choice(grammars[key]), 1)
47 if found_substitution is False: 53 if found_substitution is False:
48 break 54 break
49 return score 55 return score
50 56
51 def keyify_score(score, key): | 57 def transliterate_score(score, key, chords=False):
52 scale = make_scale(key) 58 scale = make_scale(key)
53 scale_conversion = { 59 scale_conversion = {
54 "I": 1, 60 "I": 1,
55 "II": 2, 61 "II": 2,
56 "III": 3, 62 "III": 3,
57 "IV": 4, 63 "IV": 4,
58 "V": 5, 64 "V": 5,
59 "VI": 6, 65 "VI": 6,
60 "VII": 7, 66 "VII": 7,
61 "VIII": 8, 67 "VIII": 8,
62 } 68 }
63 keyed_score = [] 69 keyed_score = []
> 70 if chords is False:
64 for token in score.split(): | 71 for token in score.split():
65 keyed_score.append(scale[scale_conversion[token]-1]) | 72 keyed_score.append(scale[scale_conversion[token]-1])
> 73 else:
> 74 for token in score.split():
> 75 chord = []
> 76 root_note_index = scale.index(key) + scale_conversion[token]
> 77 chord.append(scale[root_note_index])
> 78 chord.append(scale[(root_note_index+3) % 8])
> 79 chord.append(scale[(root_note_index+5) % 8])
> 80 keyed_score.append(chord)
66 return keyed_score 81 return keyed_score
67 82
68 83
69 def generate_csound_score(score): 84 def generate_csound_score(score):
70 csound_note_values = { 85 csound_note_values = {
71 "C": "00", 86 "C": "00",
72 "C#": "01", 87 "C#": "01",
................................................................................................................................................................................
80 "A": "09", 95 "A": "09",
81 "A#": "10", 96 "A#": "10",
82 "B": "11", 97 "B": "11",
83 } 98 }
84 t = 0 99 t = 0
85 csound_score = [] 100 csound_score = []
86 for token in score: 101 for token in score:
> 102 if isinstance(token, list): # Chords
> 103 for note in token:
> 104 note = csound_note_values[note]
> 105 csound_score.append("i2 %(time)f 1 7000 %(octave)d.%(note)s %(oc
> 106 t += 1
> 107 else: # Individual notes
87 note = csound_note_values[token] | 108 note = csound_note_values[token]
88 csound_score.append("i2 %f 2 7000 %d.%s %d.%s 0 6" % (t, random.choice([ <
> 109 csound_score.append("i2 %(time)f 1 7000 %(octave)d.%(note)s %(octave
89 t += .25 | 110 t += .25
90 return csound_score 111 return csound_score
91 112
92 113
93 if __name__ == "__main__": main() 114 if __name__ == "__main__": main()