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