]> 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 "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   if (stems_)
411     {
412       if (Beam::has_interface (info.grob_))
413         {
414           end_beam ();
415         }
416       else if (Bar_line::has_interface (info.grob_))
417         {
418           end_beam ();
419         }
420       else if (Rest::has_interface (info.grob_))
421         {
422           end_beam ();
423         }
424     }
425   
426   if (Stem::has_interface (info.grob_))
427     {
428       Item* stem = dynamic_cast<Item *> (info.grob_);
429       Music* m = info.music_cause ();
430       if (!m->is_mus_type ("rhythmic-event"))
431         {
432           programming_error ("Stem must have rhythmic structure");
433           return;
434         }
435       
436       /*
437         Don't (start) auto-beam over empty stems; skips or rests
438         */
439       if (!Stem::head_count (stem))
440         {
441           if (stems_)
442             end_beam ();
443           return;
444         }
445
446       if (Stem::get_beam (stem))
447         {
448           if (stems_)
449             junk_beam ();
450           return ;
451         }
452               
453       int durlog  = unsmob_duration (m->get_property ("duration"))->duration_log ();
454       
455       if (durlog <= 2)
456         {
457           if (stems_)
458             end_beam ();
459           return;
460         }
461
462
463       /*
464         ignore grace notes.
465        */
466       if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
467         return ;
468         
469       
470       Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
471       /* FIXME:
472
473         This comment has been here since long:
474
475            if shortest duration would change
476             consider ending and beginning beam first. 
477
478         but the code didn't match: */
479 #if 1
480       consider_end (dur);
481       consider_begin (dur);
482
483       if (dur < shortest_mom_)
484         shortest_mom_ = dur;
485 #else
486       /* I very much suspect that we wanted: */
487
488       consider_end (shortest_mom_);
489       if (dur < shortest_mom_)
490         {
491           shortest_mom_ = dur;
492           consider_end (shortest_mom_);
493         }
494       consider_begin (shortest_mom_);
495 #endif
496
497       if (!stems_)
498         return;
499       
500       Moment now = now_mom ();
501       
502       grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
503                              durlog - 2);
504       stems_->push (stem);
505       last_add_mom_ = now;
506       extend_mom_ = (extend_mom_ >? now) + m->get_length ();
507     }
508 }
509
510 void
511 Auto_beam_engraver::process_acknowledged_grobs ()
512 {
513   if (!count_)
514     {
515       consider_end (shortest_mom_);
516       consider_begin (shortest_mom_);
517     }
518   else if (count_ > 1)
519     {
520       if (stems_)
521         {
522           Moment now = now_mom ();
523           if ((extend_mom_ < now)
524               || ((extend_mom_ == now) && (last_add_mom_ != now)))
525             {
526               end_beam ();
527             }
528           else if (!stems_->size ())
529             {
530               junk_beam ();
531             }
532         }    
533     }
534   
535   count_ ++;
536 }
537
538 ADD_TRANSLATOR (Auto_beam_engraver,
539 /* descr */       "Generate beams based on measure characteristics and observed "
540 "Stems.  Uses beatLength, measureLength and measurePosition to decide "
541 "when to start and stop a beam.  Overriding beaming is done through "
542 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
543 "@code{stemRightBeamCount}. "
544 ,
545 /* creats*/       "Beam",
546 /* accepts */     "beam-forbid-event",
547 /* acks  */      "stem-interface rest-interface beam-interface bar-line-interface",
548 /* reads */       "autoBeaming autoBeamSettings beatLength subdivideBeams",
549 /* write */       "");