]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-description.cc
6fecdb99d89ef1f34c9dab19ee565e50ad1dc627
[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--1999 Han-Wen Nienhuys <hanwen@cs.uu.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_.str ();
20   s +="\ntime_signature " + (whole_per_measure_/one_beat_).str () +":" +
21          (Moment (Moment (1)/one_beat_)).str ();
22   s += "\nposition " + to_str (bars_i_) + ":"+ whole_in_measure_.str () +"\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   whole_per_measure_ = 1;
61   whole_in_measure_ =0;
62   one_beat_ = Moment (1,4);
63   when_ = 0;
64   bars_i_ = 1;                  // musician start counting at 1
65   cadenza_b_ = false;
66 }
67
68 void
69 Time_description::add (Moment dt)
70 {
71   assert (dt >= Moment (0));
72   when_ +=  dt;
73   whole_in_measure_ += dt;
74
75   while (!cadenza_b_ && whole_in_measure_ >= whole_per_measure_)
76     {
77         whole_in_measure_ -= whole_per_measure_;
78         bars_i_ ++;
79     }
80 }
81
82 void
83 Time_description::set_time_signature (int l, int o)
84 {
85   assert (o);
86   one_beat_ = Moment (1)/Moment (o);
87   whole_per_measure_ = Moment (l) * one_beat_;
88 }
89
90 bool
91 Time_description::allow_time_signature_change_b()
92 {
93   return!(whole_in_measure_);
94 }
95
96 /**
97   retrieve error messages.
98   @return
99   error messages if not possible, "" if possible
100   */
101 String
102 Time_description::try_set_partial_str (Moment p) const
103 {
104   if (p<Moment (0))
105         return (_ ("partial measure 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() const
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     {
131         assert (t1.bars_i_==t2.bars_i_);
132         assert (t1.one_beat_ == t2.one_beat_);
133         assert (t1.whole_in_measure_ == t2.whole_in_measure_);
134         assert (t1.whole_per_measure_ == t2.whole_per_measure_);
135     }
136
137   return i;
138 }
139
140 Moment
141 Time_description::next_bar_moment() const
142 {
143   return when_ + barleft();
144 }