]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
release: 1.5.13
[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 void
84 Timing_translator::initialize ()
85 {
86   daddy_trans_l_->set_property ("timing" , SCM_BOOL_T);  
87   daddy_trans_l_->set_property ("currentBarNumber" , gh_int2scm (1));
88
89   daddy_trans_l_->set_property ("timeSignatureFraction",
90                                 gh_cons (gh_int2scm (4), gh_int2scm (4)));
91   daddy_trans_l_->set_property ("measurePosition", Moment (Rational (0)).smobbed_copy ());
92   daddy_trans_l_->set_property ("measureLength", Moment (Rational (1)).smobbed_copy ());
93   daddy_trans_l_->set_property ("beatLength", Moment (Rational (1,4)).smobbed_copy ());
94 }
95
96 Rational
97 Timing_translator::measure_length () const
98 {
99   SCM l = get_property ("measureLength");
100   if (unsmob_moment (l))
101     return unsmob_moment (l)->main_part_;
102   else
103     return Rational (1);
104 }
105
106 Timing_translator::Timing_translator ()
107 {
108
109 }
110
111 Moment
112 Timing_translator::measure_position () const
113 {
114   SCM sm = get_property ("measurePosition");
115   
116   Moment m   =0;
117   if (unsmob_moment (sm))
118     {
119       m = *unsmob_moment (sm);
120       while (m.main_part_ < Rational (0))
121         m.main_part_ += measure_length ();
122     }
123   
124   return m;
125 }
126
127 void
128 Timing_translator::start_translation_timestep ()
129 {
130   check_ = 0;
131   Translator *t = this;
132   Global_translator *global_l =0;
133   do
134     {
135       t = t->daddy_trans_l_ ;
136       global_l = dynamic_cast<Global_translator*> (t);
137     }
138   while (!global_l);
139
140   Moment now = global_l->now_mom_;
141   Moment dt = now  - global_l -> prev_mom_;
142   if (dt < Moment (0))
143     {
144       programming_error ("Moving backwards in time");
145       dt = 0;
146     }
147   
148   if (!dt.to_bool ())
149     return;
150
151   Moment measposp;
152
153   SCM s = get_property ("measurePosition");
154   if (unsmob_moment (s))
155     {
156       measposp = *unsmob_moment (s);
157     }
158   else
159     {
160       measposp = now;
161       daddy_trans_l_->set_property ("measurePosition", measposp.smobbed_copy ());
162     }
163   
164   measposp += dt;
165   
166   SCM barn = get_property ("currentBarNumber");
167   int b = 0;
168   if (gh_number_p (barn))
169     {
170       b = gh_scm2int (barn);
171     }
172
173   SCM cad = get_property ("timing");
174   bool c= to_boolean (cad);
175
176   Rational len = measure_length ();
177   while (c && measposp.main_part_ >= len)
178     {
179       measposp.main_part_ -= len;
180       b ++;
181     }
182
183   daddy_trans_l_->set_property ("currentBarNumber", gh_int2scm (b));
184   daddy_trans_l_->set_property ("measurePosition", measposp.smobbed_copy ());
185 }
186