*** empty log message ***
[lilypond.git] / lily / timing-translator.cc
blob9676e982650ff99217a38cc5f93e318fa5cdcdbc
1 /*
2 timing-translator.cc -- implement Timing_translator
5 source file of the GNU LilyPond music typesetter
7 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "warn.hh"
11 #include "timing-translator.hh"
13 #include "translator-group.hh"
14 #include "global-translator.hh"
15 #include "multi-measure-rest.hh"
18 void
19 Timing_translator::stop_translation_timestep ()
21 Translator *t = this;
22 Global_translator *global =0;
25 t = t->daddy_trans_ ;
26 global = dynamic_cast<Global_translator*> (t);
28 while (!global);
30 /* allbars == ! skipbars */
31 SCM sb = get_property ("skipBars");
32 bool allbars = !to_boolean (sb);
34 // urg: multi bar rests: should always process whole of first bar?
35 SCM tim = get_property ("timing");
36 bool timb = to_boolean (tim);
37 if (timb && allbars)
39 Moment barleft = (measure_length () - measure_position ());
40 Moment now = now_mom ();
42 if (barleft > Moment (0)
44 Hmm. We insert the bar moment every time we process a
45 moment. A waste of cpu?
47 && !now.grace_part_)
48 global->add_moment_to_process (now + barleft);
52 void
53 Timing_translator::initialize ()
55 daddy_trans_->set_property ("timing" , SCM_BOOL_T);
56 daddy_trans_->set_property ("currentBarNumber" , gh_int2scm (1));
58 daddy_trans_->set_property ("timeSignatureFraction",
59 gh_cons (gh_int2scm (4), gh_int2scm (4)));
60 daddy_trans_->set_property ("measurePosition", Moment (Rational (0)).smobbed_copy ());
61 daddy_trans_->set_property ("measureLength", Moment (Rational (1)).smobbed_copy ());
62 daddy_trans_->set_property ("beatLength", Moment (Rational (1,4)).smobbed_copy ());
65 Rational
66 Timing_translator::measure_length () const
68 SCM l = get_property ("measureLength");
69 if (unsmob_moment (l))
70 return unsmob_moment (l)->main_part_;
71 else
72 return Rational (1);
75 Timing_translator::Timing_translator ()
80 Moment
81 Timing_translator::measure_position () const
83 SCM sm = get_property ("measurePosition");
85 Moment m =0;
86 if (unsmob_moment (sm))
88 m = *unsmob_moment (sm);
89 while (m.main_part_ < Rational (0))
90 m.main_part_ += measure_length ();
93 return m;
96 void
97 Timing_translator::start_translation_timestep ()
99 Translator *t = this;
100 Global_translator *global =0;
103 t = t->daddy_trans_ ;
104 global = dynamic_cast<Global_translator*> (t);
106 while (!global);
108 Moment now = global->now_mom_;
109 Moment dt = now - global -> prev_mom_;
110 if (dt < Moment (0))
112 programming_error ("Moving backwards in time");
113 dt = 0;
116 if (!dt.to_bool ())
117 return;
119 Moment measposp;
121 SCM s = get_property ("measurePosition");
122 if (unsmob_moment (s))
124 measposp = *unsmob_moment (s);
126 else
128 measposp = now;
129 daddy_trans_->set_property ("measurePosition", measposp.smobbed_copy ());
132 measposp += dt;
134 SCM barn = get_property ("currentBarNumber");
135 int b = 0;
136 if (gh_number_p (barn))
138 b = gh_scm2int (barn);
141 SCM cad = get_property ("timing");
142 bool c= to_boolean (cad);
144 Rational len = measure_length ();
145 while (c && measposp.main_part_ >= len)
147 measposp.main_part_ -= len;
148 b ++;
151 daddy_trans_->set_property ("currentBarNumber", gh_int2scm (b));
152 daddy_trans_->set_property ("measurePosition", measposp.smobbed_copy ());
155 ENTER_DESCRIPTION(Timing_translator,"","","","","","");