lilypond-1.5.10
[lilypond.git] / lily / timing-translator.cc
blob7c05a7d14bd5f292e01ceb8bdf815051fef4531d
1 /*
2 timing-translator.cc -- implement Timing_translator
5 source file of the GNU LilyPond music typesetter
7 (c) 1997--2001 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"
18 TODO: change the rest of lily, so communication with
19 Timing_translator is only done through properties. This means the
20 class declaration can go here. */
21 bool
22 Timing_translator::try_music (Music*r)
24 if (dynamic_cast<Barcheck_req*> (r))
26 check_ = r;
27 return true;
29 return false;
32 void
33 Timing_translator::process_music ()
35 if (check_ && measure_position ().main_part_)
37 check_->origin ()->warning (_f ("barcheck failed at: %s",
38 measure_position ().str ()));
39 Moment zero;
41 if (!to_boolean (get_property ("barCheckNoSynchronize")))
42 daddy_trans_l_->set_property ("measurePosition", zero.smobbed_copy ());
46 void
47 Timing_translator::stop_translation_timestep ()
49 check_ = 0;
51 Translator *t = this;
52 Global_translator *global_l =0;
55 t = t->daddy_trans_l_ ;
56 global_l = dynamic_cast<Global_translator*> (t);
58 while (!global_l);
60 /* allbars == ! skipbars */
61 SCM sb = get_property ("skipBars");
62 bool allbars = !to_boolean (sb);
64 // urg: multi bar rests: should always process whole of first bar?
65 SCM tim = get_property ("timing");
66 bool timb = to_boolean (tim);
67 if (timb && allbars)
69 Moment barleft = (measure_length () - measure_position ());
70 Moment now = now_mom ();
72 if (barleft > Moment (0)
74 Hmm. We insert the bar moment every time we process a
75 moment. A waste of cpu?
77 && !now.grace_part_)
78 global_l->add_moment_to_process (now + barleft);
83 ADD_THIS_TRANSLATOR (Timing_translator);
85 void
86 Timing_translator::initialize ()
88 daddy_trans_l_->set_property ("timing" , SCM_BOOL_T);
89 daddy_trans_l_->set_property ("currentBarNumber" , gh_int2scm (1));
91 daddy_trans_l_->set_property ("timeSignatureFraction",
92 gh_cons (gh_int2scm (4), gh_int2scm (4)));
93 daddy_trans_l_->set_property ("measurePosition", Moment (Rational (0)).smobbed_copy ());
94 daddy_trans_l_->set_property ("measureLength", Moment (Rational (1)).smobbed_copy ());
95 daddy_trans_l_->set_property ("beatLength", Moment (Rational (1,4)).smobbed_copy ());
98 Rational
99 Timing_translator::measure_length () const
101 SCM l = get_property ("measureLength");
102 if (unsmob_moment (l))
103 return unsmob_moment (l)->main_part_;
104 else
105 return Rational (1);
108 Timing_translator::Timing_translator ()
113 Moment
114 Timing_translator::measure_position () const
116 SCM sm = get_property ("measurePosition");
118 Moment m =0;
119 if (unsmob_moment (sm))
121 m = *unsmob_moment (sm);
122 while (m.main_part_ < Rational (0))
123 m.main_part_ += measure_length ();
126 return m;
129 void
130 Timing_translator::start_translation_timestep ()
132 check_ = 0;
133 Translator *t = this;
134 Global_translator *global_l =0;
137 t = t->daddy_trans_l_ ;
138 global_l = dynamic_cast<Global_translator*> (t);
140 while (!global_l);
142 Moment now = global_l->now_mom_;
143 Moment dt = now - global_l -> prev_mom_;
144 if (dt < Moment (0))
146 programming_error ("Moving backwards in time");
147 dt = 0;
150 if (!dt.to_bool ())
151 return;
153 Moment measposp;
155 SCM s = get_property ("measurePosition");
156 if (unsmob_moment (s))
158 measposp = *unsmob_moment (s);
160 else
162 measposp = now;
163 daddy_trans_l_->set_property ("measurePosition", measposp.smobbed_copy ());
166 measposp += dt;
168 SCM barn = get_property ("currentBarNumber");
169 int b = 0;
170 if (gh_number_p (barn))
172 b = gh_scm2int (barn);
175 SCM cad = get_property ("timing");
176 bool c= to_boolean (cad);
178 Rational len = measure_length ();
179 while (c && measposp.main_part_ >= len)
181 measposp.main_part_ -= len;
182 b ++;
185 daddy_trans_l_->set_property ("currentBarNumber", gh_int2scm (b));
186 daddy_trans_l_->set_property ("measurePosition", measposp.smobbed_copy ());