]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
release: 1.5.5
[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--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include "debug.hh"
11 #include "timing-translator.hh"
12 #include "command-request.hh"
13 #include "translator-group.hh"
14 #include "global-translator.hh"
15 #include "multi-measure-rest.hh"
16
17 /*
18   TODO: change the rest of lily, so communication with
19   Timing_translator is only done through properties.  This means the
20   class declaration can go here.  */
21 bool
22 Timing_translator::try_music (Music*r)
23 {
24   if (dynamic_cast<Barcheck_req*> (r))
25     {
26       check_ = r;
27       return true;
28     }
29   return false;
30 }
31
32 void
33 Timing_translator::process_music ()
34 {
35   if (check_ && measure_position ().main_part_)
36     {
37       check_->origin ()->warning (_f ("barcheck failed at: %s", 
38                                       measure_position ().str ()));
39       Moment zero; 
40       
41       if (!to_boolean (get_property ("barCheckNoSynchronize")))
42         daddy_trans_l_->set_property ("measurePosition", zero.smobbed_copy ());
43     }
44 }
45
46 void
47 Timing_translator::stop_translation_timestep ()
48 {
49   check_ = 0;
50   
51   Translator *t = this;
52   Global_translator *global_l =0;
53   do
54     {
55       t = t->daddy_trans_l_ ;
56       global_l = dynamic_cast<Global_translator*> (t);
57     }
58   while (!global_l);
59
60   /* allbars == ! skipbars */
61   SCM sb = get_property ("skipBars");
62   bool allbars = !to_boolean (sb);
63
64   // urg: multi bar rests: should always process whole of first bar?
65   SCM tim = get_property ("timing");
66   bool timb = to_boolean (tim);
67   if (timb && allbars)
68     {
69       Moment barleft = (measure_length () - measure_position ());
70       Moment now = now_mom ();
71
72       if (barleft > Moment (0)
73           /*
74             Hmm. We insert the bar moment every time we process a
75             moment.  A waste of cpu?
76            */
77           && !now.grace_part_)
78         global_l->add_moment_to_process (now + barleft);
79     }
80 }
81
82
83 ADD_THIS_TRANSLATOR (Timing_translator);
84
85 void
86 Timing_translator::initialize ()
87 {
88   daddy_trans_l_->set_property ("timing" , SCM_BOOL_T);  
89   daddy_trans_l_->set_property ("currentBarNumber" , gh_int2scm (1));
90
91   daddy_trans_l_->set_property ("timeSignatureFraction",
92                                 gh_cons (gh_int2scm (4), gh_int2scm (4)));
93   daddy_trans_l_->set_property ("measurePosition", Moment (0).smobbed_copy ());
94   daddy_trans_l_->set_property ("measureLength", Moment (1).smobbed_copy ());
95   daddy_trans_l_->set_property ("beatLength", Moment (1,4).smobbed_copy ());
96 }
97
98 Rational
99 Timing_translator::measure_length () const
100 {
101   SCM l = get_property ("measureLength");
102   if (unsmob_moment (l))
103     return unsmob_moment (l)->main_part_;
104   else
105     return Rational (1);
106 }
107
108 Timing_translator::Timing_translator ()
109 {
110
111 }
112
113 Moment
114 Timing_translator::measure_position () const
115 {
116   SCM sm = get_property ("measurePosition");
117   
118   Moment m   =0;
119   if (unsmob_moment (sm))
120     {
121       m = *unsmob_moment (sm);
122       while (m.main_part_ < Rational (0))
123         m.main_part_ += measure_length ();
124     }
125   
126   return m;
127 }
128
129 void
130 Timing_translator::start_translation_timestep ()
131 {
132   check_ = 0;
133   Translator *t = this;
134   Global_translator *global_l =0;
135   do
136     {
137       t = t->daddy_trans_l_ ;
138       global_l = dynamic_cast<Global_translator*> (t);
139     }
140   while (!global_l);
141
142   Moment now = global_l->now_mom_;
143   Moment dt = now  - global_l -> prev_mom_;
144   if (dt < Moment (0))
145     {
146       programming_error ("Moving backwards in time");
147       dt = 0;
148     }
149   
150   if (!dt.to_bool ())
151     return;
152
153   Moment measposp;
154
155   SCM s = get_property ("measurePosition");
156   if (unsmob_moment (s))
157     {
158       measposp = *unsmob_moment (s);
159     }
160   else
161     {
162       measposp = now;
163       daddy_trans_l_->set_property ("measurePosition", measposp.smobbed_copy ());
164     }
165   
166   measposp += dt;
167   
168   SCM barn = get_property ("currentBarNumber");
169   int b = 0;
170   if (gh_number_p (barn))
171     {
172       b = gh_scm2int (barn);
173     }
174
175   SCM cad = get_property ("timing");
176   bool c= to_boolean (cad);
177
178   Rational len = measure_length ();
179   while (c && measposp.main_part_ >= len)
180     {
181       measposp.main_part_ -= len;
182       b ++;
183     }
184
185   daddy_trans_l_->set_property ("currentBarNumber", gh_int2scm (b));
186   daddy_trans_l_->set_property ("measurePosition", measposp.smobbed_copy ());
187 }
188