]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-description.cc
release: 0.0.39-1
[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_ ++;
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
89 void
90 Time_description::setpartial(Moment p)
91 {
92     if (when_)
93         error_t ("Partial measure only allowed at beginning.", *this);
94     if (p<Rational(0)||p > whole_per_measure_)
95         error_t ("Partial measure has incorrect size", *this);
96     whole_in_measure_ = whole_per_measure_ - p;
97 }
98
99 Moment
100 Time_description::barleft()
101 {
102     assert(!cadenza_b_);
103     return whole_per_measure_-whole_in_measure_;
104 }
105
106 int
107 Time_description::compare(Time_description &t1, Time_description&t2)
108 {
109     int i = sign(t1.when_-t2.when_);
110
111     if (!i) {
112         assert(t1.bars_i_==t2.bars_i_);
113         assert(t1.one_beat_ == t2.one_beat_);
114         assert(t1.whole_in_measure_ == t2.whole_in_measure_);
115         assert(t1.whole_per_measure_ == t2.whole_per_measure_);
116     }
117
118     return i;
119 }