]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-description.cc
release: 0.0.64
[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     error_b_ =  false;
59     whole_per_measure_ = 1;
60     whole_in_measure_ =0;
61     one_beat_ = Moment(1,4);
62     when_ = 0;
63     bars_i_ = 0;
64     cadenza_b_ = false;
65 }
66
67 void
68 Time_description::add(Moment dt)
69 {
70     assert(dt >= Rational(0));
71     when_ +=  dt;
72     whole_in_measure_ += dt;
73         
74     while ( !cadenza_b_ && whole_in_measure_ >= whole_per_measure_ ) {
75         whole_in_measure_ -= whole_per_measure_;
76         bars_i_ ++;
77     }
78 }
79
80 void
81 Time_description::set_meter(int l, int o)
82 {
83     assert(o);
84     one_beat_ = Rational(1)/Moment(o);
85     whole_per_measure_ = Moment(l) * one_beat_;
86 }
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 (p<Rational(0))
103         return ("Partial must be non-negative");
104     if (p > whole_per_measure_)
105         return ("Partial measure too large");
106     return "";
107 }
108
109 void
110 Time_description::setpartial(Moment p)
111 {
112     whole_in_measure_ = whole_per_measure_ - p;
113 }
114
115 Moment
116 Time_description::barleft()const
117 {
118     assert(!cadenza_b_);
119     return whole_per_measure_-whole_in_measure_;
120 }
121
122 int
123 Time_description::compare(Time_description const &t1,  Time_description const&t2)
124 {
125     int i = sign(t1.when_-t2.when_);
126
127     if (!i) {
128         assert(t1.bars_i_==t2.bars_i_);
129         assert(t1.one_beat_ == t2.one_beat_);
130         assert(t1.whole_in_measure_ == t2.whole_in_measure_);
131         assert(t1.whole_per_measure_ == t2.whole_per_measure_);
132     }
133
134     return i;
135 }
136
137 Moment
138 Time_description::next_bar_moment() const
139 {
140     return when_ + barleft();
141 }
142