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