]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
* lily/moment.cc (robust_scm2moment): new function.
[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       SCM beat (get_property ("beatLength"));
192       
193       if (unsmob_moment (beat))
194         moment = *unsmob_moment (beat);
195     }
196
197   /* second guess: property generic time exception */
198   SCM m = scm_assoc (ly_append3 (function, wild, time), settings);
199   
200   if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
201     moment = * unsmob_moment (scm_cdr (m));
202
203   /* third guess: property time exception, specific for duration type */
204   m = scm_assoc (ly_append3 (function, type, time), settings);
205   if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
206     moment = * unsmob_moment (scm_cdr (m));
207
208   /* fourth guess [user override]: property plain generic */
209   m = scm_assoc (ly_append3 (function, wild, wild), settings);
210   if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
211     moment = * unsmob_moment (scm_cdr (m));
212
213   /* fifth guess [user override]: property plain, specific for duration type */
214   m = scm_assoc (ly_append3 (function, type, wild), settings);
215   if (m != SCM_BOOL_F && unsmob_moment (scm_cdr (m)))
216     moment = * unsmob_moment (scm_cdr (m));
217   
218   Rational r;
219   if (moment.to_bool ())
220     {
221       /* Ugh? measurePosition can be negative, when \partial
222          We may have to fix this elsewhere (timing translator)
223         r = unsmob_moment (get_property ("measurePosition"))->mod_rat (moment);
224       */
225       Moment pos (0);
226       if (Moment  *m = unsmob_moment (get_property ("measurePosition")))
227         {
228           pos = *m;
229         }
230           
231       if (pos < Moment (0))
232         {
233           Moment length(1);
234
235           if ( Moment *m =  unsmob_moment (get_property ("measureLength")))
236             {
237               length = *m;
238             }
239           pos = length - pos;
240         }
241       r = pos.main_part_.mod_rat (moment.main_part_);
242     }
243   else
244     {
245       if (dir == START)
246         /* if undefined, starting is ok */
247         r = 0;
248       else
249         /* but ending is not */
250         r = 1;
251     }
252
253   return !r;
254 }
255
256 void
257 Auto_beam_engraver::consider_begin (Moment test_mom)
258 {
259   bool on = to_boolean (get_property ("autoBeaming"));
260   if (!stems_ && on
261       && !forbid_)
262     {
263       bool b = test_moment (START, test_mom);
264       if (b)
265         begin_beam ();
266     }
267 }
268
269 void
270 Auto_beam_engraver::consider_end (Moment test_mom)
271 {
272   if (stems_)
273     {
274       /* Allow already started autobeam to end:
275          don't check for autoBeaming */
276       bool b = test_moment (STOP, test_mom);
277       if (b)
278         end_beam ();
279     }
280 }
281
282 Spanner*
283 Auto_beam_engraver::create_beam ()
284 {
285   if (to_boolean (get_property ("skipTypesetting")))
286     {
287      return 0;
288     }
289   
290   Spanner* beam = new Spanner (beam_settings_);
291   for (int i = 0; i < stems_->size (); i++)
292     {
293       /*
294         watch out for stem tremolos and abbreviation beams
295        */
296       if (Stem::get_beam ((*stems_)[i]))
297         {
298           scm_gc_unprotect_object (beam->self_scm ());
299           return 0;
300         }
301       Beam::add_stem (beam, (*stems_)[i]);
302     }
303   
304   announce_grob (beam, (*stems_)[0]->self_scm ());
305
306   return beam;
307 }
308
309 void
310 Auto_beam_engraver::begin_beam ()
311 {
312   if (stems_ || grouping_ )
313     {
314       programming_error ("already have autobeam");
315       return; 
316     }
317   
318   stems_ = new Link_array<Item>;
319   grouping_ = new Beaming_info_list;
320   beam_settings_ = updated_grob_properties (context (), ly_symbol2scm ("Beam"));
321   
322   beam_start_moment_ = now_mom ();
323   beam_start_location_ =
324     robust_scm2moment (unsmob_moment (get_property ("measurePosition"), Moment(0));
325   subdivide_beams_ = ly_scm2bool (get_property ("subdivideBeams"));
326   beat_length_ = robust_scm2moment (get_property ("beatLength"), Moment(1,4));
327 }
328
329 void
330 Auto_beam_engraver::junk_beam () 
331 {
332   if (!stems_)
333     return ;
334   
335   delete stems_;
336   stems_ = 0;
337   delete grouping_;
338   grouping_ = 0;
339   beam_settings_ = SCM_EOL;
340   
341   shortest_mom_ = Moment (Rational (1, 8));
342 }
343
344 void
345 Auto_beam_engraver::end_beam ()
346 {
347   if (stems_->size () < 2)
348     {
349       junk_beam ();
350     }
351   else
352     {
353       finished_beam_ = create_beam ();
354       if (finished_beam_)
355         finished_grouping_ = grouping_;
356       delete stems_;
357       stems_ = 0;
358       grouping_ = 0;
359       beam_settings_ = SCM_EOL;
360     }
361
362   shortest_mom_ = Moment (Rational (1, 8));
363 }
364
365 void
366 Auto_beam_engraver::typeset_beam ()
367 {
368   if (finished_beam_)
369     {
370       finished_grouping_->beamify (beat_length_, subdivide_beams_);
371       Beam::set_beaming (finished_beam_, finished_grouping_);
372       finished_beam_ = 0;
373     
374       delete finished_grouping_;
375       finished_grouping_= 0;
376     }
377 }
378
379 void
380 Auto_beam_engraver::start_translation_timestep ()
381 {
382   count_ = 0;
383   /*
384     don't beam over skips
385    */
386   if (stems_)
387     {
388       Moment now = now_mom ();
389       if (extend_mom_ < now)
390         {
391           end_beam ();
392         }
393     }
394   forbid_ = 0;
395 }
396
397 void
398 Auto_beam_engraver::stop_translation_timestep ()
399 {
400   typeset_beam ();
401 }
402
403 void
404 Auto_beam_engraver::finalize ()
405 {
406   /* finished beams may be typeset */
407   typeset_beam ();
408   /* but unfinished may need another announce/acknowledge pass */
409   if (stems_)
410     junk_beam ();
411 }
412
413
414 void
415 Auto_beam_engraver::acknowledge_grob (Grob_info info)
416 {
417   if (stems_)
418     {
419       if (Beam::has_interface (info.grob_))
420         {
421           end_beam ();
422         }
423       else if (Bar_line::has_interface (info.grob_))
424         {
425           end_beam ();
426         }
427       else if (Rest::has_interface (info.grob_))
428         {
429           end_beam ();
430         }
431     }
432   
433   if (Stem::has_interface (info.grob_))
434     {
435       Item* stem = dynamic_cast<Item *> (info.grob_);
436       Music* m = info.music_cause ();
437       if (!m->is_mus_type ("rhythmic-event"))
438         {
439           programming_error ("Stem must have rhythmic structure");
440           return;
441         }
442       
443       /*
444         Don't (start) auto-beam over empty stems; skips or rests
445         */
446       if (!Stem::head_count (stem))
447         {
448           if (stems_)
449             end_beam ();
450           return;
451         }
452
453       if (Stem::get_beam (stem))
454         {
455           if (stems_)
456             junk_beam ();
457           return ;
458         }
459               
460       int durlog  = unsmob_duration (m->get_property ("duration"))->duration_log ();
461       
462       if (durlog <= 2)
463         {
464           if (stems_)
465             end_beam ();
466           return;
467         }
468
469
470       /*
471         ignore grace notes.
472        */
473       if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
474         return ;
475         
476       
477       Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
478       /* FIXME:
479
480         This comment has been here since long:
481
482            if shortest duration would change
483             consider ending and beginning beam first. 
484
485         but the code didn't match: */
486 #if 1
487       consider_end (dur);
488       consider_begin (dur);
489
490       if (dur < shortest_mom_)
491         shortest_mom_ = dur;
492 #else
493       /* I very much suspect that we wanted: */
494
495       consider_end (shortest_mom_);
496       if (dur < shortest_mom_)
497         {
498           shortest_mom_ = dur;
499           consider_end (shortest_mom_);
500         }
501       consider_begin (shortest_mom_);
502 #endif
503
504       if (!stems_)
505         return;
506       
507       Moment now = now_mom ();
508       
509       grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
510                              durlog - 2);
511       stems_->push (stem);
512       last_add_mom_ = now;
513       extend_mom_ = (extend_mom_ >? now) + m->get_length ();
514     }
515 }
516
517 void
518 Auto_beam_engraver::process_acknowledged_grobs ()
519 {
520   if (!count_)
521     {
522       consider_end (shortest_mom_);
523       consider_begin (shortest_mom_);
524     }
525   else if (count_ > 1)
526     {
527       if (stems_)
528         {
529           Moment now = now_mom ();
530           if ((extend_mom_ < now)
531               || ((extend_mom_ == now) && (last_add_mom_ != now)))
532             {
533               end_beam ();
534             }
535           else if (!stems_->size ())
536             {
537               junk_beam ();
538             }
539         }    
540     }
541   
542   count_ ++;
543 }
544
545 ENTER_DESCRIPTION (Auto_beam_engraver,
546 /* descr */       "Generate beams based on measure characteristics and observed "
547 "Stems.  Uses beatLength, measureLength and measurePosition to decide "
548 "when to start and stop a beam.  Overriding beaming is done through "
549 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
550 "@code{stemRightBeamCount}. "
551 ,
552 /* creats*/       "Beam",
553 /* accepts */     "beam-forbid-event",
554 /* acks  */      "stem-interface rest-interface beam-interface bar-line-interface",
555 /* reads */       "autoBeaming autoBeamSettings beatLength subdivideBeams",
556 /* write */       "");