(readPostTable): ugh. Kludge: nglyphs in maxp
[lilypond.git] / lily / timing-translator.cc
blobfc9ddc83a8c0d0a993d61b61e4865349cb92bead
1 /*
2 timing-translator.cc -- implement Timing_translator
5 source file of the GNU LilyPond music typesetter
7 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "timing-translator.hh"
12 #include "warn.hh"
13 #include "translator-group.hh"
14 #include "global-context.hh"
15 #include "multi-measure-rest.hh"
17 void
18 Timing_translator::stop_translation_timestep ()
20 Global_context *global = get_global_context ();
22 /* allbars == ! skipbars */
23 SCM sb = get_property ("skipBars");
24 bool allbars = !to_boolean (sb);
26 // urg: multi bar rests: should always process whole of first bar?
27 SCM tim = get_property ("timing");
28 bool timb = to_boolean (tim);
29 if (timb && allbars)
31 Moment barleft = (measure_length () - measure_position ());
32 Moment now = now_mom ();
34 if (barleft > Moment (0)
36 Hmm. We insert the bar moment every time we process a
37 moment. A waste of cpu?
39 && !now.grace_part_)
40 global->add_moment_to_process (now + barleft);
44 void
45 Timing_translator::initialize ()
49 move this to engraver-init.ly?
51 context ()->add_alias (ly_symbol2scm ("Timing"));
52 context ()->set_property ("timing", SCM_BOOL_T);
53 context ()->set_property ("currentBarNumber", scm_int2num (1));
55 context ()->set_property ("timeSignatureFraction",
56 scm_cons (scm_int2num (4), scm_int2num (4)));
58 Do not init measurePosition; this should be done from global
59 context.
61 context ()->set_property ("measureLength", Moment (Rational (1)).smobbed_copy ());
62 context ()->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 ()
79 Moment
80 Timing_translator::measure_position () const
82 SCM sm = get_property ("measurePosition");
84 Moment m = 0;
85 if (unsmob_moment (sm))
87 m = *unsmob_moment (sm);
88 while (m.main_part_ < Rational (0))
89 m.main_part_ += measure_length ();
92 return m;
95 void
96 Timing_translator::start_translation_timestep ()
98 Global_context *global = get_global_context ();
100 Moment now = global->now_mom ();
101 Moment dt = now - global->previous_moment ();
102 if (dt < Moment (0))
104 programming_error ("moving backwards in time");
105 dt = 0;
107 else if (dt.main_part_.is_infinity ())
109 programming_error ("moving infinitely to future");
110 dt = 0;
113 if (!dt.to_bool ())
114 return;
116 Moment measposp;
118 SCM s = get_property ("measurePosition");
119 if (unsmob_moment (s))
121 measposp = *unsmob_moment (s);
123 else
125 measposp = now;
126 context ()->set_property ("measurePosition",
127 measposp.smobbed_copy ());
130 measposp += dt;
132 SCM barn = get_property ("currentBarNumber");
133 int b = 0;
134 if (scm_is_number (barn))
136 b = scm_to_int (barn);
139 SCM cad = get_property ("timing");
140 bool c = to_boolean (cad);
142 Rational len = measure_length ();
143 while (c && measposp.main_part_ >= len)
145 measposp.main_part_ -= len;
146 b++;
149 context ()->set_property ("currentBarNumber", scm_int2num (b));
150 context ()->set_property ("measurePosition", measposp.smobbed_copy ());
153 ADD_TRANSLATOR (Timing_translator,
154 "This engraver adds the alias "
155 "@code{Timing} to its containing context.",
157 "", "", "", "", "");