]> git.donarmstrong.com Git - lilypond.git/blob - lily/auto-beam-engraver.cc
* lily/text-item.cc (interpret_string): new file, select font with
[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 (is_string (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 (daddy_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       typeset_grob (finished_beam_);
355       finished_beam_ = 0;
356     
357       delete finished_grouping_;
358       finished_grouping_= 0;
359     }
360 }
361
362 void
363 Auto_beam_engraver::start_translation_timestep ()
364 {
365   count_ = 0;
366   /*
367     don't beam over skips
368    */
369   if (stems_)
370     {
371       Moment now = now_mom ();
372       if (extend_mom_ < now)
373         {
374           end_beam ();
375         }
376     }
377   forbid_ = 0;
378 }
379
380 void
381 Auto_beam_engraver::stop_translation_timestep ()
382 {
383   typeset_beam ();
384 }
385
386 void
387 Auto_beam_engraver::finalize ()
388 {
389   /* finished beams may be typeset */
390   typeset_beam ();
391   /* but unfinished may need another announce/acknowledge pass */
392   if (stems_)
393     junk_beam ();
394 }
395
396
397 void
398 Auto_beam_engraver::acknowledge_grob (Grob_info info)
399 {
400   if (stems_)
401     {
402       if (Beam::has_interface (info.grob_))
403         {
404           end_beam ();
405         }
406       else if (Bar_line::has_interface (info.grob_))
407         {
408           end_beam ();
409         }
410       else if (Rest::has_interface (info.grob_))
411         {
412           end_beam ();
413         }
414     }
415   
416   if (Stem::has_interface (info.grob_))
417     {
418       Item* stem = dynamic_cast<Item *> (info.grob_);
419       Music* m = info.music_cause ();
420       if (!m->is_mus_type ("rhythmic-event"))
421         {
422           programming_error ("Stem must have rhythmic structure");
423           return;
424         }
425       
426       /*
427         Don't (start) auto-beam over empty stems; skips or rests
428         */
429       if (!Stem::head_count (stem))
430         {
431           if (stems_)
432             end_beam ();
433           return;
434         }
435
436       if (Stem::get_beam (stem))
437         {
438           if (stems_)
439             junk_beam ();
440           return ;
441         }
442               
443       int durlog  = unsmob_duration (m->get_property ("duration"))->duration_log ();
444       
445       if (durlog <= 2)
446         {
447           if (stems_)
448             end_beam ();
449           return;
450         }
451
452
453       /*
454         ignore grace notes.
455        */
456       if (bool (beam_start_location_.grace_part_) != bool (now_mom ().grace_part_))
457         return ;
458         
459       
460       Moment dur = unsmob_duration (m->get_property ("duration"))->get_length ();
461       /* FIXME:
462
463         This comment has been here since long:
464
465            if shortest duration would change
466             consider ending and beginning beam first. 
467
468         but the code didn't match: */
469 #if 1
470       consider_end (dur);
471       consider_begin (dur);
472
473       if (dur < shortest_mom_)
474         shortest_mom_ = dur;
475 #else
476       /* I very much suspect that we wanted: */
477
478       consider_end (shortest_mom_);
479       if (dur < shortest_mom_)
480         {
481           shortest_mom_ = dur;
482           consider_end (shortest_mom_);
483         }
484       consider_begin (shortest_mom_);
485 #endif
486
487       if (!stems_)
488         return;
489       
490       Moment now = now_mom ();
491       
492       grouping_->add_stem (now - beam_start_moment_ + beam_start_location_,
493                              durlog - 2);
494       stems_->push (stem);
495       last_add_mom_ = now;
496       extend_mom_ = (extend_mom_ >? now) + m->get_length ();
497     }
498 }
499
500 void
501 Auto_beam_engraver::process_acknowledged_grobs ()
502 {
503   if (!count_)
504     {
505       consider_end (shortest_mom_);
506       consider_begin (shortest_mom_);
507     }
508   else if (count_ > 1)
509     {
510       if (stems_)
511         {
512           Moment now = now_mom ();
513           if ((extend_mom_ < now)
514               || ((extend_mom_ == now) && (last_add_mom_ != now)))
515             {
516               end_beam ();
517             }
518           else if (!stems_->size ())
519             {
520               junk_beam ();
521             }
522         }    
523     }
524   
525   count_ ++;
526 }
527
528 ENTER_DESCRIPTION (Auto_beam_engraver,
529 /* descr */       "Generate beams based on measure characteristics and observed "
530 "Stems.  Uses beatLength, measureLength and measurePosition to decide "
531 "when to start and stop a beam.  Overriding beaming is done through "
532 "@ref{Stem_engraver} properties @code{stemLeftBeamCount} and "
533 "@code{stemRightBeamCount}. "
534 ,
535 /* creats*/       "Beam",
536 /* accepts */     "beam-forbid-event",
537 /* acks  */      "stem-interface rest-interface beam-interface bar-line-interface",
538 /* reads */       "autoBeaming autoBeamSettings beatLength subdivideBeams",
539 /* write */       "");