]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
Merge branch 'master' of /home/jcharles/GIT/Lily/. into translation
[lilypond.git] / lily / timing-translator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "timing-translator.hh"
21
22 #include "warn.hh"
23 #include "translator-group.hh"
24 #include "global-context.hh"
25 #include "moment.hh"
26 #include "lily-imports.hh"
27
28 void
29 Timing_translator::stop_translation_timestep ()
30 {
31   Global_context *global = get_global_context ();
32
33   if (to_boolean (get_property ("timing"))
34       && !to_boolean (get_property ("skipBars")))
35     {
36       Moment barleft = (measure_length () - measure_position (context ()));
37       Moment now = now_mom ();
38
39       if (barleft > Moment (0))
40         {
41           Moment nextmom = now + barleft;
42           nextmom.grace_part_ = Rational (0);
43           global->add_moment_to_process (nextmom);
44         }
45     }
46 }
47
48 void
49 Timing_translator::initialize ()
50 {
51   Context *timing = unsmob<Context>
52     (Lily::ly_context_find (context ()->self_scm (), ly_symbol2scm ("Timing")));
53   if (timing != context ())
54     {
55       context ()->add_alias (ly_symbol2scm ("Timing"));
56
57       if (!timing)
58         {
59           programming_error ("Can't find Timing context template");
60           timing = context ();
61         }
62     }
63
64   SCM barnumber = timing->get_property ("currentBarNumber");
65   if (!scm_is_integer (barnumber))
66     barnumber = scm_from_int (1);
67   context ()->set_property ("currentBarNumber", barnumber);
68   context ()->set_property ("internalBarNumber", barnumber);
69
70   SCM timeSignatureFraction = timing->get_property ("timeSignatureFraction");
71
72   if (!scm_is_pair (timeSignatureFraction))
73     {
74       programming_error ("missing timeSignatureFraction");
75       timeSignatureFraction = scm_cons (scm_from_int (4), scm_from_int (4));
76     }
77   context ()->set_property ("timeSignatureFraction", timeSignatureFraction);
78
79   SCM measureLength = timing->get_property ("measureLength");
80
81   if (!unsmob<Moment> (measureLength))
82     {
83       measureLength =
84         Moment (ly_scm2rational
85                 (scm_divide (scm_car (timeSignatureFraction),
86                              scm_cdr (timeSignatureFraction)))).smobbed_copy ();
87     }
88   context ()->set_property ("measureLength", measureLength);
89
90   /*
91     Do not init measurePosition; this should be done from global
92     context.
93   */
94
95   SCM timeSignatureSettings = timing->get_property ("timeSignatureSettings");
96   if (!scm_is_pair (timeSignatureSettings))
97     {
98       programming_error ("missing timeSignatureSettings");
99       timeSignatureSettings = Lily::default_time_signature_settings;
100     }
101   context ()->set_property ("timeSignatureSettings", timeSignatureSettings);
102
103   SCM beamExceptions = timing->get_property ("beamExceptions");
104   if (!scm_is_pair (beamExceptions))
105     {
106       beamExceptions = Lily::beam_exceptions (timeSignatureFraction,
107                                               timeSignatureSettings);
108     }
109   context ()->set_property ("beamExceptions", beamExceptions);
110
111   SCM baseMoment = timing->get_property ("baseMoment");
112   if (!unsmob<Moment> (baseMoment))
113     {
114       baseMoment =
115         Moment (ly_scm2rational
116                 (Lily::base_length (timeSignatureFraction,
117                                     timeSignatureSettings))).smobbed_copy ();
118     }
119   context ()->set_property ("baseMoment", baseMoment);
120
121   SCM beatStructure = timing->get_property ("beatStructure");
122   if (!scm_is_pair (beatStructure))
123     {
124       beatStructure =
125         Lily::beat_structure (ly_rational2scm (unsmob<Moment> (baseMoment)->main_part_),
126                               timeSignatureFraction,
127                               timeSignatureSettings);
128     }
129   context ()->set_property ("beatStructure", beatStructure);
130
131   context ()->set_property ("beamHalfMeasure",
132                             timing->get_property ("beamHalfMeasure"));
133
134   context ()->set_property ("autoBeaming",
135                             timing->get_property ("autoBeaming"));
136 }
137
138 Rational
139 Timing_translator::measure_length () const
140 {
141   SCM l = get_property ("measureLength");
142   if (unsmob<Moment> (l))
143     return unsmob<Moment> (l)->main_part_;
144   else
145     return Rational (1);
146 }
147
148 Timing_translator::Timing_translator ()
149 {
150 }
151
152 void
153 Timing_translator::start_translation_timestep ()
154 {
155   Global_context *global = get_global_context ();
156
157   Moment now = global->now_mom ();
158   Moment dt = now - global->previous_moment ();
159   if (dt < Moment (0))
160     {
161       programming_error ("moving backwards in time");
162       dt = 0;
163     }
164   else if (dt.main_part_.is_infinity ())
165     {
166       programming_error ("moving infinitely to future");
167       dt = 0;
168     }
169
170   if (!dt.to_bool ())
171     return;
172
173   Moment measposp;
174
175   SCM s = get_property ("measurePosition");
176   if (unsmob<Moment> (s))
177     measposp = *unsmob<Moment> (s);
178   else
179     {
180       measposp = now;
181     }
182
183   int current_barnumber = robust_scm2int (get_property ("currentBarNumber"), 0);
184   int internal_barnumber = robust_scm2int (get_property ("internalBarNumber"), 0);
185
186   SCM cad = get_property ("timing");
187   bool c = to_boolean (cad);
188
189   if (c)
190     {
191       Rational len = measure_length ();
192
193       measposp += dt;
194
195       while (measposp.main_part_ >= len)
196         {
197           measposp.main_part_ -= len;
198           current_barnumber++;
199           internal_barnumber++;
200         }
201     }
202
203
204   // Because "timing" can be switched on and off asynchronously with
205   // graces, measurePosition might get into strange settings of
206   // grace_part_.  It does not actually make sense to have it diverge
207   // from the main timing.  Updating the grace part outside of the
208   // actual check for "timing" looks strange and will lead to changes
209   // of grace_part_ even when timing is off.  However, when timing is
210   // switched back on again, this will generally happen in an override
211   // that does _not_ in itself advance current_moment.  So the whole
212   // timing advance logic will only get triggered while "timing" is
213   // still of.  Maybe we should keep measurePosition.grace_part_
214   // constantly at zero anyway?
215
216   measposp.grace_part_ = now.grace_part_;
217
218
219   context ()->set_property ("currentBarNumber", scm_from_int (current_barnumber));
220   context ()->set_property ("internalBarNumber", scm_from_int (internal_barnumber));
221   context ()->set_property ("measurePosition", measposp.smobbed_copy ());
222 }
223
224 #include "translator.icc"
225
226 void
227 Timing_translator::boot ()
228 {
229
230 }
231
232 ADD_TRANSLATOR (Timing_translator,
233                 /* doc */
234                 "This engraver adds the alias @code{Timing} to its containing"
235                 " context.  Responsible for synchronizing timing information"
236                 " from staves.  Normally in @code{Score}.  In order to create"
237                 " polyrhythmic music, this engraver should be removed from"
238                 " @code{Score} and placed in @code{Staff}.",
239
240                 /* create */
241                 "",
242
243                 /* read */
244                 "baseMoment "
245                 "currentBarNumber "
246                 "internalBarNumber "
247                 "measureLength "
248                 "measurePosition "
249                 "timeSignatureFraction ",
250
251                 /* write */
252                 "baseMoment "
253                 "currentBarNumber "
254                 "internalBarNumber "
255                 "measureLength "
256                 "measurePosition "
257                 "timeSignatureFraction "
258                );