]> git.donarmstrong.com Git - lilypond.git/blob - python/musicxml2ly_conversion.py
Resurrect the file musicxml2ly_conversion.py, moving the few functions
[lilypond.git] / python / musicxml2ly_conversion.py
1 # -*- coding: utf-8 -*-
2 import lilylib as ly
3 import musicexp
4
5 def rational_to_lily_duration(rational_len):
6     d = musicexp.Duration()
7
8     rational_len.normalize_self()
9     d_log = {1: 0, 2: 1, 4:2, 8:3, 16:4, 32:5, 64:6, 128:7, 256:8, 512:9}.get(rational_len.denominator(), -1)
10
11     # Duration of the form 1/2^n or 3/2^n can be converted to a simple lilypond duration
12     dots = {1: 0, 3: 1, 7: 2, 15: 3, 31: 4, 63: 5, 127: 6}.get(rational_len.numerator(), -1)
13     if(d_log >= dots >= 0):
14         # account for the dots!
15         d.duration_log = d_log - dots
16         d.dots = dots
17     elif(d_log >= 0):
18         d.duration_log = d_log
19         d.factor = Rational(rational_len.numerator())
20     else:
21         ly.warning(_("Encountered rational duration with denominator %s, "
22                        "unable to convert to lilypond duration") %
23                     rational_len.denominator())
24         # TODO: Test the above error message
25         return None
26
27     return d
28
29 def musicxml_step_to_lily(step):
30     if step:
31         return (ord(step) - ord('A') + 7 - 2) % 7
32     else:
33         return None
34
35 class Marker(musicexp.Music):
36     def __init__(self):
37         self.direction = 0
38         self.event = None
39     def print_ly(self, printer):
40         ly.warning(_("Encountered unprocessed marker %s\n") % self)
41         pass
42     def ly_expression(self):
43         return ""
44     
45 class RepeatMarker(Marker):
46     def __init__(self):
47         Marker.__init__(self)
48         self.times = 0
49
50 class EndingMarker(Marker):
51     pass