]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
release: 1.3.84
[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--2000 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
22 bool
23 Timing_translator::do_try_music (Music*r)
24 {
25   if (Timing_req *t =  dynamic_cast <Timing_req *> (r))
26     {
27       for (int i=0; i < timing_req_l_arr_.size (); i++)
28         {
29           /*
30             merge timing reqs.
31            */
32           if (timing_req_l_arr_[i]->equal_b(t))
33             return true;
34           if (String (classname (timing_req_l_arr_[i])) == classname (r))
35             {
36               r->origin ()->warning (_ ("conflicting timing request"));
37               timing_req_l_arr_[i]->origin ()->warning (_("This is the other timing request")); 
38               return false;
39             }
40         }
41
42       /*
43         We have to do this soon enough. Maybe we'd better disguise
44         \time as a \property. Then all settings will be `immediate'.
45        */
46       if (Time_signature_change_req *c
47           = dynamic_cast <Time_signature_change_req *> (t))
48         set_time_signature (c->beats_i_, c->one_beat_i_);
49     
50       timing_req_l_arr_.push(t);
51       return true;
52     }
53   return false;
54 }
55
56 void
57 Timing_translator::do_process_music()
58 {
59   for (int i=0; i < timing_req_l_arr_.size (); i++)
60     {
61       if (!dynamic_cast <Barcheck_req *> (timing_req_l_arr_[i]))
62         continue;
63       if (measure_position ())
64         {
65           timing_req_l_arr_[i]->origin ()->warning (_f ("barcheck failed at: %s", 
66                                                         measure_position ().str ()));
67           Moment zero; 
68
69           // resync
70           daddy_trans_l_->set_property("measurePosition", zero.make_scm ());
71         }
72     }
73 }
74
75 void
76 Timing_translator::do_pre_move_processing()
77 {
78   timing_req_l_arr_.set_size (0);
79   Translator *t = this;
80   Global_translator *global_l =0;
81   do
82     {
83       t = t->daddy_trans_l_ ;
84       global_l = dynamic_cast<Global_translator*> (t);
85     }
86   while (!global_l);
87
88   /* allbars == ! skipbars */
89   SCM sb = get_property ("skipBars");
90   bool allbars = !to_boolean (sb);
91
92   // urg: multi bar rests: should always process whole of first bar?
93   SCM tim = get_property ("timing");
94   bool timb = to_boolean (tim);
95   if (timb && allbars)
96     {
97       Moment barleft = (measure_length () - measure_position ());
98
99       if (barleft > Moment (0))
100         global_l->add_moment_to_process (now_mom () + barleft);
101     }
102 }
103
104
105 ADD_THIS_TRANSLATOR(Timing_translator);
106
107 void
108 Timing_translator::do_creation_processing()
109 {
110   Moment m;
111   daddy_trans_l_->set_property ("timing" , SCM_BOOL_T);  
112   daddy_trans_l_->set_property ("currentBarNumber" , gh_int2scm (1));
113   daddy_trans_l_->set_property ("measurePosition", m.make_scm ());
114   daddy_trans_l_->set_property ("beatLength", Moment (1,4).make_scm ());
115   daddy_trans_l_->set_property ("measureLength",  Moment (1).make_scm());
116   daddy_trans_l_->set_property ("timeSignatureFraction",
117                                 gh_cons (gh_int2scm (4), gh_int2scm (4)));
118 }
119
120 Moment
121 Timing_translator::measure_length () const
122 {
123   SCM l = get_property("measureLength");
124   if (unsmob_moment(l))
125     return *unsmob_moment (l);
126   else
127     return Moment (1);
128 }
129
130
131 void
132 Timing_translator::set_time_signature (int l, int o)
133 {
134   Moment one_beat = Moment (1)/Moment (o);
135   Moment len = Moment (l) * one_beat;
136   daddy_trans_l_->set_property ("measureLength", len.make_scm ());
137   daddy_trans_l_->set_property ("beatLength", one_beat.make_scm ());
138   daddy_trans_l_->set_property ("timeSignatureFraction",
139                                 gh_cons (gh_int2scm (l), gh_int2scm (o)));
140 }
141
142 Timing_translator::Timing_translator()
143 {
144 }
145
146
147 Moment
148 Timing_translator::measure_position () const
149 {
150   SCM sm = get_property ("measurePosition");
151   
152   Moment m   =0;
153   if (unsmob_moment (sm))
154     {
155       m = *unsmob_moment(sm);
156       while (m < Moment (0))
157         m += measure_length ();
158     }
159   
160   return m;
161 }
162
163 void
164 Timing_translator::do_post_move_processing()
165 {
166   Translator *t = this;
167   Global_translator *global_l =0;
168   do
169     {
170       t = t->daddy_trans_l_ ;
171       global_l = dynamic_cast<Global_translator*> (t);
172     }
173   while (!global_l);
174
175   Moment dt = global_l->now_mom_  - global_l -> prev_mom_;
176   if (dt < Moment (0))
177     {
178       programming_error ("Moving backwards in time");
179       dt = 0;
180     }
181   
182   if (!dt)
183     return;
184
185   Moment measposp;
186
187   SCM s = get_property ("measurePosition");
188   if (unsmob_moment (s))
189     {
190       measposp = *unsmob_moment(s);
191     }
192   else
193     {
194       daddy_trans_l_->set_property ("measurePosition", measposp.make_scm());
195     }
196   
197   measposp += dt;
198   
199   SCM barn = get_property ("currentBarNumber");
200   int b = 0;
201   if (gh_number_p(barn))
202     {
203       b = gh_scm2int (barn);
204     }
205
206   SCM cad = get_property ("timing");
207   bool c= to_boolean (cad );
208
209   Moment len = measure_length ();
210   while (c && measposp >= len)
211     {
212       measposp -= len;
213       b ++;
214     }
215
216   daddy_trans_l_->set_property ("currentBarNumber", gh_int2scm (b));
217   daddy_trans_l_->set_property ("measurePosition", measposp.make_scm());
218 }
219