-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlilypondLy.py
142 lines (115 loc) · 3.94 KB
/
lilypondLy.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
138
139
140
141
142
from os.path import abspath
from configuration import Configuration
class LilypondLy:
#region private properties ################################################
__config: Configuration
__melody = []
__lyrics = []
__key = 'c'
__keyArt: str
__timeSignature: str
__makeMoment: str
#endregion ################################################################
#region getter/setter #####################################################
@property
def makeMoment(self) -> str:
return self.__makeMoment
# end makeMoment getter
@makeMoment.setter
def makeMoment(self, value: str):
self.__makeMoment = value
# end makeMoment setter
#endregion ################################################################
#region publlic methods ###################################################
def addNotes(self, notes: list) -> None:
notes.insert(0, ' ')
self.__melody.append(' '.join(notes))
# end addNotes
def addLyrics(self, lyrics: list) -> None:
lyrics.insert(0, ' ')
self.__lyrics.append(' '.join(lyrics))
# end addNotes
def setTitle(self, titel: str) -> None:
print('Lilypond -> {0}'.format(titel))
# end setTitle
def writeToFile(self, filename: str) -> None:
with open(self.__config.lilypondTemplate) as template:
contents = template.read()
template.close()
# end with
contents = contents + '\n' + \
'\n'.join(self.__buildContents())
fileHandle = open(filename, 'w')
fileHandle.write(contents)
fileHandle.close()
# end writeToFile
#endregion ################################################################
#region constructor #######################################################
def __init__(self, config: Configuration, key: str, major: bool, timeSignature: str) -> None:
self.__config = config
self.__key = key.lower()
self.__timeSignature = timeSignature
self.makeMoment = None
self.__melody = []
self.__lyrics = []
if (major == True):
self.__keyArt = '\major'
else:
self.__keyArt = '\minor'
# end constructor
#endregion ################################################################
#region private methods ###################################################
def __buildMelody(self) -> list:
head = [
'melody = {',
' \key {0} {1}'.format(self.__key, self.__keyArt),
' \\numericTimeSignature',
' \\time {0}'.format(self.__timeSignature)
]
if (self.__makeMoment is not None):
head.append(
' \set Score.proportionalNotationDuration = #(ly:make-moment {0})'.format(self.makeMoment))
return [
*head,
*self.__melody,
' \\bar "|."',
'}'
]
# end buildMelodyString
def __buildLyrics(self) -> list:
result = []
if (len(self.__lyrics) > 0):
result = [
'names = \lyricmode {',
*self.__lyrics,
'}'
]
return result
# end buildLyricsString
def __buildScore(self) -> list:
result = [
'\score {',
' <<',
' \\new Staff {',
' \\new Voice = "mel" \melody',
' }'
]
if (len(self.__lyrics) > 0):
result.append(' \\new Lyrics \lyricsto mel { \\names }')
return [
*result,
' >>',
'}'
]
# end buildScore
def __buildContents(self) -> list:
return [
*self.__buildMelody(),
'',
*self.__buildLyrics(),
'',
*self.__buildScore()
]
# end buildcontents
#endregion ################################################################
# end class