]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-description.cc
release: 0.0.58
[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 }
86
87 bool
88 Time_description::allow_meter_change_b()
89 {
90     return!(whole_in_measure_);
91 }
92
93 /**
94   retrieve error messages.
95   @return 
96   error messages if not possible, "" if possible
97   */
98 String
99 Time_description::try_set_partial_str(Moment p)const
100 {
101     if (p<Rational(0))
102         return ("Partial must be non-negative");
103     if (p > whole_per_measure_)
104         return ("Partial measure too large");
105     return "";
106 }
107
108 void
109 Time_description::setpartial(Moment p)
110 {
111     whole_in_measure_ = whole_per_measure_ - p;
112 }
113
114 Moment
115 Time_description::barleft()
116 {
117     assert(!cadenza_b_);
118     return whole_per_measure_-whole_in_measure_;
119 }
120
121 int
122 Time_description::compare(Time_description const &t1,  Time_description const&t2)
123 {
124     int i = sign(t1.when_-t2.when_);
125
126     if (!i) {
127         assert(t1.bars_i_==t2.bars_i_);
128         assert(t1.one_beat_ == t2.one_beat_);
129         assert(t1.whole_in_measure_ == t2.whole_in_measure_);
130         assert(t1.whole_per_measure_ == t2.whole_per_measure_);
131     }
132
133     return i;
134 }