*** empty log message ***
[lilypond.git] / lily / duration.cc
blob78c9275a4e971bb0b2cd4443c8bd985e859fcba9
1 /*
2 duration.cc -- implement Duration
4 source file of the LilyPond music typesetter
6 (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "duration.hh"
12 #include "misc.hh"
13 #include "lily-proto.hh"
15 #include "ly-smobs.icc"
17 int
18 Duration::compare (Duration const &left, Duration const &right)
20 return Rational::compare (left.get_length (), right.get_length ());
23 Duration::Duration ()
25 durlog_ = 0;
26 dots_ = 0;
27 factor_ = Rational (1, 1);
30 Duration::Duration (int log, int d)
32 durlog_ = log;
33 dots_ = d;
34 factor_ = Rational (1, 1);
37 Duration
38 Duration::compressed (Rational m) const
40 Duration d (*this);
41 d.factor_ *= m;
42 return d;
45 Rational
46 Duration::get_length () const
48 Rational mom (1 << abs (durlog_));
50 if (durlog_ > 0)
51 mom = Rational (1) / mom;
53 Rational delta = mom;
54 for (int i = 0; i < dots_; i++)
56 delta /= Rational (2);
57 mom += delta;
60 return mom * factor_;
63 String
64 Duration::to_string () const
66 String s;
68 if (durlog_ < 0)
69 s = "log = " + ::to_string (durlog_);
70 else
71 s = ::to_string (1 << durlog_);
73 s += ::to_string ('.', dots_);
74 if (factor_ != Moment (Rational (1, 1)))
75 s += "*" + factor_.to_string ();
76 return s;
79 IMPLEMENT_TYPE_P (Duration, "ly:duration?");
81 SCM
82 Duration::mark_smob (SCM)
84 return SCM_EOL;
87 IMPLEMENT_SIMPLE_SMOBS (Duration);
88 int
89 Duration::print_smob (SCM s, SCM port, scm_print_state *)
91 Duration *r = (Duration *) SCM_CELL_WORD_1 (s);
93 scm_puts ("#<Duration ", port);
94 scm_display (scm_makfrom0str (r->to_string ().to_str0 ()), port);
95 scm_puts (" >", port);
97 return 1;
101 Duration::equal_p (SCM a, SCM b)
103 Duration *p = (Duration *) SCM_CELL_WORD_1 (a);
104 Duration *q = (Duration *) SCM_CELL_WORD_1 (b);
106 bool eq = p->dots_ == q->dots_
107 && p->durlog_ == q->durlog_
108 && p->factor_ == q->factor_;
110 return eq ? SCM_BOOL_T : SCM_BOOL_F;
114 Duration::duration_log () const
116 return durlog_;
120 Duration::dot_count () const
122 return dots_;