]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
* lily/include/translator.hh (class Translator): rename
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include "warn.hh"
11 #include "timing-translator.hh"
12
13 #include "translator-group.hh"
14 #include "global-context.hh"
15 #include "multi-measure-rest.hh"
16
17
18 void
19 Timing_translator::stop_translation_timestep ()
20 {
21   Global_context *global = get_global_context ();
22
23   /* allbars == ! skipbars */
24   SCM sb = get_property ("skipBars");
25   bool allbars = !to_boolean (sb);
26
27   // urg: multi bar rests: should always process whole of first bar?
28   SCM tim = get_property ("timing");
29   bool timb = to_boolean (tim);
30   if (timb && allbars)
31     {
32       Moment barleft = (measure_length () - measure_position ());
33       Moment now = now_mom ();
34
35       if (barleft > Moment (0)
36           /*
37             Hmm. We insert the bar moment every time we process a
38             moment.  A waste of cpu?
39            */
40           && !now.grace_part_)
41         global->add_moment_to_process (now + barleft);
42     }
43 }
44
45 void
46 Timing_translator::initialize ()
47 {
48
49   /*
50     move this to engraver-init.ly? 
51    */
52   context ()->add_alias (ly_symbol2scm ("Timing"));
53   context ()->set_property ("timing" , SCM_BOOL_T);  
54   context ()->set_property ("currentBarNumber" , scm_int2num (1));
55
56   context ()->set_property ("timeSignatureFraction",
57                                 scm_cons (scm_int2num (4), scm_int2num (4)));
58   /*
59     Do not init measurePosition; this should be done from global
60     context.
61    */
62   context ()->set_property ("measureLength", Moment (Rational (1)).smobbed_copy ());
63   context ()->set_property ("beatLength", Moment (Rational (1,4)).smobbed_copy ());
64 }
65
66 Rational
67 Timing_translator::measure_length () const
68 {
69   SCM l = get_property ("measureLength");
70   if (unsmob_moment (l))
71     return unsmob_moment (l)->main_part_;
72   else
73     return Rational (1);
74 }
75
76 Timing_translator::Timing_translator ()
77 {
78
79 }
80
81 Moment
82 Timing_translator::measure_position () const
83 {
84   SCM sm = get_property ("measurePosition");
85   
86   Moment m   =0;
87   if (unsmob_moment (sm))
88     {
89       m = *unsmob_moment (sm);
90       while (m.main_part_ < Rational (0))
91         m.main_part_ += measure_length ();
92     }
93   
94   return m;
95 }
96
97 void
98 Timing_translator::start_translation_timestep ()
99 {
100   Global_context *global =get_global_context ();
101
102   Moment now = global->now_mom ();
103   Moment dt = now  - global->previous_moment ();
104   if (dt < Moment (0))
105     {
106       programming_error ("Moving backwards in time");
107       dt = 0;
108     }
109   else if (dt.main_part_.is_infinity ())
110     {
111       programming_error ("Moving infinitely to future");
112       dt = 0;
113     }
114   
115   if (!dt.to_bool ())
116     return;
117
118   Moment measposp;
119
120   SCM s = get_property ("measurePosition");
121   if (unsmob_moment (s))
122     {
123       measposp = *unsmob_moment (s);
124     }
125   else
126     {
127       measposp = now;
128       context ()->set_property ("measurePosition",
129                                     measposp.smobbed_copy ());
130     }
131   
132   measposp += dt;
133   
134   SCM barn = get_property ("currentBarNumber");
135   int b = 0;
136   if (ly_c_number_p (barn))
137     {
138       b = ly_scm2int (barn);
139     }
140
141   SCM cad = get_property ("timing");
142   bool c= to_boolean (cad);
143
144   Rational len = measure_length ();
145   while (c && measposp.main_part_ >= len)
146     {
147       measposp.main_part_ -= len;
148       b ++;
149     }
150
151   context ()->set_property ("currentBarNumber", scm_int2num (b));
152   context ()->set_property ("measurePosition", measposp.smobbed_copy ());
153 }
154
155 ENTER_DESCRIPTION (Timing_translator,
156                    "This engraver adds the alias "
157                    "@code{Timing} to its containing context."
158                    ,
159
160                    "","","","","");