]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
release: 1.3.41
[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->warning (_ ("conflicting timing request"));
37               timing_req_l_arr_[i]->warning (_("This is the other timing request")); 
38               return false;
39             }
40         }
41     
42       timing_req_l_arr_.push(t);
43       return true;
44     }
45   return false;
46 }
47
48 /*ugh.
49  */
50 Time_signature_change_req*
51 Timing_translator::time_signature_req_l() const
52 {
53   Time_signature_change_req *m_l=0;
54   for (int i=0; !m_l && i < timing_req_l_arr_.size (); i++)
55     {
56       m_l=dynamic_cast<Time_signature_change_req*> (timing_req_l_arr_[i]);
57     }
58   return m_l;
59 }
60
61 void
62 Timing_translator::do_process_music()
63 {
64   for (int i=0; i < timing_req_l_arr_.size (); i++)
65     {
66       Timing_req * tr_l = timing_req_l_arr_[i];
67
68       if (Time_signature_change_req *m_l = dynamic_cast <Time_signature_change_req *> (tr_l))
69         {
70           int b_i= m_l->beats_i_;
71           int o_i = m_l->one_beat_i_;
72           set_time_signature (b_i, o_i);
73         }
74       else if (dynamic_cast <Barcheck_req *> (tr_l))
75         {
76           if (measure_position ())
77             {
78               tr_l ->warning (_f ("barcheck failed at: %s", 
79                                   measure_position ().str ()));
80               // resync
81               daddy_trans_l_->set_property("measurePosition",
82                                            (new Moment)->smobify_self ());
83
84             }
85         }
86     }
87 }
88
89
90 void
91 Timing_translator::do_pre_move_processing()
92 {
93   timing_req_l_arr_.set_size (0);
94   Translator *t = this;
95   Global_translator *global_l =0;
96   do
97     {
98       t = t->daddy_trans_l_ ;
99       global_l = dynamic_cast<Global_translator*> (t);
100     }
101   while (!global_l);
102
103   /* allbars == ! skipbars */
104   SCM sb = get_property ("skipBars");
105   bool allbars = !to_boolean (sb);
106
107   // urg: multi bar rests: should always process whole of first bar?
108   SCM tim = get_property ("timing");
109   bool timb = to_boolean (tim);
110   if (timb && allbars)
111     {
112       Moment barleft = (measure_length () - measure_position ());
113
114       if (barleft > Moment (0))
115         global_l->add_moment_to_process (now_mom () + barleft);
116     }
117 }
118
119
120 ADD_THIS_TRANSLATOR(Timing_translator);
121
122 void
123 Timing_translator::do_creation_processing()
124 {
125   daddy_trans_l_->set_property ("timing" , SCM_BOOL_T);  
126   daddy_trans_l_->set_property ("currentBarNumber" , gh_int2scm (1));
127   daddy_trans_l_->set_property("measurePosition",
128                                (new Moment)->smobify_self());
129   daddy_trans_l_->set_property ("oneBeat",
130                                 (new Moment (1,4))->smobify_self ());
131   daddy_trans_l_->set_property("measureLength",
132                                (new Moment (1))->smobify_self());
133 }
134
135 Moment
136 Timing_translator::measure_length () const
137 {
138   SCM l = get_property("measureLength");
139   if (SMOB_IS_TYPE_B(Moment, l))
140     return *SMOB_TO_TYPE (Moment, l);
141   else
142     return Moment (1);
143 }
144
145
146 void
147 Timing_translator::get_time_signature (int *n, int *d) const
148 {
149   Moment one_beat (1,4);
150   SCM one = get_property ("beatLength");
151   if (SMOB_IS_TYPE_B (Moment, one))
152     one_beat = *SMOB_TO_TYPE (Moment, one);
153   *n = measure_length () / one_beat;
154   *d = one_beat.den_i ();
155 }
156
157
158 void
159 Timing_translator::set_time_signature (int l, int o)
160 {
161   Moment one_beat = Moment (1)/Moment (o);
162   Moment len = Moment (l) * one_beat;
163   daddy_trans_l_->set_property ("measureLength",
164                                 (new Moment (len))->smobify_self ());
165   daddy_trans_l_->set_property ("beatLength",
166                                 (new Moment (one_beat))->smobify_self ());
167 }
168
169 Timing_translator::Timing_translator()
170 {
171 }
172
173
174 Moment
175 Timing_translator::measure_position () const
176 {
177   SCM sm = get_property ("measurePosition");
178   
179   Moment m   =0;
180   if (SMOB_IS_TYPE_B (Moment, sm))
181     {
182       m = *SMOB_TO_TYPE (Moment, sm);
183       while (m < Moment (0))
184         m += measure_length ();
185     }
186   
187   return m;
188 }
189
190 void
191 Timing_translator::do_post_move_processing()
192 {
193   Translator *t = this;
194   Global_translator *global_l =0;
195   do
196     {
197       t = t->daddy_trans_l_ ;
198       global_l = dynamic_cast<Global_translator*> (t);
199     }
200   while (!global_l);
201
202   Moment dt = global_l->now_mom_  - global_l -> prev_mom_;
203   if (dt < Moment (0))
204     {
205       programming_error ("Moving backwards in time");
206       dt = 0;
207     }
208   
209   if (!dt)
210     return;
211
212   Moment * measposp =0;
213
214   SCM s = get_property ("measurePosition");
215   if (SMOB_IS_TYPE_B (Moment, s))
216     {
217       measposp = SMOB_TO_TYPE (Moment,s);
218     }
219   else
220     {
221       measposp = new Moment;
222       daddy_trans_l_->set_property ("measurePosition", measposp->smobify_self ());
223     }
224   
225   *measposp += dt;
226   // don't need to set_property
227   
228   SCM barn = get_property ("currentBarNumber");
229   int b = 0;
230   if (gh_number_p(barn))
231     {
232       b = gh_scm2int (barn);
233     }
234
235   SCM cad = get_property ("timing");
236   bool c= to_boolean (cad );
237
238   Moment len = measure_length ();
239   while (c && *measposp >= len)
240       {
241         *measposp -= len;
242         b ++;
243       }
244
245   daddy_trans_l_->set_property ("currentBarNumber", gh_int2scm (b));
246 }
247