-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchord.py
138 lines (124 loc) · 4.51 KB
/
chord.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python3
# Class Chord(): implements an array of pypond.Note() objects which sound simultaneously
# (i.e. have the same duration and beat number).
import pypond
class Chord(pypond.Note):
def __init__(self, notes = [], duration = None, beatNum = 0):
super().__init__("C") # A rando notestring. Need to create new 'RhythmElement' superclass
self.notes = notes
duration = self._setAttr('duration', duration, 'getDuration')
if duration != None:
self.setAllDurations(duration)
beatNum = self._setAttr('beatNum', beatNum, 'getBeatNum')
if beatNum != None:
self.setAllBeatNums(beatNum)
def _setAttr(self, attrString, attrVal, getter = None):
#print("setting attribute {} to {}".format(attrString, attrVal))
if attrVal != None:
setattr(self, attrString, attrVal)
else:
if len(notes) > 0:
if hasattr(notes[0], getter):
attrVal = getattr(notes[0], getter)()
if attrVal != None:
setattr(self, attrString, attrVal)
if attrVal == None:
print("No value determined for attribute {}".format(attrString))
#print("self.{} = {}".format(attrString, getattr(self, attrString)))
return attrVal
def setAllDurations(self, duration):
"""Set the duration of all notes in chord to 'duration'
Returns True if all items in self.notes have a 'setDuration' method.
Returns False otherwise."""
allgood = True
for note in self.notes:
if hasattr(note, 'setDuration'):
note.setDuration(duration)
else:
print("{} has no attribute setDuration".format(note))
allgood = False
return allgood
def setAllBeatNums(self, beatNum):
"""Set the beat number for all notes in chord to 'beatNum'
Returns True if all items in self.notes have a 'setDuration' method.
Returns False otherwise."""
allgood = True
for note in self.notes:
if hasattr(note, 'setBeatNum'):
note.setBeatNum(beatNum)
else:
print("{} has no attribute setBeatNum".format(note))
allgood = False
return allgood
def getDuration(self):
return self.duration
def getBeatNum(self):
return self.beatNum
def setDuration(self, duration):
dur = pypond._float(duration)
if dur == None:
raise pypond.Error_Float("Cannot interpret duration {}".format(duration))
return False
else:
self.duration = dur
self.setAllDurations(dur)
return True
def setBeatNum(self, beatNum):
nbeat = pypond._float(beatNum)
if nbeat == None:
raise pypond.Error_Float("Cannot interpret beatNum {}".format(beatNum))
return False
else:
self.beatNum = nbeat
self.setAllBeatNums(nbeat)
return True
def asLily(self):
"""Return a string representation of the chord in GNU Lilypond format"""
# <c e g>8.~
noteNames = []
for note in self.notes:
s = note.getNoteName()[0].lower() + note._getLilyAccidental() + note._getLilyOctave()
noteNames.append(s)
notestring = "<" + " ".join(noteNames) + ">"
d = self._getLilyDuration()
s = self._getLilyDot()
t = self._getLilyTie()
return "{}{}{}{}".format(notestring, d, s, t)
def testChord(argv):
USAGE = "python3 {} [-d duration] [-t] [-o] note0 note1 note2..."
dot = False
tie = False
duration = 1
nextDur = False
notestrings = []
if len(argv) > 1:
for arg in argv[1:]:
if arg == '-d':
nextDur = True
elif arg == '-t':
tie = True
elif arg == '-o':
dot = True
else:
if nextDur:
duration = pypond._float(arg)
nextDur = False
else:
notestrings.append(arg)
else:
print(USAGE)
return
if len(notestrings) == 0:
notestrings = ['C', 'E', 'G']
notes = [pypond.Note(x) for x in notestrings]
chord = Chord(notes, duration)
if tie:
chord.setTie(True)
if dot:
chord.setDot(True)
print(chord.asLily())
return
if __name__ == '__main__':
import sys
argv = sys.argv
testChord(argv)