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