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