]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
6e1e805b3d3da27d8d53d27bc274dcaac95b1d91
[lilypond.git] / lily / auto-beam-engraver.cc
1 /*   
2   auto-beam-engraver.cc --  implement Auto_beam_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9 #include "beaming.hh"
10 #include "auto-beam-engraver.hh"
11 #include "musical-request.hh"
12 #include "bar.hh"
13 #include "beam.hh"
14 #include "rest.hh"
15 #include "stem.hh"
16 #include "debug.hh"
17 #include "timing-engraver.hh"
18 #include "engraver-group-engraver.hh"
19
20 ADD_THIS_TRANSLATOR (Auto_beam_engraver);
21
22 Auto_beam_engraver::Auto_beam_engraver ()
23 {
24   stem_l_arr_p_ = 0;
25   shortest_mom_ = Moment (1, 8);
26   finished_beam_p_ = 0;
27   finished_grouping_p_ = 0;
28   grouping_p_ = 0;
29   timer_l_ =0;
30 }
31
32 void
33 Auto_beam_engraver::do_creation_processing ()
34 {
35   Translator * t = daddy_grav_l  ()->get_simple_translator ("Timing_engraver");
36   timer_l_ = dynamic_cast<Timing_engraver*> (t);
37 }
38
39 bool
40 Auto_beam_engraver::do_try_music (Music*) 
41 {
42   return false;
43
44
45 void
46 Auto_beam_engraver::do_process_requests ()
47 {
48   consider_end_and_begin (shortest_mom_);
49 }
50
51 void
52 Auto_beam_engraver::consider_end_and_begin (Moment test_mom)
53 {
54   if (!timer_l_)
55       return;
56   
57   int num;
58   int den;
59   timer_l_->get_time_signature (&num, &den);
60   
61   String time_str = String ("time") + to_str (num) + "_" + to_str (den);
62
63   String type_str;
64   if (test_mom.num () != 1)
65     type_str = to_str (test_mom.num ());
66   if (test_mom.den () != 1)
67     type_str = type_str + "_" + to_str (test_mom.den ());
68
69   /*
70     URG
71     
72     FIXME: SHOULD USE ALIST
73     
74    */
75
76   /*
77     Determine end moment for auto beaming (and begin, mostly 0==anywhere) 
78     In order of increasing priority:
79
80     i.   every beat <den>
81     ii.  time<num>_<den>beamAutoEnd
82     iii. time<num>_<den>beamAutoEnd<type>
83     iv.  beamAutoEnd
84     v.   beamAutoEnd<type>
85
86
87     Rationale:
88
89     [to be defined in config file]
90     i.   easy catch-all rule
91     ii.  exceptions for time signature
92     iii. exceptions for time signature, for specific duration type
93
94     [user override]
95     iv.  generic override
96     v.   override for specific duration type
97
98     The user overrides should be required for common cases.
99    */
100   
101   /*
102     first guess: begin beam at any position
103   */
104   Moment begin_mom (0);
105   /*
106     first guess: end beam at end of beat
107   */
108   SCM one (get_property ("beatLength", 0));
109
110   Moment end_mom;
111   if (SMOB_IS_TYPE_B(Moment, one))
112     end_mom = *SMOB_TO_TYPE (Moment, one);
113
114   /*
115     second guess: property generic time exception
116   */
117   SCM begin = get_property (time_str + "beamAutoBegin", 0);
118   if (SMOB_IS_TYPE_B(Moment, begin))
119     begin_mom = * SMOB_TO_TYPE(Moment, begin);
120
121   SCM end = get_property (time_str + "beamAutoEnd", 0);
122   if (SMOB_IS_TYPE_B (Moment, end))
123     end_mom = * SMOB_TO_TYPE(Moment,end);
124
125   /*
126     third guess: property time exception, specific for duration type
127   */
128   if (type_str.length_i ())
129     {
130       SCM end_mult = get_property (time_str + "beamAutoEnd" + type_str, 0);
131       if (SMOB_IS_TYPE_B (Moment, end_mult))
132         end_mom = * SMOB_TO_TYPE (Moment,end_mult);
133
134       SCM begin_mult = get_property (time_str + "beamAutoBegin" + type_str, 0);
135       if (SMOB_IS_TYPE_B (Moment, begin_mult))
136         begin_mom = * SMOB_TO_TYPE (Moment,begin_mult);
137     }
138
139   /*
140     fourth guess [user override]: property plain generic
141   */
142   begin = get_property ("beamAutoBegin", 0);
143   if (SMOB_IS_TYPE_B(Moment, begin))
144     begin_mom = * SMOB_TO_TYPE(Moment, begin);
145
146
147   
148   end = get_property ("beamAutoEnd", 0);
149   if (SMOB_IS_TYPE_B (Moment, end))
150     end_mom = * SMOB_TO_TYPE (Moment,end);
151
152   /*
153     fifth guess [user override]: property plain, specific for duration type
154   */
155   if (type_str.length_i ())
156     {
157       SCM end_mult = get_property (String ("beamAutoEnd") + type_str, 0);
158       if (SMOB_IS_TYPE_B (Moment, end_mult))
159         end_mom = * SMOB_TO_TYPE (Moment,end_mult);
160
161       SCM begin_mult = get_property (String ("beamAutoBegin") + type_str, 0);
162       if (SMOB_IS_TYPE_B (Moment, begin_mult))
163         begin_mom = * SMOB_TO_TYPE (Moment,begin_mult);
164     }
165
166   Rational r;
167   if (end_mom)
168     r = timer_l_->measure_position ().mod_rat (end_mom);
169   else
170     r = Moment (1);
171
172   if (stem_l_arr_p_ && !r)
173     end_beam ();
174      
175   /*
176     Allow already started autobeam to end
177    */
178   SCM on = get_property ("noAutoBeaming", 0);
179   if (gh_boolean_p (on) && gh_scm2bool (on))
180     return;
181
182   if (begin_mom)
183     r = timer_l_->measure_position ().mod_rat (begin_mom);
184   if (!stem_l_arr_p_ && (!begin_mom || !r))
185     begin_beam ();
186 }
187
188       
189 void
190 Auto_beam_engraver::begin_beam ()
191 {
192   assert (!stem_l_arr_p_);
193   stem_l_arr_p_ = new Array<Stem*>;
194   assert (!grouping_p_);
195   grouping_p_ = new Beaming_info_list;
196   beam_start_moment_ = now_mom ();
197   beam_start_location_ = timer_l_->measure_position ();
198 }
199
200 Beam*
201 Auto_beam_engraver::create_beam_p ()
202 {
203   Beam* beam_p = new Beam;
204
205   for (int i = 0; i < stem_l_arr_p_->size (); i++)
206     {
207       /*
208         watch out for stem tremolos and abbreviation beams
209        */
210       if ((*stem_l_arr_p_)[i]->beam_l ())
211         {
212           return 0;
213         }
214       beam_p->add_stem ((*stem_l_arr_p_)[i]);
215     }
216   
217   announce_element (Score_element_info (beam_p, 0));
218
219   return beam_p;
220 }
221
222 void
223 Auto_beam_engraver::end_beam ()
224 {
225   if (stem_l_arr_p_->size () < 2)
226     {
227       junk_beam ();
228     }
229   else
230     {
231       finished_beam_p_ = create_beam_p ();
232       if (finished_beam_p_)
233         finished_grouping_p_ = grouping_p_;
234       delete stem_l_arr_p_;
235       stem_l_arr_p_ = 0;
236       grouping_p_ = 0;
237       shortest_mom_ = Moment (1, 8);
238     }
239 }
240  
241 void
242 Auto_beam_engraver::typeset_beam ()
243 {
244   if (finished_beam_p_)
245     {
246       finished_grouping_p_->beamify ();
247       finished_beam_p_->set_beaming (finished_grouping_p_);
248       typeset_element (finished_beam_p_);
249       finished_beam_p_ = 0;
250     
251       delete finished_grouping_p_;
252       finished_grouping_p_= 0;
253     }
254 }
255
256 void
257 Auto_beam_engraver::do_post_move_processing ()
258 {
259   /*
260     don't beam over skips
261    */
262   if (stem_l_arr_p_)
263     {
264       Moment now = now_mom ();
265       if (extend_mom_ < now)
266         {
267           end_beam ();
268         }
269     }
270 }
271
272 void
273 Auto_beam_engraver::do_pre_move_processing ()
274 {
275   typeset_beam ();
276 }
277
278 void
279 Auto_beam_engraver::do_removal_processing ()
280 {
281   /* finished beams may be typeset */
282   typeset_beam ();
283   /* but unfinished may need another announce/acknowledge pass */
284   if (stem_l_arr_p_)
285     junk_beam ();
286 }
287
288 bool
289 Auto_beam_engraver::same_grace_state_b (Score_element* e)
290 {
291   bool gr = e->get_elt_property ("grace") == SCM_BOOL_T;
292   SCM wg =get_property ("weAreGraceContext",0);
293   return (gh_boolean_p (wg) && gh_scm2bool (wg)) == gr;
294 }
295
296 void
297 Auto_beam_engraver::acknowledge_element (Score_element_info info)
298 {
299   if (!same_grace_state_b (info.elem_l_) || !timer_l_)
300     return;
301   
302   if (stem_l_arr_p_)
303     {
304       if (Beam *b = dynamic_cast<Beam *> (info.elem_l_))
305         {
306           end_beam ();
307         }
308       else if (Bar *b = dynamic_cast<Bar *> (info.elem_l_))
309         {
310           end_beam ();
311         }
312       else if (Rest* rest_l = dynamic_cast<Rest *> (info.elem_l_))
313         {
314           end_beam ();
315         }
316     }
317   
318   if (Stem* stem_l = dynamic_cast<Stem *> (info.elem_l_))
319     {
320       Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (info.req_l_);
321       if (!rhythmic_req)
322         {
323           programming_error ("Stem must have rhythmic structure");
324           return;
325         }
326       
327       /*
328         Don't (start) auto-beam over empty stems; skips or rests
329         */
330       if (!stem_l->head_l_arr_.size ())
331         {
332           if (stem_l_arr_p_)
333             end_beam ();
334           return;
335         }
336
337       if (stem_l->beam_l ())
338         {
339           if (stem_l_arr_p_)
340             junk_beam ();
341           return ;
342         }
343               
344       int durlog  =rhythmic_req->duration_.durlog_i_;
345       if (durlog <= 2)
346         {
347           if (stem_l_arr_p_)
348             end_beam ();
349           return;
350         }
351
352       /*
353         if shortest duration would change
354         reconsider ending/starting beam first.
355       */
356       Moment mom = rhythmic_req->duration_.length_mom ();
357       consider_end_and_begin (mom);
358       if (!stem_l_arr_p_)
359         return;
360       if (mom < shortest_mom_)
361         {
362           if (stem_l_arr_p_->size ())
363             {
364               shortest_mom_ = mom;
365               consider_end_and_begin (shortest_mom_);
366               if (!stem_l_arr_p_)
367                 return;
368             }
369           shortest_mom_ = mom;
370         }
371       Moment now = now_mom ();
372       
373       grouping_p_->add_stem (now - beam_start_moment_ + beam_start_location_,
374                              durlog - 2);
375       stem_l_arr_p_->push (stem_l);
376       last_add_mom_ = now;
377       extend_mom_ = extend_mom_ >? now + rhythmic_req->length_mom ();
378     }
379 }
380
381 void
382 Auto_beam_engraver::junk_beam () 
383 {
384   assert (stem_l_arr_p_);
385   
386   delete stem_l_arr_p_;
387   stem_l_arr_p_ = 0;
388   delete grouping_p_;
389   grouping_p_ = 0;
390   shortest_mom_ = Moment (1, 8);
391 }
392
393 void
394 Auto_beam_engraver::process_acknowledged ()
395 {
396   if (stem_l_arr_p_)
397     {
398       Moment now = now_mom ();
399       if ((extend_mom_ < now)
400           || ((extend_mom_ == now) && (last_add_mom_ != now )))
401         {
402           end_beam ();
403         }
404       else if (!stem_l_arr_p_->size ())
405         {
406           junk_beam ();
407         }
408     }
409 }
410