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