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