File size: 2,290 Bytes
f65fe85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
#
# This file is part of LilyPond, the GNU music typesetter.
#
# Copyright (C) 2016--2020 John Gourlay <[email protected]>
#
# LilyPond is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LilyPond is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.


from fractions import Fraction

import lilylib as ly
import musicexp


def rational_to_lily_duration(rational_len):
    d = musicexp.Duration()

    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)

    # Duration of the form 1/2^n or 3/2^n can be converted to a simple lilypond duration
    dots = {1: 0, 3: 1, 7: 2, 15: 3, 31: 4, 63: 5,
            127: 6}.get(rational_len.numerator, -1)
    if d_log >= dots >= 0:
        # account for the dots!
        d.duration_log = d_log - dots
        d.dots = dots
    elif d_log >= 0:
        d.duration_log = d_log
        d.factor = Fraction(rational_len.numerator)
    else:
        ly.warning(_("Encountered rational duration with denominator %s, "
                     "unable to convert to lilypond duration") %
                   rational_len.denominator)
        # TODO: Test the above error message
        return None

    return d


def musicxml_step_to_lily(step):
    if step:
        return (ord(step) - ord('A') + 7 - 2) % 7
    else:
        return None


class Marker(musicexp.Music):
    def __init__(self):
        self.direction = 0
        self.event = None

    def print_ly(self, printer):
        ly.warning(_("Encountered unprocessed marker %s\n") % self)
        pass

    def ly_expression(self):
        return ""


class RepeatMarker(Marker):
    def __init__(self):
        Marker.__init__(self)
        self.times = 0


class EndingMarker(Marker):
    pass