lilypond-1.3.136
[lilypond.git] / scripts / mup-to-ly.py
blobc9eb62d4b5b6e20bb6d17b1c29d1573e95666243
1 #!@PYTHON@
3 # mup-to-ly.py --
4 #
5 # source file of the GNU LilyPond music typesetter
6 #
7 # (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
9 # TODO: regex -> re.
11 name = 'mup-to-ly'
12 version = '0.1'
14 import os
15 import sys
17 import getopt
18 from string import *
19 import regex
20 import regsub
21 import time
23 def program_id ():
24 return name + ' version ' + version;
26 def identify ():
27 sys.stdout.write (program_id () + '\n')
29 def help ():
30 sys.stdout.write ("Usage: %s [options] [files]\n"
31 "Convert mup to ly\n\n"
32 + "Options:\n"
33 + " -h, --help print this help\n"
34 % (program_name)
36 sys.exit (0)
38 identify ()
39 (options, files) = getopt.getopt (
40 sys.argv[1:], 'hp:', ['help', 'package'])
41 for opt in options:
42 o = opt[0]
43 a = opt[1]
44 if o== '--help' or o == '-h':
45 help ()
46 elif o == '-p' or o == '--package':
47 topdir = a
48 else:
49 print o
50 raise getopt.error
52 def gulp_file (f):
53 sys.stderr.write ('[%s' % f)
54 try:
55 i = open (f)
56 i.seek (0, 2)
57 n = i.tell ()
58 i.seek (0,0)
59 except:
60 sys.stderr.write ('can\'t open file %s\n ' % f)
61 return ''
62 s = i.read (n)
63 sys.stderr.write (']')
64 if len (s) <= 0:
65 sys.stderr.write ('gulped empty file: %s\n'% f)
66 return s
68 def line_to_ly (s):
69 notes = ""
70 o = 0
71 i = regex.search (";", s)
72 last_name = "c"
73 last_duration = "4"
74 while i >= 0:
75 note = s[o:o+i]
76 o = o + i
77 i = regex.search (";", s[o+1:])
78 if i >= 0 :
79 o = o + 1
80 name = regsub.gsub ("[0-9<>\.&]*", "", note)
81 duration = regsub.gsub ("[a-z+<>#+&\-]*", "", note)
82 duration = regsub.gsub (" ", "", duration)
83 if name:
84 last_name = name
85 else:
86 name = last_name
87 if duration:
88 last_duration = duration
89 else:
90 duration = last_duration
91 name = regsub.sub ("#", "is", name)
92 name = regsub.sub ("+", "'", name)
93 name = regsub.sub ("-", ",", name)
94 name = regsub.sub ("ms", "s1", name)
95 notes = notes + " %s%s" % (name, duration)
96 return notes
98 def get_voice (staff, s, staffs):
99 voice = len (staffs[staff-1]) + 1
100 tag = "^%d [0-9-]*%d[0-9-]*:" % (staff, voice)
101 notes = ""
102 o = 0
103 i = regex.search (tag, s)
104 while i >= 0:
105 o = o + i
106 n = regex.search ("$", s[o:])
107 line = s[o:o+n]
108 line = regsub.sub (tag, "", line)
109 line = line_to_ly (line)
110 notes = notes + line
111 i = regex.search (tag, s[o+n:])
112 if i >= 0:
113 i = i + n
114 if notes != "":
115 sys.stderr.write ('%d ' % voice)
116 staffs[staff-1].append (notes)
117 return notes != ""
119 def get_staff (s, staffs):
120 staff=len (staffs)
122 sys.stderr.write ('Staff %d ( ' % staff)
123 while i:
124 i = get_voice (staff, s, staffs)
125 if not i:
126 sys.stderr.write (')\n')
127 staffs.append ([])
128 staff = staff + 1
129 sys.stderr.write ('Staff %d ( ' % staff)
130 i = get_voice (staff, s, staffs)
131 if not i:
132 del staffs[staff-1]
133 return 0
135 staffs=[[]]
136 mup=files[0]
137 ly = os.path.basename (os.path.splitext (mup)[0]) + ".ly"
138 s = gulp_file (mup)
139 sys.stderr.write ('\n')
141 while i:
142 i=get_staff (s, staffs)
143 sys.stderr.write ('\n')
144 sys.stderr.write ('Ly output to: %s...' % ly)
145 lyfile = open (ly, "w")
146 for i in range (len (staffs)):
147 for v in range (len (staffs[i])):
148 lyfile.write ("$staff%d_voice_%d = \\notes {\n %s\n}\n\n" % (i+1, v+1, staffs[i][v]))
149 lyfile.write ("\\score{\n")
150 lyfile.write ("\\notes <\n")
151 for i in range (len (staffs)):
152 lyfile.write ("\\type Staff=staff%s <\n" % chr(ord('A')+i))
153 for v in range (len (staffs[i])):
154 lyfile.write ("{ \\$staff%d_voice_%d } " % (i+1, v+1))
155 lyfile.write ("\n>\n")
156 lyfile.write (">\n")
157 lyfile.write ("\n}")
158 lyfile.close ()
159 sys.stderr.write ('\n')