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