]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
* lily/context.cc (where_defined): also assign value in
[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_int2num (1));
54
55   context ()->set_property ("timeSignatureFraction",
56                             scm_cons (scm_int2num (4), scm_int2num (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
80 void
81 Timing_translator::start_translation_timestep ()
82 {
83   Global_context *global = get_global_context ();
84
85   Moment now = global->now_mom ();
86   Moment dt = now - global->previous_moment ();
87   if (dt < Moment (0))
88     {
89       programming_error ("moving backwards in time");
90       dt = 0;
91     }
92   else if (dt.main_part_.is_infinity ())
93     {
94       programming_error ("moving infinitely to future");
95       dt = 0;
96     }
97
98   if (!dt.to_bool ())
99     return;
100
101   Moment measposp;
102
103   SCM s = get_property ("measurePosition");
104   if (unsmob_moment (s))
105     {
106       measposp = *unsmob_moment (s);
107     }
108   else
109     {
110       measposp = now;
111       context ()->set_property ("measurePosition",
112                                 measposp.smobbed_copy ());
113     }
114
115   measposp += dt;
116
117   SCM barn = get_property ("currentBarNumber");
118   int b = 0;
119   if (scm_is_number (barn))
120     {
121       b = scm_to_int (barn);
122     }
123
124   SCM cad = get_property ("timing");
125   bool c = to_boolean (cad);
126
127   Rational len = measure_length ();
128   while (c && measposp.main_part_ >= len)
129     {
130       measposp.main_part_ -= len;
131       b++;
132     }
133
134   context ()->set_property ("currentBarNumber", scm_int2num (b));
135   context ()->set_property ("measurePosition", measposp.smobbed_copy ());
136 }
137
138 #include "translator.icc"
139
140 ADD_TRANSLATOR (Timing_translator,
141                 "This engraver adds the alias "
142                 "@code{Timing} to its containing context."
143                 "Responsible for synchronizing timing information from staves.  "
144                 "Normally in @code{Score}.  In order to create polyrhythmic music, "
145                 "this engraver should be removed from @code{Score} and placed in "
146                 "@code{Staff}. "
147                 "\n\nThis engraver adds the alias @code{Timing} to its containing context."
148
149                 ,
150
151                 "",  "", "", "");