Differences From Artifact [df0a6863becf2cac]:
- Executable file
cfg.py
- 2010-11-13 08:26:53 - part of checkin [729263ecd0] on branch ply - Added basic ABC tokenizing via the PLY module (user: spiffytech@gmail.com) [annotate]
To Artifact [8e359b2146853023]:
- Executable file
cfg.py
- 2010-11-16 18:37:14 - part of checkin [9bd31df856] on branch ply - Now prints csound code, both notes and chords, replicating the functionality of the original Minimaly Functional Version (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 import parse
8 9
9 def main(): 10 def main():
10 key = "A" 11 key = "A"
11 note_grammars = { 12 note_grammars = {
12 "u": ["I V V V I I IV u u", "I IV u u", "I VII IV u u" , "e"], 13 "u": ["I V V V I I IV u u", "I IV u u", "I VII IV u u" , "e"],
13 "e": [""], 14 "e": [""],
14 } 15 }
15 chord_grammars = { 16 chord_grammars = {
16 "u": ["I IV V IV I u u", "I VII IV u u", "I V IV u u", "e"], | 17 "u": ['"I" "IV" "V" "IV" "I" u u', '"I" "VII" "IV" u u', '"I" "V" "IV" u
17 "e": [""] 18 "e": [""]
18 } 19 }
19 compose_piece(key, note_grammars) 20 compose_piece(key, note_grammars)
20 compose_piece(key, chord_grammars, chords=True) 21 compose_piece(key, chord_grammars, chords=True)
21 22
22 def compose_piece(key, grammars, chords=False): 23 def compose_piece(key, grammars, chords=False):
23 score = "" 24 score = ""
24 while len(score.split()) < 200: | 25 while len(score.split()) < 10:
25 score = "u u u" 26 score = "u u u"
26 score = generate_score(score, grammars) 27 score = generate_score(score, grammars)
> 28 score = parse.parse(score)
27 score = transliterate_score(score, key, chords) 29 score = transliterate_score(score, key, chords)
28 score = generate_csound_score(score) 30 score = generate_csound_score(score)
29 print "f1 0 256 10 1 0 3 ; sine wave function table" 31 print "f1 0 256 10 1 0 3 ; sine wave function table"
30 for line in score: 32 for line in score:
31 print line 33 print line
32 34
33 35
................................................................................................................................................................................
46 while 1: 48 while 1:
47 found_substitution = False 49 found_substitution = False
48 for key,value in grammars.iteritems(): 50 for key,value in grammars.iteritems():
49 if score.find(key) != -1: 51 if score.find(key) != -1:
50 found_substitution = True 52 found_substitution = True
51 while score.find(key) != -1: 53 while score.find(key) != -1:
52 score = score.replace(key, random.choice(grammars[key]), 1) 54 score = score.replace(key, random.choice(grammars[key]), 1)
> 55 if len(score) > 200:
> 56 score = score.replace("u", "")
> 57 score = score.replace("e", "")
> 58 return score
53 if found_substitution is False: 59 if found_substitution is False:
54 break 60 break
55 return score 61 return score
56 62
57 def transliterate_score(score, key, chords=False): 63 def transliterate_score(score, key, chords=False):
58 scale = make_scale(key) 64 scale = make_scale(key)
59 scale_conversion = { 65 scale_conversion = {
................................................................................................................................................................................
64 "V": 5, 70 "V": 5,
65 "VI": 6, 71 "VI": 6,
66 "VII": 7, 72 "VII": 7,
67 "VIII": 8, 73 "VIII": 8,
68 } 74 }
69 keyed_score = [] 75 keyed_score = []
70 if chords is False: 76 if chords is False:
71 for token in score.split(): | 77 for i in range(len(score)):
72 keyed_score.append(scale[scale_conversion[token]-1]) | 78 score[i].value = scale[scale_conversion[score[i].value]-1]
73 else: 79 else:
74 for token in score.split(): | 80 for i in range(len(score)):
75 chord = [] 81 chord = []
76 root_note_index = scale.index(key) + scale_conversion[token] | 82 root_note_index = scale.index(key) + scale_conversion[score[i].value
77 chord.append(scale[root_note_index]) 83 chord.append(scale[root_note_index])
78 chord.append(scale[(root_note_index+3) % 8]) 84 chord.append(scale[(root_note_index+3) % 8])
79 chord.append(scale[(root_note_index+5) % 8]) 85 chord.append(scale[(root_note_index+5) % 8])
80 keyed_score.append(chord) | 86 score[i].chord = chord
81 return keyed_score | 87 return score
82 88
83 89
84 def generate_csound_score(score): 90 def generate_csound_score(score):
85 csound_note_values = { 91 csound_note_values = {
86 "C": "00", 92 "C": "00",
87 "C#": "01", 93 "C#": "01",
88 "D": "02", 94 "D": "02",
................................................................................................................................................................................
95 "A": "09", 101 "A": "09",
96 "A#": "10", 102 "A#": "10",
97 "B": "11", 103 "B": "11",
98 } 104 }
99 t = 0 105 t = 0
100 csound_score = [] 106 csound_score = []
101 for token in score: 107 for token in score:
102 if isinstance(token, list): # Chords | 108 if isinstance(token, parse.Chord): # Chords
103 for note in token: | 109 for note in token.chord:
104 note = csound_note_values[note] 110 note = csound_note_values[note]
105 csound_score.append("i2 %(time)f 1 7000 %(octave)d.%(note)s %(oc 111 csound_score.append("i2 %(time)f 1 7000 %(octave)d.%(note)s %(oc
106 t += 1 112 t += 1
107 else: # Individual notes 113 else: # Individual notes
108 note = csound_note_values[token] | 114 note = csound_note_values[token.value]
109 csound_score.append("i2 %(time)f 1 7000 %(octave)d.%(note)s %(octave 115 csound_score.append("i2 %(time)f 1 7000 %(octave)d.%(note)s %(octave
110 t += .25 116 t += .25
111 return csound_score 117 return csound_score
112 118
113 119
114 if __name__ == "__main__": main() 120 if __name__ == "__main__": main()