[go: up one dir, main page]

File: duration.py

package info (click to toggle)
solfege 3.10.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 12,408 kB
  • ctags: 4,270
  • sloc: python: 22,161; xml: 7,536; ansic: 4,442; makefile: 685; sh: 308
file content (68 lines) | stat: -rw-r--r-- 2,135 bytes parent folder | download
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
# GNU Solfege - free ear training software
# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2007, 2008  Tom Cato Amundsen
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

"""
>>> d1=Duration(4, 0, Rat(1, 1))
>>> d2=Duration(4, 1, Rat(1, 1))
>>> d1==d2
False
>>> d1.get_rat_value()
(Rat 1/4)
>>> d2.get_rat_value()
(Rat 3/8)
>>> d3=Duration(4, 2, Rat(2, 3))
>>> d3.get_rat_value()
(Rat 7/24)
"""

from rat import Rat

class Duration:
    def __init__(self, nh, dots, tuple=Rat(1, 1)):
        self.m_nh = nh
        self.m_dots = dots
        self.m_tuple = tuple
    def __cmp__(self, B):
        """
        >>> A=Duration(4, 1, Rat(1, 1))
        >>> B=Duration(4, 1, Rat(1, 1))
        >>> C=Duration(2, 2, Rat(4, 7))
        >>> A==None, A==B, A==C
        (False, True, False)
        >>> (cmp(A, C), cmp(A, B))
        (-1, 0)
        """
        if not B:
            return -1
        return cmp(self.get_rat_value(), B.get_rat_value())
    def get_rat_value(self):
        """
        >>> A=Duration(4, 1, Rat(1, 1))
        >>> B=Duration(4, 2, Rat(3, 5))
        >>> A.get_rat_value(), B.get_rat_value()
        ((Rat 3/8), (Rat 21/80))
        """
        d = Rat(1, self.m_nh)
        if self.m_dots > 0:
            d = d + Rat(1, self.m_nh * 2)
        if self.m_dots > 1:
            d = d + Rat(1, self.m_nh * 4)
        return d * self.m_tuple
    def clone(self):
        return Duration(self.m_nh, self.m_dots, self.m_tuple)
    def __str__(self):
        return "(Duration:%s:%idot:%s)" % (self.m_nh, self.m_dots, self.m_tuple)