Differences From Artifact [b9f73ddafd609bc5]:
- Executable file
parse.py
- 2010-11-18 16:06:54 - part of checkin [1987470dfc] on branch master - Did some composing stuff (user: spiffytech@gmail.com) [annotate]
To Artifact [9c47a4ff248a3fe2]:
- Executable file
parse.py
- 2010-11-29 07:15:15 - part of checkin [4ab39b23ec] on branch master - Chords now respect octaves (user: spiffytech@gmail.com) [annotate]
7 self.duration = duration 7 self.duration = duration
8 self.octave = octave 8 self.octave = octave
9 self.accidental = None 9 self.accidental = None
10 def __repr__(self): 10 def __repr__(self):
11 return "Note %s %s %s" % (self.value, self.duration, self.octave) 11 return "Note %s %s %s" % (self.value, self.duration, self.octave)
12 12
13 class Chord(): 13 class Chord():
14 def __init__(self, value, duration=.5, chord_type="major"): | 14 def __init__(self, value, duration=.5, chord_type="major", octave=5):
15 self.value = value 15 self.value = value
16 self.duration = duration 16 self.duration = duration
17 self.chord_type = chord_type 17 self.chord_type = chord_type
> 18 self.octave = octave
18 def __repr__(self): 19 def __repr__(self):
19 return "Chord %s %s %s" % (self.value, self.duration, self.chord_type) | 20 return "Chord %s %s %s" % (self.value, self.duration, self.chord_type, s
20 21
21 22
22 class Rest(): 23 class Rest():
23 def __init__(self, duration=.25): 24 def __init__(self, duration=.25):
24 self.duration = duration 25 self.duration = duration
25 def __repr__(self): 26 def __repr__(self):
26 return "Rest node %s" % self.duration 27 return "Rest node %s" % self.duration
................................................................................................................................................................................
111 112
112 def p_chord(p): 113 def p_chord(p):
113 '''chord : QUOTE pitch QUOTE 114 '''chord : QUOTE pitch QUOTE
114 | QUOTE pitch CHORD_TYPE QUOTE 115 | QUOTE pitch CHORD_TYPE QUOTE
115 ''' 116 '''
116 pitch = p[2].value 117 pitch = p[2].value
117 pitch = pitch.upper() 118 pitch = pitch.upper()
118 p[0] = Chord(value=pitch) | 119 p[0] = Chord(value=pitch, octave=default_octave)
119 if len(p) > 3: 120 if len(p) > 3:
120 p[0].chord_type = p[3] 121 p[0].chord_type = p[3]
121 122
122 123
123 def p_accidental(p): 124 def p_accidental(p):
124 '''pitch : ACCIDENTAL pitch 125 '''pitch : ACCIDENTAL pitch
125 ''' 126 '''
................................................................................................................................................................................
144 | REST NOTE_LENGTH 145 | REST NOTE_LENGTH
145 ''' 146 '''
146 p[0] = Rest() 147 p[0] = Rest()
147 if len(p) > 2: 148 if len(p) > 2:
148 p[0].duration = p[2] 149 p[0].duration = p[2]
149 150
150 def p_error(p): 151 def p_error(p):
151 print "Syntax error at '%s' of element type %s" % (p.value, p.type) | 152 raise Exception("Syntax error at '%s' of element type %s" % (p.value, p.
152 153
153 yacc.yacc() 154 yacc.yacc()
154 155
155 #print yacc.parse("GFG B'AB,, | g/2fg gab | GFG BAB | d2A AFD") 156 #print yacc.parse("GFG B'AB,, | g/2fg gab | GFG BAB | d2A AFD")
156 return yacc.parse(score) 157 return yacc.parse(score)