]> git.donarmstrong.com Git - lilypond.git/blob - lily/duration.cc
release: 1.5.36
[lilypond.git] / lily / duration.cc
1 /*
2   duration.cc -- implement Duration, Plet, 
3
4   source file of the LilyPond music typesetter
5
6   (c)  1997--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7            Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
9 */
10
11 #include <assert.h>
12
13 #include "lily-proto.hh"
14 #include "string.hh"
15 #include "moment.hh"
16 #include "duration.hh"
17 #include "ly-smobs.icc"
18
19
20
21 int
22 Duration::compare (Duration const &left, Duration const &right)
23 {
24   return Rational::compare (left.length_mom (), right.length_mom ());
25 }
26
27 Duration::Duration ()
28 {
29   durlog_i_ = 0;
30   dots_i_ = 0;
31   factor_ = Rational (1,1);
32 }
33
34 Duration::Duration (int l, int d)
35 {
36   durlog_i_ = l;
37   dots_i_ = d;
38   factor_ = Rational (1,1);
39 }
40
41 Duration
42 Duration::compressed (Rational m) const
43 {
44   Duration d (*this);
45   d.factor_ *= m;
46   return d;
47 }
48
49 Rational
50 Duration::length_mom () const
51 {
52   Rational mom (1 << abs (durlog_i_));
53
54   if (durlog_i_> 0)
55     mom = Rational (1)/mom;
56
57   Rational delta = mom;
58
59   for (int d = dots_i_; d; d--)
60     {
61       delta /= Rational (2);
62       mom += delta;
63     }
64
65   return mom * factor_;
66 }
67
68
69
70 String
71 Duration::str () const
72 {
73   String s =  to_str (durlog_i_) + to_str ('.', dots_i_);
74   if (factor_ != Moment (Rational (1,1)))
75     {
76       s += "*" + factor_.str ();
77     }
78   return s;
79 }
80
81
82 IMPLEMENT_TYPE_P (Duration, "duration?");
83
84 SCM
85 Duration::mark_smob (SCM)
86 {
87   return SCM_EOL;
88 }
89
90 IMPLEMENT_SIMPLE_SMOBS (Duration);
91
92
93 int
94 Duration::print_smob (SCM s, SCM port, scm_print_state *)
95 {
96   Duration  *r = (Duration *) ly_cdr (s);
97      
98   scm_puts ("#<Duration ", port);
99   scm_display (ly_str02scm (r->str ().ch_C ()), port);
100   scm_puts (" >", port);
101   
102   return 1;
103 }
104
105 SCM
106 Duration::equal_p (SCM a , SCM b)
107 {
108   Duration  *p = (Duration *) ly_cdr (a);
109   Duration  *q = (Duration *) ly_cdr (b);  
110
111   bool eq = p->dots_i_ == q->dots_i_
112     && p->durlog_i_ == q->durlog_i_
113     && p->factor_ == q->factor_;
114
115   return eq ? SCM_BOOL_T : SCM_BOOL_F;
116 }
117   
118 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
119 SCM
120 Duration::less_p (SCM p1, SCM p2)
121 {
122   Duration *a = unsmob_duration (p1);
123   Duration *b = unsmob_duration (p2);
124
125   if (compare (*a, *b) < 0)
126     return SCM_BOOL_T;
127   else
128     return SCM_BOOL_F;
129 }
130
131 static SCM
132 make_duration (SCM l, SCM d)
133 {
134   SCM_ASSERT_TYPE(gh_number_p(l), l, SCM_ARG1, __FUNCTION__, "integer");
135   SCM_ASSERT_TYPE(gh_number_p(d), d, SCM_ARG2, __FUNCTION__, "integer");
136   
137   Duration p (gh_scm2int (l), gh_scm2int (d));
138   return p.smobbed_copy ();
139 }
140
141 static void
142 add_funcs ()
143 {
144   scm_c_define_gsubr ("make-duration", 2, 0, 0,
145                       (Scheme_function_unknown)make_duration);
146 }
147
148 ADD_SCM_INIT_FUNC (duration, add_funcs);
149
150 SCM
151 Duration::smobbed_copy ()const
152 {
153   Duration *  p = new Duration (*this);
154   return p->smobbed_self ();
155 }
156
157 int
158 Duration::duration_log () const
159 {
160   return durlog_i_;
161 }
162
163 int
164 Duration::dot_count () const
165 {
166   return dots_i_;
167 }
168