]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
Nitpick run.
[lilypond.git] / lily / timing-translator.cc
1 /*
2   timing-translator.cc -- implement Timing_translator
3
4
5   source file of the GNU LilyPond music typesetter
6
7   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include "timing-translator.hh"
11
12 #include "warn.hh"
13 #include "translator-group.hh"
14 #include "global-context.hh"
15 #include "multi-measure-rest.hh"
16
17 void
18 Timing_translator::stop_translation_timestep ()
19 {
20   Global_context *global = get_global_context ();
21
22   /* allbars == ! skipbars */
23   SCM sb = get_property ("skipBars");
24   bool allbars = !to_boolean (sb);
25
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)
30     {
31       Moment barleft = (measure_length () - measure_position (context ()));
32       Moment now = now_mom ();
33
34       if (barleft > Moment (0)
35           /*
36             Hmm. We insert the bar moment every time we process a
37             moment.  A waste of cpu?
38           */
39           && !now.grace_part_)
40         global->add_moment_to_process (now + barleft);
41     }
42 }
43
44 void
45 Timing_translator::initialize ()
46 {
47
48   /*
49     move this to engraver-init.ly?
50   */
51   context ()->add_alias (ly_symbol2scm ("Timing"));
52   context ()->set_property ("timing", SCM_BOOL_T);
53   context ()->set_property ("currentBarNumber", scm_from_int (1));
54
55   context ()->set_property ("timeSignatureFraction",
56                             scm_cons (scm_from_int (4), scm_from_int (4)));
57   /*
58     Do not init measurePosition; this should be done from global
59     context.
60   */
61   context ()->set_property ("measureLength", Moment (Rational (1)).smobbed_copy ());
62   context ()->set_property ("beatLength", Moment (Rational (1, 4)).smobbed_copy ());
63 }
64
65 Rational
66 Timing_translator::measure_length () const
67 {
68   SCM l = get_property ("measureLength");
69   if (unsmob_moment (l))
70     return unsmob_moment (l)->main_part_;
71   else
72     return Rational (1);
73 }
74
75 Timing_translator::Timing_translator ()
76 {
77 }
78
79 void
80 Timing_translator::start_translation_timestep ()
81 {
82   Global_context *global = get_global_context ();
83
84   Moment now = global->now_mom ();
85   Moment dt = now - global->previous_moment ();
86   if (dt < Moment (0))
87     {
88       programming_error ("moving backwards in time");
89       dt = 0;
90     }
91   else if (dt.main_part_.is_infinity ())
92     {
93       programming_error ("moving infinitely to future");
94       dt = 0;
95     }
96
97   if (!dt.to_bool ())
98     return;
99
100   Moment measposp;
101
102   SCM s = get_property ("measurePosition");
103   if (unsmob_moment (s))
104     measposp = *unsmob_moment (s);
105   else
106     {
107       measposp = now;
108       context ()->set_property ("measurePosition",
109                                 measposp.smobbed_copy ());
110     }
111
112   measposp += dt;
113
114   SCM barn = get_property ("currentBarNumber");
115   int b = 0;
116   if (scm_is_number (barn))
117     b = scm_to_int (barn);
118
119   SCM cad = get_property ("timing");
120   bool c = to_boolean (cad);
121
122   Rational len = measure_length ();
123   while (c && measposp.main_part_ >= len)
124     {
125       measposp.main_part_ -= len;
126       b++;
127     }
128
129   context ()->set_property ("currentBarNumber", scm_from_int (b));
130   context ()->set_property ("measurePosition", measposp.smobbed_copy ());
131 }
132
133 #include "translator.icc"
134
135 ADD_TRANSLATOR (Timing_translator,
136                 "This engraver adds the alias "
137                 "@code{Timing} to its containing context."
138                 "Responsible for synchronizing timing information from staves.  "
139                 "Normally in @code{Score}.  In order to create polyrhythmic music, "
140                 "this engraver should be removed from @code{Score} and placed in "
141                 "@code{Staff}. "
142                 "\n\nThis engraver adds the alias @code{Timing} to its containing context.",
143
144                 "", "", "", "");