lilypond-1.3.18
[lilypond.git] / scripts / musedata2ly.py
blob4cfbed812719f8a525eb544eef6589abf61ab163
1 #!@PYTHON@
3 # musedata = musedata.stanford.edu
4 # musedata = COBOL for musicians.
6 import re
7 import sys
8 import string
10 f = open (sys.argv[1])
11 lines =f.readlines()
13 def chomp (x):
14 return re.sub ('[\r\n \t]+$','', x)
16 lines = map (chomp, lines)
18 default_header_dict = {
19 'tagline' :'automatically converted from Musedata',
20 'copyright' : 'all rights reserved -- free for noncommercial use'
23 # Jezus, wat een ranzig formaat. (2am)
24 def parse_header (lines):
25 d = default_header_dict
26 enter = string.split (lines[3], ' ')
27 d['enteredby'] = string.join (enter[1:])
28 d['enteredon'] = enter[0]
29 d['opus'] = lines[4]
30 d['source'] = lines[5]
31 d['title'] = lines[6]
32 d['subtitle'] = lines[7]
33 d['instrument']= lines[8]
34 d['musedatamisc'] =lines[9]
35 d['musedatagroups'] =lines[10]
36 d['musedatagroupnumber']=lines[11]
38 return d
40 clef_dict = {
41 04: 'treble',
42 13 : 'alto',
43 22: 'bass',
47 def get_clef(s):
48 return '\\clef "%s";\n' % clef_dict [string.atoi (s)]
50 def get_mudela_notename (p, ac):
51 if p > 5:
52 p = p - 7
53 s = chr (p + ord ('c'))
54 infix = 'i'
55 if ac < 0:
56 infix = 'e'
57 ac = -ac
59 while ac:
60 s = s + infix + 's'
61 ac = ac - 1
62 return s
64 def get_key (s):
65 i = string.atoi (s)
66 return ''
68 def get_timesig (s):
69 return '\\time %s;\n' % s
72 divisions = 4
73 def get_divisions_per_quarter (s):
74 divisions = string.atoi (s)
75 return ''
77 def get_directive (s):
78 return '%% %s\n' % s
80 def get_transposing (s):
81 return ''
83 def get_num_instruments (s):
84 return ''
86 attr_dict = {
87 'C' : get_clef,
88 'K' : get_key ,
89 'T' : get_timesig,
90 'Q' : get_divisions_per_quarter,
91 'D' : get_directive,
92 'X' : get_transposing,
93 'I': get_num_instruments,
96 def parse_musical_attributes (l):
97 s = ''
98 l = l[1:]
99 atts = re.split('[ \t]+', l)
100 for a in atts:
101 if not a:
102 continue
103 m = re.search ('(.):(.*)', a)
104 if m == None:
105 print 'Huh, unknown attr `%s\'' % a
106 continue
108 s = s + attr_dict[m.group(1)](m.group (2))
109 return s
112 def get_mudela_pitch (n, a, o):
113 c = '\''
114 if o < 1:
115 c = ','
116 o = 1 - o
118 return get_mudela_notename (n,a) + '%s' % c * o
120 def dump_header (h, out):
121 out.write ('\\header {\n')
122 for tup in h.items ():
123 out.write ('\t%s = \"%s\";\n' % tup)
124 out.write ('}\n')
126 header_dict = parse_header (lines[0:12])
127 dump_header (header_dict, sys.stdout)
130 lines = lines [12:]
133 def parse_line_comment (l):
134 return re.sub ('@' , '%' , l)
136 def parse_note_line (l):
137 pitch = ((ord (l[0]) -ord('A')) + 5) % 7
138 acc = 0
139 l= l[1:]
140 while l[0] == 'f':
141 l= l[1:]
142 acc = acc - 1
143 while l[0] == '#':
144 l= l[1:]
145 acc = acc + 1
146 while l[0] in ' \t':
147 l= l[1:]
149 oct = 0
150 if l[0] in '0123456789':
151 oct = string.atoi (l[0]) - 4
152 l= l[1:]
154 while l[0] in ' \t':
155 l= l[1:]
158 print get_mudela_pitch (pitch,acc,oct), parse_duration(l[:2])
159 l = l[2:]
164 def parse_duration (l):
165 s = ''
166 while l[0] in '0123456789':
167 s = s + l[0]
168 l= l[1:]
169 print l
170 num = string.atoi (s)
171 den = 4 * divisions
173 current_dots = 0
174 try_dots = [3, 2, 1]
175 for d in try_dots:
176 f = 1 << d
177 multiplier = (2*f-1)
178 if num % multiplier == 0 and den % f == 0:
179 num = num / multiplier
180 den = den / f
181 current_dots = current_dots + d
183 if num <> 1:
184 sys.stderr.write ('huh. Durations left')
185 return '%s%s' % (den, '.' * current_dots)
187 comment_switch = 0
188 for l in lines:
189 if l[0] == '&':
190 comment_switch = not comment_switch
191 if comment_switch:
192 l= l[1:]
193 print '%{'
194 else:
195 print '%}'
197 if comment_switch:
198 print l
199 continue
201 if 0:
202 pass
203 elif l[0] == '$':
204 print parse_musical_attributes (l)
205 elif l[0] == '@':
206 parse_line_comment (l)
208 elif l[0] in 'ABCDEFG':
209 parse_note_line (l)