lilypond-1.3.12
[lilypond.git] / lily / timing-translator.cc
blobd1e68dcb336bffee8e69d811cdd94eca5691bd56
1 /*
2 timing-translator.cc -- implement Timing_translator
5 source file of the GNU LilyPond music typesetter
7 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "debug.hh"
11 #include "timing-translator.hh"
12 #include "command-request.hh"
13 #include "translator-group.hh"
14 #include "global-translator.hh"
15 #include "multi-measure-rest.hh"
17 bool
18 Timing_translator::do_try_music (Music*r)
20 if (Timing_req *t = dynamic_cast <Timing_req *> (r))
22 for (int i=0; i < timing_req_l_arr_.size (); i++)
24 if (timing_req_l_arr_[i]->equal_b(t))
25 return true;
26 if (String (classname (timing_req_l_arr_[i])) == classname (r))
28 r->warning (_ ("conflicting timing request"));
29 return false;
33 timing_req_l_arr_.push(t);
34 return true;
36 return false;
39 /*ugh.
41 Time_signature_change_req*
42 Timing_translator::time_signature_req_l() const
44 Time_signature_change_req *m_l=0;
45 for (int i=0; !m_l && i < timing_req_l_arr_.size (); i++)
47 m_l=dynamic_cast<Time_signature_change_req*> (timing_req_l_arr_[i]);
49 return m_l;
52 void
53 Timing_translator::do_process_requests()
55 for (int i=0; i < timing_req_l_arr_.size (); i++)
57 Timing_req * tr_l = timing_req_l_arr_[i];
59 if (Time_signature_change_req *m_l = dynamic_cast <Time_signature_change_req *> (tr_l))
61 int b_i= m_l->beats_i_;
62 int o_i = m_l->one_beat_i_;
63 set_time_signature (b_i, o_i);
65 else if (dynamic_cast <Barcheck_req *> (tr_l))
67 if (measure_position ())
69 tr_l ->warning (_f ("barcheck failed at: %s",
70 measure_position ().str ()));
71 // resync
72 daddy_trans_l_->set_property("measurePosition",
73 (new Moment)->smobify_self ());
81 void
82 Timing_translator::do_pre_move_processing()
84 timing_req_l_arr_.set_size (0);
85 Translator *t = this;
86 Global_translator *global_l =0;
89 t = t->daddy_trans_l_ ;
90 global_l = dynamic_cast<Global_translator*> (t);
92 while (!global_l);
94 /* allbars == ! skipbars */
95 SCM sb = get_property ("skipBars", 0);
96 bool allbars = !(gh_boolean_p (sb)&&gh_scm2bool (sb));
98 // urg: multi bar rests: should always process whole of first bar?
99 SCM tim = get_property ("timing", 0);
100 bool timb = gh_boolean_p (tim) && gh_scm2bool (tim);
101 if (timb && allbars)
103 Moment barleft = (measure_length () - measure_position ());
105 if (barleft < Moment (0))
106 barleft = 0;
108 global_l->add_moment_to_process (now_mom () + barleft);
113 ADD_THIS_TRANSLATOR(Timing_translator);
115 void
116 Timing_translator::do_creation_processing()
118 daddy_trans_l_->set_property ("timing" , SCM_BOOL_T);
119 daddy_trans_l_->set_property ("currentBarNumber" , gh_int2scm (1));
120 daddy_trans_l_->set_property("measurePosition",
121 (new Moment)->smobify_self());
122 daddy_trans_l_->set_property ("oneBeat",
123 (new Moment (1,4))->smobify_self ());
124 daddy_trans_l_->set_property("measureLength",
125 (new Moment (1))->smobify_self());
128 Moment
129 Timing_translator::measure_length () const
131 SCM l = get_property("measureLength",0);
132 if (SMOB_IS_TYPE_B(Moment, l))
133 return *SMOB_TO_TYPE (Moment, l);
134 else
135 return Moment (1);
139 void
140 Timing_translator::get_time_signature (int *n, int *d) const
142 Moment one_beat (1,4);
143 SCM one = get_property ("beatLength",0);
144 if (SMOB_IS_TYPE_B (Moment, one))
145 one_beat = *SMOB_TO_TYPE (Moment, one);
146 *n = measure_length () / one_beat;
147 *d = one_beat.den_i ();
151 void
152 Timing_translator::set_time_signature (int l, int o)
154 Moment one_beat = Moment (1)/Moment (o);
155 Moment len = Moment (l) * one_beat;
156 daddy_trans_l_->set_property ("measureLength",
157 (new Moment (len))->smobify_self ());
158 daddy_trans_l_->set_property ("beatength",
159 (new Moment (one_beat))->smobify_self ());
162 Timing_translator::Timing_translator()
167 Moment
168 Timing_translator::measure_position () const
170 SCM sm = get_property ("measurePosition",0);
172 Moment m =0;
173 if (SMOB_IS_TYPE_B (Moment, sm))
175 m = *SMOB_TO_TYPE (Moment, sm);
176 while (m < Moment (0))
177 m += measure_length ();
180 return m;
183 void
184 Timing_translator::do_post_move_processing()
186 Translator *t = this;
187 Global_translator *global_l =0;
190 t = t->daddy_trans_l_ ;
191 global_l = dynamic_cast<Global_translator*> (t);
193 while (!global_l);
195 Moment dt = global_l->now_mom_ - global_l -> prev_mom_;
196 if (dt < Moment (0))
198 programming_error ("Moving backwards in time");
199 dt = 0;
202 if (!dt)
203 return;
205 Moment * measposp =0;
207 SCM s = get_property ("measurePosition", 0);
208 if (SMOB_IS_TYPE_B (Moment, s))
210 measposp = SMOB_TO_TYPE (Moment,s);
212 else
214 measposp = new Moment;
215 daddy_trans_l_->set_property ("measurePosition", measposp->smobify_self ());
218 *measposp += dt;
219 // don't need to set_property
221 Translator_group * tr =daddy_trans_l_;
222 SCM barn = get_property ("currentBarNumber", &tr);
223 int b = 0;
224 if (gh_number_p(barn))
226 b = gh_scm2int (barn);
229 SCM cad = get_property ("timing", 0);
230 bool c= gh_boolean_p (cad ) && gh_scm2bool (cad);
232 Moment len = measure_length ();
233 while (c && *measposp >= len)
235 *measposp -= len;
236 b ++;
239 tr->set_property ("currentBarNumber", gh_int2scm (b));
242 int
243 Timing_translator::bars_i () const
245 return gh_scm2int (get_property ("currentBarNumber", 0));