*** empty log message ***
[lilypond.git] / lily / timing-translator.cc
blob10d09d3d9a4aa3798702718c65a3f44b22dc4f72
1 /*
2 timing-translator.cc -- implement Timing_translator
5 source file of the GNU LilyPond music typesetter
7 (c) 1997--2004 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-context.hh"
15 #include "multi-measure-rest.hh"
18 void
19 Timing_translator::stop_translation_timestep ()
21 Global_context *global = get_global_context ();
23 /* allbars == ! skipbars */
24 SCM sb = get_property ("skipBars");
25 bool allbars = !to_boolean (sb);
27 // urg: multi bar rests: should always process whole of first bar?
28 SCM tim = get_property ("timing");
29 bool timb = to_boolean (tim);
30 if (timb && allbars)
32 Moment barleft = (measure_length () - measure_position ());
33 Moment now = now_mom ();
35 if (barleft > Moment (0)
37 Hmm. We insert the bar moment every time we process a
38 moment. A waste of cpu?
40 && !now.grace_part_)
41 global->add_moment_to_process (now + barleft);
45 void
46 Timing_translator::initialize ()
50 move this to engraver-init.ly?
52 daddy_context_->add_alias (ly_symbol2scm ("Timing"));
53 daddy_context_->set_property ("timing" , SCM_BOOL_T);
54 daddy_context_->set_property ("currentBarNumber" , gh_int2scm (1));
56 daddy_context_->set_property ("timeSignatureFraction",
57 gh_cons (gh_int2scm (4), gh_int2scm (4)));
59 Do not init measurePosition; this should be done from global
60 context.
62 daddy_context_->set_property ("measureLength", Moment (Rational (1)).smobbed_copy ());
63 daddy_context_->set_property ("beatLength", Moment (Rational (1,4)).smobbed_copy ());
66 Rational
67 Timing_translator::measure_length () const
69 SCM l = get_property ("measureLength");
70 if (unsmob_moment (l))
71 return unsmob_moment (l)->main_part_;
72 else
73 return Rational (1);
76 Timing_translator::Timing_translator ()
81 Moment
82 Timing_translator::measure_position () const
84 SCM sm = get_property ("measurePosition");
86 Moment m =0;
87 if (unsmob_moment (sm))
89 m = *unsmob_moment (sm);
90 while (m.main_part_ < Rational (0))
91 m.main_part_ += measure_length ();
94 return m;
97 void
98 Timing_translator::start_translation_timestep ()
100 Global_context *global =get_global_context ();
102 Moment now = global->now_mom ();
103 Moment dt = now - global->previous_moment ();
104 if (dt < Moment (0))
106 programming_error ("Moving backwards in time");
107 dt = 0;
109 else if (dt.main_part_.is_infinity ())
111 programming_error ("Moving infinitely to future");
112 dt = 0;
115 if (!dt.to_bool ())
116 return;
118 Moment measposp;
120 SCM s = get_property ("measurePosition");
121 if (unsmob_moment (s))
123 measposp = *unsmob_moment (s);
125 else
127 measposp = now;
128 daddy_context_->set_property ("measurePosition",
129 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_context_->set_property ("currentBarNumber", gh_int2scm (b));
152 daddy_context_->set_property ("measurePosition", measposp.smobbed_copy ());
155 ENTER_DESCRIPTION (Timing_translator,
156 "This engraver adds the alias "
157 "@code{Timing} to its containing context."
160 "","","","","");