]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
(acknowledge_grob): stop beam
[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--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7   
8  */
9
10 #include "engraver.hh"
11 #include "beaming.hh"
12 #include "beam.hh"
13 #include "stem.hh"
14 #include "warn.hh"
15 #include "bar-line.hh"
16 #include "rest.hh"
17 #include "item.hh"
18 #include "spanner.hh"
19 #include "context.hh"
20
21 class Auto_beam_engraver : public Engraver
22 {
23   TRANSLATOR_DECLARATIONS (Auto_beam_engraver);
24
25 protected:
26   virtual void stop_translation_timestep ();
27   virtual void start_translation_timestep ();
28   virtual void process_music ();
29   virtual bool try_music (Music*);
30   virtual void finalize ();
31   virtual void acknowledge_grob (Grob_info);
32   virtual void process_acknowledged_grobs ();
33
34 private:
35   bool test_moment (Direction, Moment);
36   void consider_begin (Moment);
37   void consider_end (Moment);
38   Spanner* create_beam ();
39   void begin_beam ();
40   void end_beam ();
41   void junk_beam ();
42   bool is_same_grace_state (Grob* e);
43   void typeset_beam ();
44
45   Music *forbid_;
46   /*
47     shortest_mom is the shortest note in the beam.
48    */
49   Moment shortest_mom_;
50   Spanner *finished_beam_;
51   Link_array<Item>* stems_;
52
53
54   int count_;
55   Moment last_add_mom_;
56   /*
57     Projected ending of the  beam we're working on.
58    */
59   Moment extend_mom_;
60   Moment beam_start_moment_;
61   Moment beam_start_location_;
62
63   bool subdivide_beams_;
64   Moment beat_length_;
65   
66   // We act as if beam were created, and start a grouping anyway.
67   Beaming_info_list*grouping_;
68   SCM beam_settings_ ;          // ugh. should protect ? 
69   
70   Beaming_info_list*finished_grouping_;
71 };
72
73 void
74 Auto_beam_engraver::process_music ()
75 {
76   if (scm_is_string (get_property ("whichBar")))
77     {
78       consider_end (shortest_mom_);
79       junk_beam ();
80     }
81
82   if (forbid_)
83     {
84       consider_end (shortest_mom_);
85       junk_beam ();
86     }
87 }
88
89
90 Auto_beam_engraver::Auto_beam_engraver ()
91 {
92   forbid_ = 0;
93   count_ = 0;
94   stems_ = 0;
95   shortest_mom_ = Moment (Rational (1, 8));
96   finished_beam_ = 0;
97   finished_grouping_ = 0;
98   grouping_ = 0;
99   beam_settings_ = SCM_EOL;  
100 }
101
102
103 bool
104 Auto_beam_engraver::try_music (Music*m)
105 {
106   if (m->is_mus_type  ("beam-forbid-event"))
107     {
108       forbid_ = m;
109       return true;
110     }
111
112   return false;
113 }
114
115 /*
116   Determine end moment for auto beaming (or begin moment, but mostly
117   0== anywhere) In order of increasing priority:
118   
119   i.   begin anywhere, end at every beat
120   ii.  end   *    <num> <den>
121   iii. end <type> <num> <den>
122   
123   iv.  end   *      *     *
124   v.   end <type>   *     *
125   
126   
127   Rationale:
128   
129   [to be defined in config file]
130   i.   easy catch-all rule
131   ii.  exceptions for time signature
132   iii. exceptions for time signature, for specific duration type
133   
134   [user override]
135   iv.  generic override
136   v.   override for specific duration type
137   
138 */
139 bool
140 Auto_beam_engraver::test_moment (Direction dir, Moment test_mom)
141 {
142   Moment now = now_mom ();
143   if (dir == START
144       && now.grace_part_)
145     {
146       return false;
147     }
148   
149   SCM wild = scm_list_n (ly_symbol2scm ("*"), ly_symbol2scm ("*"), SCM_UNDEFINED);
150   SCM function;
151   if (dir == START)
152     function = scm_list_n (ly_symbol2scm ("begin"), SCM_UNDEFINED);
153   else
154     function = scm_list_n (ly_symbol2scm ("end"), SCM_UNDEFINED);
155   
156   Moment beat_length (1,4);
157   if (Moment * m = unsmob_moment (get_property ("beatLength")))
158     {
159       beat_length = *m;
160     }
161   Moment measure_length (1,1);
162   int num = 4;
163   if (Moment* m = unsmob_moment (get_property ("measureLength")))
164     {
165       num = int ((*m / beat_length).main_part_);
166     }
167   
168   int den = beat_length.den ();
169   SCM time = scm_list_n (scm_int2num (num), scm_int2num (den), SCM_UNDEFINED);
170
171   SCM type = scm_list_n (scm_int2num (test_mom.num ()),
172                       scm_int2num (test_mom.den ()), SCM_UNDEFINED);
173
174   /*
175     UGH UGH.
176     settings aren't grob-properties.
177    */
178   SCM settings = get_property ("autoBeamSettings");
179   
180   /* first guess */
181   
182   /* begin beam at any position
183  (and fallback for end) */
184   Moment moment (0);
185   
186   /* end beam at end of beat */
187   if (dir == STOP)
188     {
189       moment = robust_scm2moment (get_property ("beatLength"), moment);
190     }
191
192   /* second guess: property generic time exception */
193   SCM m = scm_assoc (ly_append3 (function, wild, time), settings);
194   
195   if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
196     moment = * unsmob_moment (scm_cdr (m));
197
198   /* third guess: property time exception, specific for duration type */
199   m = scm_assoc (ly_append3 (function, type, time), settings);
200   if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
201     moment = * unsmob_moment (scm_cdr (m));
202
203   /* fourth guess [user override]: property plain generic */
204   m = scm_assoc (ly_append3 (function, wild, wild), settings);
205   if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
206     moment = * unsmob_moment (scm_cdr (m));
207
208   /* fifth guess [user override]: property plain, specific for duration type */
209   m = scm_assoc (ly_append3 (function, type, wild), settings);
210   if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
211     moment = * unsmob_moment (scm_cdr (m));
212   
213   Rational r;
214   if (moment.to_bool ())
215     {
216       /* Ugh? measurePosition can be negative, when \partial
217          We may have to fix this elsewhere (timing translator)
218         r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
219       */
220       Moment pos (0);
221       if (Moment  *m = unsmob_moment (get_property ("measurePosition")))
222         {
223           pos = *m;
224         }
225           
226       if (pos < Moment (0))
227         {
228           Moment length(1);
229
230           if ( Moment *m =  unsmob_moment (get_property ("measureLength")))
231             {
232               length = *m;
233             }
234           pos = length - pos;
235         }
236       r = pos.main_part_.mod_rat (moment.main_part_);
237     }
238   else
239     {
240       if (dir == START)
241         /* if undefined, starting is ok */
242         r = 0;
243       else
244         /* but ending is not */
245         r = 1;
246     }
247
248   return !r;
249 }
250
251 void
252 Auto_beam_engraver::consider_begin (Moment test_mom)
253 {
254   bool on = to_boolean (get_property ("autoBeaming"));
255   if (!stems_ && on
256       && !forbid_)
257     {
258       bool b = test_moment (START, test_mom);
259       if (b)
260         begin_beam ();
261     }
262 }
263
264 void
265 Auto_beam_engraver::consider_end (Moment test_mom)
266 {
267   if (stems_)
268     {
269       /* Allow already started autobeam to end:
270          don't check for autoBeaming */
271       bool b = test_moment (STOP, test_mom);
272       if (b)
273         end_beam ();
274     }
275 }
276
277 Spanner *
278 Auto_beam_engraver::create_beam ()
279 {
280   if (to_boolean (get_property ("skipTypesetting")))
281     return 0;
282   
283   Spanner *beam = new Spanner (beam_settings_, context ()->get_grob_key ("Beam"));
284   for (int i = 0; i < stems_->size (); i++)
285     {
286       /*
287         watch out for stem tremolos and abbreviation beams
288        */
289       if (Stem::get_beam ((*stems_)[i]))
290         {
291           scm_gc_unprotect_object (beam->self_scm ());
292           return 0;
293         }
294       Beam::add_stem (beam, (*stems_)[i]);
295     }
296   
297   announce_grob (beam, (*stems_)[0]->self_scm ());
298
299   return beam;
300 }
301
302 void
303 Auto_beam_engraver::begin_beam ()
304 {
305   if (stems_ || grouping_ )
306     {
307       programming_error ("already have autobeam");
308       return; 
309     }
310   
311   stems_ = new Link_array<Item>;
312   grouping_ = new Beaming_info_list;
313   beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
314   
315   beam_start_moment_ = now_mom ();
316   beam_start_location_ =
317     robust_scm2moment (get_property ("measurePosition"), Moment(0));
318   subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
319   beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment(1,4));
320 }
321
322 void
323 Auto_beam_engraver::junk_beam () 
324 {
325   if (!stems_)
326     return ;
327   
328   delete stems_;
329   stems_ = 0;
330   delete grouping_;
331   grouping_ = 0;
332   beam_settings_ = SCM_EOL;
333   
334   shortest_mom_ = Moment (Rational (1, 8));
335 }
336
337 void
338 Auto_beam_engraver::end_beam ()
339 {
340   if (stems_->size () < 2)
341     {
342       junk_beam ();
343     }
344   else
345     {
346       finished_beam_ = create_beam ();
347       if (finished_beam_)
348         finished_grouping_ = grouping_;
349       delete stems_;
350       stems_ = 0;
351       grouping_ = 0;
352       beam_settings_ = SCM_EOL;
353     }
354
355   shortest_mom_ = Moment (Rational (1, 8));
356 }
357
358 void
359 Auto_beam_engraver::typeset_beam ()
360 {
361   if (finished_beam_)
362     {
363       finished_grouping_->beamify (beat_length_, subdivide_beams_);
364       Beam::set_beaming (finished_beam_, finished_grouping_);
365       finished_beam_ = 0;
366     
367       delete finished_grouping_;
368       finished_grouping_ = 0;
369     }
370 }
371
372 void
373 Auto_beam_engraver::start_translation_timestep ()
374 {
375   count_ = 0;
376   /*
377     don't beam over skips
378    */
379   if (stems_)
380     {
381       Moment now = now_mom ();
382       if (extend_mom_ < now)
383         {
384           end_beam ();
385         }
386     }
387   forbid_ = 0;
388 }
389
390 void
391 Auto_beam_engraver::stop_translation_timestep ()
392 {
393   typeset_beam ();
394 }
395
396 void
397 Auto_beam_engraver::finalize ()
398 {
399   /* finished beams may be typeset */
400   typeset_beam ();
401   /* but unfinished may need another announce/acknowledge pass */
402   if (stems_)
403     junk_beam ();
404 }
405
406
407 void
408 Auto_beam_engraver::acknowledge_grob (Grob_info info)
409 {
410   /*
411     Duplicated from process_music(), since
412     Repeat_acknowledge_engraver::process_music() may also set whichBar
413    */
414   if (scm_is_string (get_property ("whichBar"))
415       && beam_start_moment_ < now_mom())
416     {
417       consider_end (shortest_mom_);
418       junk_beam ();
419     }
420   
421   if (stems_)
422     {
423       if (Beam::has_interface (info.grob_))
424         {
425           end_beam ();
426         }
427       else if (Bar_line::has_interface (info.grob_))
428         {
429           end_beam ();
430         }
431       else if (Rest::has_interface (info.grob_))
432         {
433           end_beam ();
434         }
435     }
436   
437   if (Stem::has_interface (info.grob_))
438     {
439       Item* stem = dynamic_cast<Item *> (info.grob_);
440       Music* m = info.music_cause ();
441       if (!m->is_mus_type ("rhythmic-event"))
442         {
443           programming_error ("Stem must have rhythmic structure");
444           return;
445         }
446       
447       /*
448         Don't (start) auto-beam over empty stems; skips or rests
449         */
450       if (!Stem::head_count (stem))
451         {
452           if (stems_)
453             end_beam ();
454           return;
455         }
456
457       if (Stem::get_beam (stem))
458         {
459           if (stems_)
460             junk_beam ();
461           return ;
462         }
463               
464       int durlog  = unsmob_duration (m->get_property ("duration"))->duration_log ();
465       
466       if (durlog <= 2)
467         {
468           if (stems_)
469             end_beam ();
470           return;
471         }
472
473
474       /*
475         ignore grace notes.
476        */
477       if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
478         return ;
479         
480       
481       Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
482       /* FIXME:
483
484         This comment has been here since long:
485
486            if shortest duration would change
487             consider ending and beginning beam first. 
488
489         but the code didn't match: */
490 #if 1
491       consider_end (dur);
492       consider_begin (dur);
493
494       if (dur < shortest_mom_)
495         shortest_mom_ = dur;
496 #else
497       /* I very much suspect that we wanted: */
498
499       consider_end (shortest_mom_);
500       if (dur < shortest_mom_)
501         {
502           shortest_mom_ = dur;
503           consider_end (shortest_mom_);
504         }
505       consider_begin (shortest_mom_);
506 #endif
507
508       if (!stems_)
509         return;
510       
511       Moment now = now_mom ();
512       
513       grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
514                              durlog - 2);
515       stems_->push (stem);
516       last_add_mom_ = now;
517       extend_mom_ = (extend_mom_ >? now) + m->get_length ();
518     }
519 }
520
521 void
522 Auto_beam_engraver::process_acknowledged_grobs ()
523 {
524   if (!count_)
525     {
526       consider_end (shortest_mom_);
527       consider_begin (shortest_mom_);
528     }
529   else if (count_ > 1)
530     {
531       if (stems_)
532         {
533           Moment now = now_mom ();
534           if ((extend_mom_ < now)
535               || ((extend_mom_ == now) && (last_add_mom_ != now)))
536             {
537               end_beam ();
538             }
539           else if (!stems_->size ())
540             {
541               junk_beam ();
542             }
543         }    
544     }
545   
546   count_ ++;
547 }
548
549 ADD_TRANSLATOR (Auto_beam_engraver,
550 /* descr */       "Generate beams based on measure characteristics and observed "
551 "Stems.  Uses beatLength, measureLength and measurePosition to decide "
552 "when to start and stop a beam.  Overriding beaming is done through "
553 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
554 "@code{stemRightBeamCount}. "
555 ,
556 /* creats*/       "Beam",
557 /* accepts */     "beam-forbid-event",
558 /* acks  */      "stem-interface rest-interface beam-interface bar-line-interface",
559 /* reads */       "autoBeaming autoBeamSettings beatLength subdivideBeams",
560 /* write */       "");