]> git.donarmstrong.com Git - lilypond.git/blob - lily/timing-translator.cc
release: 1.3.105
[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         {
49           int b = gh_scm2int (c->get_mus_property ("beats"));
50           int o = gh_scm2int (c->get_mus_property ("one-beat"));      
51           
52           set_time_signature (b, o);
53
54         }
55       timing_req_l_arr_.push(t);
56       return true;
57     }
58   return false;
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       if (!dynamic_cast <Barcheck_req *> (timing_req_l_arr_[i]))
67         continue;
68       if (measure_position ())
69         {
70           timing_req_l_arr_[i]->origin ()->warning (_f ("barcheck failed at: %s", 
71                                                         measure_position ().str ()));
72           Moment zero; 
73
74           // resync
75           daddy_trans_l_->set_property("measurePosition", zero.make_scm ());
76         }
77     }
78 }
79
80 void
81 Timing_translator::do_pre_move_processing()
82 {
83   timing_req_l_arr_.set_size (0);
84   Translator *t = this;
85   Global_translator *global_l =0;
86   do
87     {
88       t = t->daddy_trans_l_ ;
89       global_l = dynamic_cast<Global_translator*> (t);
90     }
91   while (!global_l);
92
93   /* allbars == ! skipbars */
94   SCM sb = get_property ("skipBars");
95   bool allbars = !to_boolean (sb);
96
97   // urg: multi bar rests: should always process whole of first bar?
98   SCM tim = get_property ("timing");
99   bool timb = to_boolean (tim);
100   if (timb && allbars)
101     {
102       Moment barleft = (measure_length () - measure_position ());
103
104       if (barleft > Moment (0))
105         global_l->add_moment_to_process (now_mom () + barleft);
106     }
107 }
108
109
110 ADD_THIS_TRANSLATOR(Timing_translator);
111
112 void
113 Timing_translator::do_creation_processing()
114 {
115   Moment m;
116   daddy_trans_l_->set_property ("timing" , SCM_BOOL_T);  
117   daddy_trans_l_->set_property ("currentBarNumber" , gh_int2scm (1));
118   daddy_trans_l_->set_property ("measurePosition", m.make_scm ());
119   daddy_trans_l_->set_property ("beatLength", Moment (1,4).make_scm ());
120   daddy_trans_l_->set_property ("measureLength",  Moment (1).make_scm());
121   daddy_trans_l_->set_property ("timeSignatureFraction",
122                                 gh_cons (gh_int2scm (4), gh_int2scm (4)));
123 }
124
125 Moment
126 Timing_translator::measure_length () const
127 {
128   SCM l = get_property("measureLength");
129   if (unsmob_moment(l))
130     return *unsmob_moment (l);
131   else
132     return Moment (1);
133 }
134
135
136 void
137 Timing_translator::set_time_signature (int l, int o)
138 {
139   Moment one_beat = Moment (1)/Moment (o);
140   Moment len = Moment (l) * one_beat;
141   daddy_trans_l_->set_property ("measureLength", len.make_scm ());
142   daddy_trans_l_->set_property ("beatLength", one_beat.make_scm ());
143   daddy_trans_l_->set_property ("timeSignatureFraction",
144                                 gh_cons (gh_int2scm (l), gh_int2scm (o)));
145 }
146
147 Timing_translator::Timing_translator()
148 {
149 }
150
151
152 Moment
153 Timing_translator::measure_position () const
154 {
155   SCM sm = get_property ("measurePosition");
156   
157   Moment m   =0;
158   if (unsmob_moment (sm))
159     {
160       m = *unsmob_moment(sm);
161       while (m < Moment (0))
162         m += measure_length ();
163     }
164   
165   return m;
166 }
167
168 void
169 Timing_translator::do_post_move_processing()
170 {
171   Translator *t = this;
172   Global_translator *global_l =0;
173   do
174     {
175       t = t->daddy_trans_l_ ;
176       global_l = dynamic_cast<Global_translator*> (t);
177     }
178   while (!global_l);
179
180   Moment dt = global_l->now_mom_  - global_l -> prev_mom_;
181   if (dt < Moment (0))
182     {
183       programming_error ("Moving backwards in time");
184       dt = 0;
185     }
186   
187   if (!dt)
188     return;
189
190   Moment measposp;
191
192   SCM s = get_property ("measurePosition");
193   if (unsmob_moment (s))
194     {
195       measposp = *unsmob_moment(s);
196     }
197   else
198     {
199       daddy_trans_l_->set_property ("measurePosition", measposp.make_scm());
200     }
201   
202   measposp += dt;
203   
204   SCM barn = get_property ("currentBarNumber");
205   int b = 0;
206   if (gh_number_p(barn))
207     {
208       b = gh_scm2int (barn);
209     }
210
211   SCM cad = get_property ("timing");
212   bool c= to_boolean (cad );
213
214   Moment len = measure_length ();
215   while (c && measposp >= len)
216     {
217       measposp -= len;
218       b ++;
219     }
220
221   daddy_trans_l_->set_property ("currentBarNumber", gh_int2scm (b));
222   daddy_trans_l_->set_property ("measurePosition", measposp.make_scm());
223 }
224