]> git.donarmstrong.com Git - lilypond.git/blob - lily/duration.cc
*** empty log message ***
[lilypond.git] / lily / duration.cc
1 /*
2   duration.cc -- implement Duration
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7                  Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
9 */
10
11 #include "duration.hh"
12
13 #include "misc.hh"
14 #include "lily-proto.hh"
15
16 #include "ly-smobs.icc"
17
18 int
19 Duration::compare (Duration const &left, Duration const &right)
20 {
21   return Rational::compare (left.get_length (), right.get_length ());
22 }
23
24 Duration::Duration ()
25 {
26   durlog_ = 0;
27   dots_ = 0;
28   factor_ = Rational (1, 1);
29 }
30
31 Duration::Duration (int log, int d)
32 {
33   durlog_ = log;
34   dots_ = d;
35   factor_ = Rational (1, 1);
36 }
37
38 Duration
39 Duration::compressed (Rational m) const
40 {
41   Duration d (*this);
42   d.factor_ *= m;
43   return d;
44 }
45
46 Rational
47 Duration::get_length () const
48 {
49   Rational mom (1 << abs (durlog_));
50
51   if (durlog_> 0)
52     mom = Rational (1) / mom;
53
54   Rational delta = mom;
55   for (int i = 0; i < dots_; i++)
56     {
57       delta /= Rational (2);
58       mom += delta;
59     }
60
61   return mom * factor_;
62 }
63
64 String
65 Duration::to_string () const
66 {
67   String s;
68
69   if (durlog_ < 0  )
70     s = "log = "  + ::to_string (durlog_);
71   else
72     s = ::to_string (1 << durlog_);
73
74   s += ::to_string ('.', dots_);
75   if (factor_ != Moment (Rational (1, 1)))
76     s += "*" + factor_.to_string ();
77   return s;
78 }
79
80
81 IMPLEMENT_TYPE_P (Duration, "ly:duration?");
82
83 SCM
84 Duration::mark_smob (SCM)
85 {
86   return SCM_EOL;
87 }
88
89 IMPLEMENT_SIMPLE_SMOBS (Duration);
90 int
91 Duration::print_smob (SCM s, SCM port, scm_print_state *)
92 {
93   Duration  *r = (Duration *) SCM_CELL_WORD_1 (s);
94
95   scm_puts ("#<Duration ", port);
96   scm_display (scm_makfrom0str (r->to_string ().to_str0 ()), port);
97   scm_puts (" >", port);
98
99   return 1;
100 }
101
102 SCM
103 Duration::equal_p (SCM a , SCM b)
104 {
105   Duration  *p = (Duration *) SCM_CELL_WORD_1 (a);
106   Duration  *q = (Duration *) SCM_CELL_WORD_1 (b);
107
108   bool eq = p->dots_ == q->dots_
109     && p->durlog_ == q->durlog_
110     && p->factor_ == q->factor_;
111
112   return eq ? SCM_BOOL_T : SCM_BOOL_F;
113 }
114
115
116 int
117 Duration::duration_log () const
118 {
119   return durlog_;
120 }
121
122 int
123 Duration::dot_count () const
124 {
125   return dots_;
126 }