]> git.donarmstrong.com Git - lilypond.git/blob - lily/duration.cc
2002-07-13 Han-Wen <hanwen@cs.uu.nl>
[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;
74
75   if (durlog_i_ < 0  )
76     s = "log = "  + to_str (durlog_i_);
77   else
78     s = to_str (1 << durlog_i_);
79   
80   s += to_str ('.', dots_i_);
81   if (factor_ != Moment (Rational (1,1)))
82     {
83       s += "*" + factor_.str ();
84     }
85   return s;
86 }
87
88
89 IMPLEMENT_TYPE_P (Duration, "duration?");
90
91 SCM
92 Duration::mark_smob (SCM)
93 {
94   return SCM_EOL;
95 }
96
97 IMPLEMENT_SIMPLE_SMOBS (Duration);
98 int
99 Duration::print_smob (SCM s, SCM port, scm_print_state *)
100 {
101   Duration  *r = (Duration *) ly_cdr (s);
102      
103   scm_puts ("#<Duration ", port);
104   scm_display (ly_str02scm (r->str ().ch_C ()), port);
105   scm_puts (" >", port);
106   
107   return 1;
108 }
109
110 SCM
111 Duration::equal_p (SCM a , SCM b)
112 {
113   Duration  *p = (Duration *) ly_cdr (a);
114   Duration  *q = (Duration *) ly_cdr (b);  
115
116   bool eq = p->dots_i_ == q->dots_i_
117     && p->durlog_i_ == q->durlog_i_
118     && p->factor_ == q->factor_;
119
120   return eq ? SCM_BOOL_T : SCM_BOOL_F;
121 }
122   
123 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
124 SCM
125 Duration::less_p (SCM p1, SCM p2)
126 {
127   Duration *a = unsmob_duration (p1);
128   Duration *b = unsmob_duration (p2);
129
130   if (compare (*a, *b) < 0)
131     return SCM_BOOL_T;
132   else
133     return SCM_BOOL_F;
134 }
135
136
137 LY_DEFINE(make_duration,
138           "make-duration", 2, 0, 0, (SCM length, SCM dotcount),
139           "
140 @var{length} is the negative logarithm (base 2) of the duration:
141 1 is a half note, 2 is a quarter note, 3 is an eighth
142 note, etc.  The number of dots after the note is given by
143 @var{dotcount}.
144
145
146 A duration is a musical duration, i.e. a length of time described by a
147 power of two (whole, half, quarter, etc.) and a number of augmentation
148 dots. 
149
150 ")
151 {
152   SCM_ASSERT_TYPE(gh_number_p(length), length, SCM_ARG1, __FUNCTION__, "integer");
153   SCM_ASSERT_TYPE(gh_number_p(dotcount), dotcount, SCM_ARG2, __FUNCTION__, "integer");
154   
155   Duration p (gh_scm2int (length), gh_scm2int (dotcount));
156   return p.smobbed_copy ();
157 }
158
159 SCM
160 Duration::smobbed_copy ()const
161 {
162   Duration *  p = new Duration (*this);
163   return p->smobbed_self ();
164 }
165
166 int
167 Duration::duration_log () const
168 {
169   return durlog_i_;
170 }
171
172 int
173 Duration::dot_count () const
174 {
175   return dots_i_;
176 }
177