lilypond-1.3.87
[lilypond.git] / buildscripts / musedata2ly.py
blob1bbd165db6674c24943e013806677c7b4c2bf696
1 #!@PYTHON@
3 # musedata = musedata.stanford.edu
4 # musedata = COBOL for musicians.
5 # todo: rewrite this.
7 import re
8 import sys
9 import string
11 f = open (sys.argv[1])
12 lines =f.readlines()
14 def chomp (x):
15 return re.sub ('[\r\n \t]+$','', x)
17 lines = map (chomp, lines)
19 default_header_dict = {
20 'tagline' :'automatically converted from Musedata',
21 'copyright' : 'all rights reserved -- free for noncommercial use'
24 # Jezus, wat een ranzig formaat. (2am)
25 def parse_header (lines):
26 d = default_header_dict
27 enter = string.split (lines[3], ' ')
28 d['enteredby'] = string.join (enter[1:])
29 d['enteredon'] = enter[0]
30 d['opus'] = lines[4]
31 d['source'] = lines[5]
32 d['title'] = lines[6]
33 d['subtitle'] = lines[7]
34 d['instrument']= lines[8]
35 d['musedatamisc'] =lines[9]
36 d['musedatagroups'] =lines[10]
37 d['musedatagroupnumber']=lines[11]
39 return d
41 clef_dict = {
42 04: 'treble',
43 13 : 'alto',
44 22: 'bass',
48 def get_clef(s):
49 return '\\clef "%s";\n' % clef_dict [string.atoi (s)]
51 def get_mudela_notename (p, ac):
52 if p > 5:
53 p = p - 7
54 s = chr (p + ord ('c'))
55 infix = 'i'
56 if ac < 0:
57 infix = 'e'
58 ac = -ac
60 while ac:
61 s = s + infix + 's'
62 ac = ac - 1
63 return s
65 def get_key (s):
66 i = string.atoi (s)
67 return ''
69 def get_timesig (s):
70 return '\\time %s;\n' % s
73 divisions = 4
74 def get_divisions_per_quarter (s):
75 divisions = string.atoi (s)
76 return ''
78 def get_directive (s):
79 return '%% %s\n' % s
81 def get_transposing (s):
82 return ''
84 def get_num_instruments (s):
85 return ''
87 attr_dict = {
88 'C' : get_clef,
89 'K' : get_key ,
90 'T' : get_timesig,
91 'Q' : get_divisions_per_quarter,
92 'D' : get_directive,
93 'X' : get_transposing,
94 'I': get_num_instruments,
97 def parse_musical_attributes (l):
98 s = ''
99 l = l[1:]
100 atts = re.split('[ \t]+', l)
101 for a in atts:
102 if not a:
103 continue
104 m = re.search ('(.):(.*)', a)
105 if m == None:
106 print 'Huh, unknown attr `%s\'' % a
107 continue
109 s = s + attr_dict[m.group(1)](m.group (2))
110 return s
113 def get_mudela_pitch (n, a, o):
114 c = '\''
115 if o < 1:
116 c = ','
117 o = 1 - o
119 return get_mudela_notename (n,a) + '%s' % c * o
121 def dump_header (h, out):
122 out.write ('\\header {\n')
123 for tup in h.items ():
124 out.write ('\t%s = \"%s\";\n' % tup)
125 out.write ('}\n')
127 header_dict = parse_header (lines[0:12])
128 dump_header (header_dict, sys.stdout)
131 lines = lines [12:]
134 def parse_line_comment (l):
135 return re.sub ('@' , '%' , l)
137 def parse_note_line (l):
138 pitch = ((ord (l[0]) -ord('A')) + 5) % 7
139 acc = 0
140 l= l[1:]
141 while l[0] == 'f':
142 l= l[1:]
143 acc = acc - 1
144 while l[0] == '#':
145 l= l[1:]
146 acc = acc + 1
147 while l[0] in ' \t':
148 l= l[1:]
150 oct = 0
151 if l[0] in '0123456789':
152 oct = string.atoi (l[0]) - 4
153 l= l[1:]
155 while l[0] in ' \t':
156 l= l[1:]
159 print get_mudela_pitch (pitch,acc,oct), parse_duration(l[:2])
160 l = l[2:]
165 def parse_duration (l):
166 s = ''
167 while l[0] in '0123456789':
168 s = s + l[0]
169 l= l[1:]
170 print l
171 num = string.atoi (s)
172 den = 4 * divisions
174 current_dots = 0
175 try_dots = [3, 2, 1]
176 for d in try_dots:
177 f = 1 << d
178 multiplier = (2*f-1)
179 if num % multiplier == 0 and den % f == 0:
180 num = num / multiplier
181 den = den / f
182 current_dots = current_dots + d
184 if num <> 1:
185 sys.stderr.write ('huh. Durations left')
186 return '%s%s' % (den, '.' * current_dots)
188 comment_switch = 0
189 for l in lines:
190 if l[0] == '&':
191 comment_switch = not comment_switch
192 if comment_switch:
193 l= l[1:]
194 print '%{'
195 else:
196 print '%}'
198 if comment_switch:
199 print l
200 continue
202 if 0:
203 pass
204 elif l[0] == '$':
205 print parse_musical_attributes (l)
206 elif l[0] == '@':
207 parse_line_comment (l)
209 elif l[0] in 'ABCDEFG':
210 parse_note_line (l)