lilypond-1.5.33
[lilypond.git] / buildscripts / pmx2ly.py
blob2f9155005e2ff413f40aab469852d5f34b2987c6
1 #!@PYTHON@
3 # (urg! wat een pokkeformaat (pokkenformaat?))
5 import string
6 import sys
7 import re
9 fn = sys.argv[1]
11 ls = open (fn).readlines ()
12 def stripcomment (l):
13 return re.sub ('[ \t]*%.*$\n', '', l)
15 def stripwhite (l):
16 return re.sub ('[ \n\t]+', ' ', l)
18 def stripeols (l):
19 return re.sub ('^ ', '', re.sub (' $', '', l))
21 ls = map (stripcomment, ls)
22 ls = map (stripwhite, ls)
23 ls = map (stripeols, ls)
26 ls = filter (lambda x: x <> '', ls)
28 opening = ls[0]
29 ls = ls[1:]
32 opening = map (string.atoi, re.split ('[\t ]+', opening))
34 (no_staffs, no_instruments, timesig_num,timesig_den, ptimesig_num,
35 ptimesig_den, pickup_beats,keysig_number) = tuple (opening)
38 opening = ls[0]
39 ls = ls[1:]
41 # ignore this.
42 # opening = map (string.atoi, re.split ('[\t ]+', opening))
43 # (no_pages,no_systems, musicsize, fracindent) = tuple (opening)
45 instruments = []
46 while len (instruments) < no_instruments:
47 instruments.append (ls[0])
48 ls = ls[1:]
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:]
58 staffs = map (lambda x: Staff (), range(0, no_staffs))
59 staff_idx = 0
61 for s in staffs:
62 s.clef = l[0]
63 l = l[1:]
65 # dump path
66 ls = ls[1:]
68 # dump more ?
69 ls = ls[2:]
71 actab = {-2: 'eses', -1: 'es', 0 : '', 1: 'is', 2:'isis'}
73 def pitch_to_lily_string (tup):
74 (o,n,a) = tup
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
84 class Chord:
85 def __init__ (self):
86 self.pitches = []
87 self.dots = 0
88 self.basic_duration = 0
90 def dump (self):
91 str = ''
93 for p in self.pitches:
94 if str:
95 str = str + ' '
96 str = str + pitch_to_lily_string (p)
98 if len (self.pitches) > 1:
99 str = '<%s>' % str
100 elif len (self.pitches) == 0:
101 str = 'r'
104 sd = ''
105 if self.basic_duration == 0.5:
106 sd = '\\breve'
107 else:
108 sd = '%d' % self.basic_duration
110 str = str + sd + '.' * self.dots
111 return str
114 input_left = string.join (ls, ' ')
117 input_left = re.sub ('[ \t\n]+', ' ', input_left)
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
132 class Parser:
133 def __init__ (self):
134 self.chords = []
135 self.forced_duration = None
136 self.last_octave = 4
138 def parse_note (self, str):
139 ch = Chord ()
141 name = None
142 if str[0] <> 'r':
143 name = (ord (str[0]) - ord('a') + 5) % 7
144 str = str[1:]
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
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
182 if name:
183 if oct:
184 self.last_octave =oct
185 else:
186 oct = self.last_octave
188 if name:
189 ch.pitches.append ((oct, name, alteration))
191 ch.dots = dots
194 if forced_duration:
195 self.forced_duration = ch.basic_duration / forced_duration
198 self.chords.append (ch)
199 while str[0] in SPACE:
200 str = str [1:]
201 return str
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]
210 sys.stderr.write ("\nHuh? Unknown directive %s" %input_left[0:1])
211 input_left = input_left[1:]
215 for c in parser.chords:
216 print c.dump ()