]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-description.cc
release: 1.2.12
[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   DEBUG_OUT << str ();
31 #endif
32 }
33
34 void
35 Time_description::OK() const
36 {
37 #ifndef NDEBUG
38   if (!cadenza_b_)
39     assert (whole_in_measure_ < whole_per_measure_);
40   assert (Moment (0) <= whole_in_measure_);
41   assert (one_beat_);
42 #endif
43 }
44
45 void
46 Time_description::set_cadenza (bool b)
47 {
48   if (cadenza_b_ && !b)
49     {
50       if (whole_in_measure_)
51         {
52           bars_i_ ++;           // should do?
53           whole_in_measure_ = 0;
54         }
55     }
56   cadenza_b_ = b ;
57 }
58
59 Time_description::Time_description()
60 {
61   whole_per_measure_ = 1;
62   whole_in_measure_ =0;
63   one_beat_ = Moment (1,4);
64   when_ = 0;
65   bars_i_ = 1;                  // musician start counting at 1
66   cadenza_b_ = false;
67 }
68
69 void
70 Time_description::add (Moment dt)
71 {
72   assert (dt >= Moment (0));
73   when_ +=  dt;
74   whole_in_measure_ += dt;
75
76   while (!cadenza_b_ && whole_in_measure_ >= whole_per_measure_)
77     {
78       whole_in_measure_ -= whole_per_measure_;
79       bars_i_ ++;
80     }
81 }
82
83 void
84 Time_description::set_time_signature (int l, int o)
85 {
86   assert (o);
87   one_beat_ = Moment (1)/Moment (o);
88   whole_per_measure_ = Moment (l) * one_beat_;
89 }
90
91 bool
92 Time_description::allow_time_signature_change_b()
93 {
94   return!(whole_in_measure_);
95 }
96
97 /**
98   retrieve error messages.
99   @return
100   error messages if not possible, "" if possible
101   */
102 String
103 Time_description::try_set_partial_str (Moment p) const
104 {
105   if (p<Moment (0))
106     return (_ ("Partial measure must be non-negative"));
107   if (p > whole_per_measure_)
108     return (_ ("partial measure too large"));
109   return "";
110 }
111
112 void
113 Time_description::setpartial (Moment p)
114 {
115   whole_in_measure_ = whole_per_measure_ - p;
116 }
117
118 Moment
119 Time_description::barleft() const
120 {
121   assert (!cadenza_b_);
122   return whole_per_measure_-whole_in_measure_;
123 }
124
125 Moment
126 Time_description::next_bar_moment() const
127 {
128   return when_ + barleft();
129 }