]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-description.cc
release: 0.1.11
[lilypond.git] / lily / time-description.cc
1 /*
2   time-description.cc -- implement Time_description
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include "time-description.hh"
10 #include "debug.hh"
11
12 String
13 Time_description::str() const
14 {
15   String s ("Time_description { ");
16   if (cadenza_b_)
17         s+=String (" (cadenza) ");
18   s+= "at ";
19   s+=when_;
20   s+="\nmeter " + String (whole_per_measure_/one_beat_) +":" +
21         String (Rational (Rational (1)/one_beat_));
22   s+= "\nposition "+String (bars_i_) + ":"+ whole_in_measure_ +"\n}\n";
23   return s;
24 }
25
26 void
27 Time_description::print() const
28 {
29 #ifndef NPRINT
30   DOUT << str();
31 #endif
32 }
33 void
34 Time_description::OK() const
35 {
36 #ifndef NDEBUG
37   if (!cadenza_b_)
38         assert (whole_in_measure_ < whole_per_measure_);
39   assert (Moment (0) <= whole_in_measure_);
40   assert (one_beat_);
41 #endif
42 }
43
44 void
45 Time_description::set_cadenza (bool b)
46 {
47   if (cadenza_b_ && !b) 
48     {
49         if (whole_in_measure_) 
50           {
51             bars_i_ ++;         // should do?
52             whole_in_measure_ = 0;
53           }
54     }
55   cadenza_b_ = b ;
56 }
57
58 Time_description::Time_description()
59 {
60   error_b_ =  false;
61   whole_per_measure_ = 1;
62   whole_in_measure_ =0;
63   one_beat_ = Moment (1,4);
64   when_ = 0;
65   bars_i_ = 1;                  // musician start counting at 1
66   cadenza_b_ = false;
67 }
68
69 void
70 Time_description::add (Moment dt)
71 {
72   assert (dt >= Rational (0));
73   when_ +=  dt;
74   whole_in_measure_ += dt;
75         
76   while (!cadenza_b_ && whole_in_measure_ >= whole_per_measure_) 
77     {
78         whole_in_measure_ -= whole_per_measure_;
79         bars_i_ ++;
80     }
81 }
82
83 void
84 Time_description::set_meter (int l, int o)
85 {
86   assert (o);
87   one_beat_ = Rational (1)/Moment (o);
88   whole_per_measure_ = Moment (l) * one_beat_;
89 }
90
91 bool
92 Time_description::allow_meter_change_b()
93 {
94   return!(whole_in_measure_);
95 }
96
97 /**
98   retrieve error messages.
99   @return 
100   error messages if not possible, "" if possible
101   */
102 String
103 Time_description::try_set_partial_str (Moment p) const
104 {
105   if (p<Rational (0))
106         return ("Partial must be non-negative");
107   if (p > whole_per_measure_)
108         return ("Partial measure too large");
109   return "";
110 }
111
112 void
113 Time_description::setpartial (Moment p)
114 {
115   whole_in_measure_ = whole_per_measure_ - p;
116 }
117
118 Moment
119 Time_description::barleft() const
120 {
121   assert (!cadenza_b_);
122   return whole_per_measure_-whole_in_measure_;
123 }
124
125 int
126 Time_description::compare (Time_description const &t1,  Time_description const&t2)
127 {
128   int i = sign (t1.when_-t2.when_);
129
130   if (!i) 
131     {
132         assert (t1.bars_i_==t2.bars_i_);
133         assert (t1.one_beat_ == t2.one_beat_);
134         assert (t1.whole_in_measure_ == t2.whole_in_measure_);
135         assert (t1.whole_per_measure_ == t2.whole_per_measure_);
136     }
137
138   return i;
139 }
140
141 Moment
142 Time_description::next_bar_moment() const
143 {
144   return when_ + barleft();
145 }
146