]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / timing-translator.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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
26 void
27 Timing_translator::stop_translation_timestep ()
28 {
29   Global_context *global = get_global_context ();
30
31   if (to_boolean (get_property ("timing"))
32       && !to_boolean (get_property ("skipBars")))
33     {
34       Moment barleft = (measure_length () - measure_position (context ()));
35       Moment now = now_mom ();
36
37       if (barleft > Moment (0))
38         {
39           Moment nextmom = now + barleft;
40           nextmom.grace_part_ = Rational (0);
41           global->add_moment_to_process (nextmom);
42         }
43     }
44 }
45
46 void
47 Timing_translator::initialize ()
48 {
49   context ()->add_alias (ly_symbol2scm ("Timing"));
50   context ()->set_property ("currentBarNumber", scm_from_int (1));
51   context ()->set_property ("internalBarNumber", scm_from_int (1));
52
53   context ()->set_property ("timeSignatureFraction",
54                             scm_cons (scm_from_int (4), scm_from_int (4)));
55   /*
56     Do not init measurePosition; this should be done from global
57     context.
58   */
59   context ()->set_property ("measureLength",
60                             Moment (Rational (1)).smobbed_copy ());
61   context ()->set_property ("baseMoment",
62                             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   int current_barnumber = robust_scm2int (get_property ("currentBarNumber"), 0);
115   int internal_barnumber = robust_scm2int (get_property ("internalBarNumber"), 0);
116
117   SCM cad = get_property ("timing");
118   bool c = to_boolean (cad);
119
120   Rational len = measure_length ();
121   while (c && measposp.main_part_ >= len)
122     {
123       measposp.main_part_ -= len;
124       current_barnumber++;
125       internal_barnumber++;
126     }
127
128   context ()->set_property ("currentBarNumber", scm_from_int (current_barnumber));
129   context ()->set_property ("internalBarNumber", scm_from_int (internal_barnumber));
130   context ()->set_property ("measurePosition", measposp.smobbed_copy ());
131 }
132
133 #include "translator.icc"
134
135 ADD_TRANSLATOR (Timing_translator,
136                 /* doc */
137                 "This engraver adds the alias @code{Timing} to its containing"
138                 " context.  Responsible for synchronizing timing information"
139                 " from staves.  Normally in @code{Score}.  In order to create"
140                 " polyrhythmic music, this engraver should be removed from"
141                 " @code{Score} and placed in @code{Staff}.",
142
143                 /* create */
144                 "",
145
146                 /* read */
147                 "internalBarNumber "
148                 "currentBarNumber "
149                 "measureLength "
150                 "measurePosition ",
151
152                 /* write */
153                 "baseMoment "
154                 "currentBarNumber "
155                 "internalBarNumber "
156                 "measureLength "
157                 "measurePosition "
158                 "timeSignatureFraction "
159                );