]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/pmx2ly.py
2f9155005e2ff413f40aab469852d5f34b2987c6
[lilypond.git] / buildscripts / pmx2ly.py
1 #!@PYTHON@
2
3 # (urg! wat een pokkeformaat (pokkenformaat?))  
4
5 import string
6 import sys
7 import re
8
9 fn = sys.argv[1]
10
11 ls = open (fn).readlines ()
12 def stripcomment (l):
13         return re.sub ('[ \t]*%.*$\n', '', l)
14         
15 def stripwhite (l):
16         return re.sub ('[ \n\t]+', ' ', l)
17         
18 def stripeols (l):
19         return re.sub ('^ ',  '', re.sub (' $', '', l))
20         
21 ls = map (stripcomment, ls)
22 ls = map (stripwhite, ls)
23 ls = map (stripeols, ls)
24
25
26 ls = filter (lambda x: x <> '', ls)
27
28 opening = ls[0]
29 ls = ls[1:]
30
31
32 opening = map (string.atoi, re.split ('[\t ]+', opening))
33
34 (no_staffs, no_instruments, timesig_num,timesig_den, ptimesig_num,
35  ptimesig_den, pickup_beats,keysig_number) = tuple (opening)
36
37
38 opening = ls[0]
39 ls = ls[1:]
40
41 # ignore this.
42 # opening = map (string.atoi, re.split ('[\t ]+', opening))
43 # (no_pages,no_systems, musicsize, fracindent) = tuple (opening)
44
45 instruments = []
46 while len (instruments) < no_instruments:
47         instruments.append (ls[0])
48         ls = ls[1:]
49
50 class Staff:
51         def __init__ (self): 
52                 self.voices = ([],[])
53                 self.clef = None
54                 self.instrument = 0 
55 l = ls[0]
56 ls = ls[1:]
57
58 staffs = map (lambda x: Staff (), range(0, no_staffs))
59 staff_idx = 0
60
61 for s in staffs:
62         s.clef = l[0]
63         l = l[1:]
64
65 # dump path 
66 ls = ls[1:] 
67
68 # dump more ?
69 ls = ls[2:]
70
71 actab = {-2: 'eses', -1: 'es', 0 : '', 1: 'is', 2:'isis'}
72
73 def pitch_to_lily_string (tup):
74         (o,n,a) = tup
75
76         nm = chr((n + 2) % 7 + ord ('a'))
77         nm = nm + actab[a]
78         if o > 0:
79                 nm = nm + "'" * o
80         elif o < 0:
81                 nm = nm + "," * -o
82         return nm
83
84 class Chord:
85         def __init__ (self):
86                 self.pitches = []
87                 self.dots = 0
88                 self.basic_duration = 0
89                 
90         def dump (self):
91                 str = ''
92
93                 for p in self.pitches:
94                         if str:
95                                 str = str + ' ' 
96                         str = str + pitch_to_lily_string (p)
97
98                 if len (self.pitches) > 1:
99                         str = '<%s>' % str
100                 elif len (self.pitches) == 0:
101                         str = 'r'
102                 
103                 
104                 sd = ''
105                 if self.basic_duration == 0.5:
106                         sd = '\\breve'
107                 else:
108                         sd = '%d' % self.basic_duration
109
110                 str = str + sd + '.' * self.dots 
111                 return str
112                 
113
114 input_left = string.join (ls, ' ')
115
116
117 input_left = re.sub ('[ \t\n]+',   ' ', input_left)
118
119 SPACE=' \t\n'
120 DIGITS ='0123456789'
121 basicdur_table = {
122         9: 0.5,
123         0: 0 ,
124         2: 2 ,
125         4: 4 ,
126         8: 8 ,
127         1: 16,
128         3: 32,
129         6: 64
130         }
131
132 class Parser:
133         def __init__ (self):
134                 self.chords = []
135                 self.forced_duration = None
136                 self.last_octave = 4
137                 
138         def parse_note (self, str):
139                 ch = Chord ()
140
141                 name = None
142                 if str[0] <> 'r':
143                         name = (ord (str[0]) - ord('a') + 5) % 7
144                 str = str[1:]
145                 
146                 forced_duration  = 0
147                 alteration = 0
148                 dots = 0
149                 oct = None
150                 durdigit = None
151                 multibar = 0
152                 while str[0] in 'dsfmnul0123456789.,':
153                         c = str[0]
154                         str = str[1:]
155                         if c == 'f':
156                                 alteration = alteration -1
157                         elif c == 'n':
158                                 alteration = 0
159                         elif c == 'm':
160                                 multibar = 1
161                         elif c == 's':
162                                 alteration = alteration +1
163                         elif c == 'd':
164                                 dots = dots + 1
165                         elif c in DIGITS and durdigit == None:
166                                 durdigit = string.atoi (c)
167                         elif c in DIGITS:
168                                 oct = string.atoi (c) - 4
169                         elif c == '.':
170                                 dots = dots+ 1
171                                 forced_duration = 2
172                         elif c == ',':
173                                 forced_duration = 2
174                         
175
176                 if durdigit:
177                         ch.basic_duration = basicdur_table[durdigit]
178                         self.last_basic_duration = ch.basic_duration
179                 else:
180                         ch.basic_duration = self.last_basic_duration
181
182                 if name:
183                         if oct:
184                                 self.last_octave =oct
185                         else:
186                                 oct = self.last_octave
187
188                 if name:
189                         ch.pitches.append ((oct, name,  alteration))
190                         
191                 ch.dots = dots
192
193                 
194                 if forced_duration:
195                         self.forced_duration = ch.basic_duration / forced_duration
196
197
198                 self.chords.append (ch)
199                 while str[0] in SPACE:
200                         str = str [1:]
201                 return str
202
203
204 parser =  Parser()
205 while input_left:
206         while input_left[0] in 'abcdefgr':
207                 input_left = parser.parse_note (input_left)
208         print input_left[0]
209         
210         sys.stderr.write ("\nHuh? Unknown directive %s" %input_left[0:1])
211         input_left = input_left[1:]
212
213
214
215 for c in parser.chords:
216         print c.dump ()
217