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