]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-description.cc
release: 0.0.47
[lilypond.git] / lily / time-description.cc
1 /*
2   time-description.cc -- implement Time_description
3
4   source file of the 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     mtor << 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         if (whole_in_measure_) {
49             bars_i_ ++;         // should do?
50             whole_in_measure_ = 0;
51         }
52     }
53     cadenza_b_ = b ;
54 }
55
56 Time_description::Time_description()
57 {
58     whole_per_measure_ = 1;
59     whole_in_measure_ =0;
60     one_beat_ = Moment(1,4);
61     when_ = 0;
62     bars_i_ = 0;
63     cadenza_b_ = false;
64 }
65
66 void
67 Time_description::add(Moment dt)
68 {
69     assert(dt >= Rational(0));
70     when_ +=  dt;
71     whole_in_measure_ += dt;
72         
73     while ( !cadenza_b_ && whole_in_measure_ >= whole_per_measure_ ) {
74         whole_in_measure_ -= whole_per_measure_;
75         bars_i_ ++;
76     }
77 }
78
79 void
80 Time_description::set_meter(int l, int o)
81 {
82     assert(o);
83     one_beat_ = Rational(1)/Moment(o);
84     whole_per_measure_ = Moment(l) * one_beat_;
85     if(whole_in_measure_)
86         error_t("Meterchange should be at start of measure", *this);
87 }
88 bool
89 Time_description::allow_meter_change_b()
90 {
91     return!(whole_in_measure_);
92 }
93
94 /**
95   retrieve error messages.
96   @return 
97   error messages if not possible, "" if possible
98   */
99 String
100 Time_description::try_set_partial_str(Moment p)const
101 {
102       if (when_)
103         return ("Partial measure only allowed at beginning.");
104     if (p<Rational(0))
105         return ("Partial must be non-negative");
106     if (p > whole_per_measure_)
107         return ("Partial measure too large");
108     return "";
109 }
110
111 void
112 Time_description::setpartial(Moment p)
113 {
114     whole_in_measure_ = whole_per_measure_ - p;
115 }
116
117 Moment
118 Time_description::barleft()
119 {
120     assert(!cadenza_b_);
121     return whole_per_measure_-whole_in_measure_;
122 }
123
124 int
125 Time_description::compare(Time_description const &t1,  Time_description const&t2)
126 {
127     int i = sign(t1.when_-t2.when_);
128
129     if (!i) {
130         assert(t1.bars_i_==t2.bars_i_);
131         assert(t1.one_beat_ == t2.one_beat_);
132         assert(t1.whole_in_measure_ == t2.whole_in_measure_);
133         assert(t1.whole_per_measure_ == t2.whole_per_measure_);
134     }
135
136     return i;
137 }