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